let recordToken: string | null = null let recordUserName: string | null = null let recordCharging: string | null = null // 保存 token(内存 + 本地) export function setToken(token: string) { recordToken = token uni.setStorageSync('loginToken', token) } // 读取 token export function getToken(): string | null { if (recordToken != null) { return recordToken } const t = uni.getStorageSync('loginToken') as string | null recordToken = t ?? null 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 } const t = uni.getStorageSync('loginUserName') as string | null recordUserName = t ?? null return recordUserName } // 保存充电标记 export function setCharging(flag: string) { recordCharging = flag uni.setStorageSync('ChargingFlag', flag) } // 读取充电标记 export function getCharging(): string | null { if (recordCharging != null) { return recordCharging } const t = uni.getStorageSync('ChargingFlag') as string | null recordCharging = t ?? null 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 { const t = uni.getStorageSync('savedAccount') as string | null return t ?? "" } // 读取保存的密码 export function getSavedPassword(): string { const t = uni.getStorageSync('savedPassword') as string | null return t ?? "" } // 清除保存的账号密码 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 = uni.getStorageSync('accountHistory') as string | null if (raw == null || raw.length == 0) { return result } try { const arr = JSON.parse(raw) as UTSJSONObject[] | 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: any) { 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)) }