feat: 同步管理端、用户端与后端业务,并接通线上 Web 接口
补齐平台订阅、外部登录、CRM 同步与用户端页面;Nginx 转发用户端接口,登录页使用 Google 与 Apple 标志。 Co-authored-by: Cursor <cursoragent@cursor.com>
这个提交包含在:
@@ -1,6 +1,6 @@
|
||||
/**
|
||||
* OmniCard AI (CRMmp) - 名片主 NFC 贴卡激活业务驱动层
|
||||
* H5 无天线走手动 UID;APP startScan 为占位,调不到原生 NFC 时回退手动输入
|
||||
* H5 无天线走手动 UID;APP 经 uni.getNFCAdapter 尝试读芯片 UID,失败回退手动输入
|
||||
*/
|
||||
|
||||
import { NFCRecord } from '../../../common/types.uts';
|
||||
@@ -21,7 +21,11 @@ export type ScanCopyKey =
|
||||
| 'nfcUnavailable'
|
||||
| 'success'
|
||||
| 'error'
|
||||
| 'retry';
|
||||
| 'retry'
|
||||
| 'writeSuccess'
|
||||
| 'writeFailed'
|
||||
| 'writeUnavailable'
|
||||
| 'writeCancelled';
|
||||
|
||||
export type ScanState = {
|
||||
uid: string;
|
||||
@@ -104,6 +108,18 @@ export function resolveScanText(key: ScanCopyKey): string {
|
||||
if (key === 'retry') {
|
||||
return zh ? '重试' : 'Try again';
|
||||
}
|
||||
if (key === 'writeSuccess') {
|
||||
return zh ? '已写入' : 'Written to tag';
|
||||
}
|
||||
if (key === 'writeFailed') {
|
||||
return zh ? '写入失败' : 'Could not write';
|
||||
}
|
||||
if (key === 'writeUnavailable') {
|
||||
return zh ? '暂无可写内容' : 'Nothing to write';
|
||||
}
|
||||
if (key === 'writeCancelled') {
|
||||
return zh ? '已取消写入' : 'Write cancelled';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
@@ -214,12 +230,130 @@ export function applyScanOptions(state: ScanState, options: ScanPageOptions | nu
|
||||
}
|
||||
|
||||
/**
|
||||
* APP 原生贴卡占位。调不到天线时回退手动输入,禁止伪造成功
|
||||
* NFC 标签 id(ArrayBuffer)转大写 Hex UID;空则返回空串
|
||||
*/
|
||||
function nfcIdToHex(id: ArrayBuffer | null | undefined): string {
|
||||
if (id == null) {
|
||||
return '';
|
||||
}
|
||||
const bytes = new Uint8Array(id);
|
||||
let hex = '';
|
||||
let i = 0;
|
||||
const len = bytes.length;
|
||||
while (i < len) {
|
||||
let part = bytes[i].toString(16);
|
||||
if (part.length < 2) {
|
||||
part = '0' + part;
|
||||
}
|
||||
hex += part;
|
||||
i++;
|
||||
}
|
||||
return hex.toUpperCase();
|
||||
}
|
||||
|
||||
type NfcFail = {
|
||||
errMsg?: string;
|
||||
};
|
||||
|
||||
type NfcIdResult = {
|
||||
id?: ArrayBuffer | null;
|
||||
};
|
||||
|
||||
type NfcCallbackOptions = {
|
||||
success?: () => void;
|
||||
fail?: (err: NfcFail) => void;
|
||||
};
|
||||
|
||||
type NdefWriteOptions = {
|
||||
uris?: string[];
|
||||
success?: () => void;
|
||||
fail?: (err: NfcFail) => void;
|
||||
};
|
||||
|
||||
type NdefWriter = {
|
||||
writeNdefMessage: (options: NdefWriteOptions) => void;
|
||||
connect?: (options: NfcCallbackOptions) => void;
|
||||
};
|
||||
|
||||
type NfcAdapter = {
|
||||
getNdef: () => NdefWriter | null;
|
||||
startDiscovery: (options: NfcCallbackOptions) => void;
|
||||
onDiscovered: (callback: (res: NfcIdResult) => void) => void;
|
||||
};
|
||||
|
||||
type UniWithNfc = {
|
||||
getNFCAdapter?: () => NfcAdapter | null;
|
||||
};
|
||||
|
||||
function readNfcAdapter(): NfcAdapter | null {
|
||||
const runtime = uni as UniWithNfc;
|
||||
if (runtime.getNFCAdapter == null) {
|
||||
return null;
|
||||
}
|
||||
return runtime.getNFCAdapter();
|
||||
}
|
||||
|
||||
/**
|
||||
* APP:调用官方风格 uni.getNFCAdapter + startDiscovery / onDiscovered 尝试读 UID。
|
||||
* 同步路径仅在已读到合法 UID 时返回非空;读不到、API 不存在或抛错返回空串,禁止伪造。
|
||||
*/
|
||||
function tryReadUidFromAntenna(state: ScanState): string {
|
||||
// #ifdef APP
|
||||
try {
|
||||
const adapter = readNfcAdapter();
|
||||
if (adapter == null) {
|
||||
return '';
|
||||
}
|
||||
let syncUid = '';
|
||||
adapter.startDiscovery({
|
||||
success: () => {
|
||||
adapter.onDiscovered((res: NfcIdResult) => {
|
||||
const uid = parseUid(nfcIdToHex(res.id));
|
||||
if (uid === '') {
|
||||
return;
|
||||
}
|
||||
// 异步贴卡成功:写入真实 UID,不伪造
|
||||
state.uid = uid;
|
||||
state.empty = false;
|
||||
state.scanning = false;
|
||||
state.manualInput = false;
|
||||
state.errorMessage = '';
|
||||
syncUid = uid;
|
||||
});
|
||||
},
|
||||
fail: (_err: { errMsg?: string }) => {
|
||||
// 由 startScan 统一走手动输入回退
|
||||
},
|
||||
});
|
||||
return parseUid(syncUid);
|
||||
} catch (_err) {
|
||||
return '';
|
||||
}
|
||||
// #endif
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* APP 尝试官方 NFC Adapter 读 UID;仅合法 UID 才成功。否则回退手动输入,禁止伪造成功
|
||||
*/
|
||||
export function startScan(state: ScanState): boolean {
|
||||
// #ifdef APP
|
||||
// Native NFC antenna is not wired in this skeleton.
|
||||
try {
|
||||
const uid = tryReadUidFromAntenna(state);
|
||||
if (parseUid(uid) !== '') {
|
||||
state.uid = parseUid(uid);
|
||||
state.scanning = false;
|
||||
state.manualInput = false;
|
||||
state.empty = false;
|
||||
state.errorMessage = '';
|
||||
state.record = null;
|
||||
return true;
|
||||
}
|
||||
} catch (_err) {
|
||||
// 落入下方失败回退
|
||||
}
|
||||
// #endif
|
||||
|
||||
state.scanning = false;
|
||||
state.manualInput = true;
|
||||
state.record = null;
|
||||
@@ -285,3 +419,123 @@ export async function submitScan(state: ScanState): Promise<NFCRecord | null> {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 可写 NDEF URI:有合法 cardId 才生成;否则空串表示无可写内容
|
||||
*/
|
||||
export function buildNdefUri(cardId: string | number | null | undefined): string {
|
||||
const id = parseCardId(cardId);
|
||||
if (id == null) {
|
||||
return '';
|
||||
}
|
||||
return `https://omnicard.ai/c/${id}`;
|
||||
}
|
||||
|
||||
function toastWriteUnavailable(): void {
|
||||
if (typeof uni === 'undefined' || typeof uni.showToast !== 'function') {
|
||||
return;
|
||||
}
|
||||
uni.showToast({
|
||||
title: resolveScanText('writeUnavailable'),
|
||||
icon: 'none',
|
||||
});
|
||||
}
|
||||
|
||||
function toastWriteFailed(): void {
|
||||
if (typeof uni === 'undefined' || typeof uni.showToast !== 'function') {
|
||||
return;
|
||||
}
|
||||
uni.showToast({
|
||||
title: resolveScanText('writeFailed'),
|
||||
icon: 'none',
|
||||
});
|
||||
}
|
||||
|
||||
function toastWriteCancelled(): void {
|
||||
if (typeof uni === 'undefined' || typeof uni.showToast !== 'function') {
|
||||
return;
|
||||
}
|
||||
uni.showToast({
|
||||
title: resolveScanText('writeCancelled'),
|
||||
icon: 'none',
|
||||
});
|
||||
}
|
||||
|
||||
function isWriteCancelledError(err: { errMsg?: string } | null | undefined): boolean {
|
||||
if (err == null || err.errMsg == null) {
|
||||
return false;
|
||||
}
|
||||
const msg = String(err.errMsg).toLowerCase();
|
||||
return msg.indexOf('cancel') >= 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* 贴卡写入 NDEF:仅系统写入接口的 success 回调才 toast 已写入。
|
||||
* 天线未写 / 取消 / 失败 / 无可写内容均不得报写入成功。
|
||||
*/
|
||||
export function writeNdefToTag(uri: string | null | undefined): Promise<boolean> {
|
||||
const payload = uri == null ? '' : String(uri).trim();
|
||||
if (payload === '') {
|
||||
toastWriteUnavailable();
|
||||
return Promise.resolve(false);
|
||||
}
|
||||
|
||||
return new Promise<boolean>(resolve => {
|
||||
try {
|
||||
const adapter = readNfcAdapter();
|
||||
if (adapter == null) {
|
||||
toastWriteFailed();
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
const ndef = adapter.getNdef();
|
||||
if (ndef == null) {
|
||||
toastWriteFailed();
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
|
||||
const runWrite = (): void => {
|
||||
ndef.writeNdefMessage({
|
||||
uris: [payload],
|
||||
success: () => {
|
||||
uni.showToast({
|
||||
title: resolveScanText('writeSuccess'),
|
||||
icon: 'none',
|
||||
});
|
||||
resolve(true);
|
||||
},
|
||||
fail: (err: { errMsg?: string }) => {
|
||||
if (isWriteCancelledError(err)) {
|
||||
toastWriteCancelled();
|
||||
} else {
|
||||
toastWriteFailed();
|
||||
}
|
||||
resolve(false);
|
||||
},
|
||||
});
|
||||
};
|
||||
|
||||
if (typeof ndef.connect === 'function') {
|
||||
ndef.connect({
|
||||
success: () => {
|
||||
runWrite();
|
||||
},
|
||||
fail: (err: { errMsg?: string }) => {
|
||||
if (isWriteCancelledError(err)) {
|
||||
toastWriteCancelled();
|
||||
} else {
|
||||
toastWriteFailed();
|
||||
}
|
||||
resolve(false);
|
||||
},
|
||||
});
|
||||
return;
|
||||
}
|
||||
runWrite();
|
||||
} catch (_err) {
|
||||
toastWriteFailed();
|
||||
resolve(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户