完善管理师端多店铺登录选择
This commit is contained in:
@@ -20,6 +20,35 @@ function getSelectableShopIds(session) {
|
||||
return Array.isArray(session.shopIds) ? session.shopIds : []
|
||||
}
|
||||
|
||||
function getSelectableShopNames(session) {
|
||||
const userInfo = session.userInfo || {}
|
||||
if (Array.isArray(userInfo.shopNames)) {
|
||||
return userInfo.shopNames
|
||||
}
|
||||
return Array.isArray(session.shopNames) ? session.shopNames : []
|
||||
}
|
||||
|
||||
function buildShopOptions(session) {
|
||||
const shopIds = getSelectableShopIds(session)
|
||||
const shopNames = getSelectableShopNames(session)
|
||||
return shopIds.map((shopId, index) => ({
|
||||
id: shopId,
|
||||
name: shopNames[index] || `店铺 ${shopId}`
|
||||
}))
|
||||
}
|
||||
|
||||
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}`
|
||||
}))
|
||||
}
|
||||
|
||||
function isShopSelectionRequiredError(error) {
|
||||
return !!(error && error.message && error.message.indexOf('请选择登录店铺') !== -1)
|
||||
}
|
||||
|
||||
Page({
|
||||
data: {
|
||||
loginMode: 'phone',
|
||||
@@ -35,7 +64,11 @@ Page({
|
||||
countdownTimer: null,
|
||||
agreementAccepted: false,
|
||||
agreements: [],
|
||||
appName: BRAND_CONFIG.appName
|
||||
appName: BRAND_CONFIG.appName,
|
||||
shopSelectVisible: false,
|
||||
shopSelectMode: '',
|
||||
shopOptions: [],
|
||||
switchingShop: false
|
||||
},
|
||||
|
||||
onInput(event) {
|
||||
@@ -122,13 +155,119 @@ Page({
|
||||
async resolveLoginSession(data) {
|
||||
const session = buildSession(data)
|
||||
const userInfo = session.userInfo || {}
|
||||
const shopIds = getSelectableShopIds(session)
|
||||
if (userInfo.shopId || shopIds.length !== 1) {
|
||||
if (userInfo.shopId) {
|
||||
return session
|
||||
}
|
||||
const shopOptions = buildShopOptions(session)
|
||||
if (!shopOptions.length) {
|
||||
throw new Error('当前账号没有可用店铺,无法进入管理师端')
|
||||
}
|
||||
const app = getApp()
|
||||
app.setSession(session)
|
||||
return buildSession(await authApi.switchShop(shopIds[0]))
|
||||
if (shopOptions.length === 1) {
|
||||
return buildSession(await authApi.switchShop(shopOptions[0].id))
|
||||
}
|
||||
this.setData({
|
||||
shopOptions,
|
||||
shopSelectVisible: true,
|
||||
shopSelectMode: 'switch'
|
||||
})
|
||||
return null
|
||||
},
|
||||
|
||||
buildLoginRequestData(shopId) {
|
||||
const form = this.data.form
|
||||
if (this.data.loginMode === 'phone') {
|
||||
return {
|
||||
phone: String(form.phone || '').trim(),
|
||||
verificationCode: String(form.code || '').trim(),
|
||||
...this.buildAgreementPayload(),
|
||||
...(shopId ? { shopId } : {})
|
||||
}
|
||||
}
|
||||
return {
|
||||
account: form.account,
|
||||
password: form.password,
|
||||
...(shopId ? { shopId } : {})
|
||||
}
|
||||
},
|
||||
|
||||
requestLogin(shopId) {
|
||||
const data = this.buildLoginRequestData(shopId)
|
||||
return this.data.loginMode === 'phone'
|
||||
? authApi.phoneLogin(data)
|
||||
: authApi.login(data)
|
||||
},
|
||||
|
||||
async loadLoginShopOptions() {
|
||||
const form = this.data.form
|
||||
const selection = this.data.loginMode === 'phone'
|
||||
? await authApi.getPhoneLoginShops(String(form.phone || '').trim())
|
||||
: await authApi.getLoginShops(String(form.account || '').trim())
|
||||
const shopOptions = buildShopOptionsFromSelection(selection)
|
||||
if (!shopOptions.length) {
|
||||
throw new Error((selection && selection.message) || '当前账号没有可用店铺,无法进入管理师端')
|
||||
}
|
||||
return shopOptions
|
||||
},
|
||||
|
||||
async openLoginShopSelector() {
|
||||
const shopOptions = await this.loadLoginShopOptions()
|
||||
this.setData({
|
||||
shopOptions,
|
||||
shopSelectVisible: true,
|
||||
shopSelectMode: 'login'
|
||||
})
|
||||
},
|
||||
|
||||
closeShopSelector() {
|
||||
this.setData({
|
||||
shopOptions: [],
|
||||
shopSelectMode: '',
|
||||
shopSelectVisible: false
|
||||
})
|
||||
},
|
||||
|
||||
async selectLoginShop(event) {
|
||||
if (this.data.switchingShop) {
|
||||
return
|
||||
}
|
||||
const shopId = event.currentTarget.dataset.id
|
||||
if (!shopId) {
|
||||
wx.showToast({ title: '请选择店铺', icon: 'none' })
|
||||
return
|
||||
}
|
||||
this.setData({ switchingShop: true })
|
||||
try {
|
||||
if (this.data.shopSelectMode === 'login') {
|
||||
await this.loginWithSelectedShop(shopId)
|
||||
} else {
|
||||
await this.switchSelectedShop(shopId)
|
||||
}
|
||||
} catch (error) {
|
||||
wx.showToast({ title: error.message || '进入店铺失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ switchingShop: false })
|
||||
}
|
||||
},
|
||||
|
||||
async loginWithSelectedShop(shopId) {
|
||||
const session = await this.resolveLoginSession(await this.requestLogin(shopId))
|
||||
if (!session) {
|
||||
return
|
||||
}
|
||||
const app = getApp()
|
||||
app.setSession(session)
|
||||
this.closeShopSelector()
|
||||
wx.reLaunch({ url: '/pages/planning/index' })
|
||||
},
|
||||
|
||||
async switchSelectedShop(shopId) {
|
||||
const session = buildSession(await authApi.switchShop(shopId))
|
||||
const app = getApp()
|
||||
app.setSession(session)
|
||||
this.closeShopSelector()
|
||||
wx.reLaunch({ url: '/pages/planning/index' })
|
||||
},
|
||||
|
||||
startCountdown() {
|
||||
@@ -172,21 +311,23 @@ Page({
|
||||
}
|
||||
this.setData({ loading: true })
|
||||
try {
|
||||
const data = this.data.loginMode === 'phone'
|
||||
? await authApi.phoneLogin({
|
||||
phone: String(form.phone || '').trim(),
|
||||
verificationCode: String(form.code || '').trim(),
|
||||
...this.buildAgreementPayload()
|
||||
})
|
||||
: await authApi.login({
|
||||
account: form.account,
|
||||
password: form.password
|
||||
})
|
||||
const data = await this.requestLogin('')
|
||||
const app = getApp()
|
||||
const session = await this.resolveLoginSession(data)
|
||||
if (!session) {
|
||||
return
|
||||
}
|
||||
app.setSession(session)
|
||||
wx.reLaunch({ url: '/pages/planning/index' })
|
||||
} catch (error) {
|
||||
if (isShopSelectionRequiredError(error)) {
|
||||
try {
|
||||
await this.openLoginShopSelector()
|
||||
} catch (shopError) {
|
||||
wx.showToast({ title: shopError.message || '店铺列表加载失败', icon: 'none' })
|
||||
}
|
||||
return
|
||||
}
|
||||
wx.showToast({ title: error.message || '登录失败', icon: 'none' })
|
||||
} finally {
|
||||
this.setData({ loading: false })
|
||||
|
||||
Reference in New Issue
Block a user