优化登录记住我体验
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()) {
|
||||
|
||||
@@ -38,7 +38,7 @@
|
||||
</view>
|
||||
<view class="remember-row" bindtap="toggleRememberMe">
|
||||
<view class="agreement-check {{rememberMe ? 'checked' : ''}}">{{rememberMe ? '✓' : ''}}</view>
|
||||
<view class="agreement-copy">记住登录 1 小时</view>
|
||||
<view class="agreement-copy">记住我</view>
|
||||
</view>
|
||||
<button class="primary-btn login-btn" loading="{{loading}}" bindtap="submit">登录</button>
|
||||
<view class="auth-links">
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
.auth-page {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
justify-content: center;
|
||||
padding-top: 84rpx;
|
||||
justify-content: flex-start;
|
||||
padding-top: 40rpx;
|
||||
}
|
||||
|
||||
.brand {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
margin-bottom: 54rpx;
|
||||
margin-bottom: 34rpx;
|
||||
}
|
||||
|
||||
.logo {
|
||||
|
||||
Reference in New Issue
Block a user