lrf402788946 4 年之前
父节点
当前提交
5764c8e1b1
共有 1 个文件被更改,包括 52 次插入11 次删除
  1. 52 11
      app/controller/weixin.js

+ 52 - 11
app/controller/weixin.js

@@ -24,7 +24,15 @@ class WeixinController extends Controller {
   //       store - 默认,认证结果写入sessionStore,然后重定向回请求页面(要求请求页面和认证服务在同一域名下)
   //       token - url带上token参数重定向到原始地址
   async auth() {
-    const { redirect_uri, code, test, type, response_type = 'store', uid, qrcode } = this.ctx.query;
+    const {
+      redirect_uri,
+      code,
+      test,
+      type,
+      response_type = 'store',
+      uid,
+      qrcode,
+    } = this.ctx.query;
     if (test) {
       return await this.authTest();
     }
@@ -47,7 +55,9 @@ class WeixinController extends Controller {
 
     // TODO: 生成回调地址
     const { wxapi, authUrl = this.ctx.path } = this.app.config;
-    const backUrl = encodeURI(`${this.app.config.baseUrl}${this.config.authUrl}?state=${state}`);
+    const backUrl = encodeURI(
+      `${this.app.config.baseUrl}${this.config.authUrl}?state=${state}`
+    );
     const to_uri = `${wxapi.baseUrl}/api/auth?appid=${wxapi.appid}&response_type=code&redirect_uri=${backUrl}&connect_redirect=1#wechat`;
     console.log('url-->' + to_uri);
     this.ctx.redirect(to_uri);
@@ -67,7 +77,10 @@ class WeixinController extends Controller {
     try {
       ({ openid } = await weixin.fetch(code));
     } catch (err) {
-      await this.ctx.render('error.njk', { title: err.message, message: err.details });
+      await this.ctx.render('error.njk', {
+        title: err.message,
+        message: err.details,
+      });
       return;
     }
     console.log('openid--->' + openid);
@@ -85,7 +98,6 @@ class WeixinController extends Controller {
           // TODO: 重定性页面
           console.log('to_uri000-->' + to_uri);
           this.ctx.redirect(to_uri);
-
         } else {
           console.log('rrr0000--->' + redirect_uri);
           const touri = `${this.app.config.baseUrl}/platmobile/error`;
@@ -94,33 +106,37 @@ class WeixinController extends Controller {
           this.ctx.redirect(to_uri);
         }
       } else if (type === '1') {
-        const to_uri = urljoin(redirect_uri, `?openid=${openid}&uid=${uid}&type=${type}&qrcode=${qrcode}`);
+        const to_uri = urljoin(
+          redirect_uri,
+          `?openid=${openid}&uid=${uid}&type=${type}&qrcode=${qrcode}`
+        );
         // TODO: 重定性页面
         console.log('1111---?' + to_uri);
         this.ctx.redirect(to_uri);
       }
     }
-
   }
 
   // GET 用户授权内部测试接口
   async authTest() {
     const { redirect_uri, type, uid, qrcode, openid } = this.ctx.query;
-    this.ctx.logger.debug(`[auth-test] reditect_uri - ${redirect_uri}, openid - ${openid}`);
+    this.ctx.logger.debug(
+      `[auth-test] reditect_uri - ${redirect_uri}, openid - ${openid}`
+    );
     assert(redirect_uri, '回调地址不能为空');
     assert(openid, 'openid不能为空');
     if (openid) {
       console.log('redirect_uri-->' + redirect_uri);
-      const user = await this.ctx.service.user.findByOpenid(openid);
+      let user = await this.ctx.service.user.findByOpenid(openid);
       if (type === '0') {
         // 通过openid取得用户信息
         if (user) {
+          user = this.checkVip(user);
           const token = await this.ctx.service.login.createJwt(user);
           const to_uri = urljoin(redirect_uri, `?token=${token}`);
           // TODO: 重定性页面
           console.log('to_uri000-->' + to_uri);
           this.ctx.redirect(to_uri);
-
         } else {
           console.log('rrr0000--->' + redirect_uri);
           const touri = `${this.app.config.baseUrl}/mobile/error`;
@@ -129,13 +145,38 @@ class WeixinController extends Controller {
           this.ctx.redirect(to_uri);
         }
       } else if (type === '1') {
-        const to_uri = urljoin(redirect_uri, `?openid=${openid}&uid=${uid}&type=${type}&qrcode=${qrcode}`);
+        const to_uri = urljoin(
+          redirect_uri,
+          `?openid=${openid}&uid=${uid}&type=${type}&qrcode=${qrcode}`
+        );
         // TODO: 重定性页面
         console.log('1111---?' + to_uri);
         this.ctx.redirect(to_uri);
       }
     }
-
+  }
+  async checkVip(user) {
+    const { role, id } = user;
+    if (role !== '8') return user;
+    const url = 'http://127.0.0.1:9008/api/live/dock/getdock/' + id;
+    const vipuser = await this.ctx.curl(url, {
+      method: 'post',
+      headers: {
+        'content-type': 'application/json',
+      },
+      dataType: 'json',
+    });
+    if (vipuser.status === 200) {
+      if (vipuser.data.errcode === 0) {
+        const vd = vipuser.data.res;
+        if (vd.length > 0) {
+          const f = _.head(vd);
+          const fid = _.get(f, 'id');
+          user = { ...user, remark: fid };
+        }
+      }
+    }
+    return user;
   }
 }