修复管理师端预约与登录体验
This commit is contained in:
@@ -4,11 +4,12 @@ const { BRAND_CONFIG } = require('../../utils/config')
|
||||
const PHONE_REGEX = /^1[3-9]\d{9}$/
|
||||
|
||||
function buildSession(data) {
|
||||
const userInfo = data.userInfo || data.user || {}
|
||||
return {
|
||||
...data,
|
||||
accessToken: data.accessToken || data.token,
|
||||
userInfo: data.userInfo || data.user || {},
|
||||
shopName: data.shopName || (data.shop && data.shop.shopName)
|
||||
userInfo,
|
||||
shopName: data.shopName || userInfo.shopName || (data.shop && data.shop.shopName)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -41,12 +42,22 @@ function buildShopOptionsFromSelection(selection) {
|
||||
const shops = selection && Array.isArray(selection.shops) ? selection.shops : []
|
||||
return shops.map(shop => ({
|
||||
id: shop.id,
|
||||
name: shop.shopName || shop.name || `店铺 ${shop.id}`
|
||||
name: shop.shopName || shop.name || `店铺 ${shop.id}`,
|
||||
description: selection && selection.message ? selection.message : ''
|
||||
}))
|
||||
}
|
||||
|
||||
function isShopSelectionRequiredError(error) {
|
||||
return !!(error && error.message && error.message.indexOf('请选择登录店铺') !== -1)
|
||||
const message = error && error.message ? error.message : ''
|
||||
return message.indexOf('选择店铺') !== -1 || message.indexOf('请选择店铺') !== -1 || message.indexOf('请选择登录店铺') !== -1
|
||||
}
|
||||
|
||||
function assertShopSession(session) {
|
||||
const userInfo = session.userInfo || {}
|
||||
if (!userInfo.shopId) {
|
||||
throw new Error('请选择店铺后再进入管理师端')
|
||||
}
|
||||
return session
|
||||
}
|
||||
|
||||
Page({
|
||||
@@ -63,6 +74,7 @@ Page({
|
||||
countdown: 0,
|
||||
countdownTimer: null,
|
||||
agreementAccepted: false,
|
||||
rememberMe: false,
|
||||
agreements: [],
|
||||
appName: BRAND_CONFIG.appName,
|
||||
shopSelectVisible: false,
|
||||
@@ -125,6 +137,12 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
toggleRememberMe() {
|
||||
this.setData({
|
||||
rememberMe: !this.data.rememberMe
|
||||
})
|
||||
},
|
||||
|
||||
buildAgreementPayload() {
|
||||
const agreementVersions = {}
|
||||
;(this.data.agreements || []).forEach(item => {
|
||||
@@ -162,15 +180,13 @@ Page({
|
||||
if (!shopOptions.length) {
|
||||
throw new Error('当前账号没有可用店铺,无法进入管理师端')
|
||||
}
|
||||
const app = getApp()
|
||||
app.setSession(session)
|
||||
if (shopOptions.length === 1) {
|
||||
return buildSession(await authApi.switchShop(shopOptions[0].id))
|
||||
return this.loginForShop(shopOptions[0].id)
|
||||
}
|
||||
this.setData({
|
||||
shopOptions,
|
||||
shopSelectVisible: true,
|
||||
shopSelectMode: 'switch'
|
||||
shopSelectMode: 'login'
|
||||
})
|
||||
return null
|
||||
},
|
||||
@@ -181,6 +197,7 @@ Page({
|
||||
return {
|
||||
phone: String(form.phone || '').trim(),
|
||||
verificationCode: String(form.code || '').trim(),
|
||||
rememberMe: !!this.data.rememberMe,
|
||||
...this.buildAgreementPayload(),
|
||||
...(shopId ? { shopId } : {})
|
||||
}
|
||||
@@ -188,6 +205,7 @@ Page({
|
||||
return {
|
||||
account: form.account,
|
||||
password: form.password,
|
||||
rememberMe: !!this.data.rememberMe,
|
||||
...(shopId ? { shopId } : {})
|
||||
}
|
||||
},
|
||||
@@ -199,6 +217,10 @@ Page({
|
||||
: authApi.login(data)
|
||||
},
|
||||
|
||||
async loginForShop(shopId) {
|
||||
return assertShopSession(buildSession(await this.requestLogin(shopId)))
|
||||
},
|
||||
|
||||
async loadLoginShopOptions() {
|
||||
const form = this.data.form
|
||||
const selection = this.data.loginMode === 'phone'
|
||||
@@ -228,6 +250,15 @@ Page({
|
||||
})
|
||||
},
|
||||
|
||||
async tryOpenLoginShopSelector() {
|
||||
try {
|
||||
await this.openLoginShopSelector()
|
||||
return true
|
||||
} catch (error) {
|
||||
return false
|
||||
}
|
||||
},
|
||||
|
||||
async selectLoginShop(event) {
|
||||
if (this.data.switchingShop) {
|
||||
return
|
||||
@@ -252,10 +283,7 @@ Page({
|
||||
},
|
||||
|
||||
async loginWithSelectedShop(shopId) {
|
||||
const session = await this.resolveLoginSession(await this.requestLogin(shopId))
|
||||
if (!session) {
|
||||
return
|
||||
}
|
||||
const session = await this.loginForShop(shopId)
|
||||
const app = getApp()
|
||||
app.setSession(session)
|
||||
this.closeShopSelector()
|
||||
@@ -296,7 +324,7 @@ Page({
|
||||
async submit() {
|
||||
const form = this.data.form
|
||||
if (this.data.loginMode === 'password' && (!form.account || !form.password)) {
|
||||
wx.showToast({ title: '请填写手机号和密码', icon: 'none' })
|
||||
wx.showToast({ title: '请填写账号和密码', icon: 'none' })
|
||||
return
|
||||
}
|
||||
if (this.data.loginMode === 'phone' && (!PHONE_REGEX.test(String(form.phone || '').trim()) || !String(form.code || '').trim())) {
|
||||
@@ -311,18 +339,20 @@ Page({
|
||||
}
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const data = await this.requestLogin('')
|
||||
const app = getApp()
|
||||
const data = await this.requestLogin('')
|
||||
const session = await this.resolveLoginSession(data)
|
||||
if (!session) {
|
||||
return
|
||||
}
|
||||
app.setSession(session)
|
||||
app.setSession(assertShopSession(session))
|
||||
wx.reLaunch({ url: '/pages/planning/index' })
|
||||
} catch (error) {
|
||||
if (isShopSelectionRequiredError(error)) {
|
||||
if (isShopSelectionRequiredError(error) || await this.tryOpenLoginShopSelector()) {
|
||||
try {
|
||||
await this.openLoginShopSelector()
|
||||
if (!this.data.shopSelectVisible) {
|
||||
await this.openLoginShopSelector()
|
||||
}
|
||||
} catch (shopError) {
|
||||
wx.showToast({ title: shopError.message || '店铺列表加载失败', icon: 'none' })
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user