- uni-app x官方无内置蓝牙API(文档明确见插件市场),为实现去插件化,工程整体迁移为uni-app vue3(vue/ts),App端内置完整BLE API,标准基座即可运行 - 新建src/BluetoothManager.ts:基于uni内置BLE API(createBLEConnection/notifyBLECharacteristicValueChange/writeBLECharacteristicValue(writeNoResponse)/setBLEMTU/getBLEDeviceRSSI等),保持原插件type约定,页面业务逻辑不变 - 删除uni_modules/android-ble及未使用的lime-*、uni-wifi;保留uni-icons、uni-scss - uts模块全部移植为ts(src/mcm、api、common):UTSJSONObject改Record<string,any>,适配vue3 getStorageSync空值返回空串的差异 - 7个页面uvue转vue:模板不变,显式import ref与生命周期钩子;OTA固件读取由java.io改为plus.io+uni.base64ToArrayBuffer - manifest.json去uni-app-x改app-plus结构,补Android 12+蓝牙权限;删除platformConfig.json;运行改用标准基座
165 lines
4.2 KiB
TypeScript
165 lines
4.2 KiB
TypeScript
let recordToken: string | null = null
|
||
let recordUserName: string | null = null
|
||
let recordCharging: string | null = null
|
||
|
||
// 读取本地存储字符串;vue3 的 getStorageSync 无 key 时返回 "",统一转成 null
|
||
function getStorageStr(key: string): string | null {
|
||
const t = uni.getStorageSync(key) as string
|
||
return (t != null && t.length > 0) ? t : null
|
||
}
|
||
|
||
// 保存 token(内存 + 本地)
|
||
export function setToken(token: string) {
|
||
recordToken = token
|
||
uni.setStorageSync('loginToken', token)
|
||
}
|
||
|
||
// 读取 token
|
||
export function getToken(): string | null {
|
||
if (recordToken != null) {
|
||
return recordToken
|
||
}
|
||
recordToken = getStorageStr('loginToken')
|
||
return recordToken
|
||
}
|
||
|
||
// 退出登录时清掉
|
||
export function clearAllData() {
|
||
recordToken = null
|
||
recordUserName = null
|
||
recordCharging = null
|
||
uni.removeStorageSync('loginToken')
|
||
uni.removeStorageSync('loginUserName')
|
||
uni.removeStorageSync('ChargingFlag')
|
||
|
||
}
|
||
|
||
export function checkToken(): boolean {
|
||
const t = getToken()
|
||
if(t == null || (t.length) == 0) {
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
|
||
// 保存用户名
|
||
export function setUserName(userName: string) {
|
||
recordUserName = userName
|
||
uni.setStorageSync('loginUserName', userName)
|
||
}
|
||
|
||
// 读取用户名
|
||
export function getUserName(): string | null {
|
||
if (recordUserName != null) {
|
||
return recordUserName
|
||
}
|
||
recordUserName = getStorageStr('loginUserName')
|
||
return recordUserName
|
||
}
|
||
|
||
|
||
// 保存充电标记
|
||
export function setCharging(flag: string) {
|
||
recordCharging = flag
|
||
uni.setStorageSync('ChargingFlag', flag)
|
||
}
|
||
|
||
// 读取充电标记
|
||
export function getCharging(): string | null {
|
||
if (recordCharging != null) {
|
||
return recordCharging
|
||
}
|
||
recordCharging = getStorageStr('ChargingFlag')
|
||
return recordCharging
|
||
}
|
||
|
||
// 清除充电标记
|
||
export function clearCharging() {
|
||
recordCharging = null
|
||
uni.removeStorageSync('ChargingFlag')
|
||
}
|
||
|
||
// 保存登录账号密码(记住密码)
|
||
export function setSavedAccount(account: string, password: string) {
|
||
uni.setStorageSync('savedAccount', account)
|
||
uni.setStorageSync('savedPassword', password)
|
||
}
|
||
|
||
// 读取保存的账号
|
||
export function getSavedAccount(): string {
|
||
return getStorageStr('savedAccount') ?? ""
|
||
}
|
||
|
||
// 读取保存的密码
|
||
export function getSavedPassword(): string {
|
||
return getStorageStr('savedPassword') ?? ""
|
||
}
|
||
|
||
// 清除保存的账号密码
|
||
export function clearSavedAccount() {
|
||
uni.removeStorageSync('savedAccount')
|
||
uni.removeStorageSync('savedPassword')
|
||
}
|
||
|
||
// 历史账号记录
|
||
export type AccountRecord = {
|
||
account: string,
|
||
password: string
|
||
}
|
||
|
||
const ACCOUNT_HISTORY_MAX = 5
|
||
|
||
// 读取历史账号(最新登录的在前)
|
||
export function getAccountHistory(): AccountRecord[] {
|
||
const result: AccountRecord[] = []
|
||
const raw = getStorageStr('accountHistory')
|
||
if (raw == null) {
|
||
return result
|
||
}
|
||
try {
|
||
const arr = JSON.parse(raw) as Record<string, any>[] | null
|
||
if (arr == null) return result
|
||
for (let i = 0; i < arr.length; i++) {
|
||
const obj = arr[i]
|
||
const acc = obj["account"] as string | null
|
||
const pwd = obj["password"] as string | null
|
||
if (acc != null && pwd != null) {
|
||
const rec: AccountRecord = { account: acc, password: pwd }
|
||
result.push(rec)
|
||
}
|
||
}
|
||
} catch (e) {
|
||
console.log('parse accountHistory error = ', e)
|
||
}
|
||
return result
|
||
}
|
||
|
||
// 保存一条历史账号(同账号去重置顶,最多保留 5 条)
|
||
export function addAccountHistory(account: string, password: string) {
|
||
const old = getAccountHistory()
|
||
const list: AccountRecord[] = []
|
||
const first: AccountRecord = { account: account, password: password }
|
||
list.push(first)
|
||
for (let i = 0; i < old.length && list.length < ACCOUNT_HISTORY_MAX; i++) {
|
||
const item = old[i]
|
||
if (item.account != account) {
|
||
list.push(item)
|
||
}
|
||
}
|
||
uni.setStorageSync('accountHistory', JSON.stringify(list))
|
||
}
|
||
|
||
// 删除指定账号的历史记录
|
||
export function removeAccountHistory(account: string) {
|
||
const old = getAccountHistory()
|
||
const list: AccountRecord[] = []
|
||
for (let i = 0; i < old.length; i++) {
|
||
const item = old[i]
|
||
if (item.account != account) {
|
||
list.push(item)
|
||
}
|
||
}
|
||
uni.setStorageSync('accountHistory', JSON.stringify(list))
|
||
}
|