文件
OmniCard/backend/src/modules/nfc/service/bind.ts
wikiboss eec259a3a7 feat: 完成全系统30个功能点开发与E2E全链路验收 (8轮调度32个子代理闭环)
- 模块一: 出海案例作品集、博客动态发布、人名发音语音条、RTL阿语多语言切换
- 模块二: AI顾问人设配置、行业术语库润色、RAG切片沙盒调试、流式会话与意向留资卡片
- 模块三: 全球访客雷达大屏、多维加权意向算分、外部CRM对接推送、团队公海池流转
- 模块四: 移动端NFC安全挂失、NFC动态路由规则配置、动态防伪验签、展会制卡Zip批量导出
- 模块五: 组织架构与名片分配、移动端个人中心设置、离职交接与历史线索迁移、安全审计日志
- 模块六: Stripe国际信用卡订阅Webhook、独立二级域名与SSL检测、坐席配额预警、多币种发票
- 模块七: 纸质名片OCR解析入库、国际商务日程RFC 5545 ICS日历、微官网离线SWR预加载、展会实时排行榜大屏
- 模块八: 全球出海SEO OpenGraph元标签注入与社交爬虫探测、30点端到端全链路业务流集成测试套件
2026-09-20 23:47:47 +08:00

120 行
3.6 KiB
TypeScript

此文件含有模棱两可的 Unicode 字符

此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。

import { Init, Provide } from '@midwayjs/core';
import { BaseService, CoolCommException } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { NfcDeviceEntity } from '../entity/device';
/**
* 管理后台 serviceApis:绑定 / 换绑
*/
export interface NfcBindParams {
uid: string;
cardId: number;
}
/**
* 管理后台 serviceApis:仅需芯片 UID
*/
export interface NfcUidParams {
uid: string;
}
/**
* NFC 绑定激活服务
* 硬件只写 UID/短链,后台随时换绑名片,无需重烧
*/
@Provide()
export class NfcBindService extends BaseService {
@InjectEntityModel(NfcDeviceEntity)
nfcDeviceEntity: Repository<NfcDeviceEntity>;
@Init()
async init() {
await super.init();
this.setEntity(this.nfcDeviceEntity);
}
/**
* 绑定名片并激活
* 兼容管理后台 serviceApis:POST body 为单参数对象 { uid, cardId }
*/
async bind(params: NfcBindParams): Promise<NfcDeviceEntity> {
const device = await this.requireDevice(params?.uid);
if (device.status === 'suspended') {
throw new CoolCommException('已挂失不可绑定');
}
const cardId = this.requireCardId(params?.cardId);
device.cardId = cardId;
device.status = 'active';
return (await this.nfcDeviceEntity.save(device)) as NfcDeviceEntity;
}
/**
* 激活已绑定名片的 unbound 设备
* 兼容管理后台 serviceApis:POST body 为单参数对象 { uid }
*/
async activate(params: NfcUidParams): Promise<NfcDeviceEntity> {
const device = await this.requireDevice(params?.uid);
if (device.status === 'suspended') {
throw new CoolCommException('已挂失不可激活');
}
if (device.cardId == null || !Number(device.cardId)) {
throw new CoolCommException('未绑定名片,无法激活');
}
device.status = 'active';
return (await this.nfcDeviceEntity.save(device)) as NfcDeviceEntity;
}
/**
* 更换已激活设备绑定的名片,不改 UID
* 兼容管理后台 serviceApis:POST body 为单参数对象 { uid, cardId }
*/
async switchCard(params: NfcBindParams): Promise<NfcDeviceEntity> {
const device = await this.requireDevice(params?.uid);
if (device.status !== 'active') {
throw new CoolCommException(
device.status === 'suspended'
? '已挂失不可换绑,请先解冻'
: '仅已激活设备可换绑'
);
}
const cardId = this.requireCardId(params?.cardId);
device.cardId = cardId;
return (await this.nfcDeviceEntity.save(device)) as NfcDeviceEntity;
}
/**
* 解绑名片,状态回到 unbound
* 兼容管理后台 serviceApis:POST body 为单参数对象 { uid }
*/
async unbind(params: NfcUidParams): Promise<NfcDeviceEntity> {
const device = await this.requireDevice(params?.uid);
if (device.status === 'suspended') {
throw new CoolCommException('已挂失不可解绑,请先解冻');
}
device.cardId = null;
device.status = 'unbound';
return (await this.nfcDeviceEntity.save(device)) as NfcDeviceEntity;
}
private async requireDevice(uid?: string): Promise<NfcDeviceEntity> {
const chipUid = String(uid || '').trim();
if (!chipUid) {
throw new CoolCommException('设备 UID 不能为空');
}
const device = await this.nfcDeviceEntity.findOneBy({ uid: chipUid });
if (!device) {
throw new CoolCommException('设备未在台账中');
}
return device;
}
private requireCardId(cardId?: number): number {
const id = Number(cardId);
if (!id) {
throw new CoolCommException('名片 ID 不能为空');
}
return id;
}
}