personalModify.vue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <!-- 个人资料修改 -->
  2. <template>
  3. <view>
  4. <u-cell-group>
  5. <u-cell-item title="头像修改">
  6. <u-upload slot="right-icon" :action="action" :custom-btn="true" :before-upload="beforeUpload"
  7. :multiple="false" :show-upload-list="false" :header="{
  8. Authorization: `Bearer ${vuex_token}`
  9. }" :limitType="['png', 'jpg']" @on-success="uploadSuccess">
  10. <u-avatar slot="addBtn" :src="avatarUrl" mode="circle" size="100"></u-avatar>
  11. </u-upload>
  12. </u-cell-item>
  13. </u-cell-group>
  14. <u-toast ref="uToast" />
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. // 上传地址
  22. action: this.config.uploadUrl,
  23. avatarUrl: ''
  24. }
  25. },
  26. onShow() {
  27. this.getMemberinfo()
  28. },
  29. methods: {
  30. /**
  31. * 获取用户基本信息
  32. */
  33. getMemberinfo() {
  34. uni.showLoading({
  35. mask: true,
  36. title: '数据请求中...'
  37. })
  38. this.$u.api.getmemberinfo().then(res => {
  39. if (res.code === 200) {
  40. this.avatarUrl = res.data.avatar;
  41. uni.hideLoading()
  42. } else {
  43. this.$refs.uToast.show({
  44. title: res.msg,
  45. type: 'error'
  46. })
  47. uni.hideLoading()
  48. }
  49. }).catch(err => {
  50. this.$refs.uToast.show({
  51. title: err.msg,
  52. type: 'error'
  53. })
  54. uni.hideLoading()
  55. })
  56. },
  57. /**
  58. * 上传组件上传触发
  59. * @param {Object} index
  60. * @param {Object} list
  61. */
  62. beforeUpload(index, list) {
  63. uni.showLoading({
  64. title: '图片上传中...',
  65. mask: true
  66. })
  67. },
  68. /**
  69. * 上传组件成功触发
  70. * @param { Object } res
  71. */
  72. uploadSuccess(res) {
  73. if (res.code === 200) {
  74. this.$u.api.modifyUserInfo({
  75. avatar: res.data.url
  76. }).then(res => {
  77. if (res.code === 200) {
  78. this.getMemberinfo()
  79. this.$refs.uToast.show({
  80. title: '修改成功!',
  81. type: 'success'
  82. })
  83. uni.hideLoading()
  84. } else {
  85. this.$refs.uToast.show({
  86. title: res.msg,
  87. type: 'error'
  88. })
  89. uni.hideLoading()
  90. }
  91. }).catch(() => {
  92. uni.hideLoading()
  93. this.$refs.uToast.show({
  94. title: '系统异常',
  95. type: 'error'
  96. })
  97. })
  98. } else {
  99. uni.hideLoading();
  100. this.$refs.uToast.show({
  101. title: res.msg,
  102. type: 'error'
  103. })
  104. }
  105. }
  106. }
  107. }
  108. </script>
  109. <style>
  110. </style>