user-info.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { VueLikePage } from '../../utils/page'
  2. import UserApi from '../../apis/user'
  3. import DisplayApi from '../../apis/exhibition'
  4. import { saveUserInfo } from '../../utils/storage'
  5. import { API_BASE_URL } from '../../config/config'
  6. VueLikePage([], {
  7. data: {
  8. userInfo: {},
  9. tradeList: []
  10. },
  11. methods: {
  12. onLoad () {
  13. this.getAllTrade()
  14. let userInfo = Object.assign({}, getApp().globalData.userinfo)
  15. UserApi.getUserInfoById(userInfo.viewerId).then(res => {
  16. this.setData({
  17. userInfo: res.data
  18. })
  19. getApp().globalData.userinfo = res.data
  20. })
  21. },
  22. onShow () {
  23. },
  24. chooseImage () {
  25. return wx.chooseImage({
  26. count: 1,
  27. success: (res) => {
  28. const { tempFilePaths } = res
  29. return this.uploadAvatar(tempFilePaths[0])
  30. },
  31. })
  32. },
  33. updateUserInfo () {
  34. UserApi.updateUserInfo(this.data.userInfo).then(res => {
  35. wx.showToast({
  36. title: '修改成功',
  37. })
  38. getApp().globalData.userinfo = this.data.userInfo
  39. saveUserInfo(this.data.userInfo)
  40. })
  41. },
  42. uploadAvatar (filePath) {
  43. return new Promise((resolve, reject) => {
  44. wx.uploadFile({
  45. filePath: filePath,
  46. name: 'file',
  47. url: `${API_BASE_URL}/im/upload`,
  48. header: {
  49. "Content-Type": "multipart/form-data"
  50. },
  51. success: (res) => {
  52. res = JSON.parse(res.data)
  53. let userInfo = this.data.userInfo
  54. userInfo.avatar = res.data
  55. this.setData({
  56. userInfo
  57. })
  58. resolve({url: res.data});
  59. },
  60. fail: (err) => {
  61. console.log(err, 'err')
  62. }
  63. })
  64. })
  65. },
  66. bindInput (e) {
  67. let userInfo = this.data.userInfo
  68. const { key } = e.currentTarget.dataset
  69. userInfo[key] = e.detail.value
  70. this.setData({
  71. userInfo
  72. })
  73. },
  74. getAllTrade () {
  75. return DisplayApi.getTradeList().then(res => {
  76. this.originTradeList = res.data
  77. this.setData({
  78. tradeList: res.data.map(item => item.name)
  79. })
  80. })
  81. },
  82. bindPickerChange (e) {
  83. let userInfo = this.data.userInfo
  84. userInfo.companyTrade = this.originTradeList[e.detail.value].name
  85. this.setData({
  86. userInfo
  87. })
  88. },
  89. changeCheckStatus (e) {
  90. const { value } = e.currentTarget.dataset
  91. this.setData({
  92. 'userInfo.gender': value
  93. })
  94. }
  95. }
  96. })