()
| 166 | } |
| 167 | |
| 168 | async login() { |
| 169 | const cached = this.getCachedToken(); |
| 170 | if (cached) { |
| 171 | this.token = cached; |
| 172 | $.log(`账号[${this.index}] 使用缓存token: ${maskToken(this.token)}`); |
| 173 | const ok = await this.checkToken(); |
| 174 | if (ok) return; |
| 175 | $.log(`账号[${this.index}] 缓存token失效,重新登录`); |
| 176 | this.removeCachedToken(); |
| 177 | } |
| 178 | |
| 179 | const code = await getWxCode(this.openid); |
| 180 | const res = await this.gql( |
| 181 | `mutation codeLogin($client_id: String! $code: String!) { |
| 182 | login(client_id: $client_id, code: $code) |
| 183 | }`, |
| 184 | { client_id: APP.appid, code }, |
| 185 | "", |
| 186 | true |
| 187 | ); |
| 188 | const token = res?.data?.login || ""; |
| 189 | if (!token) throw new Error(`登录未返回token: ${short(res)}`); |
| 190 | this.token = token; |
| 191 | $.log(`账号[${this.index}] 登录成功: ${maskToken(this.token)}`); |
| 192 | } |
| 193 | |
| 194 | async checkToken() { |
| 195 | const res = await this.gql( |
no test coverage detected