index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. updatePhoneNumber(e) {
  31. var that = this
  32. if (e.detail.errMsg == 'getPhoneNumber:ok') {
  33. wx.checkSession({
  34. success: () => {
  35. var data = Object.assign({}, e.detail, {
  36. session_key: wx.getStorageSync('session_key')
  37. })
  38. that.parsePhone(data)
  39. },
  40. fail: () => {
  41. wx.login({
  42. success: function(res) {
  43. var data = Object.assign({}, e.detail, {
  44. code: res.code
  45. })
  46. that.parsePhone(data)
  47. }
  48. })
  49. }
  50. })
  51. }
  52. },
  53. parsePhone(data) {
  54. var that = this
  55. http({
  56. url: 'parsePhone',
  57. data: data,
  58. success: function (res) {
  59. if(res.code == 0) {
  60. getApp().updateUserInfo(res.data)
  61. that.userInfo = res.data
  62. } else {
  63. util.error('发生错误')
  64. }
  65. }
  66. })
  67. },
  68. chooseImage() {
  69. var that = this
  70. wx.chooseImage({
  71. count: 1,
  72. sizeType: ['original', 'compressed'],
  73. sourceType: ['album', 'camera'],
  74. success (res) {
  75. const paths = res.tempFilePaths
  76. if(paths.length > 0) {
  77. that.setData({
  78. avatar: paths[0]
  79. })
  80. }
  81. }
  82. })
  83. },
  84. updateInput: function(e) {
  85. app.updateInput(this, e)
  86. },
  87. updateInfo: function() {
  88. http({
  89. url: 'users/update',
  90. data: {
  91. avatar: this.data.avatar,
  92. name: this.data.name,
  93. phone: this.data.phone
  94. },
  95. success: function (res) {
  96. if (res.code == 0) {
  97. util.success('操作成功')
  98. app.updateUserInfo(res.data)
  99. } else {
  100. util.error('操作失败')
  101. }
  102. }
  103. })
  104. },
  105. save: function() {
  106. if(!util.checkMobile(this.data.phone)) {
  107. util.error('手机号错误')
  108. return false
  109. }
  110. if(!this.data.name) {
  111. util.error('姓名必填')
  112. return false
  113. }
  114. if(this.data.userInfo.avatar != this.data.avatar) {
  115. var that = this;
  116. util.uploadFile(this.data.avatar, function(res) {
  117. that.setData({
  118. avatar: res.path
  119. })
  120. that.updateInfo()
  121. })
  122. } else {
  123. that.updateInfo()
  124. }
  125. },
  126. /**
  127. * 生命周期函数--监听页面初次渲染完成
  128. */
  129. onReady: function () {
  130. },
  131. /**
  132. * 生命周期函数--监听页面显示
  133. */
  134. onShow: function () {
  135. },
  136. /**
  137. * 生命周期函数--监听页面隐藏
  138. */
  139. onHide: function () {
  140. },
  141. /**
  142. * 生命周期函数--监听页面卸载
  143. */
  144. onUnload: function () {
  145. },
  146. /**
  147. * 页面相关事件处理函数--监听用户下拉动作
  148. */
  149. onPullDownRefresh: function () {
  150. },
  151. /**
  152. * 页面上拉触底事件的处理函数
  153. */
  154. onReachBottom: function () {
  155. },
  156. /**
  157. * 用户点击右上角分享
  158. */
  159. onShareAppMessage: function () {
  160. }
  161. })