- backend/:Egg 3 纯 JSON 接口服务(MySQL 8 + Redis),全部走 header 令牌鉴权; 已彻底去掉服务端渲染(删除 page 控制器 / app/view / nunjucks / session / CSRF), 开发时把页面请求反代到前端 dev server,生产托管 frontend/dist(SPA 回退)。 单测 91 个通过(不需要本机数据库)。 - frontend/:Vite + Vue3 + TypeScript 的 SPA(登录 / 注册 / 找回密码 / 扫码登录与确认 / 首页统计 / 设备管理 / 扫码记录 / 个人中心 / 测试页),vue-tsc 类型检查与构建通过。 - extension/:MV3 浏览器扩展(零构建),注入第三方 WMS 页面识别运单号并打开溯源页。 - 忽略规则统一放根 .gitignore:node_modules、dist、logs / logs-*、run、typings、 .env*、config.local.js(含本机数据库与 Redis 密码,绝不入库)。
98 lines
3.1 KiB
JavaScript
98 lines
3.1 KiB
JavaScript
'use strict';
|
|
|
|
const { Service } = require('egg');
|
|
|
|
/** 拼装写库的授权信息(只写入明确传入的字段,避免把旧值覆盖成空) */
|
|
function buildPayload(data) {
|
|
const payload = {};
|
|
if (data.unionid !== undefined) payload.unionid = data.unionid || null;
|
|
if (data.nickname !== undefined) payload.nickname = data.nickname || '';
|
|
if (data.avatar !== undefined) payload.avatar = data.avatar || '';
|
|
if (data.sessionKey !== undefined) payload.sessionKey = data.sessionKey || null;
|
|
if (data.accessToken !== undefined) payload.accessToken = data.accessToken || null;
|
|
if (data.refreshToken !== undefined) payload.refreshToken = data.refreshToken || null;
|
|
if (data.scope !== undefined) payload.scope = data.scope || null;
|
|
if (data.expiresIn !== undefined && data.expiresIn !== null) {
|
|
payload.tokenExpiresAt = new Date(Date.now() + Number(data.expiresIn) * 1000);
|
|
} else if (data.tokenExpiresAt !== undefined) {
|
|
payload.tokenExpiresAt = data.tokenExpiresAt;
|
|
}
|
|
payload.lastAuthAt = new Date();
|
|
return payload;
|
|
}
|
|
|
|
class WechatService extends Service {
|
|
get WechatOauth() {
|
|
return this.app.model.WechatOauth;
|
|
}
|
|
|
|
/**
|
|
* 绑定微信授权:同一 appid + openid 已存在则更新,否则新建
|
|
* @param {object} data userId / appid / openid 必填
|
|
*/
|
|
async bind(data) {
|
|
const { userId, appid, openid } = data;
|
|
const existing = await this.WechatOauth.findOne({ where: { appid, openid } });
|
|
const payload = buildPayload(data);
|
|
|
|
if (existing) {
|
|
// 换绑:允许把已存在的授权记录改到另一个用户
|
|
if (userId && Number(existing.userId) !== Number(userId)) {
|
|
payload.userId = userId;
|
|
}
|
|
await existing.update(payload);
|
|
return existing.toJSON();
|
|
}
|
|
|
|
const created = await this.WechatOauth.create(Object.assign({ userId, appid, openid }, payload));
|
|
return created.toJSON();
|
|
}
|
|
|
|
/** 按 appid + openid 查询授权记录,并带出关联用户 */
|
|
async findByOpenid(appid, openid) {
|
|
const oauth = await this.WechatOauth.findOne({
|
|
where: { appid, openid },
|
|
include: [{ model: this.app.model.User, as: 'user', required: false }],
|
|
});
|
|
if (!oauth) return null;
|
|
|
|
const result = oauth.toJSON();
|
|
if (result.user) {
|
|
// 不把用户敏感字段透出去
|
|
delete result.user.deleted_at;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/** 查询某个用户的全部微信授权 */
|
|
async listByUser(userId) {
|
|
const rows = await this.WechatOauth.findAll({
|
|
where: { userId },
|
|
order: [[ 'id', 'DESC' ]],
|
|
});
|
|
return rows.map(row => row.toJSON());
|
|
}
|
|
|
|
/** 更新小程序 session_key / 令牌 */
|
|
async updateTokens(id, data) {
|
|
const oauth = await this.WechatOauth.findByPk(id);
|
|
if (!oauth) return null;
|
|
|
|
await oauth.update(buildPayload(data));
|
|
return oauth.toJSON();
|
|
}
|
|
|
|
/** 解绑(软删除) */
|
|
async unbind(id) {
|
|
const affected = await this.WechatOauth.destroy({ where: { id } });
|
|
return affected > 0;
|
|
}
|
|
|
|
/** 总数(首页统计用) */
|
|
async count(where = {}) {
|
|
return this.WechatOauth.count({ where });
|
|
}
|
|
}
|
|
|
|
module.exports = WechatService;
|