lrf402788946 4 vuotta sitten
vanhempi
commit
ded2e86f46
1 muutettua tiedostoa jossa 17 lisäystä ja 12 poistoa
  1. 17 12
      app/controller/weixin.js

+ 17 - 12
app/controller/weixin.js

@@ -6,6 +6,7 @@ const uuid = require('uuid');
 const urljoin = require('url-join');
 const stringRandom = require('string-random');
 const Controller = require('egg').Controller;
+const { BusinessError, ErrorCode } = require('naf-core').Error;
 
 /**
  * 微信认证,获得openid和用户信息,生成微信Jwt
@@ -177,18 +178,22 @@ class WeixinController extends Controller {
 
   // GET 签到小程序请求用户
   async appAuth() {
-    console.warn('in appAuth');
-    const { js_code, login = true } = this.ctx.query;
-    console.log(js_code);
-    const appopenid = await this.ctx.service.weixin.appAuth(js_code);
-    if (!login) this.ctx.ok(appopenid);
-    console.warn(`openid=>${appopenid}`);
-    // const state = uuid();
-    // const key = `appinfo:${state}`;
-    // await this.app.redis.set(key, appopenid, 'EX', 600);
-    const res = await this.ctx.service.user.findByAppOpenid(appopenid);
-    const obj = { user: res, openid: appopenid };
-    this.ctx.ok(obj);
+    try {
+      const { js_code, login = true } = this.ctx.query;
+      console.log(js_code);
+      const appopenid = await this.ctx.service.weixin.appAuth(js_code);
+      if (!login) this.ctx.ok(appopenid);
+      console.warn(`openid=>${appopenid}`);
+      // const state = uuid();
+      // const key = `appinfo:${state}`;
+      // await this.app.redis.set(key, appopenid, 'EX', 600);
+      const res = await this.ctx.service.user.findByAppOpenid(appopenid);
+      const obj = { user: res, openid: appopenid };
+      this.ctx.ok(obj);
+    } catch (error) {
+      throw new BusinessError(ErrorCode.NETWORK, '获取小程序信息失败,请尝试重新进入');
+    }
+
   }
 }