2026-09-04 08:56:15 +08:00

165 lines
4.2 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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))
}