user.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // pages/user/user.js
  2. const api = require('../../config/api.js');
  3. const util = require('../../utils/util.js');
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. birthday: '',
  10. region: [],
  11. customItem: '全部',
  12. gender: 0,
  13. mobile: null
  14. },
  15. updateNickName(ev) {
  16. this.setData({
  17. nickname: ev.detail.value
  18. })
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. this.getUserInfo()
  25. let userInfo = wx.getStorageSync('userInfo')
  26. userInfo.avatar = userInfo.avatarUrl
  27. this.sessionKey = userInfo.sessionKey
  28. delete userInfo.avatarUrl
  29. delete userInfo.sessionKey
  30. this.setData({
  31. ...userInfo
  32. })
  33. },
  34. loginSuccess() {
  35. this.onLoad()
  36. },
  37. async getUserInfo() {
  38. const {
  39. data
  40. } = await util.request(api.UserInfo)
  41. data.region = data.city ? data.city.split(',') : []
  42. data.birthday = data.birthday || '1990-01-01'
  43. this.setData({
  44. ...data
  45. })
  46. },
  47. async getPhoneNumber(e) {
  48. let {
  49. encryptedData,
  50. iv
  51. } = e.detail
  52. console.log(e)
  53. if (!encryptedData) return;
  54. let res = await util.request(api.decryptedPhoneNum, {
  55. encryptedData,
  56. iv,
  57. sessionKey: this.sessionKey
  58. })
  59. console.log(res)
  60. this.setData({
  61. mobile: res.phoneNum
  62. })
  63. },
  64. updateSex(e) {
  65. this.setData({
  66. gender: e.currentTarget.dataset.sex
  67. })
  68. },
  69. bindDateChange(e) {
  70. this.setData({
  71. birthday: e.detail.value
  72. })
  73. },
  74. bindRegionChange(e) {
  75. this.setData({
  76. region: e.detail.value
  77. })
  78. },
  79. selectPhoto() {
  80. // wx.chooseImage({
  81. // count: 1,
  82. // sizeType: 'compressed',
  83. // success: (res) => {
  84. // const src = res.tempFilePaths[0]
  85. // // this.file = res.tempFiles[0]
  86. // wx.navigateTo({
  87. // url: './imageCropper?image=' + src,
  88. // })
  89. // // this.setData({ avatar: src })
  90. // }
  91. // })
  92. },
  93. changPhone(e) {
  94. this.setData({
  95. 'mobile': e.detail.value,
  96. });
  97. },
  98. async save(e) {
  99. let type = e.currentTarget.dataset.type
  100. console.log(e)
  101. if (this.data.nickname == '') {
  102. wx.showToast({
  103. title: '请输入昵称',
  104. icon: 'error'
  105. })
  106. return
  107. }
  108. if (this.file) {
  109. let avatar = await (new Promise(r => {
  110. wx.uploadFile({
  111. filePath: this.data.avatar,
  112. name: 'file',
  113. url: api.UploadFile,
  114. header: {
  115. 'X-Nideshop-Token': wx.getStorageSync('token')
  116. },
  117. success: (res) => {
  118. this.file = null
  119. r(JSON.parse(res.data).data)
  120. }
  121. })
  122. }));
  123. this.data.avatar = avatar
  124. }
  125. this.data.city = this.data.region && this.data.region.join(',')
  126. const body = {
  127. ...this.data
  128. }
  129. delete body.region
  130. const {
  131. data
  132. } = await util.request(api.UpdateUserInfo, body, 'POST', 'application/json')
  133. if (type == '1') {
  134. wx.navigateBack()
  135. } else {
  136. }
  137. },
  138. /**
  139. * 生命周期函数--监听页面初次渲染完成
  140. */
  141. onReady: function () {
  142. },
  143. /**
  144. * 生命周期函数--监听页面显示
  145. */
  146. onShow: function () {},
  147. /**
  148. * 生命周期函数--监听页面隐藏
  149. */
  150. onHide: function () {
  151. },
  152. /**
  153. * 生命周期函数--监听页面卸载
  154. */
  155. onUnload: function () {
  156. },
  157. /**
  158. * 页面相关事件处理函数--监听用户下拉动作
  159. */
  160. onPullDownRefresh: function () {
  161. },
  162. /**
  163. * 页面上拉触底事件的处理函数
  164. */
  165. onReachBottom: function () {
  166. },
  167. /**
  168. * 用户点击右上角分享
  169. */
  170. onShareAppMessage: function () {
  171. }
  172. })