personalModify.vue 2.3 KB

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