diff --git a/miniprogram/images/share/share.jpg b/miniprogram/images/share/share.jpg new file mode 100644 index 0000000..3d6c7fc Binary files /dev/null and b/miniprogram/images/share/share.jpg differ diff --git a/miniprogram/lefu/index.ts b/miniprogram/lefu/index.ts index c182766..183fad0 100644 --- a/miniprogram/lefu/index.ts +++ b/miniprogram/lefu/index.ts @@ -8,4 +8,6 @@ export type { LeFuConfig, DeviceSetting, DeviceMember, + DeviceInfo, + FirmwareVersion, } from './types' diff --git a/miniprogram/lefu/service.ts b/miniprogram/lefu/service.ts index fd2d733..e1d7bf2 100644 --- a/miniprogram/lefu/service.ts +++ b/miniprogram/lefu/service.ts @@ -1,3 +1,4 @@ +import { get } from '../utils/request/index' import type { RawDevice, LeFuPlugin, @@ -9,14 +10,18 @@ import type { LeFuConfig, DeviceSetting, DeviceMember, + DeviceInfo, + FirmwareVersion, } from './types' /** 乐福 SDK 配置 */ const LEFU_CONFIG: LeFuConfig = { key: 'lefudc91611833e18d94', secret: 'eQ50JYimMp0X7uhNLj6D3lTEk4TUUvEG7dvaqKM9t1U=', - domain1: 'http://device.shuziweidao.com:80/gateway', - domain2: 'http://device.shuziweidao.com:80/weight', + domain1: 'http://192.168.1.31:8889', // 测试环境(老版本 无鉴权) + domain2: 'http://192.168.1.31:8889/weight', // 测试环境(新版本 有鉴权) + // domain1: 'http://device.shuziweidao.com:80/gateway', // 正式环境 + // domain2: 'http://device.shuziweidao.com:80/weight', // 正式环境 } /** 支持的设备型号配置列表(5款) */ @@ -76,6 +81,9 @@ class LeFuService { /** 保活心跳定时器 */ private _keepAliveTimer: number | null = null + /** 连接后从 deviceInfo 事件获取的固件信息 */ + private _deviceInfo: DeviceInfo | null = null + /** 回调注册(页面通过 on* 方法注册,stopScan 时清空) */ private _onDevicesListCb: ((devices: ScannedDevice[]) => void) | null = null private _onConnectStateCb: ((state: string) => void) | null = null @@ -161,6 +169,11 @@ class LeFuService { this._activeProtocol = res }) + // 连接成功后获取固件信息(用于后续 OTA 版本比对) + plugin.bus.subscribe('deviceInfo', (res: DeviceInfo) => { + this._deviceInfo = res + }) + // 连接成功 plugin.bus.subscribe('deviceConnect', () => { this._reconnectCount = 0 @@ -225,6 +238,7 @@ class LeFuService { this._stopReconnectTimer() this._p.Blue.stop() this._activeProtocol = null + this._deviceInfo = null this._onDevicesListCb = null this._onConnectStateCb = null this._onProgressCb = null @@ -326,6 +340,61 @@ class LeFuService { }) } + // ─── OTA 升级 ───────────────────────────────── + + /** + * 检查固件版本并按需触发 OTA 升级 + * @returns true = 已触发升级,false = 无需升级 + * 调用时机由业务层决定,不自动触发 + */ + checkAndUpgrade(): Promise { + return new Promise((resolve, reject) => { + if (!this._activeProtocol) { + reject(new Error('[LeFu] 设备未连接')) + return + } + if (!this._deviceInfo) { + reject(new Error('[LeFu] 固件信息未就绪,请稍后重试')) + return + } + + const { firmwareRevision, modelNumber } = this._deviceInfo + const [mcuVer, bleVer, wifiVer, resVer] = (firmwareRevision || '').split('.') + + // 设备单段版本(如 "006")vs 服务端 x.y.z 格式(如 "0.0.6")比对 + const isServerNewer = (deviceVer: string, serverVer: string): boolean => { + const deviceNum = parseInt(deviceVer, 10) + const serverNum = (serverVer || '').split('.').map(v => parseInt(v, 10)).reduce((acc, v) => acc * 1000 + v, 0) + return serverNum > deviceNum + } + + get('weighingScale/v2/firmware/version', { type: modelNumber }, { loading: false }) + .then(res => { + const { mcuVersion, bleVersion, wifiVersion, resVersion } = res.result + const needUpdate = + isServerNewer(mcuVer, mcuVersion) || + isServerNewer(bleVer, bleVersion) || + isServerNewer(wifiVer, wifiVersion) || + isServerNewer(resVer, resVersion) + + if (!needUpdate) { + resolve(false) + return + } + + // status: false = 成功,true = 失败 + this._activeProtocol.codeOtaUpdate((status: boolean) => { + if (!status) { + resolve(true) + } else { + reject(new Error('[LeFu] OTA 升级失败')) + } + }) + }) + .catch(reject) + }) + } + // ─── 私有:保活与重连 ───────────────────────── private _startKeepAlive(): void { diff --git a/miniprogram/lefu/types.ts b/miniprogram/lefu/types.ts index 80270d3..086eceb 100644 --- a/miniprogram/lefu/types.ts +++ b/miniprogram/lefu/types.ts @@ -80,6 +80,23 @@ export interface LeFuConfig { domain2: string } +/** 设备固件信息(deviceInfo 事件返回) */ +export interface DeviceInfo { + /** 固件版本号,格式:mcu.ble.wifi.res,如 "006.005.004.305" */ + firmwareRevision: string + /** 设备型号,用于请求服务端版本接口 */ + modelNumber: string + [key: string]: any +} + +/** 服务端固件版本(firmware/version 接口 result 字段) */ +export interface FirmwareVersion { + mcuVersion: string + bleVersion: string + wifiVersion: string + resVersion: string +} + /** 下发给设备的成员信息(lefu 层使用,业务层转换后传入) */ export interface DeviceMember { /** 成员唯一 ID */ diff --git a/miniprogram/pages/home/home.ts b/miniprogram/pages/home/home.ts index 451f7c8..4446f96 100644 --- a/miniprogram/pages/home/home.ts +++ b/miniprogram/pages/home/home.ts @@ -33,7 +33,11 @@ Page({ onReachBottom() {}, onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent { - return {} + return { + title: '智能蓝牙秤', + path: '/pages/home/home', + imageUrl: '/images/share/share.jpg', + } }, /** 加载首页数据(当前使用 mock,后续替换为真实接口) */ diff --git a/miniprogram/pages/login/login.ts b/miniprogram/pages/login/login.ts index f3fdf4a..5d94cc5 100644 --- a/miniprogram/pages/login/login.ts +++ b/miniprogram/pages/login/login.ts @@ -3,6 +3,14 @@ Page({ loading: false, }, + onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent { + return { + title: '智能蓝牙秤', + path: '/pages/home/home', + imageUrl: '/images/share/share.jpg', + } + }, + /* 微信一键登录 */ onLogin() { wx.navigateTo({ diff --git a/miniprogram/pages/my/my.ts b/miniprogram/pages/my/my.ts index d9046bc..bd65cf6 100644 --- a/miniprogram/pages/my/my.ts +++ b/miniprogram/pages/my/my.ts @@ -1,5 +1,13 @@ Page({ - data: {}, + data: {}, - onLoad() {}, + onLoad() {}, + + onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent { + return { + title: '智能蓝牙秤', + path: '/pages/home/home', + imageUrl: '/images/share/share.jpg', + } + }, }) diff --git a/miniprogram/utils/request/index.ts b/miniprogram/utils/request/index.ts index e7a75ca..92b1445 100644 --- a/miniprogram/utils/request/index.ts +++ b/miniprogram/utils/request/index.ts @@ -2,7 +2,8 @@ import type { ApiResponse, HttpMethod, RequestOptions } from './types' export type { ApiResponse, RequestOptions } -const BASE_URL = 'https://device.shuziweidao.com/gateway/' +const BASE_URL = 'http://192.168.1.31:8889' // 测试环境 +// const BASE_URL = 'https://device.shuziweidao.com/gateway' // 正式环境 let _loadingCount = 0