新增编辑资料入口,优化用户个人信息管理体验。更新 app.json 文件以包含新页面路径,确保导航功能正常。调整样式以提升界面友好性。
This commit is contained in:
184
miniprogram/pages/profile-edit/profile-edit.js
Normal file
184
miniprogram/pages/profile-edit/profile-edit.js
Normal file
@@ -0,0 +1,184 @@
|
||||
/**
|
||||
* Soul创业派对 - 编辑资料页
|
||||
* 图二样式,行业/业务体量拆成两个独立输入框
|
||||
*/
|
||||
|
||||
const app = getApp()
|
||||
|
||||
const MBTI_LIST = ['INTJ', 'INTP', 'ENTJ', 'ENTP', 'INFJ', 'INFP', 'ENFJ', 'ENFP', 'ISTJ', 'ISFJ', 'ESTJ', 'ESFJ', 'ISTP', 'ISFP', 'ESTP', 'ESFP']
|
||||
|
||||
Page({
|
||||
data: {
|
||||
statusBarHeight: 44,
|
||||
avatar: '',
|
||||
nickname: '',
|
||||
mbtiList: MBTI_LIST,
|
||||
mbtiIndex: 0,
|
||||
mbti: '',
|
||||
region: '',
|
||||
industry: '',
|
||||
businessVolume: '',
|
||||
position: '',
|
||||
mostProfitableMonth: '',
|
||||
howCanHelp: ''
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
this.setData({
|
||||
statusBarHeight: app.globalData.statusBarHeight || 44
|
||||
})
|
||||
this.loadProfile()
|
||||
},
|
||||
|
||||
loadProfile() {
|
||||
const userInfo = app.globalData.userInfo || wx.getStorageSync('userInfo') || {}
|
||||
const ext = wx.getStorageSync('userProfileExt') || {}
|
||||
const mbti = ext.mbti || ''
|
||||
const mbtiIndex = MBTI_LIST.indexOf(mbti)
|
||||
this.setData({
|
||||
avatar: userInfo.avatar || '',
|
||||
nickname: userInfo.nickname || '',
|
||||
mbti: mbti,
|
||||
mbtiIndex: mbtiIndex >= 0 ? mbtiIndex : 0,
|
||||
region: ext.region || '',
|
||||
industry: ext.industry || '',
|
||||
businessVolume: ext.businessVolume || '',
|
||||
position: ext.position || '',
|
||||
mostProfitableMonth: ext.mostProfitableMonth || '',
|
||||
howCanHelp: ext.howCanHelp || ''
|
||||
})
|
||||
},
|
||||
|
||||
goBack() {
|
||||
wx.navigateBack()
|
||||
},
|
||||
|
||||
onNicknameInput(e) {
|
||||
this.setData({ nickname: e.detail.value })
|
||||
},
|
||||
|
||||
onMbtiChange(e) {
|
||||
const i = parseInt(e.detail.value, 10)
|
||||
this.setData({
|
||||
mbtiIndex: i,
|
||||
mbti: MBTI_LIST[i] || ''
|
||||
})
|
||||
},
|
||||
|
||||
onRegionInput(e) {
|
||||
this.setData({ region: e.detail.value })
|
||||
},
|
||||
|
||||
onIndustryInput(e) {
|
||||
this.setData({ industry: e.detail.value })
|
||||
},
|
||||
|
||||
onBusinessVolumeInput(e) {
|
||||
this.setData({ businessVolume: e.detail.value })
|
||||
},
|
||||
|
||||
onPositionInput(e) {
|
||||
this.setData({ position: e.detail.value })
|
||||
},
|
||||
|
||||
onMostProfitableMonthInput(e) {
|
||||
this.setData({ mostProfitableMonth: e.detail.value })
|
||||
},
|
||||
|
||||
onHowCanHelpInput(e) {
|
||||
this.setData({ howCanHelp: e.detail.value })
|
||||
},
|
||||
|
||||
async onChooseAvatar(e) {
|
||||
const tempAvatarUrl = e.detail.avatarUrl
|
||||
if (!tempAvatarUrl) return
|
||||
|
||||
wx.showLoading({ title: '上传中...', mask: true })
|
||||
|
||||
try {
|
||||
const uploadRes = await new Promise((resolve, reject) => {
|
||||
wx.uploadFile({
|
||||
url: app.globalData.baseUrl + '/api/miniprogram/upload',
|
||||
filePath: tempAvatarUrl,
|
||||
name: 'file',
|
||||
formData: { folder: 'avatars' },
|
||||
success: (res) => {
|
||||
try {
|
||||
const data = JSON.parse(res.data)
|
||||
if (data.success) resolve(data)
|
||||
else reject(new Error(data.error || '上传失败'))
|
||||
} catch (err) {
|
||||
reject(new Error('解析响应失败'))
|
||||
}
|
||||
},
|
||||
fail: (err) => reject(err)
|
||||
})
|
||||
})
|
||||
|
||||
const avatarUrl = app.globalData.baseUrl + uploadRes.data.url
|
||||
this.setData({ avatar: avatarUrl })
|
||||
|
||||
const userInfo = app.globalData.userInfo
|
||||
if (userInfo && userInfo.id) {
|
||||
await app.request({
|
||||
url: '/api/miniprogram/user/update',
|
||||
method: 'POST',
|
||||
data: { userId: userInfo.id, avatar: avatarUrl }
|
||||
})
|
||||
userInfo.avatar = avatarUrl
|
||||
app.globalData.userInfo = userInfo
|
||||
wx.setStorageSync('userInfo', userInfo)
|
||||
}
|
||||
|
||||
wx.hideLoading()
|
||||
wx.showToast({ title: '头像已更新', icon: 'success' })
|
||||
} catch (e) {
|
||||
wx.hideLoading()
|
||||
wx.showToast({ title: e.message || '上传失败', icon: 'none' })
|
||||
}
|
||||
},
|
||||
|
||||
async saveProfile() {
|
||||
const { nickname, mbti, region, industry, businessVolume, position, mostProfitableMonth, howCanHelp } = this.data
|
||||
const userInfo = app.globalData.userInfo
|
||||
|
||||
wx.showLoading({ title: '保存中...', mask: true })
|
||||
|
||||
try {
|
||||
if (userInfo && userInfo.id) {
|
||||
const payload = { userId: userInfo.id }
|
||||
if (nickname !== undefined && nickname !== userInfo.nickname) payload.nickname = nickname.trim()
|
||||
if (Object.keys(payload).length > 1) {
|
||||
await app.request({
|
||||
url: '/api/miniprogram/user/update',
|
||||
method: 'POST',
|
||||
data: payload
|
||||
})
|
||||
if (payload.nickname !== undefined) {
|
||||
userInfo.nickname = payload.nickname
|
||||
app.globalData.userInfo = userInfo
|
||||
wx.setStorageSync('userInfo', userInfo)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const ext = {
|
||||
mbti: (mbti || '').trim(),
|
||||
region: (region || '').trim(),
|
||||
industry: (industry || '').trim(),
|
||||
businessVolume: (businessVolume || '').trim(),
|
||||
position: (position || '').trim(),
|
||||
mostProfitableMonth: (mostProfitableMonth || '').trim(),
|
||||
howCanHelp: (howCanHelp || '').trim()
|
||||
}
|
||||
wx.setStorageSync('userProfileExt', ext)
|
||||
|
||||
wx.hideLoading()
|
||||
wx.showToast({ title: '保存成功', icon: 'success' })
|
||||
setTimeout(() => wx.navigateBack(), 500)
|
||||
} catch (e) {
|
||||
wx.hideLoading()
|
||||
wx.showToast({ title: e.message || '保存失败', icon: 'none' })
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user