user.js 2.7 KB

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