({ method = "GET", apiPath, params = {}, data = {} })
| 179 | } |
| 180 | |
| 181 | async request({ method = "GET", apiPath, params = {}, data = {} }) { |
| 182 | const upperMethod = method.toUpperCase(); |
| 183 | const options = { |
| 184 | method: upperMethod, |
| 185 | url: `${API_BASE}${apiPath.startsWith("/") ? apiPath : `/${apiPath}`}`, |
| 186 | headers: this.getHeaders(), |
| 187 | timeout: 20000, |
| 188 | validateStatus: () => true, |
| 189 | }; |
| 190 | if (upperMethod === "GET") options.params = { ...params, _: Date.now() }; |
| 191 | else { |
| 192 | options.params = { _: Date.now() }; |
| 193 | options.data = data; |
| 194 | } |
| 195 | |
| 196 | const { data: result, status } = await axios.request(options); |
| 197 | if (status !== 200) throw new Error(`HTTP ${status}: ${JSON.stringify(result)}`); |
| 198 | if (!result || (String(result.code) !== "0" && Number(result.code) !== 2)) { |
| 199 | throw new Error(result?.msg || result?.message || JSON.stringify(result)); |
| 200 | } |
| 201 | return result; |
| 202 | } |
| 203 | |
| 204 | async getLoginCode() { |
| 205 | if (!process.env.wx_auth) throw new Error("缺少 wx_auth,无法从 wx_server 获取 code"); |
no test coverage detected