'use strict'; const { Controller } = require('egg'); /** * 扫码登录的**公开**接口(发码 / 查码)—— 谁来调都行,不需要令牌 * * 面向:Win 客户端、设备端、任何想自己画二维码显示的前端。 * 确认环节在 `app/controller/client/wechat.js`(需要 user JWT);设备端扫码登录在 `app/controller/device/auth.js`。 * * ⚠️ 登录页扫码登录、主体成员绑定、个人中心绑定微信都已改走微信登录网关(`/api/wechat-gateway/*`), * 这里只剩「设备扫码登录」在用(手机页 `/wechat?spm=…` 点确认)。 */ class WechatAuthController extends Controller { /** * GET /api/wechat/qrcode.svg?code=xxxxxx —— 登录码的二维码(SVG,**本地生成**,零外链) * * 内容是本站地址 `/wechat?spm=1.0.<码>.1`(设备扫码登录的手机页)。 */ async qrcode() { const { ctx } = this; const code = String(ctx.query.code || ''); const origin = ctx.app.config.email.siteUrl || ctx.origin; const content = ctx.service.wechatLogin.qrContent({ code, origin }); ctx.set('Cache-Control', 'no-store'); ctx.type = 'image/svg+xml; charset=utf-8'; ctx.body = ctx.service.wechatLogin.qrSvg(content); } /** * GET /api/wechat/status —— 查串码状态(**无需登录**,小程序/网页先问一下再决定怎么提示) * * query: `?code=` 或 `?spm=`(也可以直接把二维码里扫到的整条地址丢进来) * 返回 `data.status`:pending(待扫码)/ confirmed(已被确认)/ expired(不存在或已过期,即“没这个码”) */ async status() { const { ctx } = this; const svc = ctx.service.wechatLogin; const code = svc.parseSpm(ctx.query.code || ctx.query.spm); if (!code) { ctx.status = 422; ctx.body = { ok: false, status: 'bad_format', message: `登录码格式不对(${Number(ctx.app.config.wechat.codeLength || 6)} 位数字 + 字母)`, }; return; } const state = await svc.statusOf(code); ctx.body = { ok: true, data: { status: state.status, code, spm: svc.spm(code), qrUrl: `/api/wechat/qrcode.svg?code=${encodeURIComponent(code)}`, expiresIn: state.expiresIn, }, }; } } module.exports = WechatAuthController;