user.js 3.3 KB

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