index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // pages/user/index.js
  2. import http from '../../utils/http'
  3. import api from '../../utils/api'
  4. import util from '../../utils/util'
  5. const app = getApp()
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. userInfo: null,
  12. avatar: '',
  13. name: '',
  14. phone: ''
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function (options) {
  20. if(app.globalData.userInfo) {
  21. var user = app.globalData.userInfo
  22. this.setData({
  23. userInfo: user,
  24. avatar: user.avatar,
  25. name: user.name,
  26. phone: user.phone
  27. })
  28. }
  29. },
  30. chooseImage() {
  31. var that = this
  32. wx.chooseImage({
  33. count: 1,
  34. sizeType: ['original', 'compressed'],
  35. sourceType: ['album', 'camera'],
  36. success (res) {
  37. const paths = res.tempFilePaths
  38. if(paths.length > 0) {
  39. that.setData({
  40. avatar: paths[0]
  41. })
  42. }
  43. }
  44. })
  45. },
  46. updateInput: function(e) {
  47. app.updateInput(this, e)
  48. },
  49. updateInfo: function() {
  50. http({
  51. url: 'users/update',
  52. data: {
  53. avatar: this.data.avatar,
  54. name: this.data.name,
  55. phone: this.data.phone
  56. },
  57. success: function (res) {
  58. if (res.code == 0) {
  59. util.success('操作成功')
  60. app.updateUserInfo(res.data)
  61. } else {
  62. util.error('操作失败')
  63. }
  64. }
  65. })
  66. },
  67. save: function() {
  68. if(!util.checkMobile(this.data.phone)) {
  69. util.error('手机号错误')
  70. return false
  71. }
  72. if(!this.data.name) {
  73. util.error('姓名必填')
  74. return false
  75. }
  76. if(this.data.userInfo.avatar != this.data.avatar) {
  77. var that = this;
  78. util.uploadFile(this.data.avatar, function(res) {
  79. that.setData({
  80. avatar: res.path
  81. })
  82. that.updateInfo()
  83. })
  84. } else {
  85. that.updateInfo()
  86. }
  87. },
  88. /**
  89. * 生命周期函数--监听页面初次渲染完成
  90. */
  91. onReady: function () {
  92. },
  93. /**
  94. * 生命周期函数--监听页面显示
  95. */
  96. onShow: function () {
  97. },
  98. /**
  99. * 生命周期函数--监听页面隐藏
  100. */
  101. onHide: function () {
  102. },
  103. /**
  104. * 生命周期函数--监听页面卸载
  105. */
  106. onUnload: function () {
  107. },
  108. /**
  109. * 页面相关事件处理函数--监听用户下拉动作
  110. */
  111. onPullDownRefresh: function () {
  112. },
  113. /**
  114. * 页面上拉触底事件的处理函数
  115. */
  116. onReachBottom: function () {
  117. },
  118. /**
  119. * 用户点击右上角分享
  120. */
  121. onShareAppMessage: function () {
  122. }
  123. })