setting.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import {userModify} 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. async onSave(){
  46. let params = {
  47. nickname:this.data.userInfo.nickname,
  48. avatar:this.data.pathUrl
  49. }
  50. const res = await userModify(params);
  51. if(res.code == 200){
  52. wx.showToast({
  53. title: '保存成功',
  54. icon: 'none',
  55. duration: 2000
  56. });
  57. app.globalData.userInfo = res.data;
  58. wx.navigateBack({ delta: 1 });
  59. }else{
  60. wx.showToast({
  61. title: res.message || '保存失败',
  62. icon: 'none',
  63. duration: 2000
  64. });
  65. }
  66. }
  67. })