优化登录记住我体验
This commit is contained in:
@@ -2,6 +2,7 @@ const authApi = require('../../api/auth')
|
||||
const { BRAND_CONFIG } = require('../../utils/config')
|
||||
|
||||
const PHONE_REGEX = /^1[3-9]\d{9}$/
|
||||
const LOGIN_PREFERENCE_KEY = 'fingertip_manager_login_preference'
|
||||
|
||||
function buildSession(data) {
|
||||
const userInfo = data.userInfo || data.user || {}
|
||||
@@ -60,6 +61,22 @@ function assertShopSession(session) {
|
||||
return session
|
||||
}
|
||||
|
||||
function getLoginPreference() {
|
||||
try {
|
||||
return wx.getStorageSync(LOGIN_PREFERENCE_KEY) || {}
|
||||
} catch (error) {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
function setLoginPreference(preference) {
|
||||
wx.setStorageSync(LOGIN_PREFERENCE_KEY, preference || {})
|
||||
}
|
||||
|
||||
function clearLoginPreference() {
|
||||
wx.removeStorageSync(LOGIN_PREFERENCE_KEY)
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
loginMode: 'phone',
|
||||
@@ -83,6 +100,19 @@ Page({
|
||||
switchingShop: false
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
const preference = getLoginPreference()
|
||||
if (!preference.rememberMe) {
|
||||
return
|
||||
}
|
||||
this.setData({
|
||||
loginMode: preference.loginMode === 'password' ? 'password' : 'phone',
|
||||
rememberMe: true,
|
||||
'form.account': preference.account || '',
|
||||
'form.phone': preference.phone || ''
|
||||
})
|
||||
},
|
||||
|
||||
onInput(event) {
|
||||
const field = event.currentTarget.dataset.field
|
||||
this.setData({
|
||||
@@ -138,8 +168,24 @@ Page({
|
||||
},
|
||||
|
||||
toggleRememberMe() {
|
||||
this.setData({
|
||||
rememberMe: !this.data.rememberMe
|
||||
const rememberMe = !this.data.rememberMe
|
||||
this.setData({ rememberMe })
|
||||
if (!rememberMe) {
|
||||
clearLoginPreference()
|
||||
}
|
||||
},
|
||||
|
||||
saveLoginPreference() {
|
||||
if (!this.data.rememberMe) {
|
||||
clearLoginPreference()
|
||||
return
|
||||
}
|
||||
const form = this.data.form
|
||||
setLoginPreference({
|
||||
rememberMe: true,
|
||||
loginMode: this.data.loginMode,
|
||||
account: String(form.account || '').trim(),
|
||||
phone: String(form.phone || '').trim()
|
||||
})
|
||||
},
|
||||
|
||||
@@ -286,6 +332,7 @@ Page({
|
||||
const session = await this.loginForShop(shopId)
|
||||
const app = getApp()
|
||||
app.setSession(session)
|
||||
this.saveLoginPreference()
|
||||
this.closeShopSelector()
|
||||
wx.reLaunch({ url: '/pages/planning/index' })
|
||||
},
|
||||
@@ -346,6 +393,7 @@ Page({
|
||||
return
|
||||
}
|
||||
app.setSession(assertShopSession(session))
|
||||
this.saveLoginPreference()
|
||||
wx.reLaunch({ url: '/pages/planning/index' })
|
||||
} catch (error) {
|
||||
if (isShopSelectionRequiredError(error) || await this.tryOpenLoginShopSelector()) {
|
||||
|
||||
Reference in New Issue
Block a user