Browse Source

微信登录修改

laiguoran 4 years ago
parent
commit
31ac178a22

+ 2 - 1
app/controller/login_controller.js

@@ -36,12 +36,12 @@ module.exports = app => {
             if (!ctx.app.config.is_debug) {
                 await ctx.service.maintain.syncMaintainData();
             }
-
             const renderData = {
                 maintainData,
                 maintainConst,
                 errorMessage,
                 hostUrl: ctx.protocol + '://' + ctx.host,
+                appid: ctx.app.config.wxCode.appid,
             };
             await ctx.render('login/login.ejs', renderData);
         }
@@ -56,6 +56,7 @@ module.exports = app => {
             try {
                 const client = new OAuth(ctx.app.config.wxCode.appid, ctx.app.config.wxCode.appsecret);
                 const token = await client.getAccessToken(code);
+                ctx.session.wxbindtoken = token;
                 // const user = await client.getUser(token.data.openid);
                 // console.log(user);
                 if (!token) {

+ 1 - 1
app/controller/wechat_controller.js

@@ -263,7 +263,7 @@ module.exports = app => {
                     const user = await app.wechat.api.getUser(wx.wx_openid);
                     updateList.push({ id: wx.id, wx_unionid: user.unionid });
                 }
-                await ctx.service.projectAccount.updateRows(updateList);
+                await ctx.service.projectAccount.updateUnionid(updateList);
                 ctx.body = 'success';
             } catch (error) {
                 console.log(error);

+ 8 - 2
app/service/project_account.js

@@ -792,10 +792,16 @@ module.exports = app => {
         }
 
         async getUnionIdList() {
-            const sql = 'SELECT * FROM ?? WHERE wx_openid != ? and wx_unionid = ?';
-            const sqlParam = [this.tableName, null, null];
+            const sql = 'SELECT * FROM ?? WHERE wx_openid is not null and wx_unionid is null';
+            const sqlParam = [this.tableName];
             return await this.db.query(sql, sqlParam);
         }
+
+        async updateUnionid(updateList) {
+            if (updateList.length > 0) {
+                return await this.db.updateRows(updateList);
+            }
+        }
     }
 
     return ProjectAccount;

+ 2 - 2
app/view/login/login.ejs

@@ -143,13 +143,13 @@
     const csrf = '<%= ctx.csrf %>'
 </script>
 <script src="/public/js/login.js"></script>
-<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
+<script src="https://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>
 <script>
     $(function () {
         const obj = new WxLogin({
             self_redirect:false,
             id:"wx-code",
-            appid: "wx5320cd30cecdbd68",
+            appid: "<%- appid %>",
             scope: "snsapi_login",
             redirect_uri: "<%- hostUrl %>/wxAuth",
             state: getQueryString('referer'),

+ 2 - 2
config/config.local.js

@@ -85,8 +85,8 @@ module.exports = appInfo => {
     };
     // wx扫码登录
     config.wxCode = {
-        appid: 'wx5320cd30cecdbd68',
-        appsecret: 'ca7c0dbd9e94dc3b1c3b0e73865743f4',
+        appid: 'wx3d5394b238a3bc9a',
+        appsecret: '457d64c55f48f57cd22eca47e53d15cb',
     };
 
     config.is_debug = true;