lrf402788946 4 年之前
父節點
當前提交
d22c79a69b
共有 1 個文件被更改,包括 19 次插入9 次删除
  1. 19 9
      app/service/wxpay.js

+ 19 - 9
app/service/wxpay.js

@@ -20,16 +20,26 @@ class WxpayService extends CrudService {
   async toAuth({ code, id }) {
     const token = await this.redis.get(id);
     if (token) return;
-    const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${this.appInfo.id}&secret=${this.appInfo.secret}&js_code=${code}&grant_type=authorization_code`;
+    let url;
+    try {
+      url = `https://api.weixin.qq.com/sns/jscode2session?appid=${this.appInfo.id}&secret=${this.appInfo.secret}&js_code=${code}&grant_type=authorization_code`;
+      console.log(`url=>${url}`);
+    } catch (error) {
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, 'url拼接出错');
+    }
+    try {
+      const res = await this.ctx.curl(url, {
+        method: 'get',
+        dataType: 'json',
+      });
+      console.log(`res=>${res}`);
+      const info = JSON.stringify(res.data);
+      await this.redis.set(id, info, 'EX', 3600);
+    } catch (error) {
+      throw new BusinessError(ErrorCode.SERVICE_FAULT, '请求出错');
+    }
     // const url = appInfo => `https://api.weixin.qq.com/sns/jscode2session?appid=${appInfo.id}&secret=${appInfo.secret}&js_code=${code}&grant_type=authorization_code`;
-    console.log(`url=>${url}`);
-    const res = await this.ctx.curl(url, {
-      method: 'get',
-      dataType: 'json',
-    });
-    console.log(`res=>${res}`);
-    const info = JSON.stringify(res.data);
-    await this.redis.set(id, info, 'EX', 3600);
+
   }
 
   async cash({ id, name, money }) {