(apiPath, data = {}, method = "GET")
| 194 | } |
| 195 | |
| 196 | async request(apiPath, data = {}, method = "GET") { |
| 197 | const payload = data || {}; |
| 198 | const ts = Date.now(); |
| 199 | const signData = method === "POST" |
| 200 | ? { body: JSON.stringify(payload), secretKey: SECRET_KEY, ts } |
| 201 | : { ...payload, secretKey: SECRET_KEY, ts }; |
| 202 | const options = { |
| 203 | method, |
| 204 | url: `${API_BASE}${apiPath}`, |
| 205 | headers: this.getHeaders(signPayload(signData), ts), |
| 206 | timeout: 20000, |
| 207 | validateStatus: () => true, |
| 208 | }; |
| 209 | if (method === "GET") options.params = payload; |
| 210 | else options.data = payload; |
| 211 | |
| 212 | const { status, data: result } = await axios.request(options); |
| 213 | if (status !== 200) throw new Error(`HTTP ${status}: ${JSON.stringify(result)}`); |
| 214 | if (!result || result.success !== true) { |
| 215 | const err = new Error(result?.msg || result?.message || JSON.stringify(result)); |
| 216 | err.code = result?.code; |
| 217 | throw err; |
| 218 | } |
| 219 | return result.result; |
| 220 | } |
| 221 | |
| 222 | async getLoginCode() { |
| 223 | const { data } = await wechat.getCode(this.openid); |
no test coverage detected