- 头像 / 文档图片改由后端校验并转发到 COS,密钥只留在服务端 - 新增 file_records 登记表(迁移 20261123170000),记录对象键、大小、上传者等 - 引用关系实时计算(用户头像 / 文档正文),改删业务数据后状态自动同步 - 新增平台管理「存储管理」页:引用状态与明细、单删、两步清理未引用 - 新增接口 GET /api/web/admin/files、POST /api/web/admin/files/cleanup、 DELETE /api/web/admin/files/:id(仍被引用默认 409,force 才删) - COS 目录前缀(COS_DIR_AVATAR / COS_DIR_DOC)、大小上限、清理保护期改为可配置 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
215 lines
7.6 KiB
TypeScript
215 lines
7.6 KiB
TypeScript
/**
|
||
* web 用户端接口(`/api/web/*`)—— 只碰「自己的」数据,令牌由 `api/client.ts` 统一带上
|
||
*
|
||
* 后端返回 `{ ok:true, data }`,失败会抛 `ApiError`(401 会自动清令牌)。
|
||
*/
|
||
import { api } from '@/api/client';
|
||
import type {
|
||
Channel,
|
||
Device,
|
||
DeviceLoginResult,
|
||
Paged,
|
||
ScanRecord,
|
||
ScanReportResult,
|
||
Stats,
|
||
WechatCodeStatus,
|
||
} from '@/api/types';
|
||
|
||
export interface UploadedImage {
|
||
key: string;
|
||
url: string;
|
||
}
|
||
|
||
/** 上传当前用户头像至 COS */
|
||
export function uploadAvatar(file: File): Promise<UploadedImage> {
|
||
const body = new FormData();
|
||
body.append('file', file);
|
||
return api.post('/api/web/me/avatar', body);
|
||
}
|
||
|
||
/**
|
||
* 首页统计
|
||
*
|
||
* @param params days:近 N 天(趋势 / 工作台榜的天数,后端限定 1~30,默认 7);
|
||
* period:`today` / `yesterday` 切到「今日 / 昨日」维度(趋势改成按小时)
|
||
*/
|
||
export function fetchStats(params: { days?: number; period?: '' | 'today' | 'yesterday' } = {}): Promise<Stats> {
|
||
const query = new URLSearchParams({ days: String(params.days ?? 7) });
|
||
if (params.period) query.set('period', params.period);
|
||
return api.get<Stats>(`/api/web/stats?${query}`);
|
||
}
|
||
|
||
/** 我的设备列表 */
|
||
export function fetchDevices(params: {
|
||
page?: number;
|
||
pageSize?: number;
|
||
keyword?: string;
|
||
status?: number | '';
|
||
} = {}): Promise<Paged<Device>> {
|
||
const query = new URLSearchParams();
|
||
if (params.page) query.set('page', String(params.page));
|
||
if (params.pageSize) query.set('pageSize', String(params.pageSize));
|
||
if (params.keyword) query.set('keyword', params.keyword);
|
||
if (params.status !== undefined && params.status !== '') query.set('status', String(params.status));
|
||
const qs = query.toString();
|
||
return api.get<Paged<Device>>(`/api/web/devices${qs ? `?${qs}` : ''}`);
|
||
}
|
||
|
||
/** 某台设备下已登记的通道 */
|
||
export function fetchDeviceChannels(id: number): Promise<{ deviceId: number; list: Channel[] }> {
|
||
return api.get<{ deviceId: number; list: Channel[] }>(`/api/web/devices/${id}/channels`);
|
||
}
|
||
|
||
/** 解绑设备(记录保留,设备可再扫码认领) */
|
||
export function unbindDevice(id: number): Promise<Device> {
|
||
return api.post<Device>(`/api/web/devices/${id}/unbind`);
|
||
}
|
||
|
||
/** 删除设备(软删除) */
|
||
export function deleteDevice(id: number): Promise<{ id: number }> {
|
||
return api.del<{ id: number }>(`/api/web/devices/${id}`);
|
||
}
|
||
|
||
/** 我的扫码记录 */
|
||
export function fetchRecords(params: {
|
||
page?: number;
|
||
pageSize?: number;
|
||
deviceId?: number | '';
|
||
channelId?: string;
|
||
barcode?: string;
|
||
startTime?: string;
|
||
endTime?: string;
|
||
} = {}): Promise<Paged<ScanRecord>> {
|
||
const query = new URLSearchParams();
|
||
if (params.page) query.set('page', String(params.page));
|
||
if (params.pageSize) query.set('pageSize', String(params.pageSize));
|
||
if (params.deviceId !== undefined && params.deviceId !== '') query.set('deviceId', String(params.deviceId));
|
||
if (params.channelId) query.set('channelId', params.channelId);
|
||
if (params.barcode) query.set('barcode', params.barcode);
|
||
if (params.startTime) query.set('startTime', params.startTime);
|
||
if (params.endTime) query.set('endTime', params.endTime);
|
||
const qs = query.toString();
|
||
return api.get<Paged<ScanRecord>>(`/api/web/records${qs ? `?${qs}` : ''}`);
|
||
}
|
||
|
||
// ---------------- 测试页:模拟真机链路 ----------------
|
||
|
||
/** 模拟「设备发码 → 有人扫码确认」:以当前登录账号为确认人建/取设备并换发长期 JWT */
|
||
export function testDeviceLogin(body: {
|
||
sn?: string;
|
||
name?: string;
|
||
vendor?: string;
|
||
model?: string;
|
||
firmware?: string;
|
||
expiresIn?: string;
|
||
}): Promise<DeviceLoginResult> {
|
||
return api.post<DeviceLoginResult>('/api/web/test/device-login', body);
|
||
}
|
||
|
||
/** 模拟「中控上报」(拿 SN + 长期 JWT,与真机同一套校验) */
|
||
export function testScanReport(body: {
|
||
sn: string;
|
||
token: string;
|
||
channelId: string;
|
||
channelName?: string;
|
||
barcode: string;
|
||
scanTime?: string;
|
||
}): Promise<ScanReportResult> {
|
||
return api.post<ScanReportResult>('/api/web/test/scan', body);
|
||
}
|
||
|
||
/** 查扫码登录码状态(公开接口,不需要登录) */
|
||
export function fetchWechatStatus(codeOrSpm: string): Promise<WechatCodeStatus> {
|
||
const query = new URLSearchParams(
|
||
codeOrSpm.includes('.') ? { spm: codeOrSpm } : { code: codeOrSpm },
|
||
);
|
||
return api.get<WechatCodeStatus>(`/api/wechat/status?${query}`, { auth: false });
|
||
}
|
||
|
||
/** 模拟扫码授权:用当前登录账号确认某个登录码(小程序里就是这一步) */
|
||
export function confirmWechatLogin(codeOrSpm: string): Promise<{ status: string; code: string; user: { id: number } }> {
|
||
const body = codeOrSpm.includes('.') ? { spm: codeOrSpm } : { code: codeOrSpm };
|
||
return api.post<{ status: string; code: string; user: { id: number } }>('/api/wechat/confirm', body);
|
||
}
|
||
|
||
// ---------------- 微信登录网关(扫码登录 / 绑定微信) ----------------
|
||
// 二维码内容由网关下发(`pageUrl`),本地只负责画图(`qrUrl`)与长轮询结果。
|
||
|
||
/** 扫码二维码:`state` 是本地会话 id,用来长轮询结果 */
|
||
export interface WxQrTicket {
|
||
state: string;
|
||
pageUrl: string;
|
||
qrUrl: string;
|
||
expiresIn: number;
|
||
}
|
||
|
||
/** 扫码结果(长轮询) */
|
||
export interface WxQrStatus {
|
||
/** pending 还在等扫码 / confirmed 成功 / failed 失败(附 message)/ expired 二维码已过期 */
|
||
status: 'pending' | 'confirmed' | 'failed' | 'expired';
|
||
message?: string;
|
||
}
|
||
|
||
/** 申请「登录」二维码(公开接口;没开通网关时后端返回 503) */
|
||
export function createWechatLoginQr(): Promise<WxQrTicket> {
|
||
return api.post<WxQrTicket>('/api/web/auth/wechat-gateway/qrcode', {}, { auth: false });
|
||
}
|
||
|
||
/** 长轮询登录结果;确认后带 `token` + `user`(一份完整登录态) */
|
||
export interface WxLoginStatus extends WxQrStatus {
|
||
token?: string;
|
||
tokenType?: string;
|
||
expiresIn?: number | string;
|
||
user?: {
|
||
id: number;
|
||
username: string;
|
||
nickname?: string | null;
|
||
email?: string | null;
|
||
phone?: string | null;
|
||
avatar?: string | null;
|
||
};
|
||
}
|
||
|
||
/** 长轮询登录结果(`wait` 秒内有人确认就立即返回) */
|
||
export function fetchWechatLoginStatus(state: string, wait = 0, signal?: AbortSignal): Promise<WxLoginStatus> {
|
||
const query = new URLSearchParams({ state });
|
||
if (wait) query.set('wait', String(wait));
|
||
return api.get<WxLoginStatus>(`/api/web/auth/wechat-gateway/login?${query.toString()}`, { auth: false, signal });
|
||
}
|
||
|
||
/** 已绑定的微信 */
|
||
export interface WxBinding {
|
||
id: number;
|
||
openid: string;
|
||
unionid?: string | null;
|
||
bindScene?: string | null;
|
||
lastLoginAt?: string | null;
|
||
createdAt?: string | null;
|
||
}
|
||
|
||
/** 申请「绑定微信」二维码(需登录) */
|
||
export function createWechatBindQr(): Promise<WxQrTicket> {
|
||
return api.post<WxQrTicket>('/api/web/me/wechat/qrcode');
|
||
}
|
||
|
||
/** 长轮询绑定结果 */
|
||
export interface WxBindStatus extends WxQrStatus {
|
||
binding?: WxBinding;
|
||
}
|
||
|
||
export function fetchWechatBindStatus(state: string, wait = 0, signal?: AbortSignal): Promise<WxBindStatus> {
|
||
const query = new URLSearchParams({ state });
|
||
if (wait) query.set('wait', String(wait));
|
||
return api.get<WxBindStatus>(`/api/web/me/wechat/status?${query.toString()}`, { signal });
|
||
}
|
||
|
||
/** 当前账号绑定的微信(`enabled=false` 表示服务端没配网关) */
|
||
export function fetchWechatBindings(): Promise<{ list: WxBinding[]; enabled: boolean }> {
|
||
return api.get<{ list: WxBinding[]; enabled: boolean }>('/api/web/me/wechat/bindings');
|
||
}
|
||
|
||
/** 解绑某个微信 */
|
||
export function unbindWechat(id: number): Promise<{ id: number }> {
|
||
return api.del<{ id: number }>(`/api/web/me/wechat/bindings/${id}`);
|
||
}
|