/** * 卡若创业派对 - 头像昵称引导页 * 登录后资料未完善时引导用户修改默认头像和昵称,仅包含头像+昵称两项 */ const app = getApp() Page({ data: { statusBarHeight: 44, avatar: '', nickname: '', saving: false, showPrivacyModal: false, nicknameInputFocus: 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 }) }, onNicknameBlur() { this.setData({ nicknameInputFocus: false }) }, onNicknameAreaTouch() { if (typeof wx.requirePrivacyAuthorize !== 'function') return wx.requirePrivacyAuthorize({ success: () => { this.setData({ nicknameInputFocus: true }) }, fail: () => {}, }) }, preventMove() {}, handleAgreePrivacy() { const app = getApp() if (app._privacyResolve) { app._privacyResolve({ buttonId: 'agree-btn', event: 'agree' }) app._privacyResolve = null } this.setData({ showPrivacyModal: false, nicknameInputFocus: true }) }, handleDisagreePrivacy() { const app = getApp() if (app._privacyResolve) { app._privacyResolve({ event: 'disagree' }) app._privacyResolve = null } this.setData({ showPrivacyModal: false }) }, async onChooseAvatar(e) { const tempAvatarUrl = e.detail?.avatarUrl 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, }) }) let avatarUrl = uploadRes.data?.url || uploadRes.url if (avatarUrl && !avatarUrl.startsWith('http')) { avatarUrl = app.globalData.baseUrl + avatarUrl } 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' }) }, })