avatar.vue 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <template>
  2. <view>
  3. <u-navbar :border-bottom="false" :is-back="true" title="设置头像"></u-navbar>
  4. <view class="wrap">
  5. <u-upload ref="uUpload" :action="action" :auto-upload="true" :custom-btn="true" :max-size="10 * 1024 * 1024"
  6. :size-type="siteType" height="690" max-count="1" width="690">
  7. <view slot="addBtn" class="slot-btn" hover-class="slot-btn__hover" hover-stay-time="150">
  8. <u-icon :color="$u.color['lightColor']" name="plus" size="60"></u-icon>
  9. </view>
  10. </u-upload>
  11. <view class="u-m-t-20 btn">
  12. <u-button type="primary" @click="subProfile">提交</u-button>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. import config from "@/common/config.js" // 全局配置文件
  19. export default {
  20. data() {
  21. return {
  22. // 服务器地址
  23. action: config.staticUrl + '/common/upload',
  24. siteType: ['compressed']
  25. };
  26. },
  27. methods: {
  28. subProfile() {
  29. let files = [];
  30. // 通过filter,筛选出上传进度为100的文件(因为某些上传失败的文件,进度值不为100,这个是可选的操作)
  31. files = this.$refs.uUpload.lists.filter(val => {
  32. return val.progress == 100;
  33. })
  34. if (this.$u.test.isEmpty(files)) {
  35. return this.$mytip.toast('请选择图片')
  36. }
  37. let imageList = files.map(val => {
  38. return {
  39. // imgUrl: val.response.url,
  40. imgUrl: val.response.fileName
  41. }
  42. })
  43. let avatar = imageList[0].imgUrl
  44. let vuex_user = uni.getStorageSync('lifeData').vuex_user
  45. let user = vuex_user.user
  46. let userId = user.userId;
  47. this.$u.put("api/profile/updateProfile", {
  48. userId: userId,
  49. avatar: avatar,
  50. }).then(data => {
  51. // 关闭
  52. user.avatar = avatar
  53. this.$u.vuex('vuex_user', vuex_user);
  54. this.$mytip.toast('头像修改成功')
  55. setTimeout(() => {
  56. uni.$emit('updateAvatar', {});
  57. uni.switchTab({
  58. url: '/pages/center/center'
  59. })
  60. }, 1000);
  61. });
  62. },
  63. }
  64. };
  65. </script>
  66. <style>
  67. page {
  68. background-color: #FFFFFF;
  69. }
  70. </style>
  71. <style lang="scss" scoped>
  72. .wrap {
  73. padding: 30 rpx;
  74. }
  75. .slot-btn {
  76. width: 690 rpx;
  77. height: 690 rpx;
  78. display: flex;
  79. justify-content: center;
  80. align-items: center;
  81. background: rgb(244, 245, 246);
  82. border-radius: 10 rpx;
  83. }
  84. .slot-btn__hover {
  85. background-color: rgb(235, 236, 238);
  86. }
  87. </style>