| 271 | } |
| 272 | |
| 273 | async getEncryptKey() { |
| 274 | if (!this.openid) throw new Error("缺少 openid,无法生成微信 encryptData"); |
| 275 | const { data } = await axios.post( |
| 276 | `${wechat.serverUrl}/wx/encryptkey`, |
| 277 | { appid: MINI_APP_ID, openid: this.openid }, |
| 278 | { |
| 279 | headers: { auth: wechat.auth }, |
| 280 | timeout: 30000, |
| 281 | validateStatus: () => true, |
| 282 | } |
| 283 | ); |
| 284 | if (!data?.status) throw new Error(data?.message || "wx_server 获取 encryptkey 失败"); |
| 285 | const info = data.data || {}; |
| 286 | const encryptKey = info.encryptKey || info.encrypt_key; |
| 287 | const iv = info.iv; |
| 288 | const version = info.version; |
| 289 | if (!encryptKey || !iv || version === undefined) { |
| 290 | throw new Error(`wx_server encryptkey 缺少必要字段: ${JSON.stringify(data)}`); |
| 291 | } |
| 292 | return { encryptKey, iv, version }; |
| 293 | } |
| 294 | |
| 295 | async encryptData(data = {}) { |
| 296 | const payload = data && typeof data === "object" ? { ...data } : {}; |