功能:新增微信小程序界面

This commit is contained in:
xiaowang
2026-04-01 15:18:35 +08:00
parent bb2d8c4410
commit 7fa67d0511
92 changed files with 7463 additions and 0 deletions
+562
View File
@@ -0,0 +1,562 @@
const authApi = require('../../api/auth')
const shopApi = require('../../api/shop')
const userApi = require('../../api/user')
const navigation = require('../../utils/navigation')
const { runWithLoading } = require('../../utils/loading')
const phoneRegex = /^1[3-9]\d{9}$/
Page({
data: {
redirect: '',
activeTab: 'login',
loginType: 'password',
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,
form: {
account: '',
password: '',
confirmPassword: '',
nickname: '',
phone: '',
verificationCode: '',
shopId: '',
rememberMe: true
},
resetForm: {
phone: '',
verificationCode: '',
newPassword: ''
}
},
onLoad(options) {
this.setData({
redirect: options && options.redirect ? options.redirect : ''
})
this.loadShopOptions()
},
async loadShopOptions() {
try {
const shops = await shopApi.getShopsSimple()
this.setData({
shops: Array.isArray(shops) ? shops : []
})
} catch (error) {
wx.showToast({
title: '门店列表加载失败',
icon: 'none'
})
}
},
handleTabChange(event) {
const tab = event.currentTarget.dataset.tab
this.setData({
activeTab: tab,
showResetPanel: false,
loginAccountStatusText: '',
loginAccountStatusType: '',
registerAccountStatusText: '',
registerAccountStatusType: '',
registerPhoneStatusText: '',
registerPhoneStatusType: ''
})
},
handleLoginTypeChange(event) {
const type = event.currentTarget.dataset.type
this.setData({
loginType: type,
loginShops: [],
loginShopIndex: -1,
loginShopDisplayName: '',
allowLoginShopless: false,
loginShopMessage: '',
loginAccountStatusText: '',
loginAccountStatusType: '',
'form.shopId': ''
})
},
handleToggleResetPanel() {
this.setData({
showResetPanel: !this.data.showResetPanel
})
},
handleInputChange(event) {
const field = event.currentTarget.dataset.field
this.setData({
[`form.${field}`]: event.detail.value
})
},
handleResetInputChange(event) {
const field = event.currentTarget.dataset.field
this.setData({
[`resetForm.${field}`]: event.detail.value
})
},
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(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 : ''
})
},
async handleAccountBlur() {
const account = this.data.form.account.trim()
if (!account) {
return
}
if (this.data.activeTab === 'register') {
await this.checkRegisterAccountAvailability()
return
}
if (this.data.loginType !== 'password') {
return
}
await Promise.all([
this.checkLoginAccountAvailability(),
this.loadLoginShopsByAccount()
])
},
async handlePhoneBlur() {
const phone = this.data.form.phone.trim()
if (!phone) {
return
}
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(selection) {
const shops = Array.isArray(selection && selection.shops) ? selection.shops : []
const allowLoginShopless = Boolean(selection && selection.allowShoplessLogin)
let loginShopIndex = -1
let loginShopDisplayName = ''
let shopId = ''
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(fieldName) {
this.setData({
[fieldName]: 60
})
const timer = setInterval(() => {
const nextValue = this.data[fieldName] - 1
this.setData({
[fieldName]: nextValue
})
if (nextValue <= 0) {
clearInterval(timer)
}
}, 1000)
},
async handleSendCode() {
if (this.data.countdown > 0) {
return
}
const phone = this.data.form.phone.trim()
if (!phoneRegex.test(phone)) {
wx.showToast({
title: '请输入正确的手机号',
icon: 'none'
})
return
}
if (this.data.activeTab === 'register') {
const phoneAvailable = await this.checkRegisterPhoneAvailability()
if (!phoneAvailable) {
wx.showToast({
title: this.data.registerPhoneStatusText || '该手机号已被注册',
icon: 'none'
})
return
}
}
try {
await runWithLoading(() => userApi.sendVerificationCode({
phone
}))
wx.showToast({
title: '验证码已发送',
icon: 'none'
})
this.startCountdown('countdown')
} catch (error) {
wx.showToast({
title: error.message || '验证码发送失败',
icon: 'none'
})
}
},
async handleSendForgotCode() {
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
}))
wx.showToast({
title: '验证码已发送',
icon: 'none'
})
this.startCountdown('forgotCountdown')
} catch (error) {
wx.showToast({
title: error.message || '验证码发送失败',
icon: 'none'
})
}
},
validateLoginForm() {
const form = this.data.form
if (this.data.loginType === 'password') {
if (!form.account.trim() || !form.password) {
return '请输入账号和密码'
}
} else if (!phoneRegex.test(form.phone.trim()) || !form.verificationCode.trim()) {
return '请输入正确的手机号和验证码'
}
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 (!form.password || form.password.length < 6) {
return '密码至少 6 位'
}
if (form.password !== form.confirmPassword) {
return '两次输入的密码不一致'
}
if (!form.shopId) {
return '请选择门店'
}
const [accountAvailable, phoneAvailable] = await Promise.all([
this.checkRegisterAccountAvailability(),
this.checkRegisterPhoneAvailability()
])
if (!accountAvailable || !phoneAvailable) {
return '请先处理注册信息中的占用问题'
}
return ''
},
async handleSubmit() {
this.setData({
loading: true
})
try {
if (this.data.activeTab === 'login') {
const loginError = this.validateLoginForm()
if (loginError) {
throw new Error(loginError)
}
let payload
await runWithLoading(async () => {
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
})
: await authApi.login({
account: this.data.form.account.trim(),
password: this.data.form.password,
rememberMe: true,
shopId: this.data.form.shopId || undefined
})
})
getApp().setUserSession(payload)
wx.showToast({
title: '登录成功',
icon: 'none'
})
try {
await navigation.redirectAfterLogin(this.data.redirect)
} catch (redirectError) {
console.error('登录后跳转失败,已回退到首页:', redirectError)
await navigation.switchTab('/pages/home/index')
}
return
}
const registerError = await this.validateRegisterForm()
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
}))
wx.showToast({
title: '注册成功,请登录',
icon: 'none'
})
this.setData({
activeTab: 'login',
loginType: '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 (!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
})
}
}
})
+5
View File
@@ -0,0 +1,5 @@
{
"usingComponents": {
"navigation-bar": "/components/navigation-bar/navigation-bar"
}
}
+132
View File
@@ -0,0 +1,132 @@
<navigation-bar title="账号中心" back="{{false}}" color="#2d2430" background="#fff7f8"></navigation-bar>
<view class="page-shell auth-page">
<view class="page-content auth-content">
<view class="page-hero auth-hero">
<text class="hero-kicker">YUMI NAIL</text>
<text class="hero-title">{{activeTab === 'login' ? '登录后继续预约与收藏' : '注册账号,开始你的美甲旅程'}}</text>
<text class="hero-desc">支持账号密码或手机验证码,绑定门店后,下单和查看记录都会更顺手。</text>
<view class="auth-benefits">
<view class="auth-benefit">
<text class="auth-benefit-title">收藏同步</text>
<text class="auth-benefit-desc">喜欢的作品随时可回看</text>
</view>
<view class="auth-benefit">
<text class="auth-benefit-title">预约顺手</text>
<text class="auth-benefit-desc">登录后直接补全预约信息</text>
</view>
<view class="auth-benefit">
<text class="auth-benefit-title">门店绑定</text>
<text class="auth-benefit-desc">注册时即可关联常用门店</text>
</view>
</view>
</view>
<view class="page-card auth-card">
<view class="tab-row">
<view class="tab-item {{activeTab === 'login' ? 'active' : ''}}" data-tab="login" bindtap="handleTabChange">登录</view>
<view class="tab-item {{activeTab === 'register' ? 'active' : ''}}" data-tab="register" bindtap="handleTabChange">注册</view>
</view>
<view wx:if="{{activeTab === 'login'}}" class="toggle-row">
<view class="toggle-item {{loginType === 'password' ? 'active' : ''}}" data-type="password" bindtap="handleLoginTypeChange">账号密码登录</view>
<view class="toggle-item {{loginType === 'phone' ? 'active' : ''}}" data-type="phone" bindtap="handleLoginTypeChange">验证码登录</view>
</view>
<view class="auth-helper">
{{activeTab === 'login' ? (loginType === 'password' ? '输入账号和密码即可登录。' : '手机号登录时会发送短信验证码。') : '注册前会实时检查账号和手机号是否已被占用。'}}
</view>
<block wx:if="{{activeTab === 'register'}}">
<view class="field-block">
<text class="field-label">昵称</text>
<input class="field-input" placeholder="请输入昵称" placeholder-class="field-placeholder" value="{{form.nickname}}" data-field="nickname" bindinput="handleInputChange"></input>
</view>
</block>
<view class="field-block">
<text class="field-label">账号</text>
<input class="field-input" placeholder="请输入账号" placeholder-class="field-placeholder" value="{{form.account}}" data-field="account" bindinput="handleInputChange" bindblur="handleAccountBlur"></input>
<text wx:if="{{activeTab === 'login' && loginAccountStatusText}}" class="status-text {{loginAccountStatusType}}">{{loginAccountStatusText}}</text>
<text wx:if="{{activeTab === 'register' && registerAccountStatusText}}" class="status-text {{registerAccountStatusType}}">{{registerAccountStatusText}}</text>
</view>
<view wx:if="{{activeTab === 'register' || loginType === 'phone'}}" class="field-block">
<text class="field-label">手机号</text>
<input class="field-input" type="number" placeholder="请输入手机号" placeholder-class="field-placeholder" value="{{form.phone}}" data-field="phone" bindinput="handleInputChange" bindblur="handlePhoneBlur"></input>
<text wx:if="{{activeTab === 'register' && registerPhoneStatusText}}" class="status-text {{registerPhoneStatusType}}">{{registerPhoneStatusText}}</text>
</view>
<view wx:if="{{activeTab === 'login' && loginType === 'password'}}" class="field-block">
<text class="field-label">密码</text>
<input class="field-input" password placeholder="请输入密码" placeholder-class="field-placeholder" value="{{form.password}}" data-field="password" bindinput="handleInputChange"></input>
</view>
<view wx:if="{{activeTab === 'register' || loginType === 'phone'}}" class="field-block">
<text class="field-label">验证码</text>
<view class="code-row">
<input class="field-input code-input" type="number" placeholder="请输入验证码" placeholder-class="field-placeholder" value="{{form.verificationCode}}" data-field="verificationCode" bindinput="handleInputChange"></input>
<view class="code-btn {{countdown > 0 ? 'disabled' : ''}}" bindtap="handleSendCode">{{countdown > 0 ? countdown + 's' : '发送验证码'}}</view>
</view>
</view>
<view wx:if="{{activeTab === 'register'}}" class="field-block">
<text class="field-label">密码</text>
<input class="field-input" password placeholder="请输入密码" placeholder-class="field-placeholder" value="{{form.password}}" data-field="password" bindinput="handleInputChange"></input>
</view>
<view wx:if="{{activeTab === 'register'}}" class="field-block">
<text class="field-label">确认密码</text>
<input class="field-input" password placeholder="请再次输入密码" placeholder-class="field-placeholder" value="{{form.confirmPassword}}" data-field="confirmPassword" bindinput="handleInputChange"></input>
</view>
<view wx:if="{{activeTab === 'register'}}" class="field-block">
<text class="field-label">选择门店</text>
<picker mode="selector" range="{{shops}}" range-key="shopName" value="{{registerShopIndex}}" bindchange="handleRegisterShopChange">
<view class="field-picker">{{registerShopDisplayName || '请选择门店'}}</view>
</picker>
</view>
<view wx:if="{{activeTab === 'login' && !allowLoginShopless && loginShops.length}}" class="field-block">
<text class="field-label">选择登录门店</text>
<picker mode="selector" range="{{loginShops}}" range-key="shopName" value="{{loginShopIndex}}" bindchange="handleLoginShopChange">
<view class="field-picker">{{loginShopDisplayName || '请选择登录门店'}}</view>
</picker>
</view>
<view wx:if="{{activeTab === 'login' && loginShopMessage}}" class="hint-card login-shop-tip">{{loginShopMessage}}</view>
<view wx:if="{{activeTab === 'login'}}" class="aux-row">
<text class="aux-link" bindtap="handleToggleResetPanel">忘记密码?</text>
</view>
<view class="primary-btn submit-btn" bindtap="handleSubmit">{{activeTab === 'login' ? '立即登录' : '完成注册'}}</view>
</view>
<view wx:if="{{showResetPanel}}" class="page-card reset-card">
<view class="section-title">重置密码</view>
<view class="section-subtitle">使用手机验证码设置新的登录密码。</view>
<view class="field-block">
<text class="field-label">手机号</text>
<input class="field-input" type="number" placeholder="请输入绑定手机号" placeholder-class="field-placeholder" value="{{resetForm.phone}}" data-field="phone" bindinput="handleResetInputChange"></input>
</view>
<view class="field-block">
<text class="field-label">验证码</text>
<view class="code-row">
<input class="field-input code-input" type="number" placeholder="请输入验证码" placeholder-class="field-placeholder" value="{{resetForm.verificationCode}}" data-field="verificationCode" bindinput="handleResetInputChange"></input>
<view class="code-btn {{forgotCountdown > 0 ? 'disabled' : ''}}" bindtap="handleSendForgotCode">{{forgotCountdown > 0 ? forgotCountdown + 's' : '发送验证码'}}</view>
</view>
</view>
<view class="field-block">
<text class="field-label">新密码</text>
<input class="field-input" password placeholder="请输入新密码" placeholder-class="field-placeholder" value="{{resetForm.newPassword}}" data-field="newPassword" bindinput="handleResetInputChange"></input>
</view>
<view class="primary-btn submit-btn" bindtap="handleResetPassword">确认重置密码</view>
</view>
</view>
</view>
+154
View File
@@ -0,0 +1,154 @@
.auth-page {
background:
radial-gradient(circle at top right, rgba(255, 204, 219, 0.4), transparent 30%),
linear-gradient(180deg, #fff7f8 0%, #fffdfb 100%);
}
.auth-content {
padding-top: 12rpx;
}
.auth-hero {
background:
radial-gradient(circle at top right, rgba(255, 190, 209, 0.44), transparent 34%),
linear-gradient(145deg, #fffdfd 0%, #fff2f6 58%, #ffe8ef 100%);
}
.auth-benefits {
display: flex;
gap: 14rpx;
margin-top: 28rpx;
}
.auth-benefit {
flex: 1;
min-width: 0;
padding: 20rpx 18rpx;
border-radius: 24rpx;
background: rgba(255, 255, 255, 0.82);
}
.auth-benefit-title {
display: block;
font-size: 24rpx;
font-weight: 700;
color: #2d2430;
}
.auth-benefit-desc {
display: block;
margin-top: 10rpx;
font-size: 21rpx;
line-height: 1.6;
color: #8d7a84;
}
.auth-card,
.reset-card {
padding: 28rpx;
}
.auth-card {
margin-top: 24rpx;
}
.reset-card {
margin-top: 24rpx;
}
.tab-row,
.toggle-row {
display: flex;
align-items: center;
gap: 12rpx;
}
.tab-row {
margin-bottom: 22rpx;
}
.toggle-row {
margin-bottom: 18rpx;
padding: 10rpx;
border-radius: 999rpx;
background: #fbf5f7;
}
.tab-item,
.toggle-item {
display: flex;
align-items: center;
justify-content: center;
flex: 1;
height: 76rpx;
border-radius: 999rpx;
color: #8d7a84;
font-size: 26rpx;
font-weight: 600;
}
.tab-item.active,
.toggle-item.active {
background: #fde8ef;
color: #c6597d;
}
.auth-helper {
margin-bottom: 10rpx;
font-size: 23rpx;
line-height: 1.7;
color: #8d7a84;
}
.field-block {
margin-top: 18rpx;
}
.code-row {
display: flex;
align-items: center;
gap: 14rpx;
}
.code-input {
flex: 1;
}
.code-btn {
display: inline-flex;
align-items: center;
justify-content: center;
min-width: 180rpx;
height: 92rpx;
padding: 0 18rpx;
border-radius: 24rpx;
background: #fde8ef;
color: #c6597d;
font-size: 24rpx;
font-weight: 700;
}
.code-btn.disabled {
background: #f3e7eb;
color: #a998a0;
}
.aux-row {
display: flex;
justify-content: flex-end;
margin-top: 20rpx;
}
.aux-link {
font-size: 24rpx;
color: #c6597d;
font-weight: 600;
}
.login-shop-tip {
margin-top: 18rpx;
}
.submit-btn {
margin-top: 28rpx;
}