user.js 3.9 KB

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