import {userModify,logout} from '../../api/user'; import { uploadImage } from '../../utils/upload.js'; const app = getApp(); Page({ data: { userInfo:{}, pathUrl:'',//保存接口需要传的值 }, onReady() { this.setData({ userInfo: app.globalData.userInfo }); }, // 昵称 onInputChange(e) { this.setData({ "userInfo.nickname": e.detail.value }) }, // 头像 async onChooseAvatar(e) { const { avatarUrl } = e.detail; // 上传到服务器 const res = await uploadImage(avatarUrl); this.setData({ "userInfo.avatar":avatarUrl, pathUrl: res.path }); }, async getPhoneNumber(e) { if (e.detail.errMsg === "getPhoneNumber:ok") { const { encryptedData, iv } = e.detail; console.log('加密手机号数据:', encryptedData, iv); const res = await userModify({phone:{ encryptedData, iv }}); this.setData({ "userInfo.phone":res.data.phone, }); } else { wx.showToast({ title: '授权失败', icon: 'none' }); } }, // 退出登录 async onExit(){ const res = await logout(); if(res.code == 200){ wx.showToast({ title: res.code || '登出成功', icon: 'none' }); wx.removeStorageSync('token'); wx.removeStorageSync('expires_in'); wx.removeStorageSync('posterCache'); wx.removeStorageSync('userInfo'); wx.removeStorageSync('programConfig'); app.globalData.userInfo = null; app.globalData.programConfig = null; wx.navigateBack({ delta: 1 }); } }, async onSave(){ let params = { nickname:this.data.userInfo.nickname, avatar:this.data.pathUrl } const res = await userModify(params); if(res.code == 200){ wx.showToast({ title: '保存成功', icon: 'none', duration: 2000 }); wx.setStorageSync('userInfo', res.data) app.globalData.userInfo = res.data; wx.navigateBack({ delta: 1 }); }else{ wx.showToast({ title: res.message || '保存失败', icon: 'none', duration: 2000 }); } } })