[AI Generated]: feat(*): 补全分享功能、新增 lefu OTA 升级封装及多环境请求配置

This commit is contained in:
17792275749
2026-05-14 11:01:59 +08:00
parent 3f12e34afd
commit 89d4832c7c
8 changed files with 115 additions and 6 deletions
Binary file not shown.

After

Width:  |  Height:  |  Size: 408 KiB

+2
View File
@@ -8,4 +8,6 @@ export type {
LeFuConfig, LeFuConfig,
DeviceSetting, DeviceSetting,
DeviceMember, DeviceMember,
DeviceInfo,
FirmwareVersion,
} from './types' } from './types'
+71 -2
View File
@@ -1,3 +1,4 @@
import { get } from '../utils/request/index'
import type { import type {
RawDevice, RawDevice,
LeFuPlugin, LeFuPlugin,
@@ -9,14 +10,18 @@ import type {
LeFuConfig, LeFuConfig,
DeviceSetting, DeviceSetting,
DeviceMember, DeviceMember,
DeviceInfo,
FirmwareVersion,
} from './types' } from './types'
/** 乐福 SDK 配置 */ /** 乐福 SDK 配置 */
const LEFU_CONFIG: LeFuConfig = { const LEFU_CONFIG: LeFuConfig = {
key: 'lefudc91611833e18d94', key: 'lefudc91611833e18d94',
secret: 'eQ50JYimMp0X7uhNLj6D3lTEk4TUUvEG7dvaqKM9t1U=', secret: 'eQ50JYimMp0X7uhNLj6D3lTEk4TUUvEG7dvaqKM9t1U=',
domain1: 'http://device.shuziweidao.com:80/gateway', domain1: 'http://192.168.1.31:8889', // 测试环境(老版本 无鉴权)
domain2: 'http://device.shuziweidao.com:80/weight', domain2: 'http://192.168.1.31:8889/weight', // 测试环境(新版本 有鉴权)
// domain1: 'http://device.shuziweidao.com:80/gateway', // 正式环境
// domain2: 'http://device.shuziweidao.com:80/weight', // 正式环境
} }
/** 支持的设备型号配置列表(5款) */ /** 支持的设备型号配置列表(5款) */
@@ -76,6 +81,9 @@ class LeFuService {
/** 保活心跳定时器 */ /** 保活心跳定时器 */
private _keepAliveTimer: number | null = null private _keepAliveTimer: number | null = null
/** 连接后从 deviceInfo 事件获取的固件信息 */
private _deviceInfo: DeviceInfo | null = null
/** 回调注册(页面通过 on* 方法注册,stopScan 时清空) */ /** 回调注册(页面通过 on* 方法注册,stopScan 时清空) */
private _onDevicesListCb: ((devices: ScannedDevice[]) => void) | null = null private _onDevicesListCb: ((devices: ScannedDevice[]) => void) | null = null
private _onConnectStateCb: ((state: string) => void) | null = null private _onConnectStateCb: ((state: string) => void) | null = null
@@ -161,6 +169,11 @@ class LeFuService {
this._activeProtocol = res this._activeProtocol = res
}) })
// 连接成功后获取固件信息(用于后续 OTA 版本比对)
plugin.bus.subscribe('deviceInfo', (res: DeviceInfo) => {
this._deviceInfo = res
})
// 连接成功 // 连接成功
plugin.bus.subscribe('deviceConnect', () => { plugin.bus.subscribe('deviceConnect', () => {
this._reconnectCount = 0 this._reconnectCount = 0
@@ -225,6 +238,7 @@ class LeFuService {
this._stopReconnectTimer() this._stopReconnectTimer()
this._p.Blue.stop() this._p.Blue.stop()
this._activeProtocol = null this._activeProtocol = null
this._deviceInfo = null
this._onDevicesListCb = null this._onDevicesListCb = null
this._onConnectStateCb = null this._onConnectStateCb = null
this._onProgressCb = null this._onProgressCb = null
@@ -326,6 +340,61 @@ class LeFuService {
}) })
} }
// ─── OTA 升级 ─────────────────────────────────
/**
* 检查固件版本并按需触发 OTA 升级
* @returns true = 已触发升级,false = 无需升级
* 调用时机由业务层决定,不自动触发
*/
checkAndUpgrade(): Promise<boolean> {
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<FirmwareVersion>('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 { private _startKeepAlive(): void {
+17
View File
@@ -80,6 +80,23 @@ export interface LeFuConfig {
domain2: string 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 层使用,业务层转换后传入) */ /** 下发给设备的成员信息(lefu 层使用,业务层转换后传入) */
export interface DeviceMember { export interface DeviceMember {
/** 成员唯一 ID */ /** 成员唯一 ID */
+5 -1
View File
@@ -33,7 +33,11 @@ Page({
onReachBottom() {}, onReachBottom() {},
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent { onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
return {} return {
title: '智能蓝牙秤',
path: '/pages/home/home',
imageUrl: '/images/share/share.jpg',
}
}, },
/** 加载首页数据(当前使用 mock,后续替换为真实接口) */ /** 加载首页数据(当前使用 mock,后续替换为真实接口) */
+8
View File
@@ -3,6 +3,14 @@ Page({
loading: false, loading: false,
}, },
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
return {
title: '智能蓝牙秤',
path: '/pages/home/home',
imageUrl: '/images/share/share.jpg',
}
},
/* 微信一键登录 */ /* 微信一键登录 */
onLogin() { onLogin() {
wx.navigateTo({ wx.navigateTo({
+10 -2
View File
@@ -1,5 +1,13 @@
Page({ Page({
data: {}, data: {},
onLoad() {}, onLoad() {},
onShareAppMessage(): WechatMiniprogram.Page.ICustomShareContent {
return {
title: '智能蓝牙秤',
path: '/pages/home/home',
imageUrl: '/images/share/share.jpg',
}
},
}) })
+2 -1
View File
@@ -2,7 +2,8 @@ import type { ApiResponse, HttpMethod, RequestOptions } from './types'
export type { ApiResponse, RequestOptions } 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 let _loadingCount = 0