const authApi = require('../../api/auth') const auth = require('../../utils/auth') const navigation = require('../../utils/navigation') const { runWithLoading } = require('../../utils/loading') const ACCOUNT_REGEX = /[^A-Za-z0-9]/g Page({ data: { redirect: '', form: { account: '', password: '', shopId: '' }, loginShops: [], loginShopIndex: -1, loginShopDisplayName: '', allowShoplessLogin: false, loginShopMessage: '', accountStatusText: '', accountStatusType: '', loading: false }, onLoad(options) { this._lookupTicket = 0 this.setData({ redirect: options && options.redirect ? options.redirect : '' }) if (auth.isLoggedIn()) { navigation.redirectAfterLogin(this.data.redirect).catch(() => undefined) } }, onUnload() { if (this._accountTimer) { clearTimeout(this._accountTimer) } }, 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 }) }, handleShopChange(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 : '' }) }, resetAccountState() { if (this._accountTimer) { clearTimeout(this._accountTimer) } this._lookupTicket += 1 this.setData({ accountStatusText: '', accountStatusType: '', loginShops: [], loginShopIndex: -1, loginShopDisplayName: '', allowShoplessLogin: false, loginShopMessage: '', 'form.shopId': '' }) }, scheduleAccountLookup(account) { if (this._accountTimer) { clearTimeout(this._accountTimer) } const ticket = this._lookupTicket + 1 this._lookupTicket = ticket this._accountTimer = setTimeout(() => { this.loadLoginShops(account, ticket) }, 420) }, 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({ loginShops: shops, allowShoplessLogin, loginShopMessage: (selection && selection.message) || '', loginShopIndex, loginShopDisplayName, 'form.shopId': shopId, accountStatusText: shops.length || allowShoplessLogin ? '该账号可用于后台登录' : '当前账号暂时不可用于后台登录', 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({ loginShops: [], loginShopIndex: -1, loginShopDisplayName: '', allowShoplessLogin: false, loginShopMessage: '', 'form.shopId': '', accountStatusText: error.message || '该后台账号不存在或不可登录', accountStatusType: 'error' }) } }, 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({ title: errorMessage, icon: 'none' }) return } this.setData({ loading: true }) try { const payload = await runWithLoading(() => authApi.login({ account: this.data.form.account, password: this.data.form.password, shopId: this.data.form.shopId || undefined })) await getApp().setAdminSession(payload) wx.showToast({ title: '登录成功', icon: 'none' }) await navigation.redirectAfterLogin(this.data.redirect) } catch (error) { wx.showToast({ title: error.message || '登录失败', icon: 'none' }) } finally { this.setData({ loading: false }) } } })