setting.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {userModify,logout} from '../../api/user';
  2. import { uploadImage } from '../../utils/upload.js';
  3. const app = getApp();
  4. Page({
  5. data: {
  6. userInfo:{},
  7. pathUrl:'',//保存接口需要传的值
  8. },
  9. onReady() {
  10. this.setData({
  11. userInfo: app.globalData.userInfo
  12. });
  13. },
  14. // 昵称
  15. onInputChange(e) {
  16. this.setData({
  17. "userInfo.nickname": e.detail.value
  18. })
  19. },
  20. // 头像
  21. async onChooseAvatar(e) {
  22. const { avatarUrl } = e.detail;
  23. // 上传到服务器
  24. const res = await uploadImage(avatarUrl);
  25. this.setData({
  26. "userInfo.avatar":avatarUrl,
  27. pathUrl: res.path
  28. });
  29. },
  30. async getPhoneNumber(e) {
  31. if (e.detail.errMsg === "getPhoneNumber:ok") {
  32. const { encryptedData, iv } = e.detail;
  33. console.log('加密手机号数据:', encryptedData, iv);
  34. const res = await userModify({phone:{
  35. encryptedData,
  36. iv
  37. }});
  38. this.setData({
  39. "userInfo.phone":res.data.phone,
  40. });
  41. } else {
  42. wx.showToast({ title: '授权失败', icon: 'none' });
  43. }
  44. },
  45. // 退出登录
  46. async onExit(){
  47. const res = await logout();
  48. if(res.code == 200){
  49. wx.showToast({
  50. title: res.code || '登出成功',
  51. icon: 'none'
  52. });
  53. wx.removeStorageSync('token');
  54. wx.removeStorageSync('expires_in');
  55. wx.removeStorageSync('posterCache');
  56. wx.removeStorageSync('userInfo');
  57. wx.removeStorageSync('programConfig');
  58. app.globalData.userInfo = null;
  59. app.globalData.programConfig = null;
  60. wx.navigateBack({ delta: 1 });
  61. }
  62. },
  63. async onSave(){
  64. let params = {
  65. nickname:this.data.userInfo.nickname,
  66. avatar:this.data.pathUrl
  67. }
  68. const res = await userModify(params);
  69. if(res.code == 200){
  70. wx.showToast({
  71. title: '保存成功',
  72. icon: 'none',
  73. duration: 2000
  74. });
  75. wx.setStorageSync('userInfo', res.data)
  76. app.globalData.userInfo = res.data;
  77. wx.navigateBack({ delta: 1 });
  78. }else{
  79. wx.showToast({
  80. title: res.message || '保存失败',
  81. icon: 'none',
  82. duration: 2000
  83. });
  84. }
  85. }
  86. })