user.js 3.1 KB

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