index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. console.log(this.data.phone)
  91. http({
  92. url: 'users/update',
  93. data: {
  94. avatar: this.data.avatar,
  95. name: this.data.name,
  96. phone: this.data.userInfo.phone
  97. },
  98. success: function (res) {
  99. if (res.code == 0) {
  100. util.success('操作成功')
  101. app.updateUserInfo(res.data)
  102. setTimeout(function() {
  103. wx.navigateBack({
  104. delta: 0,
  105. })
  106. }, 1000)
  107. } else {
  108. util.error('操作失败')
  109. }
  110. }
  111. })
  112. },
  113. save: function() {
  114. // if(!util.checkMobile(this.data.phone)) {
  115. // util.error('手机号错误')
  116. // return false
  117. // }
  118. if(!this.data.name) {
  119. util.error('姓名必填')
  120. return false
  121. }
  122. var that = this;
  123. if(this.data.userInfo.avatar != this.data.avatar) {
  124. util.uploadFile(this.data.avatar, function(res) {
  125. that.setData({
  126. avatar: res.path
  127. })
  128. that.updateInfo()
  129. })
  130. } else {
  131. that.updateInfo()
  132. }
  133. },
  134. /**
  135. * 生命周期函数--监听页面初次渲染完成
  136. */
  137. onReady: function () {
  138. },
  139. /**
  140. * 生命周期函数--监听页面显示
  141. */
  142. onShow: function () {
  143. },
  144. /**
  145. * 生命周期函数--监听页面隐藏
  146. */
  147. onHide: function () {
  148. },
  149. /**
  150. * 生命周期函数--监听页面卸载
  151. */
  152. onUnload: function () {
  153. },
  154. /**
  155. * 页面相关事件处理函数--监听用户下拉动作
  156. */
  157. onPullDownRefresh: function () {
  158. },
  159. /**
  160. * 页面上拉触底事件的处理函数
  161. */
  162. onReachBottom: function () {
  163. },
  164. /**
  165. * 用户点击右上角分享
  166. */
  167. onShareAppMessage: function () {
  168. }
  169. })