| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- /** 本地历史账号(登录页「记住密码」与个人中心「切换账号」共用) */
- export const LOGIN_ACCOUNTS_KEY = 'login_saved_accounts'
- export const LOGIN_PENDING_SWITCH_KEY = 'login_pending_switch'
- export function loadSavedAccounts() {
- try {
- const raw = uni.getStorageSync(LOGIN_ACCOUNTS_KEY)
- if (raw == null || raw === '') return []
- const arr = typeof raw === 'string' ? JSON.parse(raw) : raw
- return Array.isArray(arr) ? arr : []
- } catch (e) {
- return []
- }
- }
- export function saveAccounts(list) {
- try {
- uni.setStorageSync(LOGIN_ACCOUNTS_KEY, JSON.stringify(list))
- } catch (e) {}
- }
- /** 历史账号展示:姓名 + 空格 + 手机号(仅存本地) */
- export function formatSavedAccountLabel(acc) {
- const mobile = String(acc && acc.mobile != null ? acc.mobile : '').trim()
- const name = acc && acc.name != null ? String(acc.name).trim() : ''
- if (name && mobile) return `${name} ${mobile}`
- return mobile || name
- }
- /**
- * 切换账号:清会话后写入待登录信息,登录页 onLoad 消费并自动密码登录
- * @param {{ mobile: string, password: string }} account
- */
- export function setPendingSwitchAccount(account) {
- try {
- uni.setStorageSync(
- LOGIN_PENDING_SWITCH_KEY,
- JSON.stringify({
- mobile: String(account.mobile || '').trim(),
- password: account.password != null ? String(account.password) : ''
- })
- )
- } catch (e) {}
- }
- /** 读取并清除待切换账号;无效则返回 null */
- export function consumePendingSwitchAccount() {
- let raw
- try {
- raw = uni.getStorageSync(LOGIN_PENDING_SWITCH_KEY)
- } catch (e) {
- return null
- }
- try {
- uni.removeStorageSync(LOGIN_PENDING_SWITCH_KEY)
- } catch (e) {}
- if (raw == null || raw === '') return null
- try {
- const o = typeof raw === 'string' ? JSON.parse(raw) : raw
- const mobile = o && String(o.mobile || '').trim()
- const password = o && o.password != null ? String(o.password) : ''
- if (!mobile || !password) return null
- return { mobile, password }
- } catch (e) {
- return null
- }
- }
|