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

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
+244
View File
@@ -0,0 +1,244 @@
const authApi = require('../../api/auth')
const userApi = require('../../api/user')
const auth = require('../../utils/auth')
const navigation = require('../../utils/navigation')
const { DEFAULT_AVATAR } = require('../../utils/config')
const { runWithLoading } = require('../../utils/loading')
Page({
data: {
isLoggedIn: false,
loading: false,
savingProfile: false,
updatingPassword: false,
countdown: 0,
userInfo: null,
defaultAvatar: DEFAULT_AVATAR,
form: {
nickname: '',
signature: ''
},
passwordForm: {
verificationCode: '',
password: ''
}
},
onShow() {
this.loadSettings()
},
async loadSettings() {
const isLoggedIn = auth.isLoggedIn()
this.setData({
isLoggedIn,
loading: isLoggedIn
})
if (!isLoggedIn) {
this.setData({
userInfo: null,
loading: false
})
return
}
try {
const userInfo = await userApi.getUserInfo()
getApp().refreshUserInfo(userInfo)
this.setData({
userInfo,
'form.nickname': userInfo.nickname || '',
'form.signature': userInfo.signature || ''
})
} catch (error) {
wx.showToast({
title: error.message || '设置页加载失败',
icon: 'none'
})
} finally {
this.setData({
loading: false
})
}
},
handleBack() {
navigation.goBack('/pages/profile/index')
},
handleOpenAuth() {
navigation.openAuth('/pages/settings/index')
},
handleFormInput(event) {
const field = event.currentTarget.dataset.field
this.setData({
[`form.${field}`]: event.detail.value
})
},
handlePasswordInput(event) {
const field = event.currentTarget.dataset.field
this.setData({
[`passwordForm.${field}`]: event.detail.value
})
},
async handleSaveProfile() {
if (!this.data.form.nickname.trim()) {
wx.showToast({
title: '请输入昵称',
icon: 'none'
})
return
}
this.setData({
savingProfile: true
})
try {
await runWithLoading(() => userApi.updateUserInfo({
nickname: this.data.form.nickname.trim(),
signature: this.data.form.signature.trim()
}))
wx.showToast({
title: '资料已更新',
icon: 'none'
})
this.loadSettings()
} catch (error) {
wx.showToast({
title: error.message || '资料更新失败',
icon: 'none'
})
} finally {
this.setData({
savingProfile: false
})
}
},
startCountdown() {
this.setData({
countdown: 60
})
const timer = setInterval(() => {
const next = this.data.countdown - 1
this.setData({
countdown: next
})
if (next <= 0) {
clearInterval(timer)
}
}, 1000)
},
async handleSendPasswordCode() {
if (this.data.countdown > 0) {
return
}
const userInfo = this.data.userInfo
if (!userInfo || !userInfo.phone) {
wx.showToast({
title: '当前账号未绑定手机号',
icon: 'none'
})
return
}
try {
await runWithLoading(() => authApi.sendVerificationCode({
phone: userInfo.phone
}))
wx.showToast({
title: '验证码已发送',
icon: 'none'
})
this.startCountdown()
} catch (error) {
wx.showToast({
title: error.message || '验证码发送失败',
icon: 'none'
})
}
},
async handleUpdatePassword() {
if (!this.data.passwordForm.verificationCode.trim()) {
wx.showToast({
title: '请输入验证码',
icon: 'none'
})
return
}
if (!this.data.passwordForm.password || this.data.passwordForm.password.length < 6) {
wx.showToast({
title: '新密码至少 6 位',
icon: 'none'
})
return
}
this.setData({
updatingPassword: true
})
try {
await runWithLoading(() => userApi.updatePassword({
password: this.data.passwordForm.password,
verificationCode: this.data.passwordForm.verificationCode.trim()
}))
auth.setNeedSetPassword(false)
wx.showToast({
title: '密码已更新',
icon: 'none'
})
this.setData({
passwordForm: {
verificationCode: '',
password: ''
}
})
} catch (error) {
wx.showToast({
title: error.message || '密码更新失败',
icon: 'none'
})
} finally {
this.setData({
updatingPassword: false
})
}
},
async handleLogout() {
const confirmed = await new Promise(resolve => {
wx.showModal({
title: '退出登录',
content: '退出后需要重新登录才能查看订单和继续下单。',
success: res => resolve(Boolean(res.confirm))
})
})
if (!confirmed) {
return
}
try {
await runWithLoading(() => authApi.logout())
} catch (error) {}
getApp().clearUserSession()
wx.showToast({
title: '已退出登录',
icon: 'none'
})
wx.switchTab({
url: '/pages/home/index'
})
}
})
+5
View File
@@ -0,0 +1,5 @@
{
"usingComponents": {
"navigation-bar": "/components/navigation-bar/navigation-bar"
}
}
+68
View File
@@ -0,0 +1,68 @@
<view class="settings-page-root">
<navigation-bar title="设置中心" color="#2d2430" background="#fff7f8" bindback="handleBack"></navigation-bar>
<scroll-view class="settings-scroll" scroll-y enable-flex enhanced show-scrollbar="false">
<view class="page-shell settings-page-shell">
<view class="page-content">
<view wx:if="{{!isLoggedIn}}" class="page-card empty-card">
<text class="empty-title">登录后进入设置中心</text>
<text class="empty-desc">这里可以修改昵称、个性签名和登录密码。</text>
<view class="ghost-btn" bindtap="handleOpenAuth">去登录</view>
</view>
<block wx:elif="{{userInfo}}">
<view class="page-hero settings-hero">
<text class="hero-kicker">个人设置</text>
<text class="hero-title">维护你的资料与登录信息</text>
<text class="hero-desc">资料会同步到小程序端展示区域,密码更新后下次登录立即生效。</text>
</view>
<view class="page-card settings-card">
<view class="section-title">个人资料</view>
<view class="profile-preview">
<image class="avatar" src="{{userInfo.avatar || defaultAvatar}}" mode="aspectFill"></image>
<view class="profile-preview-copy">
<view class="preview-name">{{userInfo.nickname || 'Yumi 用户'}}</view>
<view class="preview-phone">{{userInfo.phone || '未绑定手机号'}}</view>
</view>
</view>
<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="handleFormInput"></input>
</view>
<view class="field-block">
<text class="field-label">个性签名</text>
<textarea class="field-textarea" placeholder="写一句想让客人认识你的话" placeholder-class="field-placeholder" value="{{form.signature}}" data-field="signature" bindinput="handleFormInput"></textarea>
</view>
<view class="primary-btn save-btn" bindtap="handleSaveProfile">{{savingProfile ? '保存中...' : '保存资料'}}</view>
</view>
<view class="page-card settings-card password-card">
<view class="section-title">登录密码</view>
<view class="section-subtitle">向已绑定手机号发送验证码后更新登录密码。</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="{{passwordForm.verificationCode}}" data-field="verificationCode" bindinput="handlePasswordInput"></input>
<view class="code-btn {{countdown > 0 ? 'disabled' : ''}}" bindtap="handleSendPasswordCode">{{countdown > 0 ? countdown + 's' : '发送验证码'}}</view>
</view>
</view>
<view class="field-block">
<text class="field-label">新密码</text>
<input class="field-input" password placeholder="请输入新密码" placeholder-class="field-placeholder" value="{{passwordForm.password}}" data-field="password" bindinput="handlePasswordInput"></input>
</view>
<view class="secondary-btn save-btn" bindtap="handleUpdatePassword">{{updatingPassword ? '更新中...' : '更新密码'}}</view>
</view>
<view class="logout-btn" bindtap="handleLogout">退出登录</view>
</block>
</view>
</view>
</scroll-view>
</view>
+111
View File
@@ -0,0 +1,111 @@
.settings-page-root {
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
.settings-scroll {
flex: 1;
min-height: 0;
}
.settings-page-shell {
min-height: 100%;
}
.settings-hero {
margin-bottom: 20rpx;
}
.settings-card + .settings-card {
margin-top: 20rpx;
}
.settings-card {
padding: 28rpx;
}
.profile-preview {
display: flex;
align-items: center;
gap: 18rpx;
margin-top: 22rpx;
}
.avatar {
width: 110rpx;
height: 110rpx;
border-radius: 50%;
background: #f7edf1;
}
.profile-preview-copy {
flex: 1;
}
.preview-name {
font-size: 32rpx;
font-weight: 700;
color: #2d2430;
}
.preview-phone {
margin-top: 10rpx;
font-size: 24rpx;
color: #8d7a84;
}
.field-block {
margin-top: 20rpx;
}
.save-btn {
margin-top: 26rpx;
}
.password-card {
margin-top: 20rpx;
}
.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;
}
.logout-btn {
display: flex;
align-items: center;
justify-content: center;
height: 92rpx;
margin-top: 24rpx;
border-radius: 999rpx;
background: #fff1f5;
color: #d5536f;
font-size: 28rpx;
font-weight: 700;
}