1432 lines
46 KiB
JavaScript
1432 lines
46 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: shopApi supports index.js by holding or executing the shop 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 shopApi = require('../../api/shop')
|
|
/**
|
|
* Purpose: userApi supports index.js by holding or executing the user 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 userApi = require('../../api/user')
|
|
/**
|
|
* Purpose: authStore supports index.js by holding or executing the auth store 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 authStore = require('../../utils/auth')
|
|
/**
|
|
* Purpose: actionGuard supports index.js by holding or executing the action guard 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 actionGuard = require('../../utils/action-guard')
|
|
/**
|
|
* 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 sessionSwitch = require('../../utils/session-switch')
|
|
/**
|
|
* Purpose: theme supports index.js by holding or executing the theme 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 theme = require('../../utils/theme')
|
|
const { runWithLoading } = require('../../utils/loading')
|
|
const {
|
|
createKeyboardSafeData,
|
|
attachKeyboardSafe,
|
|
detachKeyboardSafe,
|
|
handleKeyboardFocus,
|
|
handleKeyboardBlur,
|
|
resetKeyboardSafe
|
|
} = require('../../utils/keyboard-safe')
|
|
|
|
/**
|
|
* Purpose: phoneRegex supports index.js by holding or executing the phone 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 phoneRegex = /^1[3-9]\d{9}$/
|
|
|
|
/**
|
|
* auth page logic.
|
|
* Call guide: page data drives the WXML template, and each method is called by user events or lifecycle callbacks.
|
|
*/
|
|
Page({
|
|
data: Object.assign(createKeyboardSafeData({
|
|
cursorSpacing: 108
|
|
}), theme.pageThemeData(getApp().globalData.shopTheme), {
|
|
redirect: '',
|
|
activeTab: 'login',
|
|
loginType: 'password',
|
|
authHelperText: '',
|
|
loading: false,
|
|
shops: [],
|
|
registerShopIndex: -1,
|
|
registerShopDisplayName: '',
|
|
loginShops: [],
|
|
loginShopIndex: -1,
|
|
loginShopDisplayName: '',
|
|
allowLoginShopless: false,
|
|
loginShopMessage: '',
|
|
loginAccountStatusText: '',
|
|
loginAccountStatusType: '',
|
|
registerAccountStatusText: '',
|
|
registerAccountStatusType: '',
|
|
registerPhoneStatusText: '',
|
|
registerPhoneStatusType: '',
|
|
countdown: 0,
|
|
forgotCountdown: 0,
|
|
showResetPanel: false,
|
|
traditionalLoginExpanded: false,
|
|
wechatMiniSupported: false,
|
|
wechatMiniDisabledReason: '',
|
|
wechatMiniLoading: false,
|
|
showMiniShopSelector: false,
|
|
miniShopMessage: '',
|
|
miniShopTicket: '',
|
|
miniShops: [],
|
|
miniShopIndex: -1,
|
|
miniShopDisplayName: '',
|
|
miniShopNewUser: false,
|
|
miniShopSubmitting: false,
|
|
agreementAccepted: false,
|
|
agreements: [],
|
|
agreementModalVisible: false,
|
|
selectedAgreement: null,
|
|
form: {
|
|
account: '',
|
|
password: '',
|
|
confirmPassword: '',
|
|
nickname: '',
|
|
phone: '',
|
|
verificationCode: '',
|
|
shopId: '',
|
|
rememberMe: true
|
|
},
|
|
resetForm: {
|
|
phone: '',
|
|
verificationCode: '',
|
|
newPassword: ''
|
|
}
|
|
}),
|
|
|
|
/**
|
|
* `onLoad` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
onLoad(options) {
|
|
this.ensureKeyboardSafe()
|
|
this.setData({
|
|
redirect: options && options.redirect ? options.redirect : '',
|
|
authHelperText: this.buildAuthHelperText('login', 'password')
|
|
})
|
|
this.detectWechatMiniCapability()
|
|
this.loadShopOptions()
|
|
this.loadAgreements()
|
|
this.consumePendingWechatMiniLogin()
|
|
this.waitForPendingWechatMiniAutoLogin()
|
|
},
|
|
|
|
/**
|
|
* `onShow` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
onShow() {
|
|
this.ensureKeyboardSafe()
|
|
this.consumePendingWechatMiniLogin()
|
|
},
|
|
|
|
/**
|
|
* `onHide` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
onHide() {
|
|
resetKeyboardSafe(this)
|
|
detachKeyboardSafe(this)
|
|
},
|
|
|
|
/**
|
|
* `onUnload` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
onUnload() {
|
|
resetKeyboardSafe(this)
|
|
detachKeyboardSafe(this)
|
|
},
|
|
|
|
/**
|
|
* Purpose: noop supports index.js by holding or executing the noop 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.
|
|
*/
|
|
noop() {},
|
|
|
|
/**
|
|
* `ensureKeyboardSafe` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
ensureKeyboardSafe() {
|
|
attachKeyboardSafe(this, {
|
|
cursorSpacing: 108,
|
|
spacerExtraRpx: 56
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `handleKeyboardFocus` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleKeyboardFocus(event) {
|
|
handleKeyboardFocus(this, event)
|
|
},
|
|
|
|
/**
|
|
* `handleKeyboardBlur` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleKeyboardBlur() {
|
|
handleKeyboardBlur(this)
|
|
},
|
|
|
|
/**
|
|
* `detectWechatMiniCapability` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
detectWechatMiniCapability() {
|
|
let supported = false
|
|
let disabledReason = ''
|
|
|
|
try {
|
|
supported = wx.canIUse('button.open-type.getPhoneNumber')
|
|
const systemInfo = wx.getSystemInfoSync()
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (systemInfo && systemInfo.platform === 'devtools') {
|
|
supported = false
|
|
disabledReason = '当前环境无法使用微信手机号登录,请使用账号密码或验证码登录'
|
|
}
|
|
} catch (error) {
|
|
supported = false
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!supported && !disabledReason) {
|
|
disabledReason = '当前环境无法使用微信手机号登录,请使用账号密码或验证码登录'
|
|
}
|
|
|
|
this.setData({
|
|
wechatMiniSupported: supported,
|
|
wechatMiniDisabledReason: disabledReason
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `waitForPendingWechatMiniAutoLogin` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
waitForPendingWechatMiniAutoLogin() {
|
|
const app = getApp()
|
|
if (
|
|
!app
|
|
|| typeof app.isWechatMiniAutoLoginPending !== 'function'
|
|
|| typeof app.waitForWechatMiniAutoLogin !== 'function'
|
|
|| !app.isWechatMiniAutoLoginPending()
|
|
) {
|
|
return
|
|
}
|
|
|
|
Promise.resolve(app.waitForWechatMiniAutoLogin())
|
|
.catch(() => false)
|
|
.then(async () => {
|
|
if (authStore.isLoggedIn()) {
|
|
try {
|
|
await navigation.redirectAfterLogin(this.data.redirect)
|
|
} catch (redirectError) {
|
|
console.error('自动登录后跳转失败,已回退到首页', redirectError)
|
|
await navigation.switchTab('/pages/home/index')
|
|
}
|
|
return
|
|
}
|
|
this.consumePendingWechatMiniLogin()
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `consumePendingWechatMiniLogin` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
consumePendingWechatMiniLogin() {
|
|
const app = getApp()
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!app || typeof app.consumePendingWechatMiniLogin !== 'function') {
|
|
return
|
|
}
|
|
|
|
const pending = app.consumePendingWechatMiniLogin()
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (pending && pending.status === 'SELECT_SHOP') {
|
|
this.openMiniShopSelector(pending)
|
|
}
|
|
},
|
|
|
|
async loadShopOptions() {
|
|
try {
|
|
const shops = await shopApi.getShopsSimple()
|
|
this.setData({
|
|
shops: Array.isArray(shops) ? shops : []
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: '门店列表加载失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
|
|
async loadAgreements() {
|
|
try {
|
|
const agreements = await authApi.getCurrentAgreements()
|
|
this.setData({
|
|
agreements: Array.isArray(agreements) ? agreements : []
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: '协议内容加载失败,请稍后重试',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: buildAgreementPayload supports index.js by holding or executing the build agreement payload 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.
|
|
*/
|
|
buildAgreementPayload() {
|
|
const agreementVersions = {}
|
|
;(this.data.agreements || []).forEach((item) => {
|
|
if (item && item.agreementType && item.version) {
|
|
agreementVersions[item.agreementType] = item.version
|
|
}
|
|
})
|
|
return {
|
|
agreementAccepted: !!this.data.agreementAccepted,
|
|
agreementVersions,
|
|
clientType: 'WECHAT_MINI'
|
|
}
|
|
},
|
|
|
|
/**
|
|
* Purpose: ensureAgreementAccepted supports index.js by holding or executing the ensure agreement accepted 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.
|
|
*/
|
|
ensureAgreementAccepted() {
|
|
if (!this.data.agreementAccepted) {
|
|
wx.showToast({
|
|
title: '请先阅读并同意用户协议和隐私政策',
|
|
icon: 'none'
|
|
})
|
|
return false
|
|
}
|
|
|
|
const versions = this.buildAgreementPayload().agreementVersions
|
|
if (!versions.USER_TERMS || !versions.PRIVACY_POLICY) {
|
|
wx.showToast({
|
|
title: '协议版本加载中,请稍后再试',
|
|
icon: 'none'
|
|
})
|
|
return false
|
|
}
|
|
return true
|
|
},
|
|
|
|
/**
|
|
* Purpose: handleAgreementToggle supports index.js by holding or executing the handle agreement toggle 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.
|
|
*/
|
|
handleAgreementToggle() {
|
|
this.setData({
|
|
agreementAccepted: !this.data.agreementAccepted
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Purpose: openAgreement supports index.js by holding or executing the open agreement 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.
|
|
*/
|
|
openAgreement(event) {
|
|
const type = event.currentTarget.dataset.type
|
|
const selectedAgreement = (this.data.agreements || []).find((item) => item.agreementType === type)
|
|
if (!selectedAgreement) {
|
|
wx.showToast({
|
|
title: '协议内容暂不可用',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
this.setData({
|
|
selectedAgreement,
|
|
agreementModalVisible: true
|
|
})
|
|
},
|
|
|
|
/**
|
|
* Purpose: closeAgreementModal supports index.js by holding or executing the close agreement modal 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.
|
|
*/
|
|
closeAgreementModal() {
|
|
this.setData({
|
|
agreementModalVisible: false,
|
|
selectedAgreement: null
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `handleTabChange` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleTabChange(event) {
|
|
const tab = event.currentTarget.dataset.tab
|
|
resetKeyboardSafe(this)
|
|
this.setData({
|
|
activeTab: tab,
|
|
authHelperText: this.buildAuthHelperText(tab, this.data.loginType),
|
|
showResetPanel: false,
|
|
traditionalLoginExpanded: false,
|
|
loginAccountStatusText: '',
|
|
loginAccountStatusType: '',
|
|
registerAccountStatusText: '',
|
|
registerAccountStatusType: '',
|
|
registerPhoneStatusText: '',
|
|
registerPhoneStatusType: ''
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `handleLoginTypeChange` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleLoginTypeChange(event) {
|
|
const type = event.currentTarget.dataset.type
|
|
resetKeyboardSafe(this)
|
|
this.setData({
|
|
loginType: type,
|
|
authHelperText: this.buildAuthHelperText(this.data.activeTab, type),
|
|
loginShops: [],
|
|
loginShopIndex: -1,
|
|
loginShopDisplayName: '',
|
|
allowLoginShopless: false,
|
|
loginShopMessage: '',
|
|
loginAccountStatusText: '',
|
|
loginAccountStatusType: '',
|
|
'form.shopId': ''
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `handleShowTraditionalLogin` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleShowTraditionalLogin() {
|
|
resetKeyboardSafe(this)
|
|
this.setData({
|
|
traditionalLoginExpanded: true,
|
|
showResetPanel: false
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `handleToggleResetPanel` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleToggleResetPanel() {
|
|
resetKeyboardSafe(this)
|
|
this.setData({
|
|
showResetPanel: !this.data.showResetPanel
|
|
})
|
|
},
|
|
|
|
handleOpenAdminLogin() {
|
|
sessionSwitch.openAdminLogin()
|
|
},
|
|
|
|
/**
|
|
* `handleInputChange` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleInputChange(event) {
|
|
const field = event.currentTarget.dataset.field
|
|
const value = event.detail.value
|
|
const patch = {
|
|
[`form.${field}`]: value
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (field === 'account') {
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.activeTab === 'login') {
|
|
patch.loginAccountStatusText = ''
|
|
patch.loginAccountStatusType = ''
|
|
if (!String(value || '').trim()) {
|
|
patch.loginShops = []
|
|
patch.loginShopIndex = -1
|
|
patch.loginShopDisplayName = ''
|
|
patch.allowLoginShopless = false
|
|
patch.loginShopMessage = ''
|
|
patch['form.shopId'] = ''
|
|
}
|
|
} else {
|
|
patch.registerAccountStatusText = ''
|
|
patch.registerAccountStatusType = ''
|
|
}
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (field === 'phone') {
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.activeTab === 'register') {
|
|
patch.registerPhoneStatusText = ''
|
|
patch.registerPhoneStatusType = ''
|
|
} else if (this.data.loginType === 'phone' && !String(value || '').trim()) {
|
|
patch.loginShops = []
|
|
patch.loginShopIndex = -1
|
|
patch.loginShopDisplayName = ''
|
|
patch.allowLoginShopless = false
|
|
patch.loginShopMessage = ''
|
|
patch['form.shopId'] = ''
|
|
}
|
|
}
|
|
|
|
this.setData(patch)
|
|
},
|
|
|
|
/**
|
|
* `handleResetInputChange` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleResetInputChange(event) {
|
|
const field = event.currentTarget.dataset.field
|
|
this.setData({
|
|
[`resetForm.${field}`]: event.detail.value
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `handleRegisterShopChange` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleRegisterShopChange(event) {
|
|
const index = Number(event.detail.value)
|
|
const shop = this.data.shops[index]
|
|
this.setData({
|
|
registerShopIndex: index,
|
|
registerShopDisplayName: shop ? shop.shopName || shop.name || '' : '',
|
|
'form.shopId': shop ? shop.id : ''
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `handleLoginShopChange` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleLoginShopChange(event) {
|
|
const index = Number(event.detail.value)
|
|
const shop = this.data.loginShops[index]
|
|
this.setData({
|
|
loginShopIndex: index,
|
|
loginShopDisplayName: shop ? shop.shopName || '' : '',
|
|
'form.shopId': shop ? shop.id : ''
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `handleMiniShopChange` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleMiniShopChange(event) {
|
|
const index = Number(event.detail.value)
|
|
const shop = this.data.miniShops[index]
|
|
this.setData({
|
|
miniShopIndex: index,
|
|
miniShopDisplayName: shop ? shop.shopName || '' : ''
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `handleCloseMiniShopSelector` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleCloseMiniShopSelector() {
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.miniShopSubmitting) {
|
|
return
|
|
}
|
|
this.setData({
|
|
showMiniShopSelector: false
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `handleResumeMiniShopSelector` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleResumeMiniShopSelector() {
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!this.data.miniShopTicket || !this.data.miniShops.length) {
|
|
return
|
|
}
|
|
this.setData({
|
|
activeTab: 'login',
|
|
showMiniShopSelector: true
|
|
})
|
|
},
|
|
|
|
async handleAccountBlur() {
|
|
this.handleKeyboardBlur()
|
|
const account = this.data.form.account.trim()
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!account) {
|
|
return
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.activeTab === 'register') {
|
|
await this.checkRegisterAccountAvailability()
|
|
return
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.loginType !== 'password') {
|
|
return
|
|
}
|
|
|
|
await Promise.all([
|
|
this.checkLoginAccountAvailability(),
|
|
this.loadLoginShopsByAccount()
|
|
])
|
|
},
|
|
|
|
async handlePhoneBlur() {
|
|
this.handleKeyboardBlur()
|
|
const phone = this.data.form.phone.trim()
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!phone) {
|
|
return
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.activeTab === 'register') {
|
|
if (phoneRegex.test(phone)) {
|
|
await this.checkRegisterPhoneAvailability()
|
|
}
|
|
return
|
|
}
|
|
|
|
if (this.data.loginType !== 'phone' || !phoneRegex.test(phone)) {
|
|
return
|
|
}
|
|
|
|
await this.loadLoginShopsByPhone()
|
|
},
|
|
|
|
/**
|
|
* `applyLoginShopSelection` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
applyLoginShopSelection(selection) {
|
|
const shops = Array.isArray(selection && selection.shops) ? selection.shops : []
|
|
const allowLoginShopless = Boolean(selection && selection.allowShoplessLogin)
|
|
let loginShopIndex = -1
|
|
let loginShopDisplayName = ''
|
|
let shopId = ''
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!allowLoginShopless && shops.length === 1) {
|
|
loginShopIndex = 0
|
|
loginShopDisplayName = shops[0].shopName || ''
|
|
shopId = shops[0].id
|
|
}
|
|
|
|
this.setData({
|
|
loginShops: shops,
|
|
allowLoginShopless,
|
|
loginShopMessage: (selection && selection.message) || '',
|
|
loginShopIndex,
|
|
loginShopDisplayName,
|
|
'form.shopId': shopId
|
|
})
|
|
},
|
|
|
|
async checkLoginAccountAvailability() {
|
|
try {
|
|
const result = await userApi.checkLoginAccountAvailability(this.data.form.account.trim())
|
|
this.setData({
|
|
loginAccountStatusText: result && result.message ? result.message : '',
|
|
loginAccountStatusType: result && result.exists ? 'success' : 'error'
|
|
})
|
|
return Boolean(result && result.exists)
|
|
} catch (error) {
|
|
this.setData({
|
|
loginAccountStatusText: '',
|
|
loginAccountStatusType: ''
|
|
})
|
|
return false
|
|
}
|
|
},
|
|
|
|
async loadLoginShopsByAccount() {
|
|
try {
|
|
const result = await userApi.getLoginShopsByAccount(this.data.form.account.trim())
|
|
this.applyLoginShopSelection(result)
|
|
} catch (error) {
|
|
this.applyLoginShopSelection({
|
|
allowShoplessLogin: true,
|
|
message: '暂时无法加载门店,可直接登录',
|
|
shops: []
|
|
})
|
|
}
|
|
},
|
|
|
|
async loadLoginShopsByPhone() {
|
|
try {
|
|
const result = await userApi.getLoginShopsByPhone(this.data.form.phone.trim())
|
|
this.applyLoginShopSelection(result)
|
|
} catch (error) {
|
|
this.applyLoginShopSelection({
|
|
allowShoplessLogin: true,
|
|
message: '暂时无法加载门店,可直接登录',
|
|
shops: []
|
|
})
|
|
}
|
|
},
|
|
|
|
async checkRegisterAccountAvailability() {
|
|
try {
|
|
const result = await userApi.checkAccountAvailability(this.data.form.account.trim())
|
|
this.setData({
|
|
registerAccountStatusText: result && result.message ? result.message : '',
|
|
registerAccountStatusType: result && result.available ? 'success' : 'error'
|
|
})
|
|
return Boolean(result && result.available)
|
|
} catch (error) {
|
|
this.setData({
|
|
registerAccountStatusText: '',
|
|
registerAccountStatusType: ''
|
|
})
|
|
return false
|
|
}
|
|
},
|
|
|
|
async checkRegisterPhoneAvailability() {
|
|
try {
|
|
const result = await userApi.checkPhoneAvailability(this.data.form.phone.trim())
|
|
this.setData({
|
|
registerPhoneStatusText: result && result.message ? result.message : '',
|
|
registerPhoneStatusType: result && result.available ? 'success' : 'error'
|
|
})
|
|
return Boolean(result && result.available)
|
|
} catch (error) {
|
|
this.setData({
|
|
registerPhoneStatusText: '',
|
|
registerPhoneStatusType: ''
|
|
})
|
|
return false
|
|
}
|
|
},
|
|
|
|
/**
|
|
* `startCountdown` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
startCountdown(fieldName) {
|
|
this.setData({
|
|
[fieldName]: 60
|
|
})
|
|
|
|
const timer = setInterval(() => {
|
|
const nextValue = this.data[fieldName] - 1
|
|
this.setData({
|
|
[fieldName]: nextValue
|
|
})
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (nextValue <= 0) {
|
|
clearInterval(timer)
|
|
}
|
|
}, 1000)
|
|
},
|
|
|
|
async handleSendCode() {
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.countdown > 0) {
|
|
return
|
|
}
|
|
|
|
const phone = this.data.form.phone.trim()
|
|
if (!phoneRegex.test(phone)) {
|
|
wx.showToast({
|
|
title: '请输入正确的手机号',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.activeTab === 'register') {
|
|
const phoneAvailable = await this.checkRegisterPhoneAvailability()
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!phoneAvailable) {
|
|
wx.showToast({
|
|
title: this.data.registerPhoneStatusText || '该手机号已被注册',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
}
|
|
|
|
try {
|
|
await runWithLoading(() => userApi.sendVerificationCode({
|
|
phone,
|
|
sceneCode: this.data.activeTab === 'register' ? 'USER_REGISTER_SMS_CODE' : 'USER_LOGIN_SMS_CODE'
|
|
}))
|
|
wx.showToast({
|
|
title: '验证码已发送',
|
|
icon: 'none'
|
|
})
|
|
this.startCountdown('countdown')
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '验证码发送失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
|
|
async handleSendForgotCode() {
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.forgotCountdown > 0) {
|
|
return
|
|
}
|
|
|
|
const phone = this.data.resetForm.phone.trim()
|
|
if (!phoneRegex.test(phone)) {
|
|
wx.showToast({
|
|
title: '请输入正确的手机号',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
|
|
try {
|
|
await runWithLoading(() => authApi.sendVerificationCode({
|
|
phone,
|
|
sceneCode: 'USER_RESET_PASSWORD_SMS_CODE'
|
|
}))
|
|
wx.showToast({
|
|
title: '验证码已发送',
|
|
icon: 'none'
|
|
})
|
|
this.startCountdown('forgotCountdown')
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '验证码发送失败',
|
|
icon: 'none'
|
|
})
|
|
}
|
|
},
|
|
|
|
/**
|
|
* `validateLoginForm` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
validateLoginForm() {
|
|
const form = this.data.form
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.loginType === 'password') {
|
|
if (!form.account.trim() || !form.password) {
|
|
return '请输入账号和密码'
|
|
}
|
|
} else if (!phoneRegex.test(form.phone.trim()) || !form.verificationCode.trim()) {
|
|
return '请输入正确的手机号和验证码'
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!this.data.allowLoginShopless && this.data.loginShops.length && !form.shopId) {
|
|
return '请选择登录门店'
|
|
}
|
|
|
|
return ''
|
|
},
|
|
|
|
async validateRegisterForm() {
|
|
const form = this.data.form
|
|
if (!form.nickname.trim()) {
|
|
return '请输入昵称'
|
|
}
|
|
if (!form.account.trim()) {
|
|
return '请输入账号'
|
|
}
|
|
if (!phoneRegex.test(form.phone.trim())) {
|
|
return '请输入正确的手机号'
|
|
}
|
|
if (!form.verificationCode.trim()) {
|
|
return '请输入验证码'
|
|
}
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!form.password || form.password.length < 6) {
|
|
return '密码至少 6 位'
|
|
}
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (form.password !== form.confirmPassword) {
|
|
return '两次输入的密码不一致'
|
|
}
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!form.shopId) {
|
|
return '请选择门店'
|
|
}
|
|
|
|
const [accountAvailable, phoneAvailable] = await Promise.all([
|
|
this.checkRegisterAccountAvailability(),
|
|
this.checkRegisterPhoneAvailability()
|
|
])
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!accountAvailable || !phoneAvailable) {
|
|
return '请先处理注册信息中的占用问题'
|
|
}
|
|
|
|
return ''
|
|
},
|
|
|
|
async handleSubmit() {
|
|
if (!this.ensureAgreementAccepted()) {
|
|
return
|
|
}
|
|
|
|
this.setData({
|
|
loading: true
|
|
})
|
|
|
|
try {
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (this.data.activeTab === 'login') {
|
|
const loginError = this.validateLoginForm()
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (loginError) {
|
|
throw new Error(loginError)
|
|
}
|
|
|
|
let payload
|
|
await runWithLoading(async () => {
|
|
const agreementPayload = this.buildAgreementPayload()
|
|
payload = this.data.loginType === 'phone'
|
|
? await authApi.phoneLogin({
|
|
phone: this.data.form.phone.trim(),
|
|
verificationCode: this.data.form.verificationCode.trim(),
|
|
shopId: this.data.form.shopId || undefined,
|
|
...agreementPayload
|
|
})
|
|
: await authApi.login({
|
|
account: this.data.form.account.trim(),
|
|
password: this.data.form.password,
|
|
rememberMe: true,
|
|
shopId: this.data.form.shopId || undefined,
|
|
...agreementPayload
|
|
})
|
|
})
|
|
|
|
await this.finishLogin(payload, '登录成功')
|
|
return
|
|
}
|
|
|
|
const registerError = await this.validateRegisterForm()
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (registerError) {
|
|
throw new Error(registerError)
|
|
}
|
|
|
|
await runWithLoading(() => authApi.register({
|
|
account: this.data.form.account.trim(),
|
|
password: this.data.form.password,
|
|
nickname: this.data.form.nickname.trim(),
|
|
phone: this.data.form.phone.trim(),
|
|
verificationCode: this.data.form.verificationCode.trim(),
|
|
shopId: this.data.form.shopId,
|
|
...this.buildAgreementPayload()
|
|
}))
|
|
|
|
wx.showToast({
|
|
title: '注册成功,请登录',
|
|
icon: 'none'
|
|
})
|
|
|
|
this.setData({
|
|
activeTab: 'login',
|
|
loginType: 'password',
|
|
authHelperText: this.buildAuthHelperText('login', 'password'),
|
|
'form.password': '',
|
|
'form.confirmPassword': '',
|
|
'form.verificationCode': ''
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || (this.data.activeTab === 'login' ? '登录失败' : '注册失败'),
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
this.setData({
|
|
loading: false
|
|
})
|
|
}
|
|
},
|
|
|
|
async handleResetPassword() {
|
|
const form = this.data.resetForm
|
|
if (!phoneRegex.test(form.phone.trim())) {
|
|
wx.showToast({
|
|
title: '请输入正确的手机号',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
if (!form.verificationCode.trim()) {
|
|
wx.showToast({
|
|
title: '请输入验证码',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!form.newPassword || form.newPassword.length < 6) {
|
|
wx.showToast({
|
|
title: '新密码至少 6 位',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
|
|
this.setData({
|
|
loading: true
|
|
})
|
|
|
|
try {
|
|
await runWithLoading(() => authApi.resetPasswordByPhone({
|
|
phone: form.phone.trim(),
|
|
verificationCode: form.verificationCode.trim(),
|
|
newPassword: form.newPassword
|
|
}))
|
|
wx.showToast({
|
|
title: '密码已重置,请重新登录',
|
|
icon: 'none'
|
|
})
|
|
this.setData({
|
|
showResetPanel: false,
|
|
resetForm: {
|
|
phone: '',
|
|
verificationCode: '',
|
|
newPassword: ''
|
|
}
|
|
})
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '密码重置失败',
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
this.setData({
|
|
loading: false
|
|
})
|
|
}
|
|
},
|
|
|
|
/**
|
|
* `handleWechatMiniUnavailableTap` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
handleWechatMiniUnavailableTap() {
|
|
wx.showToast({
|
|
title: this.data.wechatMiniDisabledReason || '请使用账号密码或验证码登录',
|
|
icon: 'none'
|
|
})
|
|
},
|
|
|
|
async handleWechatMiniPhoneLogin(event) {
|
|
if (!this.ensureAgreementAccepted()) {
|
|
return
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!this.data.wechatMiniSupported || this.data.wechatMiniLoading) {
|
|
this.handleWechatMiniUnavailableTap()
|
|
return
|
|
}
|
|
|
|
const detail = event.detail || {}
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!detail.code) {
|
|
wx.showToast({
|
|
title: this.resolveWechatAuthorizeError(detail),
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
|
|
this.setData({
|
|
wechatMiniLoading: true
|
|
})
|
|
|
|
try {
|
|
const wxCode = await this.requestWechatMiniCode()
|
|
const result = await runWithLoading(() => authApi.wechatMiniPhoneLogin({
|
|
wxCode,
|
|
phoneCode: detail.code,
|
|
preferredShopId: authStore.getPreferredMiniLoginShopId() || undefined,
|
|
...this.buildAgreementPayload()
|
|
}))
|
|
await this.handleWechatMiniResult(result)
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '微信手机号一键登录失败',
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
this.setData({
|
|
wechatMiniLoading: false
|
|
})
|
|
}
|
|
},
|
|
|
|
async handleConfirmMiniShopLogin() {
|
|
if (!this.ensureAgreementAccepted()) {
|
|
return
|
|
}
|
|
|
|
const shop = this.data.miniShops[this.data.miniShopIndex]
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!this.data.miniShopTicket) {
|
|
wx.showToast({
|
|
title: '登录票据已失效,请重新发起微信登录',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!shop || !shop.id) {
|
|
wx.showToast({
|
|
title: '请选择门店',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
|
|
this.setData({
|
|
miniShopSubmitting: true
|
|
})
|
|
|
|
try {
|
|
const result = await runWithLoading(() => authApi.confirmWechatMiniLogin({
|
|
ticket: this.data.miniShopTicket,
|
|
shopId: shop.id,
|
|
...this.buildAgreementPayload()
|
|
}))
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!result || result.status !== 'SUCCESS') {
|
|
throw new Error((result && result.message) || '确认登录失败')
|
|
}
|
|
this.clearMiniShopSelector()
|
|
await this.finishLogin(result, result.newUser ? '注册并登录成功' : '登录成功')
|
|
} catch (error) {
|
|
wx.showToast({
|
|
title: error.message || '确认登录失败',
|
|
icon: 'none'
|
|
})
|
|
} finally {
|
|
this.setData({
|
|
miniShopSubmitting: false
|
|
})
|
|
}
|
|
},
|
|
|
|
/**
|
|
* `resolveWechatAuthorizeError` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
resolveWechatAuthorizeError(detail) {
|
|
const errMsg = String((detail && detail.errMsg) || '')
|
|
if (errMsg.includes('user deny') || errMsg.includes('user cancel')) {
|
|
return '你已取消手机号授权,可继续使用账号密码或验证码登录。'
|
|
}
|
|
if (errMsg.includes('no permission')) {
|
|
return '当前小程序未开通手机号授权能力,请联系管理员检查微信配置。'
|
|
}
|
|
return '未获取到微信手机号授权,请稍后重试。'
|
|
},
|
|
|
|
/**
|
|
* `requestWechatMiniCode` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
requestWechatMiniCode() {
|
|
const app = getApp()
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (app && typeof app.requestWechatMiniCode === 'function') {
|
|
return app.requestWechatMiniCode()
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
wx.login({
|
|
success: res => {
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (res && res.code) {
|
|
resolve(res.code)
|
|
return
|
|
}
|
|
reject(new Error('获取微信登录凭证失败'))
|
|
},
|
|
fail: () => reject(new Error('获取微信登录凭证失败'))
|
|
})
|
|
})
|
|
},
|
|
|
|
async handleWechatMiniResult(result) {
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!result) {
|
|
throw new Error('微信登录未返回结果')
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (result.status === 'SUCCESS') {
|
|
await this.finishLogin(result, result.newUser ? '注册并登录成功' : '登录成功')
|
|
return
|
|
}
|
|
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (result.status === 'SELECT_SHOP') {
|
|
this.openMiniShopSelector(result)
|
|
return
|
|
}
|
|
|
|
wx.showToast({
|
|
title: result.message || '请先使用传统方式登录',
|
|
icon: 'none'
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `openMiniShopSelector` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
openMiniShopSelector(result) {
|
|
resetKeyboardSafe(this)
|
|
const shops = Array.isArray(result && result.shops) ? result.shops : []
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (!shops.length) {
|
|
wx.showToast({
|
|
title: result && result.message ? result.message : '暂无可选门店,请稍后重试',
|
|
icon: 'none'
|
|
})
|
|
return
|
|
}
|
|
const defaultIndex = shops.length ? 0 : -1
|
|
const defaultShop = defaultIndex >= 0 ? shops[defaultIndex] : null
|
|
|
|
this.setData({
|
|
activeTab: 'login',
|
|
authHelperText: this.buildAuthHelperText('login', this.data.loginType),
|
|
showMiniShopSelector: true,
|
|
miniShopMessage: (result && result.message) || '',
|
|
miniShopTicket: (result && result.ticket) || '',
|
|
miniShops: shops,
|
|
miniShopIndex: defaultIndex,
|
|
miniShopDisplayName: defaultShop ? defaultShop.shopName || '' : '',
|
|
miniShopNewUser: Boolean(result && result.newUser)
|
|
})
|
|
},
|
|
|
|
/**
|
|
* `clearMiniShopSelector` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
clearMiniShopSelector() {
|
|
this.setData({
|
|
showMiniShopSelector: false,
|
|
miniShopMessage: '',
|
|
miniShopTicket: '',
|
|
miniShops: [],
|
|
miniShopIndex: -1,
|
|
miniShopDisplayName: '',
|
|
miniShopNewUser: false,
|
|
miniShopSubmitting: false
|
|
})
|
|
},
|
|
|
|
async finishLogin(payload, successMessage) {
|
|
resetKeyboardSafe(this)
|
|
getApp().setUserSession(payload)
|
|
this.clearMiniShopSelector()
|
|
this.setData({
|
|
loading: false,
|
|
wechatMiniLoading: false
|
|
})
|
|
wx.showToast({
|
|
title: successMessage || '登录成功',
|
|
icon: 'none'
|
|
})
|
|
|
|
try {
|
|
await navigation.redirectAfterLogin(this.data.redirect)
|
|
} catch (redirectError) {
|
|
console.error('登录后跳转失败,已回退到首页', redirectError)
|
|
await navigation.switchTab('/pages/home/index')
|
|
}
|
|
},
|
|
|
|
/**
|
|
* `buildAuthHelperText` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
buildAuthHelperText(activeTab, loginType) {
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (activeTab === 'register') {
|
|
return '注册前请先选门店'
|
|
}
|
|
/**
|
|
* `if` page method. Use this from WXML event bindings or lifecycle hooks.
|
|
* Call guide: read or update page data with `this.setData(...)` inside the method when state changes.
|
|
*/
|
|
if (loginType === 'phone') {
|
|
return '未注册手机号会先选门店'
|
|
}
|
|
return '多门店账号需先选门店'
|
|
}
|
|
})
|