user.js 3.1 KB

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