profilePatch.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // component/profilePatch.js
  2. const api = require('../../config/api')
  3. const util = require('../../utils/util.js');
  4. Component({
  5. /**
  6. * 组件的属性列表
  7. */
  8. properties: {
  9. show: { // 属性名
  10. type: Boolean,
  11. value: false,
  12. },
  13. },
  14. /**
  15. * 组件的初始数据
  16. */
  17. observers: {
  18. show: function (val) {
  19. // 在 numberA 或者 numberB 被设置时,执行这个函数
  20. console.log('gemer', val)
  21. this.setData({
  22. ifShow: val
  23. })
  24. }
  25. },
  26. data: {
  27. ifShow: true,
  28. bottom: 0,
  29. defaultAvatarUrl: 'https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180727/150547696d798c.png',
  30. avatar: '',
  31. nickname: ''
  32. },
  33. attached() {
  34. // this.setData({
  35. // bottom: 0
  36. // })
  37. // debugger
  38. },
  39. /**
  40. * 组件的方法列表
  41. */
  42. methods: {
  43. updateNickName(ev) {
  44. this.setData({
  45. nickname: ev.detail.value
  46. })
  47. },
  48. quitHandle: function () {
  49. getApp().setLoginProps(true)
  50. },
  51. async onChooseAvatar(e) {
  52. const {
  53. avatarUrl
  54. } = e.detail
  55. const dataURL = await (new Promise(r => {
  56. wx.uploadFile({
  57. filePath: avatarUrl,
  58. name: 'file',
  59. url: api.UploadFile,
  60. header: {
  61. 'X-Nideshop-Token': wx.getStorageSync('token')
  62. },
  63. success: (res) => {
  64. const data = JSON.parse(res.data)
  65. r(data.message || defaultAvatarUrl)
  66. }
  67. })
  68. }));
  69. console.log('upload-file', dataURL)
  70. this.setData({
  71. avatar: dataURL,
  72. })
  73. },
  74. async updateUserInfo() {
  75. const {
  76. avatar,
  77. nickname
  78. } = this.data
  79. if (avatar == '' || nickname == '') {
  80. return
  81. }
  82. const {
  83. data: getUserData
  84. } = await util.request(api.UserInfo)
  85. const body = {
  86. userId: getUserData.userId,
  87. avatar: this.data.avatar,
  88. nickname: this.data.nickname,
  89. }
  90. console.error('userInfo', body)
  91. const {
  92. data
  93. } = await util.request(api.UpdateUserInfo, body, 'POST', 'application/json')
  94. console.log('data', data)
  95. // wx.setStorageSync('token', data.token);
  96. const lastUpdate = {
  97. ...getUserData,
  98. ...body
  99. }
  100. console.error('userInfo-lastUpdate', lastUpdate)
  101. wx.setStorageSync('userInfo', lastUpdate);
  102. this.setData({
  103. ifShow: false
  104. })
  105. this.triggerEvent('save', lastUpdate)
  106. }
  107. }
  108. })