|
@@ -69,6 +69,8 @@ module.exports = app => {
|
|
|
|
|
|
|
|
// 微信快捷登录
|
|
// 微信快捷登录
|
|
|
async weappLogin(code) {
|
|
async weappLogin(code) {
|
|
|
|
|
+ const transaction = await this.db.beginTransaction();
|
|
|
|
|
+
|
|
|
try {
|
|
try {
|
|
|
const wxRes = await axios.get('https://api.weixin.qq.com/sns/jscode2session', {
|
|
const wxRes = await axios.get('https://api.weixin.qq.com/sns/jscode2session', {
|
|
|
params: {
|
|
params: {
|
|
@@ -79,11 +81,26 @@ module.exports = app => {
|
|
|
},
|
|
},
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- const { openid, errcode } = wxRes.data;
|
|
|
|
|
|
|
+ const { unionid, openid, errcode } = wxRes.data;
|
|
|
|
|
|
|
|
if (errcode || !openid) throw '微信授权登录失败';
|
|
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 pidList = this.ctx.app._.uniq(this.ctx.app._.map(paList, 'project_id'));
|
|
|
const pList = [];
|
|
const pList = [];
|
|
|
for (const p of pidList) {
|
|
for (const p of pidList) {
|
|
@@ -111,9 +128,10 @@ module.exports = app => {
|
|
|
// 3. 存入 Redis:pid 对应最新 refreshToken
|
|
// 3. 存入 Redis:pid 对应最新 refreshToken
|
|
|
const key = `user:${openid}:refresh_token`;
|
|
const key = `user:${openid}:refresh_token`;
|
|
|
await app.redis.set(key, refreshToken, 'EX', weappConfig.redisExpire);
|
|
await app.redis.set(key, refreshToken, 'EX', weappConfig.redisExpire);
|
|
|
-
|
|
|
|
|
|
|
+ await transaction.commit();
|
|
|
return { accessToken, refreshToken, pList };
|
|
return { accessToken, refreshToken, pList };
|
|
|
} catch (error) {
|
|
} catch (error) {
|
|
|
|
|
+ await transaction.rollback();
|
|
|
throw error.toString();
|
|
throw error.toString();
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|