personinfo.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.sex,
  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. let userinfo = await userApi.updateUserInfo(name, birthDay, avatar, sex);
  91. var userInfo = await app.getWxUserInfo();
  92. wx.showToast({
  93. title: '修改成功',
  94. icon: 'success',
  95. success: function () {
  96. userInfo.name = userinfo.info.name;
  97. userInfo.birthday = userinfo.info.birthDay;
  98. userInfo.avatar = userinfo.info.avatar;
  99. userInfo.sex = userinfo.info.sex;
  100. app.setUserInfo(userInfo)
  101. setTimeout(() => {
  102. wx.switchTab({
  103. url: '/pages/person/person',
  104. })
  105. }, 1000);
  106. }
  107. })
  108. },
  109. /**
  110. * 生命周期函数--监听页面初次渲染完成
  111. */
  112. onReady: function () {
  113. },
  114. /**
  115. * 生命周期函数--监听页面显示
  116. */
  117. onShow: function () {
  118. },
  119. /**
  120. * 生命周期函数--监听页面隐藏
  121. */
  122. onHide: function () {
  123. },
  124. /**
  125. * 生命周期函数--监听页面卸载
  126. */
  127. onUnload: async function () {
  128. },
  129. /**
  130. * 页面相关事件处理函数--监听用户下拉动作
  131. */
  132. onPullDownRefresh: function () {
  133. },
  134. /**
  135. * 页面上拉触底事件的处理函数
  136. */
  137. onReachBottom: function () {
  138. },
  139. /**
  140. * 用户点击右上角分享
  141. */
  142. onShareAppMessage: function () {
  143. }
  144. })