189 lines
5.2 KiB
JavaScript
189 lines
5.2 KiB
JavaScript
/**
|
|
* 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,
|
|
navBarTotalHeight: 88,
|
|
avatar: '',
|
|
nickname: '',
|
|
mbtiList: MBTI_LIST,
|
|
mbtiIndex: 0,
|
|
mbti: '',
|
|
region: '',
|
|
industry: '',
|
|
businessVolume: '',
|
|
position: '',
|
|
mostProfitableMonth: '',
|
|
howCanHelp: ''
|
|
},
|
|
|
|
onLoad() {
|
|
const statusBarHeight = app.globalData.statusBarHeight || 44
|
|
const navBarTotalHeight = statusBarHeight + 44
|
|
this.setData({
|
|
statusBarHeight,
|
|
navBarTotalHeight
|
|
})
|
|
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' })
|
|
}
|
|
}
|
|
})
|