- 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 密码,绝不入库)。
96 lines
2.8 KiB
JavaScript
96 lines
2.8 KiB
JavaScript
'use strict';
|
||
|
||
const { Controller } = require('egg');
|
||
|
||
/**
|
||
* 运维/内部:微信授权(绑定关系)的读写
|
||
*
|
||
* 挂在 `middleware.internalAuth()` 后面(见 app/middleware/internal_auth.js)。
|
||
*
|
||
* 用户侧的扫码确认在 `app/controller/client/wechat.js`,公开的发码/查码在 `app/controller/auth/wechat.js`。
|
||
*/
|
||
class InternalWechatController extends Controller {
|
||
/** GET /api/users/:id/wechats —— 某用户的全部微信授权 */
|
||
async indexByUser() {
|
||
const { ctx } = this;
|
||
const userId = Number(ctx.params.id);
|
||
if (!Number.isInteger(userId) || userId <= 0) {
|
||
ctx.status = 400;
|
||
ctx.body = { ok: false, message: 'id 必须是正整数' };
|
||
return;
|
||
}
|
||
|
||
const list = await ctx.service.wechat.listByUser(userId);
|
||
ctx.body = { ok: true, data: { list, total: list.length } };
|
||
}
|
||
|
||
/** GET /api/wechat/:appid/:openid —— 按 appid + openid 反查授权与用户 */
|
||
async show() {
|
||
const { ctx } = this;
|
||
const { appid, openid } = ctx.params;
|
||
const data = await ctx.service.wechat.findByOpenid(appid, openid);
|
||
if (!data) {
|
||
ctx.status = 404;
|
||
ctx.body = { ok: false, message: '授权记录不存在' };
|
||
return;
|
||
}
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** POST /api/wechat/bind —— 绑定(已存在则更新) */
|
||
async bind() {
|
||
const { ctx } = this;
|
||
const { userId, appid, openid } = ctx.request.body || {};
|
||
|
||
if (!userId || !appid || !openid) {
|
||
ctx.status = 422;
|
||
ctx.body = { ok: false, message: 'userId、appid、openid 均为必填' };
|
||
return;
|
||
}
|
||
|
||
const data = await ctx.service.wechat.bind(ctx.request.body);
|
||
ctx.status = 201;
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** PUT /api/wechat/:id/tokens —— 更新 session_key / 令牌 */
|
||
async updateTokens() {
|
||
const { ctx } = this;
|
||
const id = Number(ctx.params.id);
|
||
if (!Number.isInteger(id) || id <= 0) {
|
||
ctx.status = 400;
|
||
ctx.body = { ok: false, message: 'id 必须是正整数' };
|
||
return;
|
||
}
|
||
|
||
const data = await ctx.service.wechat.updateTokens(id, ctx.request.body || {});
|
||
if (!data) {
|
||
ctx.status = 404;
|
||
ctx.body = { ok: false, message: '授权记录不存在' };
|
||
return;
|
||
}
|
||
ctx.body = { ok: true, data };
|
||
}
|
||
|
||
/** DELETE /api/wechat/:id —— 解绑 */
|
||
async unbind() {
|
||
const { ctx } = this;
|
||
const id = Number(ctx.params.id);
|
||
if (!Number.isInteger(id) || id <= 0) {
|
||
ctx.status = 400;
|
||
ctx.body = { ok: false, message: 'id 必须是正整数' };
|
||
return;
|
||
}
|
||
|
||
const removed = await ctx.service.wechat.unbind(id);
|
||
if (!removed) {
|
||
ctx.status = 404;
|
||
ctx.body = { ok: false, message: '授权记录不存在' };
|
||
return;
|
||
}
|
||
ctx.body = { ok: true, data: { id } };
|
||
}
|
||
}
|
||
|
||
module.exports = InternalWechatController;
|