user.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // pages/user/user.js
  2. const api = require('../../config/api.js');
  3. const util = require('../../utils/util.js');
  4. const services = require('../../services/user.js');
  5. Page({
  6. /**
  7. * 页面的初始数据
  8. */
  9. data: {
  10. birthday: '',
  11. region: [],
  12. customItem: '全部',
  13. gender: 0,
  14. mobile: null,
  15. nickname: '',
  16. defaultAvatarUrl: 'https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180727/150547696d798c.png'
  17. },
  18. updateNickName(ev) {
  19. this.setData({
  20. nickname: ev.detail.value
  21. })
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: async function (options) {
  27. this.getUserInfo()
  28. let userInfo = wx.getStorageSync('userInfo')
  29. try {
  30. userInfo.avatar = userInfo.avatarUrl
  31. this.sessionKey = userInfo.sessionKey
  32. delete userInfo.avatarUrl
  33. delete userInfo.sessionKey
  34. this.setData({
  35. ...userInfo
  36. })
  37. } catch (err) {
  38. }
  39. },
  40. loginSuccess() {
  41. this.onLoad()
  42. },
  43. async getUserInfo() {
  44. const {
  45. data
  46. } = await util.request(api.UserInfo)
  47. data.region = data.city ? data.city.split(',') : []
  48. data.birthday = data.birthday || '1990-01-01'
  49. data.nickname = data.nickname ? data.nickname : data.username
  50. this.setData({
  51. ...data
  52. })
  53. },
  54. async getPhoneNumber(e) {
  55. const isLogin = await services.checkLogin();
  56. let userInfo = wx.getStorageSync('userInfo')
  57. if (!isLogin) {
  58. await services.loginByWeixin(userInfo)
  59. }
  60. let {
  61. encryptedData,
  62. iv
  63. } = e.detail
  64. console.log(e)
  65. if (!encryptedData) return;
  66. let res = await util.request(api.decryptedPhoneNum, {
  67. encryptedData,
  68. iv,
  69. sessionKey: this.sessionKey
  70. })
  71. console.log(res)
  72. this.setData({
  73. mobile: res.phoneNum
  74. })
  75. },
  76. updateSex(e) {
  77. this.setData({
  78. gender: e.currentTarget.dataset.sex
  79. })
  80. },
  81. bindDateChange(e) {
  82. this.setData({
  83. birthday: e.detail.value
  84. })
  85. },
  86. bindRegionChange(e) {
  87. this.setData({
  88. region: e.detail.value
  89. })
  90. },
  91. selectPhoto() {
  92. // wx.chooseImage({
  93. // count: 1,
  94. // sizeType: 'compressed',
  95. // success: (res) => {
  96. // const src = res.tempFilePaths[0]
  97. // // this.file = res.tempFiles[0]
  98. // wx.navigateTo({
  99. // url: './imageCropper?image=' + src,
  100. // })
  101. // // this.setData({ avatar: src })
  102. // }
  103. // })
  104. },
  105. changPhone(e) {
  106. this.setData({
  107. 'mobile': e.detail.value,
  108. });
  109. },
  110. async save(e) {
  111. let type = e.currentTarget.dataset.type
  112. console.log(e)
  113. const {
  114. nickname
  115. } = this.data;
  116. if (!nickname || nickname == '') {
  117. wx.showToast({
  118. title: '请输入昵称',
  119. icon: 'error'
  120. })
  121. return
  122. }
  123. this.data.city = this.data.region && this.data.region.join(',')
  124. const body = {
  125. ...this.data
  126. }
  127. delete body.region
  128. const {
  129. data
  130. } = await util.request(api.UpdateUserInfo, body, 'POST', 'application/json')
  131. if (type == '1') {
  132. console.log(data)
  133. wx.setStorageSync('token', data.token);
  134. wx.navigateBack()
  135. } else {
  136. }
  137. },
  138. async onChooseAvatar(e) {
  139. const {
  140. avatarUrl
  141. } = e.detail
  142. const dataURL = await (new Promise(r => {
  143. wx.uploadFile({
  144. filePath: avatarUrl,
  145. name: 'file',
  146. url: api.UploadFile,
  147. header: {
  148. 'X-Nideshop-Token': wx.getStorageSync('token')
  149. },
  150. success: (res) => {
  151. const data = JSON.parse(res.data)
  152. r(data.message || defaultAvatarUrl)
  153. }
  154. })
  155. }));
  156. console.log('upload-file', dataURL)
  157. this.setData({
  158. avatar: dataURL,
  159. })
  160. },
  161. /**
  162. * 生命周期函数--监听页面初次渲染完成
  163. */
  164. onReady: function () {
  165. },
  166. /**
  167. * 生命周期函数--监听页面显示
  168. */
  169. onShow: function () {},
  170. /**
  171. * 生命周期函数--监听页面隐藏
  172. */
  173. onHide: function () {
  174. },
  175. /**
  176. * 生命周期函数--监听页面卸载
  177. */
  178. onUnload: function () {
  179. },
  180. /**
  181. * 页面相关事件处理函数--监听用户下拉动作
  182. */
  183. onPullDownRefresh: function () {
  184. },
  185. /**
  186. * 页面上拉触底事件的处理函数
  187. */
  188. onReachBottom: function () {
  189. },
  190. /**
  191. * 用户点击右上角分享
  192. */
  193. onShareAppMessage: function () {
  194. }
  195. })