({ method = "GET", apiPath, params = {}, data = {}, token = true, json = false })
| 137 | } |
| 138 | |
| 139 | async request({ method = "GET", apiPath, params = {}, data = {}, token = true, json = false }) { |
| 140 | const options = { |
| 141 | method, |
| 142 | url: `${API_BASE}${apiPath.startsWith("/") ? apiPath : `/${apiPath}`}`, |
| 143 | headers: this.getHeaders(json ? { "content-type": "application/json" } : {}), |
| 144 | timeout: 15000, |
| 145 | validateStatus: () => true, |
| 146 | }; |
| 147 | if (!token) delete options.headers.token; |
| 148 | if (method === "GET") options.params = params; |
| 149 | else options.data = data; |
| 150 | |
| 151 | const { status, data: result } = await axios.request(options); |
| 152 | if (status !== 200) throw new Error(`HTTP ${status}: ${typeof result === "string" ? result.slice(0, 200) : JSON.stringify(result)}`); |
| 153 | if (!result || result.code !== 0) { |
| 154 | const err = new Error(result?.msg || result?.message || JSON.stringify(result)); |
| 155 | err.code = result?.code; |
| 156 | throw err; |
| 157 | } |
| 158 | return result; |
| 159 | } |
| 160 | |
| 161 | async getWxCode() { |
| 162 | const wxServerUrl = process.env.wx_server_url; |
no test coverage detected