(pathname, { method = "GET", params, data, auth = true, isJson = false, base = API_BASE } = {})
| 191 | } |
| 192 | |
| 193 | async request(pathname, { method = "GET", params, data, auth = true, isJson = false, base = API_BASE } = {}) { |
| 194 | const options = { |
| 195 | method, |
| 196 | url: `${base}${pathname}`, |
| 197 | params, |
| 198 | headers: this.headers({}, auth, isJson), |
| 199 | timeout: 20000, |
| 200 | validateStatus: () => true, |
| 201 | }; |
| 202 | if (data !== undefined) options.data = isJson ? data : new URLSearchParams(data).toString(); |
| 203 | |
| 204 | const { data: result, status } = await axios.request(options); |
| 205 | if (status === 401 || status === 403) throw new Error(`HTTP ${status}: ${result?.message || JSON.stringify(result)}`); |
| 206 | if (status < 200 || status >= 300) throw new Error(`HTTP ${status}: ${JSON.stringify(result)}`); |
| 207 | if (result && typeof result === "object" && result.code && !["200", "0"].includes(String(result.code))) { |
| 208 | throw new Error(`${result.code} ${result.message || result.msg || JSON.stringify(result)}`.trim()); |
| 209 | } |
| 210 | return result; |
| 211 | } |
| 212 | |
| 213 | async getOperateData() { |
| 214 | if (!process.env.wx_auth) throw new Error("缺少 wx_auth,无法从 wx_server 获取登录数据"); |
no test coverage detected