setting.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. app.globalData.userInfo = null;
  57. app.globalData.programConfig = null;
  58. wx.navigateBack({ delta: 1 });
  59. }
  60. },
  61. async onSave(){
  62. let params = {
  63. nickname:this.data.userInfo.nickname,
  64. avatar:this.data.pathUrl
  65. }
  66. const res = await userModify(params);
  67. if(res.code == 200){
  68. wx.showToast({
  69. title: '保存成功',
  70. icon: 'none',
  71. duration: 2000
  72. });
  73. app.globalData.userInfo = res.data;
  74. wx.navigateBack({ delta: 1 });
  75. }else{
  76. wx.showToast({
  77. title: res.message || '保存失败',
  78. icon: 'none',
  79. duration: 2000
  80. });
  81. }
  82. }
  83. })