user.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.data.nickname==""){
  76. wx.showToast({
  77. title: '请输入昵称',
  78. icon:'error'
  79. })
  80. return
  81. }
  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. wx.navigateBack()
  102. },
  103. /**
  104. * 生命周期函数--监听页面初次渲染完成
  105. */
  106. onReady: function () {
  107. },
  108. /**
  109. * 生命周期函数--监听页面显示
  110. */
  111. onShow: function () {
  112. },
  113. /**
  114. * 生命周期函数--监听页面隐藏
  115. */
  116. onHide: function () {
  117. },
  118. /**
  119. * 生命周期函数--监听页面卸载
  120. */
  121. onUnload: function () {
  122. },
  123. /**
  124. * 页面相关事件处理函数--监听用户下拉动作
  125. */
  126. onPullDownRefresh: function () {
  127. },
  128. /**
  129. * 页面上拉触底事件的处理函数
  130. */
  131. onReachBottom: function () {
  132. },
  133. /**
  134. * 用户点击右上角分享
  135. */
  136. onShareAppMessage: function () {
  137. }
  138. })