setting.js 1.9 KB

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