| 166 | } |
| 167 | |
| 168 | async login() { |
| 169 | const code = await this.getWxCode(); |
| 170 | const result = assertOk( |
| 171 | await apiRequest("POST", "/app/auth/authorization", { |
| 172 | params: { |
| 173 | code, |
| 174 | type: "register", |
| 175 | acceptCode: "", |
| 176 | }, |
| 177 | }), |
| 178 | "登录授权" |
| 179 | ); |
| 180 | |
| 181 | if (Number(result.mobileAuthStatus) !== 1 || !result.token) { |
| 182 | throw new Error("账号未完成手机号授权,需先在小程序登录一次"); |
| 183 | } |
| 184 | |
| 185 | this.token = result.token; |
| 186 | this.userId = result.userId || ""; |
| 187 | this.saveCache({ |
| 188 | openid: result.openid || "", |
| 189 | unionid: result.unionid || "", |
| 190 | userName: result.userName || "", |
| 191 | inviteCode: result.inviteCode || "", |
| 192 | }); |
| 193 | $.log(`账号[${this.index}] 登录成功: ${mask(this.userId || this.token)}`); |
| 194 | } |
| 195 | |
| 196 | async ensureLogin() { |
| 197 | if (!this.token) this.token = this.getCached().token || ""; |