user.js 3.7 KB

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