user.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. changPhone(e){
  74. this.setData({
  75. 'mobile': e.detail.value,
  76. });
  77. },
  78. async save(e) {
  79. let type = e.currentTarget.dataset.type
  80. console.log(e)
  81. if (this.file) {
  82. let avatar = await (new Promise(r => {
  83. wx.uploadFile({
  84. filePath: this.data.avatar,
  85. name: 'file',
  86. url: api.UploadFile,
  87. header: {'X-Nideshop-Token': wx.getStorageSync('token')},
  88. success: (res) => {
  89. this.file = null
  90. r(JSON.parse(res.data).data)
  91. }
  92. })
  93. }));
  94. this.data.avatar = avatar
  95. }
  96. this.data.city = this.data.region && this.data.region.join(',')
  97. const body = {...this.data}
  98. delete body.region
  99. const {data} = await util.request(api.UpdateUserInfo, body, 'POST', 'application/json')
  100. if(type=='1'){
  101. wx.navigateBack()
  102. }else{
  103. }
  104. },
  105. /**
  106. * 生命周期函数--监听页面初次渲染完成
  107. */
  108. onReady: function () {
  109. },
  110. /**
  111. * 生命周期函数--监听页面显示
  112. */
  113. onShow: function () {
  114. },
  115. /**
  116. * 生命周期函数--监听页面隐藏
  117. */
  118. onHide: function () {
  119. },
  120. /**
  121. * 生命周期函数--监听页面卸载
  122. */
  123. onUnload: function () {
  124. },
  125. /**
  126. * 页面相关事件处理函数--监听用户下拉动作
  127. */
  128. onPullDownRefresh: function () {
  129. },
  130. /**
  131. * 页面上拉触底事件的处理函数
  132. */
  133. onReachBottom: function () {
  134. },
  135. /**
  136. * 用户点击右上角分享
  137. */
  138. onShareAppMessage: function () {
  139. }
  140. })