448 lines
21 KiB
JavaScript
448 lines
21 KiB
JavaScript
/**
|
|
* Purpose: authApi supports index.js by holding or executing the auth api page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const authApi = require('../../api/auth')
|
|
/**
|
|
* Purpose: auth supports index.js by holding or executing the auth page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const auth = require('../../utils/auth')
|
|
/**
|
|
* Purpose: navigation supports index.js by holding or executing the navigation page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const navigation = require('../../utils/navigation')
|
|
const { runWithLoading } = require('../../utils/loading')
|
|
const sessionSwitch = require('../../../utils/session-switch')
|
|
|
|
/**
|
|
* Purpose: ACCOUNT_REGEX supports index.js by holding or executing the account regex page or API role.
|
|
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
const ACCOUNT_REGEX = /[^A-Za-z0-9]/g
|
|
|
|
Page({
|
|
/**
|
|
* Purpose: data supports index.js by holding or executing the data page or API role.
|
|
* Usage: import, call, read, or update this page-data from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
data: {
|
|
/**
|
|
* Purpose: redirect supports index.js by holding or executing the redirect page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
redirect: '',
|
|
/**
|
|
* Purpose: form supports index.js by holding or executing the form page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
form: {
|
|
/**
|
|
* Purpose: account supports index.js by holding or executing the account page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
account: '',
|
|
/**
|
|
* Purpose: password supports index.js by holding or executing the password page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
password: '',
|
|
/**
|
|
* Purpose: shopId supports index.js by holding or executing the shop id page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
shopId: ''
|
|
},
|
|
/**
|
|
* Purpose: loginShops supports index.js by holding or executing the login shops page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShops: [],
|
|
/**
|
|
* Purpose: loginShopIndex supports index.js by holding or executing the login shop index page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopIndex: -1,
|
|
/**
|
|
* Purpose: loginShopDisplayName supports index.js by holding or executing the login shop display name page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopDisplayName: '',
|
|
/**
|
|
* Purpose: allowShoplessLogin supports index.js by holding or executing the allow shopless login page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
allowShoplessLogin: false,
|
|
/**
|
|
* Purpose: loginShopMessage supports index.js by holding or executing the login shop message page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopMessage: '',
|
|
/**
|
|
* Purpose: accountStatusText supports index.js by holding or executing the account status text page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
accountStatusText: '',
|
|
/**
|
|
* Purpose: accountStatusType supports index.js by holding or executing the account status type page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
accountStatusType: '',
|
|
/**
|
|
* Purpose: loading supports index.js by holding or executing the loading page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loading: false
|
|
},
|
|
|
|
/**
|
|
* Purpose: onLoad supports index.js by holding or executing the on load page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
onLoad(options) {
|
|
this._lookupTicket = 0
|
|
this.setData({
|
|
/**
|
|
* Purpose: redirect supports index.js by holding or executing the redirect page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
redirect: options && options.redirect ? options.redirect : ''
|
|
})
|
|
|
|
if (auth.isLoggedIn()) {
|
|
navigation.redirectAfterLogin(this.data.redirect).catch(() => undefined)
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: onUnload supports index.js by holding or executing the on unload page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
onUnload() {
|
|
if (this._accountTimer) {
|
|
clearTimeout(this._accountTimer)
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: handleInputChange supports index.js by holding or executing the handle input change page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
handleInputChange(event) {
|
|
const field = event.currentTarget.dataset.field
|
|
let value = event.detail.value
|
|
|
|
if (field === 'account') {
|
|
value = String(value || '').replace(ACCOUNT_REGEX, '')
|
|
this.resetAccountState()
|
|
if (value) {
|
|
this.scheduleAccountLookup(value)
|
|
}
|
|
}
|
|
|
|
this.setData({
|
|
[`form.${field}`]: value
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Purpose: handleShopChange supports index.js by holding or executing the handle shop change page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
handleShopChange(event) {
|
|
const index = Number(event.detail.value)
|
|
const shop = this.data.loginShops[index]
|
|
|
|
this.setData({
|
|
/**
|
|
* Purpose: loginShopIndex supports index.js by holding or executing the login shop index page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopIndex: index,
|
|
/**
|
|
* Purpose: loginShopDisplayName supports index.js by holding or executing the login shop display name page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopDisplayName: shop ? shop.shopName || '' : '',
|
|
'form.shopId': shop ? shop.id : ''
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Purpose: resetAccountState supports index.js by holding or executing the reset account state page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
resetAccountState() {
|
|
if (this._accountTimer) {
|
|
clearTimeout(this._accountTimer)
|
|
}
|
|
|
|
this._lookupTicket += 1
|
|
|
|
this.setData({
|
|
/**
|
|
* Purpose: accountStatusText supports index.js by holding or executing the account status text page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
accountStatusText: '',
|
|
/**
|
|
* Purpose: accountStatusType supports index.js by holding or executing the account status type page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
accountStatusType: '',
|
|
/**
|
|
* Purpose: loginShops supports index.js by holding or executing the login shops page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShops: [],
|
|
/**
|
|
* Purpose: loginShopIndex supports index.js by holding or executing the login shop index page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopIndex: -1,
|
|
/**
|
|
* Purpose: loginShopDisplayName supports index.js by holding or executing the login shop display name page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopDisplayName: '',
|
|
/**
|
|
* Purpose: allowShoplessLogin supports index.js by holding or executing the allow shopless login page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
allowShoplessLogin: false,
|
|
/**
|
|
* Purpose: loginShopMessage supports index.js by holding or executing the login shop message page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopMessage: '',
|
|
'form.shopId': ''
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Purpose: scheduleAccountLookup supports index.js by holding or executing the schedule account lookup page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
scheduleAccountLookup(account) {
|
|
if (this._accountTimer) {
|
|
clearTimeout(this._accountTimer)
|
|
}
|
|
|
|
const ticket = this._lookupTicket + 1
|
|
this._lookupTicket = ticket
|
|
this._accountTimer = setTimeout(() => {
|
|
this.loadLoginShops(account, ticket)
|
|
}, 420)
|
|
},
|
|
|
|
/**
|
|
* Purpose: applyLoginShopSelection supports index.js by holding or executing the apply login shop selection page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
applyLoginShopSelection(selection) {
|
|
const shops = Array.isArray(selection && selection.shops) ? selection.shops : []
|
|
const allowShoplessLogin = Boolean(selection && selection.allowShoplessLogin)
|
|
let loginShopIndex = -1
|
|
let loginShopDisplayName = ''
|
|
let shopId = ''
|
|
|
|
if (!allowShoplessLogin && shops.length === 1) {
|
|
loginShopIndex = 0
|
|
loginShopDisplayName = shops[0].shopName || ''
|
|
shopId = shops[0].id
|
|
}
|
|
|
|
this.setData({
|
|
/**
|
|
* Purpose: loginShops supports index.js by holding or executing the login shops page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShops: shops,
|
|
allowShoplessLogin,
|
|
/**
|
|
* Purpose: loginShopMessage supports index.js by holding or executing the login shop message page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopMessage: (selection && selection.message) || '',
|
|
loginShopIndex,
|
|
loginShopDisplayName,
|
|
'form.shopId': shopId,
|
|
/**
|
|
* Purpose: accountStatusText supports index.js by holding or executing the account status text page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
accountStatusText: shops.length || allowShoplessLogin ? '该账号可用于后台登录' : '当前账号暂时不可用于后台登录',
|
|
/**
|
|
* Purpose: accountStatusType supports index.js by holding or executing the account status type page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
accountStatusType: shops.length || allowShoplessLogin ? 'success' : 'warning'
|
|
})
|
|
},
|
|
|
|
async loadLoginShops(account, ticket) {
|
|
if (!account) {
|
|
this.resetAccountState()
|
|
return
|
|
}
|
|
|
|
try {
|
|
const result = await authApi.getLoginShops(account)
|
|
if (ticket !== this._lookupTicket || this.data.form.account !== account) {
|
|
return
|
|
}
|
|
this.applyLoginShopSelection(result)
|
|
} catch (error) {
|
|
if (ticket !== this._lookupTicket || this.data.form.account !== account) {
|
|
return
|
|
}
|
|
this.setData({
|
|
/**
|
|
* Purpose: loginShops supports index.js by holding or executing the login shops page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShops: [],
|
|
/**
|
|
* Purpose: loginShopIndex supports index.js by holding or executing the login shop index page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopIndex: -1,
|
|
/**
|
|
* Purpose: loginShopDisplayName supports index.js by holding or executing the login shop display name page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopDisplayName: '',
|
|
/**
|
|
* Purpose: allowShoplessLogin supports index.js by holding or executing the allow shopless login page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
allowShoplessLogin: false,
|
|
/**
|
|
* Purpose: loginShopMessage supports index.js by holding or executing the login shop message page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loginShopMessage: '',
|
|
'form.shopId': '',
|
|
/**
|
|
* Purpose: accountStatusText supports index.js by holding or executing the account status text page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
accountStatusText: error.message || '该后台账号不存在或不可登录',
|
|
/**
|
|
* Purpose: accountStatusType supports index.js by holding or executing the account status type page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
accountStatusType: 'error'
|
|
})
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: validateForm supports index.js by holding or executing the validate form page or API role.
|
|
* Usage: import, call, read, or update this method from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
validateForm() {
|
|
const form = this.data.form
|
|
if (!form.account) {
|
|
return '请输入后台账号'
|
|
}
|
|
if (!form.password) {
|
|
return '请输入登录密码'
|
|
}
|
|
if (!this.data.allowShoplessLogin && this.data.loginShops.length && !form.shopId) {
|
|
return '请选择登录店铺'
|
|
}
|
|
return ''
|
|
},
|
|
|
|
async handleSubmit() {
|
|
const errorMessage = this.validateForm()
|
|
if (errorMessage) {
|
|
wx.showToast({
|
|
/**
|
|
* Purpose: title supports index.js by holding or executing the title page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
title: errorMessage,
|
|
/**
|
|
* Purpose: icon supports index.js by holding or executing the icon page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
|
|
this.setData({
|
|
/**
|
|
* Purpose: loading supports index.js by holding or executing the loading page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loading: true
|
|
})
|
|
|
|
try {
|
|
const payload = await runWithLoading(() => authApi.login({
|
|
/**
|
|
* Purpose: account supports index.js by holding or executing the account page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
account: this.data.form.account,
|
|
/**
|
|
* Purpose: password supports index.js by holding or executing the password page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
password: this.data.form.password,
|
|
/**
|
|
* Purpose: shopId supports index.js by holding or executing the shop id page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
shopId: this.data.form.shopId || undefined
|
|
}))
|
|
|
|
await getApp().setAdminSession(payload)
|
|
|
|
wx.showToast({
|
|
/**
|
|
* Purpose: title supports index.js by holding or executing the title page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
title: '登录成功',
|
|
/**
|
|
* Purpose: icon supports index.js by holding or executing the icon page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
icon: 'none'
|
|
})
|
|
|
|
await navigation.redirectAfterLogin(this.data.redirect)
|
|
} catch (error) {
|
|
wx.showToast({
|
|
/**
|
|
* Purpose: title supports index.js by holding or executing the title page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
title: error.message || '登录失败',
|
|
/**
|
|
* Purpose: icon supports index.js by holding or executing the icon page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
this.setData({
|
|
/**
|
|
* Purpose: loading supports index.js by holding or executing the loading page or API role.
|
|
* Usage: import, call, read, or update this page-data-field from the owning page/module while preserving the surrounding loading and permission flow.
|
|
*/
|
|
loading: false
|
|
})
|
|
}
|
|
},
|
|
|
|
handleOpenCustomerHome() {
|
|
sessionSwitch.openCustomerHome()
|
|
}
|
|
})
|