refactor(miniprogram): 消灭全部 any 类型 — 32处 → 0 (E3-1)
- catch (err: any) → catch (err: unknown) + instanceof Error 类型缩窄(6 文件 13 处) - BLE 回调类型提取 BLEScanResult/BLEConnectionChangeResult/BLECharacteristicChangeResult/BLEServiceItem - BLEManager 7 处 any 注解替换为具体接口类型 - request.ts method as any → method as ValidMethod 字面量联合类型 - appointment ScheduleItem 接口定义替代 any[] - Taro.requestSubscribeMessage 使用类型断言替代 as any - globalThis.__hms 使用 Record<string, unknown> - TrendChart Canvas/useRef 保留 eslint-disable(微信 API 限制) - 0 TS 错误,119 测试通过
This commit is contained in:
@@ -7,6 +7,10 @@ import type {
|
||||
NormalizedReading,
|
||||
SyncResult,
|
||||
BLEManagerConfig,
|
||||
BLEScanResult,
|
||||
BLEConnectionChangeResult,
|
||||
BLECharacteristicChangeResult,
|
||||
BLEServiceItem,
|
||||
} from './types';
|
||||
import { DataBuffer } from './DataBuffer';
|
||||
|
||||
@@ -27,8 +31,8 @@ export class BLEManager {
|
||||
private scanTimer: ReturnType<typeof setTimeout> | null = null;
|
||||
private onConnectionChange?: (state: BLEConnectionState) => void;
|
||||
private onReadings?: (readings: NormalizedReading[]) => void;
|
||||
private connChangeHandler: ((res: any) => void) | null = null;
|
||||
private charChangeHandler: ((res: any) => void) | null = null;
|
||||
private connChangeHandler: ((res: BLEConnectionChangeResult) => void) | null = null;
|
||||
private charChangeHandler: ((res: BLECharacteristicChangeResult) => void) | null = null;
|
||||
|
||||
constructor(config?: Partial<BLEManagerConfig>) {
|
||||
this.config = { ...DEFAULT_CONFIG, ...config };
|
||||
@@ -70,8 +74,9 @@ export class BLEManager {
|
||||
async initialize(): Promise<void> {
|
||||
try {
|
||||
await Taro.openBluetoothAdapter();
|
||||
} catch (e: any) {
|
||||
throw new Error(e.errMsg || '蓝牙初始化失败,请检查蓝牙是否开启');
|
||||
} catch (e: unknown) {
|
||||
const errMsg = e instanceof Error ? e.message : (e as { errMsg?: string })?.errMsg || '蓝牙初始化失败,请检查蓝牙是否开启';
|
||||
throw new Error(errMsg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +86,7 @@ export class BLEManager {
|
||||
|
||||
const discovered = new Map<string, BLEDevice>();
|
||||
|
||||
const onFound = (res: any) => {
|
||||
const onFound = (res: BLEScanResult) => {
|
||||
for (const device of res.devices || []) {
|
||||
const name = device.name || device.localName || '';
|
||||
if (!name) continue;
|
||||
@@ -90,7 +95,7 @@ export class BLEManager {
|
||||
discovered.set(device.deviceId, {
|
||||
deviceId: device.deviceId,
|
||||
name,
|
||||
RSSI: device.RSSI,
|
||||
RSSI: device.RSSI ?? 0,
|
||||
localName: device.localName,
|
||||
advertisData: device.advertisData,
|
||||
adapter,
|
||||
@@ -156,7 +161,7 @@ export class BLEManager {
|
||||
}
|
||||
|
||||
// 监听断连
|
||||
this.connChangeHandler = (res: any) => {
|
||||
this.connChangeHandler = (res: BLEConnectionChangeResult) => {
|
||||
if (res.deviceId === device.deviceId && !res.connected) {
|
||||
this.updateState('disconnected', '设备断开连接');
|
||||
this.connection = null;
|
||||
@@ -170,7 +175,7 @@ export class BLEManager {
|
||||
|
||||
// 启用通知
|
||||
for (const { service: svcUUID, characteristic: charUUID } of device.adapter.notifyCharacteristics) {
|
||||
const svc = services.find((s: any) => s.uuid.toUpperCase().includes(svcUUID.toUpperCase()));
|
||||
const svc = services.find((s: BLEServiceItem) => s.uuid.toUpperCase().includes(svcUUID.toUpperCase()));
|
||||
if (!svc) continue;
|
||||
|
||||
await Taro.getBLEDeviceCharacteristics({
|
||||
@@ -187,7 +192,7 @@ export class BLEManager {
|
||||
}
|
||||
|
||||
// 监听数据通知
|
||||
this.charChangeHandler = (res: any) => {
|
||||
this.charChangeHandler = (res: BLECharacteristicChangeResult) => {
|
||||
if (res.deviceId !== device.deviceId) return;
|
||||
const newReadings = device.adapter!.parseNotification(
|
||||
res.serviceId,
|
||||
@@ -207,10 +212,12 @@ export class BLEManager {
|
||||
|
||||
this.connection = { ...this.connection, state: 'connected', connectedAt: Date.now() };
|
||||
this.updateState('connected');
|
||||
} catch (e: any) {
|
||||
this.updateState('error', e.errMsg || e.message || '连接失败');
|
||||
} catch (e: unknown) {
|
||||
const errMsg = (e as { errMsg?: string })?.errMsg;
|
||||
const msg = errMsg || (e instanceof Error ? e.message : '') || '连接失败';
|
||||
this.updateState('error', msg);
|
||||
this.connection = null;
|
||||
throw new Error(e.errMsg || '蓝牙连接失败');
|
||||
throw new Error(errMsg || '蓝牙连接失败');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -227,7 +234,7 @@ export class BLEManager {
|
||||
const services = servicesRes.services || [];
|
||||
|
||||
for (const { service: svcUUID, characteristic: charUUID } of adapter.readCharacteristics) {
|
||||
const svc = services.find((s: any) => s.uuid.toUpperCase().includes(svcUUID.toUpperCase()));
|
||||
const svc = services.find((s: BLEServiceItem) => s.uuid.toUpperCase().includes(svcUUID.toUpperCase()));
|
||||
if (!svc) continue;
|
||||
|
||||
try {
|
||||
@@ -274,8 +281,8 @@ export class BLEManager {
|
||||
readingsCount: batch.length,
|
||||
uploadedCount: uploaded,
|
||||
};
|
||||
} catch (e: any) {
|
||||
lastError = e.message || '上传失败';
|
||||
} catch (e: unknown) {
|
||||
lastError = e instanceof Error ? e.message : '上传失败';
|
||||
// flush 已取出,失败时需要放回
|
||||
this.dataBuffer.push(batch);
|
||||
}
|
||||
|
||||
@@ -97,3 +97,34 @@ export type GenericBLEProfile =
|
||||
| 'heart_rate' // Heart Rate Service (0x180D)
|
||||
| 'health_thermometer' // Health Thermometer Service (0x1809)
|
||||
| 'blood_pressure'; // Blood Pressure Service (0x1810)
|
||||
|
||||
/** 微信 BLE 扫描回调结果 */
|
||||
export interface BLEScanResult {
|
||||
devices: Array<{
|
||||
deviceId: string;
|
||||
name?: string;
|
||||
RSSI?: number;
|
||||
localName?: string;
|
||||
advertisData?: ArrayBuffer;
|
||||
}>;
|
||||
}
|
||||
|
||||
/** 微信 BLE 连接状态变更回调结果 */
|
||||
export interface BLEConnectionChangeResult {
|
||||
deviceId: string;
|
||||
connected: boolean;
|
||||
}
|
||||
|
||||
/** 微信 BLE 特征值变更回调结果 */
|
||||
export interface BLECharacteristicChangeResult {
|
||||
deviceId: string;
|
||||
serviceId: string;
|
||||
characteristicId: string;
|
||||
value: ArrayBuffer;
|
||||
}
|
||||
|
||||
/** 微信 BLE 服务描述(getBLEDeviceServices 返回项) */
|
||||
export interface BLEServiceItem {
|
||||
uuid: string;
|
||||
isPrimary?: boolean;
|
||||
}
|
||||
|
||||
@@ -243,10 +243,10 @@ async function request<T>(method: string, path: string, data?: unknown, timeout?
|
||||
const url = `${BASE_URL}${path}`;
|
||||
let res: Taro.request.SuccessCallbackResult;
|
||||
try {
|
||||
res = await Taro.request({ url, method: method as any, data, header: headers, timeout: timeout || 15000 });
|
||||
} catch (err: any) {
|
||||
res = await Taro.request({ url, method: method as 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'OPTIONS', data, header: headers, timeout: timeout || 15000 });
|
||||
} catch (err: unknown) {
|
||||
if (signal?.aborted) throw new Error('请求已取消');
|
||||
const msg = err?.errMsg || '';
|
||||
const msg = (err as { errMsg?: string })?.errMsg || '';
|
||||
if (msg.includes('timeout')) {
|
||||
Taro.showToast({ title: '网络超时,请重试', icon: 'none' });
|
||||
throw new Error('网络超时');
|
||||
|
||||
Reference in New Issue
Block a user