lanjianrong пре 4 часа
родитељ
комит
14371d45bf
5 измењених фајлова са 43 додато и 4 уклоњено
  1. 18 0
      app/controller/weapp_controller.js
  2. 1 1
      app/middleware/weapp_auth.js
  3. 1 0
      app/router.js
  4. 21 3
      app/service/weapp.js
  5. 2 0
      sql/weapp.sql

+ 18 - 0
app/controller/weapp_controller.js

@@ -42,6 +42,24 @@ module.exports = app => {
             }
         }
 
+        async getProjectList(ctx) {
+            try {
+                const paList = await ctx.service.projectAccount.getAllDataByCondition({ where: { weapp_openid: ctx.session.sessionUser.weapp_openid, enable: 1 } });
+                const pidList = this.ctx.app._.uniq(this.ctx.app._.map(paList, 'project_id'));
+                const pList = [];
+                for (const p of pidList) {
+                    const project = await this.ctx.service.project.getDataById(p);
+                    if (project) {
+                        pList.push({ code: project.code, name: project.name });
+                    }
+                }
+                ctx.body = { code: 0, msg: '', data: { platformProjects: pList } };
+            } catch (error) {
+                this.log(error);
+                ctx.body = { code: -1, msg: error.toString(), data: null };
+            }
+        }
+
         // 获取手机验证码
         async getCaptcha(ctx) {
             try {

+ 1 - 1
app/middleware/weapp_auth.js

@@ -20,7 +20,7 @@ module.exports = (options, app) => {
             }
 
             const projectAccount = await ctx.service.projectAccount.getDataByCondition({
-                wx_openid: decoded.openid,
+                weapp_openid: decoded.openid,
                 project_id: projectData.id,
                 enable: 1,
             });

+ 1 - 0
app/router.js

@@ -1239,6 +1239,7 @@ module.exports = app => {
     // weapp
     app.post('/wx/weapp/accountLogin', 'weappController.accountLogin'); // 测试用,正式环境要删除
     app.post('/wx/weapp/wxLogin', 'weappController.wxLogin');
+    app.get('/wx/weapp/project/list', weappAuth, 'weappController.getProjectList');
     app.get('/wx/weapp/getCaptcha', 'weappController.getCaptcha');
     app.post('/wx/weapp/auth/refresh', 'weappController.refresh');
     app.get('/wx/weapp/userInfo', weappAuth, 'weappController.getUserInfo');

+ 21 - 3
app/service/weapp.js

@@ -69,6 +69,8 @@ module.exports = app => {
 
         // 微信快捷登录
         async weappLogin(code) {
+            const transaction = await this.db.beginTransaction();
+
             try {
                 const wxRes = await axios.get('https://api.weixin.qq.com/sns/jscode2session', {
                     params: {
@@ -79,11 +81,26 @@ module.exports = app => {
                     },
                 });
 
-                const { openid, errcode } = wxRes.data;
+                const { unionid, openid, errcode } = wxRes.data;
 
                 if (errcode || !openid) throw '微信授权登录失败';
 
-                const paList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { wx_openid: openid } });
+                let paList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { weapp_openid: openid, enable: 1 } });
+                // 如果没有 paList,则尝试使用 unionid 查询
+                if ((!paList || paList.length === 0) && unionid) {
+                    paList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { wx_unionid: unionid, enable: 1 } });
+                    if (paList.length > 0) {
+                        const updateData = paList.map(account => ({
+                            id: account.id,
+                            weapp_openid: openid,
+                        }));
+
+                        await transaction.updateRows(
+                            this.ctx.service.projectAccount.tableName,
+                            updateData
+                        );
+                    }
+                }
                 const pidList = this.ctx.app._.uniq(this.ctx.app._.map(paList, 'project_id'));
                 const pList = [];
                 for (const p of pidList) {
@@ -111,9 +128,10 @@ module.exports = app => {
                 // 3. 存入 Redis:pid 对应最新 refreshToken
                 const key = `user:${openid}:refresh_token`;
                 await app.redis.set(key, refreshToken, 'EX', weappConfig.redisExpire);
-
+                await transaction.commit();
                 return { accessToken, refreshToken, pList };
             } catch (error) {
+                await transaction.rollback();
                 throw error.toString();
             }
         }

+ 2 - 0
sql/weapp.sql

@@ -0,0 +1,2 @@
+ALTER TABLE `zh_project_account`
+ADD COLUMN `weapp_openid` varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '小程序绑定openid(小程序专用)' AFTER `wx_openid`;