profilePatch.js 3.3 KB

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