index.js 3.5 KB

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