156 lines
4.4 KiB
JavaScript
156 lines
4.4 KiB
JavaScript
|
|
/**
|
|||
|
|
* Soul创业派对 - 头像昵称引导页
|
|||
|
|
* 登录后资料未完善时引导用户修改默认头像和昵称,仅包含头像+昵称两项
|
|||
|
|
*/
|
|||
|
|
const app = getApp()
|
|||
|
|
|
|||
|
|
Page({
|
|||
|
|
data: {
|
|||
|
|
statusBarHeight: 44,
|
|||
|
|
avatar: '',
|
|||
|
|
nickname: '',
|
|||
|
|
saving: false,
|
|||
|
|
showAvatarModal: false,
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
onLoad() {
|
|||
|
|
this.setData({ statusBarHeight: app.globalData.statusBarHeight || 44 })
|
|||
|
|
this.loadFromUser()
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
loadFromUser() {
|
|||
|
|
const user = app.globalData.userInfo
|
|||
|
|
if (!app.globalData.isLoggedIn || !user?.id) {
|
|||
|
|
wx.showToast({ title: '请先登录', icon: 'none' })
|
|||
|
|
setTimeout(() => getApp().goBackOrToHome(), 1500)
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
const nickname = (user.nickname || user.nickName || '').trim()
|
|||
|
|
const avatar = user.avatar || user.avatarUrl || ''
|
|||
|
|
this.setData({ nickname, avatar })
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
goBack() {
|
|||
|
|
getApp().goBackOrToHome()
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
onNicknameInput(e) {
|
|||
|
|
this.setData({ nickname: e.detail.value })
|
|||
|
|
},
|
|||
|
|
onNicknameChange(e) {
|
|||
|
|
this.setData({ nickname: e.detail.value })
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
onAvatarTap() {
|
|||
|
|
wx.showActionSheet({
|
|||
|
|
itemList: ['使用微信头像', '从相册选择'],
|
|||
|
|
success: (res) => {
|
|||
|
|
if (res.tapIndex === 0) {
|
|||
|
|
this.setData({ showAvatarModal: true })
|
|||
|
|
} else if (res.tapIndex === 1) {
|
|||
|
|
this.chooseAvatarFromAlbum()
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
})
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
closeAvatarModal() {
|
|||
|
|
this.setData({ showAvatarModal: false })
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
chooseAvatarFromAlbum() {
|
|||
|
|
wx.chooseMedia({
|
|||
|
|
count: 1,
|
|||
|
|
mediaType: ['image'],
|
|||
|
|
sourceType: ['album', 'camera'],
|
|||
|
|
success: async (res) => {
|
|||
|
|
const tempPath = res.tempFiles[0].tempFilePath
|
|||
|
|
await this.uploadAndSaveAvatar(tempPath)
|
|||
|
|
},
|
|||
|
|
})
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
async onChooseAvatar(e) {
|
|||
|
|
const tempAvatarUrl = e.detail?.avatarUrl
|
|||
|
|
this.setData({ showAvatarModal: false })
|
|||
|
|
if (!tempAvatarUrl) return
|
|||
|
|
await this.uploadAndSaveAvatar(tempAvatarUrl)
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
async uploadAndSaveAvatar(tempPath) {
|
|||
|
|
wx.showLoading({ title: '上传中...', mask: true })
|
|||
|
|
try {
|
|||
|
|
const uploadRes = await new Promise((resolve, reject) => {
|
|||
|
|
wx.uploadFile({
|
|||
|
|
url: app.globalData.baseUrl + '/api/miniprogram/upload',
|
|||
|
|
filePath: tempPath,
|
|||
|
|
name: 'file',
|
|||
|
|
formData: { folder: 'avatars' },
|
|||
|
|
success: (r) => {
|
|||
|
|
try {
|
|||
|
|
const data = JSON.parse(r.data)
|
|||
|
|
if (data.success) resolve(data)
|
|||
|
|
else reject(new Error(data.error || '上传失败'))
|
|||
|
|
} catch {
|
|||
|
|
reject(new Error('解析失败'))
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
fail: reject,
|
|||
|
|
})
|
|||
|
|
})
|
|||
|
|
const avatarUrl = app.globalData.baseUrl + uploadRes.data.url
|
|||
|
|
this.setData({ avatar: avatarUrl })
|
|||
|
|
await app.request({
|
|||
|
|
url: '/api/miniprogram/user/profile',
|
|||
|
|
method: 'POST',
|
|||
|
|
data: { userId: app.globalData.userInfo?.id, avatar: avatarUrl },
|
|||
|
|
})
|
|||
|
|
if (app.globalData.userInfo) {
|
|||
|
|
app.globalData.userInfo.avatar = avatarUrl
|
|||
|
|
wx.setStorageSync('userInfo', app.globalData.userInfo)
|
|||
|
|
}
|
|||
|
|
wx.hideLoading()
|
|||
|
|
wx.showToast({ title: '头像已更新', icon: 'success' })
|
|||
|
|
} catch (e) {
|
|||
|
|
wx.hideLoading()
|
|||
|
|
wx.showToast({ title: e.message || '上传失败', icon: 'none' })
|
|||
|
|
}
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
async saveProfile() {
|
|||
|
|
const userId = app.globalData.userInfo?.id
|
|||
|
|
if (!userId) {
|
|||
|
|
wx.showToast({ title: '请先登录', icon: 'none' })
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
const nickname = (this.data.nickname || '').trim()
|
|||
|
|
const avatar = (this.data.avatar || '').trim()
|
|||
|
|
if (!nickname) {
|
|||
|
|
wx.showToast({ title: '请输入昵称', icon: 'none' })
|
|||
|
|
return
|
|||
|
|
}
|
|||
|
|
this.setData({ saving: true })
|
|||
|
|
try {
|
|||
|
|
await app.request({
|
|||
|
|
url: '/api/miniprogram/user/profile',
|
|||
|
|
method: 'POST',
|
|||
|
|
data: { userId, avatar: avatar || undefined, nickname },
|
|||
|
|
})
|
|||
|
|
if (app.globalData.userInfo) {
|
|||
|
|
if (nickname) app.globalData.userInfo.nickname = nickname
|
|||
|
|
if (avatar) app.globalData.userInfo.avatar = avatar
|
|||
|
|
wx.setStorageSync('userInfo', app.globalData.userInfo)
|
|||
|
|
}
|
|||
|
|
wx.showToast({ title: '保存成功', icon: 'success' })
|
|||
|
|
setTimeout(() => getApp().goBackOrToHome(), 800)
|
|||
|
|
} catch (e) {
|
|||
|
|
wx.showToast({ title: e.message || '保存失败', icon: 'none' })
|
|||
|
|
}
|
|||
|
|
this.setData({ saving: false })
|
|||
|
|
},
|
|||
|
|
|
|||
|
|
goToFullProfile() {
|
|||
|
|
wx.navigateTo({ url: '/pages/profile-edit/profile-edit' })
|
|||
|
|
},
|
|||
|
|
})
|