Files

1432 lines
46 KiB
JavaScript
Raw Permalink Normal View History

2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
const authApi = require('../../api/auth')
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
const shopApi = require('../../api/shop')
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
const userApi = require('../../api/user')
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-02 13:20:28 +08:00
const authStore = require('../../utils/auth')
2026-05-23 18:45:41 +08:00
/**
* 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')
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
const navigation = require('../../utils/navigation')
2026-06-04 16:39:06 +08:00
const sessionSwitch = require('../../utils/session-switch')
2026-05-23 18:45:41 +08:00
/**
* 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')
2026-04-01 15:18:35 +08:00
const { runWithLoading } = require('../../utils/loading')
2026-04-22 16:53:32 +08:00
const {
createKeyboardSafeData,
attachKeyboardSafe,
detachKeyboardSafe,
handleKeyboardFocus,
handleKeyboardBlur,
resetKeyboardSafe
} = require('../../utils/keyboard-safe')
2026-04-01 15:18:35 +08:00
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
Page({
2026-04-22 16:53:32 +08:00
data: Object.assign(createKeyboardSafeData({
cursorSpacing: 108
}), theme.pageThemeData(getApp().globalData.shopTheme), {
2026-04-01 15:18:35 +08:00
redirect: '',
activeTab: 'login',
loginType: 'password',
2026-04-02 13:20:28 +08:00
authHelperText: '',
2026-04-01 15:18:35 +08:00
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,
2026-04-02 14:22:13 +08:00
traditionalLoginExpanded: false,
2026-04-02 13:20:28 +08:00
wechatMiniSupported: false,
wechatMiniDisabledReason: '',
wechatMiniLoading: false,
showMiniShopSelector: false,
miniShopMessage: '',
miniShopTicket: '',
miniShops: [],
miniShopIndex: -1,
miniShopDisplayName: '',
miniShopNewUser: false,
miniShopSubmitting: false,
agreementAccepted: false,
agreements: [],
agreementModalVisible: false,
selectedAgreement: null,
2026-04-01 15:18:35 +08:00
form: {
account: '',
password: '',
confirmPassword: '',
nickname: '',
phone: '',
verificationCode: '',
shopId: '',
rememberMe: true
},
resetForm: {
phone: '',
verificationCode: '',
newPassword: ''
}
2026-04-22 16:53:32 +08:00
}),
2026-04-01 15:18:35 +08:00
/**
* `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.
*/
2026-04-01 15:18:35 +08:00
onLoad(options) {
2026-04-22 16:53:32 +08:00
this.ensureKeyboardSafe()
2026-04-01 15:18:35 +08:00
this.setData({
2026-04-02 13:20:28 +08:00
redirect: options && options.redirect ? options.redirect : '',
authHelperText: this.buildAuthHelperText('login', 'password')
2026-04-01 15:18:35 +08:00
})
2026-04-02 13:20:28 +08:00
this.detectWechatMiniCapability()
2026-04-01 15:18:35 +08:00
this.loadShopOptions()
this.loadAgreements()
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
onShow() {
2026-04-22 16:53:32 +08:00
this.ensureKeyboardSafe()
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-22 16:53:32 +08:00
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.
*/
2026-04-22 16:53:32 +08:00
onUnload() {
resetKeyboardSafe(this)
detachKeyboardSafe(this)
},
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-22 16:53:32 +08:00
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.
*/
2026-04-22 16:53:32 +08:00
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.
*/
2026-04-22 16:53:32 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
if (systemInfo && systemInfo.platform === 'devtools') {
supported = false
disabledReason = '当前环境无法使用微信手机号登录,请使用账号密码或验证码登录'
2026-04-02 13:20:28 +08:00
}
} 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.
*/
2026-04-02 13:20:28 +08:00
if (!supported && !disabledReason) {
disabledReason = '当前环境无法使用微信手机号登录,请使用账号密码或验证码登录'
2026-04-02 13:20:28 +08:00
}
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
if (pending && pending.status === 'SELECT_SHOP') {
this.openMiniShopSelector(pending)
}
2026-04-01 15:18:35 +08:00
},
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'
})
}
},
2026-05-23 18:45:41 +08:00
/**
* 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'
}
},
2026-05-23 18:45:41 +08:00
/**
* 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
},
2026-05-23 18:45:41 +08:00
/**
* 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
})
},
2026-05-23 18:45:41 +08:00
/**
* 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
})
},
2026-05-23 18:45:41 +08:00
/**
* 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.
*/
2026-04-01 15:18:35 +08:00
handleTabChange(event) {
const tab = event.currentTarget.dataset.tab
2026-04-22 16:53:32 +08:00
resetKeyboardSafe(this)
2026-04-01 15:18:35 +08:00
this.setData({
activeTab: tab,
2026-04-02 13:20:28 +08:00
authHelperText: this.buildAuthHelperText(tab, this.data.loginType),
2026-04-01 15:18:35 +08:00
showResetPanel: false,
2026-04-02 14:22:13 +08:00
traditionalLoginExpanded: false,
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
handleLoginTypeChange(event) {
const type = event.currentTarget.dataset.type
2026-04-22 16:53:32 +08:00
resetKeyboardSafe(this)
2026-04-01 15:18:35 +08:00
this.setData({
loginType: type,
2026-04-02 13:20:28 +08:00
authHelperText: this.buildAuthHelperText(this.data.activeTab, type),
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-02 14:22:13 +08:00
handleShowTraditionalLogin() {
2026-04-22 16:53:32 +08:00
resetKeyboardSafe(this)
2026-04-02 14:22:13 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
handleToggleResetPanel() {
2026-04-22 16:53:32 +08:00
resetKeyboardSafe(this)
2026-04-01 15:18:35 +08:00
this.setData({
showResetPanel: !this.data.showResetPanel
})
},
2026-06-04 16:39:06 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
handleInputChange(event) {
const field = event.currentTarget.dataset.field
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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)
2026-04-01 15:18:35 +08:00
},
/**
* `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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
if (!this.data.miniShopTicket || !this.data.miniShops.length) {
return
}
this.setData({
activeTab: 'login',
showMiniShopSelector: true
})
},
2026-04-01 15:18:35 +08:00
async handleAccountBlur() {
2026-04-22 16:53:32 +08:00
this.handleKeyboardBlur()
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
if (this.data.loginType !== 'password') {
return
}
await Promise.all([
this.checkLoginAccountAvailability(),
this.loadLoginShopsByAccount()
])
},
async handlePhoneBlur() {
2026-04-22 16:53:32 +08:00
this.handleKeyboardBlur()
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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: '暂时无法加载门店,可直接登录',
2026-04-01 15:18:35 +08:00
shops: []
})
}
},
async loadLoginShopsByPhone() {
try {
const result = await userApi.getLoginShopsByPhone(this.data.form.phone.trim())
this.applyLoginShopSelection(result)
} catch (error) {
this.applyLoginShopSelection({
allowShoplessLogin: true,
message: '暂时无法加载门店,可直接登录',
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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'
}))
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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'
}))
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
if (!accountAvailable || !phoneAvailable) {
return '请先处理注册信息中的占用问题'
}
return ''
},
async handleSubmit() {
if (!this.ensureAgreementAccepted()) {
return
}
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
if (loginError) {
throw new Error(loginError)
}
let payload
await runWithLoading(async () => {
const agreementPayload = this.buildAgreementPayload()
2026-04-01 15:18:35 +08:00
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
2026-04-01 15:18:35 +08:00
})
: await authApi.login({
account: this.data.form.account.trim(),
password: this.data.form.password,
rememberMe: true,
shopId: this.data.form.shopId || undefined,
...agreementPayload
2026-04-01 15:18:35 +08:00
})
})
2026-04-02 13:20:28 +08:00
await this.finishLogin(payload, '登录成功')
2026-04-01 15:18:35 +08:00
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.
*/
2026-04-01 15:18:35 +08:00
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()
2026-04-01 15:18:35 +08:00
}))
wx.showToast({
title: '注册成功,请登录',
icon: 'none'
})
this.setData({
activeTab: 'login',
loginType: 'password',
2026-04-02 13:20:28 +08:00
authHelperText: this.buildAuthHelperText('login', 'password'),
2026-04-01 15:18:35 +08:00
'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.
*/
2026-04-01 15:18:35 +08:00
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
})
}
2026-04-02 13:20:28 +08:00
},
/**
* `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.
*/
2026-04-02 13:20:28 +08:00
handleWechatMiniUnavailableTap() {
wx.showToast({
title: this.data.wechatMiniDisabledReason || '请使用账号密码或验证码登录',
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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()
2026-04-02 13:20:28 +08:00
}))
await this.handleWechatMiniResult(result)
} catch (error) {
wx.showToast({
title: error.message || '微信手机号一键登录失败',
icon: 'none'
})
} finally {
this.setData({
wechatMiniLoading: false
})
}
},
async handleConfirmMiniShopLogin() {
if (!this.ensureAgreementAccepted()) {
return
}
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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()
2026-04-02 13:20:28 +08:00
}))
/**
* `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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
openMiniShopSelector(result) {
2026-04-22 16:53:32 +08:00
resetKeyboardSafe(this)
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
clearMiniShopSelector() {
this.setData({
showMiniShopSelector: false,
miniShopMessage: '',
miniShopTicket: '',
miniShops: [],
miniShopIndex: -1,
miniShopDisplayName: '',
miniShopNewUser: false,
miniShopSubmitting: false
})
},
async finishLogin(payload, successMessage) {
2026-04-22 16:53:32 +08:00
resetKeyboardSafe(this)
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
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.
*/
2026-04-02 13:20:28 +08:00
if (activeTab === 'register') {
return '注册前请先选门店'
2026-04-02 13:20:28 +08:00
}
/**
* `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.
*/
2026-04-02 13:20:28 +08:00
if (loginType === 'phone') {
return '未注册手机号会先选门店'
2026-04-02 13:20:28 +08:00
}
return '多门店账号需先选门店'
2026-04-01 15:18:35 +08:00
}
})