|
|
@@ -35,8 +35,9 @@ module.exports = app => {
|
|
|
if (!isPasswordValid) {
|
|
|
throw '帐号或密码错误';
|
|
|
}
|
|
|
- const paList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { wx_openid: projectAccount.openid } });
|
|
|
+ const paList = await this.ctx.service.projectAccount.getAllDataByCondition({ where: { wx_openid: projectAccount.wx_openid } });
|
|
|
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);
|
|
|
@@ -49,19 +50,20 @@ module.exports = app => {
|
|
|
}
|
|
|
|
|
|
const accessToken = jwt.sign(
|
|
|
- { accountId: projectAccount.openid, type: 'access' },
|
|
|
+ { openid: projectAccount.openid, type: 'access' },
|
|
|
weappConfig.jwtSecret,
|
|
|
{ expiresIn: weappConfig.accessTokenExpiresIn }
|
|
|
);
|
|
|
|
|
|
const refreshToken = jwt.sign(
|
|
|
- { accountId: projectAccount.openid, type: 'refresh' },
|
|
|
+ { openid: projectAccount.openid, type: 'refresh' },
|
|
|
weappConfig.jwtSecret,
|
|
|
{ expiresIn: weappConfig.refreshTokenExpiresIn }
|
|
|
);
|
|
|
// 3. 存入 Redis:pid 对应最新 refreshToken
|
|
|
const key = `user:${projectAccount.openid}:refresh_token`;
|
|
|
await app.redis.set(key, refreshToken, 'EX', weappConfig.redisExpire);
|
|
|
+
|
|
|
return { accessToken, refreshToken, pList };
|
|
|
}
|
|
|
|
|
|
@@ -78,8 +80,6 @@ module.exports = app => {
|
|
|
});
|
|
|
|
|
|
const { openid, errcode } = wxRes.data;
|
|
|
- console.log('openid', openid);
|
|
|
-
|
|
|
|
|
|
if (errcode || !openid) throw '微信授权登录失败';
|
|
|
|
|
|
@@ -98,19 +98,20 @@ module.exports = app => {
|
|
|
|
|
|
// 2. 生成 access_token + refresh_token
|
|
|
const accessToken = jwt.sign(
|
|
|
- { accountId: openid, type: 'access' },
|
|
|
+ { openid, type: 'access' },
|
|
|
weappConfig.jwtSecret,
|
|
|
{ expiresIn: weappConfig.accessTokenExpiresIn }
|
|
|
);
|
|
|
|
|
|
const refreshToken = jwt.sign(
|
|
|
- { accountId: openid, type: 'refresh' },
|
|
|
+ { openid, type: 'refresh' },
|
|
|
weappConfig.jwtSecret,
|
|
|
{ expiresIn: weappConfig.refreshTokenExpiresIn }
|
|
|
);
|
|
|
// 3. 存入 Redis:pid 对应最新 refreshToken
|
|
|
const key = `user:${openid}:refresh_token`;
|
|
|
await app.redis.set(key, refreshToken, 'EX', weappConfig.redisExpire);
|
|
|
+
|
|
|
return { accessToken, refreshToken, pList };
|
|
|
} catch (error) {
|
|
|
throw error.toString();
|
|
|
@@ -131,18 +132,18 @@ module.exports = app => {
|
|
|
throw '验证码错误或已过期';
|
|
|
}
|
|
|
const accessToken = jwt.sign(
|
|
|
- { accountId: projectAccount.id, type: 'access' },
|
|
|
+ { openid: projectAccount.wx_openid, type: 'access' },
|
|
|
weappConfig.jwtSecret,
|
|
|
{ expiresIn: weappConfig.accessTokenExpiresIn }
|
|
|
);
|
|
|
|
|
|
const refreshToken = jwt.sign(
|
|
|
- { accountId: projectAccount.id, type: 'refresh' },
|
|
|
+ { openid: projectAccount.wx_openid, type: 'refresh' },
|
|
|
weappConfig.jwtSecret,
|
|
|
{ expiresIn: weappConfig.refreshTokenExpiresIn }
|
|
|
);
|
|
|
// 3. 存入 Redis:pid 对应最新 refreshToken
|
|
|
- const key = `user:${projectAccount.id}:refresh_token`;
|
|
|
+ const key = `user:${projectAccount.wx_openid}:refresh_token`;
|
|
|
await app.redis.set(key, refreshToken, 'EX', weappConfig.redisExpire);
|
|
|
return { accessToken, refreshToken };
|
|
|
}
|
|
|
@@ -170,22 +171,17 @@ module.exports = app => {
|
|
|
if (decoded.type !== 'refresh') {
|
|
|
throw 'token 类型错误';
|
|
|
}
|
|
|
- const { accountId } = decoded;
|
|
|
+ const { openid } = decoded;
|
|
|
// 2. 校验 Redis 中是否存在(防止篡改/过期/被踢)
|
|
|
- const redisKey = `user:${accountId}:refresh_token`;
|
|
|
+ const redisKey = `user:${openid}:refresh_token`;
|
|
|
const redisRefreshToken = await app.redis.get(redisKey);
|
|
|
if (!redisRefreshToken || redisRefreshToken !== refreshToken) {
|
|
|
throw 'refresh_token 无效或已过期';
|
|
|
}
|
|
|
- const newAccessToken = jwt.sign({ accountId, type: 'access' }, weappConfig.jwtSecret, { expiresIn: weappConfig.accessTokenExpiresIn });
|
|
|
+ const newAccessToken = jwt.sign({ openid, type: 'access' }, weappConfig.jwtSecret, { expiresIn: weappConfig.accessTokenExpiresIn });
|
|
|
return newAccessToken;
|
|
|
}
|
|
|
|
|
|
- // 获取用户信息以及参与的标段等
|
|
|
- async getUserInfo() {
|
|
|
- const accountInfo = this.ctx.accountInfo;
|
|
|
-
|
|
|
- }
|
|
|
}
|
|
|
return Weappchat;
|
|
|
};
|