({ method = "GET", apiPath, params = {}, data = {}, skipToken = false })
| 181 | } |
| 182 | |
| 183 | async request({ method = "GET", apiPath, params = {}, data = {}, skipToken = false }) { |
| 184 | const upperMethod = method.toUpperCase(); |
| 185 | const headers = this.getHeaders(); |
| 186 | if (skipToken) headers["X-USER-TOKEN"] = ""; |
| 187 | |
| 188 | const options = { |
| 189 | method: upperMethod, |
| 190 | url: `${API_BASE}${apiPath.startsWith("/") ? apiPath : `/${apiPath}`}`, |
| 191 | headers, |
| 192 | timeout: 20000, |
| 193 | validateStatus: () => true, |
| 194 | }; |
| 195 | if (upperMethod === "GET") options.params = params; |
| 196 | else options.data = data; |
| 197 | |
| 198 | const { data: result, status } = await axios.request(options); |
| 199 | if (status !== 200) throw new Error(`HTTP ${status}: ${JSON.stringify(result)}`); |
| 200 | |
| 201 | const code = result && result.code; |
| 202 | if (code && ![200, 0].includes(Number(code))) { |
| 203 | const err = new Error(result.message || result.msg || JSON.stringify(result)); |
| 204 | err.code = code; |
| 205 | err.raw = result; |
| 206 | throw err; |
| 207 | } |
| 208 | return result; |
| 209 | } |
| 210 | |
| 211 | async getLoginCode() { |
| 212 | if (!process.env.wx_auth) throw new Error("缺少 wx_auth,无法从 wx_server 获取 code"); |
no test coverage detected