setting.js 2.2 KB

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