12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- import {userModify,logout} from '../../api/user';
- import { uploadImage } from '../../utils/upload.js';
- import {BASE_URL} from '../../utils/request';
- const app = getApp();
- Page({
- data: {
- userInfo:{},
- baseUrl:BASE_URL,
- 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
- });
- }
- }
- })
|