user.js 4.1 KB

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