liufumingren b2eea28001 feat(微信网关): 扫码登录 / 绑定微信 / 工作台扫码登入,废弃自建登录码
对接 https://gateway.gztbyc.com:二维码与「扫码确认」由网关提供,本服务只负责
申请临时码 → 验签收通知 → 落地自己的业务。

- 新增 service/wx_gateway.js + controller/gateway/notify.js(POST /api/wechat-gateway/notify,验签 401 / 会话过期 200 ignored)
- 新增 controller/web/wx_gateway.js(登录二维码 / 长轮询 / SVG+GIF / 个人中心绑定与解绑)
- 新表 wechat_gateway_bindings(uk(app_key, openid)):一个微信只绑一个账号,解绑是软删,
  再绑定走 restore 复用(否则会撞唯一键报 Validation error)
- 「这个微信已绑别的账号」策略:原账号正常 → 拒绝并提示对方账号名;原账号禁用/已删 → 直接改绑;
  拿不到原账号 → 运维接口 GET/DELETE /api/internal/wechat-bindings 强制解绑
- 主体成员改为「扫码绑定」:管理员出二维码 → 成员微信扫码 → 已有账号直接继承(自动建的临时账号删掉),
  没有账号则由服务端建号并绑成其登录方式;subject_members.temp_user_id 记录临时账号
- 废弃自建登录码:删除 GET /api/wechat/login(6 位串码)与 workstations.login_code(迁移 20261028121000),
  工作台改扫码登入,成员绑定码与 /wechat?bind= 手机确认页下线
- 前端:登录页/个人中心/主体成员/工作台全部切到网关;桌面端登录窗改网关二维码 + 长轮询
- 测试:wx-gateway.test.js 覆盖登录/绑定/解绑-再绑/禁用账号改绑/运维强制解绑/成员绑定/工作台登入

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-10-29 17:20:05 +08:00

86 lines
3.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 接口客户端:统一带上 `Authorization: Bearer <user JWT>` + `X-Subject-Id`,
* 统一处理 `{ ok, data, message }` 结构
*
* 后端约定(见 backend/README):
* - 成功 `{ ok: true, data }`;失败 `{ ok: false, message, status? }`
* - 401 = 令牌缺失/过期/无效 → 清掉本地令牌并跳登录页
* - 400 `status='subject_required'` = 没选主体;403 `status='subject_expired'` = 主体到期
* (这两种由页面自己提示,不清令牌)
*/
import { clearToken, getToken } from '@/composables/useAuth';
import { getStoredSubjectId } from '@/composables/subjectStorage';
/** 开发时走 Vite 代理(同源 /api),生产时后端同源托管,所以默认空字符串即可 */
const BASE = (import.meta.env.VITE_API_BASE as string | undefined) || '';
export interface ApiResult<T> {
ok: boolean;
data?: T;
message?: string;
status?: string;
}
export class ApiError extends Error {
status: number;
code?: string;
constructor(message: string, status: number, code?: string) {
super(message);
this.status = status;
this.code = code;
}
}
interface RequestOptions {
method?: 'GET' | 'POST' | 'PUT' | 'DELETE';
body?: unknown;
/** 带上令牌(默认带;登录等公开接口可传 false) */
auth?: boolean;
/** 中断信号:长轮询(扫码登录 / 绑定 / 工作台登入)在切面板、关弹窗时要能立刻断开 */
signal?: AbortSignal;
}
async function request<T>(path: string, options: RequestOptions = {}): Promise<T> {
const { method = 'GET', body, auth = true, signal } = options;
const headers: Record<string, string> = {};
if (body !== undefined) headers['Content-Type'] = 'application/json';
if (auth) {
const token = getToken();
if (token) headers.Authorization = `Bearer ${token}`;
// 主体范围:业务接口都靠这个 header 限定操作范围(没有主体时不带,后端回 subject_required)
const subjectId = getStoredSubjectId();
if (subjectId) headers['X-Subject-Id'] = String(subjectId);
}
const res = await fetch(`${BASE}${path}`, {
method,
headers,
body: body === undefined ? undefined : JSON.stringify(body),
signal,
});
let payload: ApiResult<T> | null = null;
try {
payload = (await res.json()) as ApiResult<T>;
} catch {
payload = null;
}
if (res.status === 401) {
clearToken();
throw new ApiError(payload?.message || '登录已过期,请重新登录', 401, payload?.status);
}
if (!res.ok || !payload || payload.ok === false) {
throw new ApiError(payload?.message || `请求失败(${res.status})`, res.status, payload?.status);
}
return payload.data as T;
}
export const api = {
get: <T>(path: string, options?: Omit<RequestOptions, 'method' | 'body'>) => request<T>(path, { ...options, method: 'GET' }),
post: <T>(path: string, body?: unknown, options?: Omit<RequestOptions, 'method' | 'body'>) => request<T>(path, { ...options, method: 'POST', body }),
put: <T>(path: string, body?: unknown, options?: Omit<RequestOptions, 'method' | 'body'>) => request<T>(path, { ...options, method: 'PUT', body }),
del: <T>(path: string, body?: unknown, options?: Omit<RequestOptions, 'method' | 'body'>) => request<T>(path, { ...options, method: 'DELETE', body }),
};