mirror of
https://gitee.com/qingqing-coconut-tea/RcKtFezyRAZX
synced 2026-07-21 10:32:32 +08:00
195 lines
4.7 KiB
JavaScript
195 lines
4.7 KiB
JavaScript
// pages/profile/profile.js
|
||
// 个人资料逻辑处理
|
||
|
||
const app = getApp()
|
||
const api = require('../../services/api')
|
||
|
||
Page({
|
||
data: {
|
||
userInfo: null,
|
||
formData: {
|
||
nickname: '',
|
||
gender: '',
|
||
birthday: '',
|
||
bio: '',
|
||
email: '',
|
||
phone: ''
|
||
},
|
||
avatarUrl: '',
|
||
genderIndex: 0,
|
||
genderList: ['未知', '男', '女'],
|
||
birthday: '',
|
||
loading: false
|
||
},
|
||
|
||
onLoad() {
|
||
this.loadUserInfo()
|
||
},
|
||
|
||
onShow() {
|
||
this.checkLoginStatus()
|
||
},
|
||
|
||
// 检查登录状态
|
||
checkLoginStatus() {
|
||
const userInfo = wx.getStorageSync('userInfo')
|
||
if (!userInfo) {
|
||
wx.redirectTo({ url: '/pages/login/login' })
|
||
return
|
||
}
|
||
this.setData({ userInfo })
|
||
},
|
||
|
||
// 加载用户信息
|
||
loadUserInfo() {
|
||
api.getUserInfo().then(res => {
|
||
if (res.code === 200) {
|
||
const userInfo = res.data
|
||
this.setData({
|
||
userInfo,
|
||
avatarUrl: userInfo.avatar || '',
|
||
formData: {
|
||
nickname: userInfo.nickname || '',
|
||
gender: userInfo.gender || '',
|
||
birthday: userInfo.birthday || '',
|
||
bio: userInfo.bio || '',
|
||
email: userInfo.email || '',
|
||
phone: userInfo.phone || ''
|
||
},
|
||
genderIndex: this.getGenderIndex(userInfo.gender)
|
||
})
|
||
}
|
||
})
|
||
},
|
||
|
||
// 获取性别索引
|
||
getGenderIndex(gender) {
|
||
const index = this.data.genderList.findIndex(item => item === gender)
|
||
return index >= 0 ? index : 0
|
||
},
|
||
|
||
// 选择头像
|
||
onChooseAvatar(e) {
|
||
const { avatarUrl } = e.detail
|
||
this.setData({ avatarUrl })
|
||
this.uploadAvatar(avatarUrl)
|
||
},
|
||
|
||
// 上传头像
|
||
uploadAvatar(filePath) {
|
||
wx.showLoading({ title: '上传中...' })
|
||
|
||
wx.uploadFile({
|
||
url: api.getBaseUrl() + '/upload',
|
||
filePath: filePath,
|
||
name: 'file',
|
||
header: { 'Authorization': 'Bearer ' + wx.getStorageSync('token') },
|
||
success: res => {
|
||
wx.hideLoading()
|
||
const data = JSON.parse(res.data)
|
||
if (data.code === 200) {
|
||
this.setData({ avatarUrl: data.data.url })
|
||
wx.showToast({ title: '头像更新成功', icon: 'success' })
|
||
} else {
|
||
wx.showToast({ title: data.message || '上传失败', icon: 'none' })
|
||
}
|
||
},
|
||
fail: () => {
|
||
wx.hideLoading()
|
||
wx.showToast({ title: '上传失败', icon: 'none' })
|
||
}
|
||
})
|
||
},
|
||
|
||
// 昵称输入
|
||
onNicknameInput(e) {
|
||
const formData = this.data.formData
|
||
formData.nickname = e.detail.value
|
||
this.setData({ formData })
|
||
},
|
||
|
||
// 性别选择
|
||
onGenderChange(e) {
|
||
const index = e.detail.value
|
||
const formData = this.data.formData
|
||
formData.gender = this.data.genderList[index]
|
||
this.setData({ formData, genderIndex: index })
|
||
},
|
||
|
||
// 生日选择
|
||
onBirthdayChange(e) {
|
||
const formData = this.data.formData
|
||
formData.birthday = e.detail.value
|
||
this.setData({ formData })
|
||
},
|
||
|
||
// 简介输入
|
||
onBioInput(e) {
|
||
const formData = this.data.formData
|
||
formData.bio = e.detail.value
|
||
this.setData({ formData })
|
||
},
|
||
|
||
// 邮箱输入
|
||
onEmailInput(e) {
|
||
const formData = this.data.formData
|
||
formData.email = e.detail.value
|
||
this.setData({ formData })
|
||
},
|
||
|
||
// 保存资料
|
||
saveProfile() {
|
||
const { formData, avatarUrl } = this.data
|
||
|
||
if (!formData.nickname.trim()) {
|
||
wx.showToast({ title: '请输入昵称', icon: 'none' })
|
||
return
|
||
}
|
||
|
||
this.setData({ loading: true })
|
||
|
||
api.updateUserInfo({
|
||
nickname: formData.nickname,
|
||
gender: formData.gender,
|
||
birthday: formData.birthday,
|
||
bio: formData.bio,
|
||
email: formData.email,
|
||
avatar: avatarUrl
|
||
}).then(res => {
|
||
this.setData({ loading: false })
|
||
if (res.code === 200) {
|
||
wx.setStorageSync('userInfo', res.data)
|
||
wx.showToast({ title: '保存成功', icon: 'success' })
|
||
setTimeout(() => {
|
||
wx.navigateBack()
|
||
}, 1500)
|
||
} else {
|
||
wx.showToast({ title: res.message || '保存失败', icon: 'none' })
|
||
}
|
||
}).catch(() => {
|
||
this.setData({ loading: false })
|
||
wx.showToast({ title: '保存失败', icon: 'none' })
|
||
})
|
||
},
|
||
|
||
// 修改密码
|
||
goToChangePassword() {
|
||
wx.navigateTo({
|
||
url: '/pages/change-password/change-password'
|
||
})
|
||
},
|
||
|
||
// 绑定手机号
|
||
goToBindPhone() {
|
||
wx.navigateTo({
|
||
url: '/pages/bind-phone/bind-phone'
|
||
})
|
||
}
|
||
})
|
||
|
||
/**
|
||
* @联系作者:16768118056
|
||
* @描述:(此项目非免费分享)源码百分百可用,搞定毕设不发愁,支持远程调试安装、二次开发、定制、讲解、文档类。
|
||
* @访问:https://www.notmaker.com/detail/8326a897119d4542896c863a69d7bbe7/gtt20260415,查看完整运行演示。
|
||
*/
|