({ method = "POST", url, data = {}, params = {}, withAuth = true, withTmpToken = true })
| 147 | } |
| 148 | |
| 149 | async request({ method = "POST", url, data = {}, params = {}, withAuth = true, withTmpToken = true }) { |
| 150 | const timestamp = Date.now(); |
| 151 | const sign = md5(`${SMALL_APPLICATION_ID}${SMALL_CRYPTO}${timestamp}`).toLowerCase(); |
| 152 | const bodyForSign = method.toUpperCase() === "GET" ? params : data; |
| 153 | const parameterSign = buildParameterSign(bodyForSign, timestamp); |
| 154 | const headers = { |
| 155 | "User-Agent": USER_AGENT, |
| 156 | Referer: `https://servicewechat.com/${MINI_APP_ID}/${PAGE_VERSION}/page-frame.html`, |
| 157 | Accept: "application/json, text/plain, */*", |
| 158 | "Content-Type": "application/json", |
| 159 | "X-Customer": this.memberId || "", |
| 160 | brandCode: BRAND_CODE, |
| 161 | appid: SMALL_APPLICATION_ID, |
| 162 | sign, |
| 163 | timestamp, |
| 164 | versionNumber: VERSION_NUMBER, |
| 165 | }; |
| 166 | if (parameterSign) headers.parameterSign = parameterSign; |
| 167 | if (withAuth && this.accessToken) headers.AccessToken = this.accessToken; |
| 168 | if (withTmpToken && this.tmpToken) headers.tmpToken = this.tmpToken; |
| 169 | |
| 170 | const res = await axios.request({ |
| 171 | method, |
| 172 | url, |
| 173 | data, |
| 174 | params, |
| 175 | headers, |
| 176 | timeout: 20000, |
| 177 | validateStatus: () => true, |
| 178 | }); |
| 179 | |
| 180 | if (res.status !== 200) { |
| 181 | throw new Error(`HTTP ${res.status}: ${JSON.stringify(res.data)}`); |
| 182 | } |
| 183 | const result = res.data || {}; |
| 184 | if (result.code !== undefined && ![0, 401, 402, 515].includes(Number(result.code))) { |
| 185 | throw new Error(result.message || result.msg || JSON.stringify(result)); |
| 186 | } |
| 187 | return result; |
| 188 | } |
| 189 | |
| 190 | async getWxCode() { |
| 191 | const { data } = await wechat.getCode(this.openid); |
no test coverage detected