personinfo.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // pages/personinfo/personinfo.js
  2. import userApi from '../../api/user'
  3. import uploadApi from '../../api/upload'
  4. const app = getApp();
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. endDate: "",
  11. birth: "",
  12. sex: 0,
  13. array: ["未知", "男", "女"],
  14. userName: "",
  15. imgUrl: "",
  16. userInfo: {}
  17. },
  18. /**
  19. * 生命周期函数--监听页面加载
  20. */
  21. onLoad: async function (options) {
  22. //设置标题栏的高度
  23. var that = this
  24. wx.getSystemInfo({
  25. success: function (res) {
  26. console.log(res)
  27. console.log(res.statusBarHeight)
  28. that.setData({
  29. statusBarHeight: res.statusBarHeight
  30. })
  31. },
  32. })
  33. //设置结束日期为今日
  34. var d = new Date();
  35. var endDate = d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate()
  36. this.setData({
  37. endDate
  38. })
  39. let userInfo = await app.getWxUserInfo();
  40. let userId = userInfo.userId;
  41. let user = await userApi.getUserById(userId);
  42. let birth = user.info.birthday;
  43. birth = birth == "" ? '1970-01-01' : birth;
  44. console.log(user)
  45. this.setData({
  46. sex: userInfo.gender,
  47. imgUrl: user.info.avatar,
  48. userName: user.info.name,
  49. birth: birth
  50. })
  51. },
  52. changeUserName: function (e) {
  53. this.setData({
  54. userName: e.detail.value
  55. })
  56. },
  57. changeBirth: function (e) {
  58. this.setData({
  59. birth: e.detail.value
  60. })
  61. },
  62. changeSex: function (e) {
  63. console.log(e.detail)
  64. this.setData({
  65. sex: e.detail.value
  66. })
  67. },
  68. upload: async function () {
  69. var _this = this;
  70. wx.chooseImage({
  71. count: 1, // 默认9
  72. sizeType: ['original', 'compressed'],
  73. // 指定是原图还是压缩图,默认两个都有
  74. sourceType: ['album', 'camera'],
  75. // 指定来源是相册还是相机,默认两个都有
  76. success: async function (res) {
  77. // 返回选定照片的本地文件路径tempFilePath可以作为img标签的src属性显示图片
  78. var imgUrl = await uploadApi.uploadAvatar(res.tempFilePaths[0]);
  79. _this.setData({
  80. imgUrl: imgUrl
  81. })
  82. }
  83. })
  84. },
  85. updateUser: async function () {
  86. var name = this.data.userName;
  87. var birthDay = this.data.birth;
  88. var avatar = this.data.imgUrl;
  89. var sex = this.data.sex;
  90. var userInfo = await app.getWxUserInfo();
  91. userInfo.name = name;
  92. userInfo.birthday = birthDay;
  93. userInfo.avatar = avatar;
  94. userInfo.sex = sex;
  95. wx.setStorage({
  96. data: userInfo,
  97. key: 'userInfo',
  98. })
  99. await userApi.updateUserInfo(name, birthDay, avatar, sex);
  100. wx.showToast({
  101. title: '修改成功',
  102. icon: 'success',
  103. success: function () {
  104. setTimeout(() => {
  105. wx.switchTab({
  106. url: '/pages/person/person',
  107. })
  108. }, 1000);
  109. }
  110. })
  111. },
  112. /**
  113. * 生命周期函数--监听页面初次渲染完成
  114. */
  115. onReady: function () {
  116. },
  117. /**
  118. * 生命周期函数--监听页面显示
  119. */
  120. onShow: function () {
  121. },
  122. /**
  123. * 生命周期函数--监听页面隐藏
  124. */
  125. onHide: function () {
  126. },
  127. /**
  128. * 生命周期函数--监听页面卸载
  129. */
  130. onUnload: async function () {
  131. },
  132. /**
  133. * 页面相关事件处理函数--监听用户下拉动作
  134. */
  135. onPullDownRefresh: function () {
  136. },
  137. /**
  138. * 页面上拉触底事件的处理函数
  139. */
  140. onReachBottom: function () {
  141. },
  142. /**
  143. * 用户点击右上角分享
  144. */
  145. onShareAppMessage: function () {
  146. }
  147. })