information.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const api = require('../../config/api')
  2. const util = require('../../utils/util')
  3. const upload = require('../../utils/upload')
  4. Page({
  5. data: {
  6. userInfo: {}
  7. },
  8. onShow() {
  9. this.getUserInfo()
  10. },
  11. getUserInfo() {
  12. util.showLoad('加载中')
  13. util.request(api.UserInfo).then(res => {
  14. util.hideLoad()
  15. if (res.errno === 0) {
  16. wx.setStorageSync('userInfo', res.data)
  17. this.setData({ userInfo: res.data })
  18. } else util.showToast(res.errmsg)
  19. }).catch(() => {
  20. util.hideLoad()
  21. util.showErrorToast('网络连接失败')
  22. })
  23. },
  24. // 更新头像
  25. updateUser(avatar) {
  26. util.request(api.UserUpdate, { id: this.data.userInfo.id, avatar }, 'POST').then(res => {
  27. util.hideLoad()
  28. if (res.errno === 0) this.getUserInfo()
  29. else util.showErrorToast(res.errmsg)
  30. }).catch(() => {
  31. util.hideLoad()
  32. util.showErrorToast('网络连接失败')
  33. })
  34. },
  35. // 更换头像
  36. chooseMedia() {
  37. const that = this
  38. wx.chooseMedia({
  39. count: 1,
  40. mediaType: ['image'],
  41. success: async suc => {
  42. try {
  43. util.showLoad('上传中')
  44. const filePath = suc.tempFiles[0].tempFilePath
  45. const url = await upload.uploadImage(filePath)
  46. that.updateUser(url)
  47. } catch (error) {
  48. util.hideLoad()
  49. util.showErrorToast(error)
  50. }
  51. },
  52. fail: () => {
  53. util.hideLoad()
  54. util.showToast('已取消')
  55. }
  56. })
  57. },
  58. // 退出登录
  59. logout() {
  60. wx.showModal({
  61. confirmColor: '#D32D2F',
  62. content: '确定退出登录吗?',
  63. success: res => {
  64. if (res.confirm) {
  65. util.request(api.Logout, {}, 'POST')
  66. wx.removeStorageSync('token')
  67. wx.removeStorageSync('userInfo')
  68. wx.reLaunch({ url: '/pages/index/index' })
  69. }
  70. }
  71. })
  72. }
  73. })