avatar.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <view class="container">
  3. <view class="image-cropper">
  4. <image :src="selectedImage.path" mode="aspectFill"></image>
  5. <!-- <image src="@/static/images/mine/headImg.jpg"></image> -->
  6. </view>
  7. <button type="default" @click="chooseImage">选择头像</button>
  8. <button type="primary" @click="submitImage">提交</button>
  9. </view>
  10. </template>
  11. <script setup>
  12. import {onMounted, ref} from 'vue';
  13. import headImg from "@/static/images/mine/headImg.jpg"
  14. import {uploadAvatarFile,updateUserInfoHeaderFileId} from '@/api/mine';
  15. import { useUserStore } from '@/store/user.js';
  16. import $modal from '@/plugins/modal.js';
  17. import $tab from '@/plugins/tab.js'
  18. import config from '@/config';
  19. const userStore=useUserStore();
  20. //所选头像信息
  21. const selectedImage = ref({});
  22. onMounted(()=>{
  23. selectedImage.value={
  24. path:headImg
  25. }
  26. })
  27. function chooseImage() {
  28. let chooseFile = uni.chooseFile;
  29. if (typeof wx !== 'undefined' && typeof wx.chooseMessageFile === 'function') {
  30. //切换微信小程序选择文件的方法
  31. chooseFile = wx.chooseMessageFile;
  32. }
  33. if (typeof chooseFile !== 'function') {
  34. return reject({
  35. errMsg: ERR_MSG_FAIL + ' 请指定 type 类型,该平台仅支持选择 image 或 video。',
  36. });
  37. }
  38. chooseFile({
  39. count: 1,
  40. type: 'image',
  41. success: (res) => {
  42. selectedImage.value = res.tempFiles[0];
  43. }
  44. });
  45. };
  46. const submitImage = () => {
  47. if (!selectedImage.value) return;
  48. const data = {
  49. name: selectedImage.value.name,
  50. filePath: selectedImage.value.path
  51. }
  52. uploadAvatarFile(data).then(res => {
  53. if('success'===res.returnMsg){
  54. const params={
  55. userid:userStore.useId,
  56. headerFileId:res.returnParams
  57. }
  58. updateUserInfoHeaderFileId(params).then(res=>{
  59. if('success'===res.returnMsg){
  60. $modal.msgSuccess('提交成功');
  61. setTimeout(() => {
  62. $tab.navigateBack()
  63. }, 1000);
  64. }
  65. })
  66. }
  67. }).catch(err => {
  68. $modal.msgError('文件' + data.name + '上传失败,请重新上传')
  69. switch (err) {
  70. case -201:
  71. console.log('文件上传失败 未找到该文件');
  72. break;
  73. case -20201:
  74. console.log('文件上传失败 返回值不是JSON字符串');
  75. break;
  76. }
  77. })
  78. };
  79. </script>
  80. <style>
  81. .container {
  82. display: flex;
  83. flex-direction: column;
  84. align-items: center;
  85. padding: 20px;
  86. box-sizing: border-box;
  87. height: 100vh;
  88. background-color: #f5f5f5;
  89. }
  90. .image-cropper image {
  91. width: 200px;
  92. height: 200px;
  93. border-radius: 50%;
  94. margin-bottom: 30px;
  95. }
  96. button {
  97. margin-top: 10px;
  98. width: 90%;
  99. }
  100. </style>