index.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <view class="updata-img">
  3. <view class="updata-title-box">
  4. <view class="updata-title-title font-title ">{{ data.title }} <span style="color: #f56c6c;"
  5. v-if="data.required">*</span> </view>
  6. <view class="updata-title-text font-text ">{{ data.text }}</view>
  7. </view>
  8. <view class="updata-img-box" @click="updataFile">
  9. <image :src="img" style="width: 117px;height: 75px;" />
  10. </view>
  11. </view>
  12. </template>
  13. <script setup>
  14. import { ref, toRaw } from 'vue';
  15. import { uploadFile } from '../../api/file';
  16. import { getToken } from '@/utils/auth'
  17. import config from '@/config'
  18. const baseUrl = config.baseUrl
  19. const props = defineProps({
  20. data: {
  21. type: Object,
  22. default: {
  23. title: '上传头像',
  24. text: '上传您的头像',
  25. img: "/static/img/updata-user-img.png"
  26. },
  27. },
  28. });
  29. const emit = defineEmits(['onSubmit']);
  30. const file = ref(null);
  31. const img = ref(props.data.img);
  32. function updataFile() {
  33. wx.chooseMessageFile({
  34. count: 1,
  35. type: 'file',
  36. success: (res) => {
  37. {
  38. const tempFilePath = res.tempFiles[0].path; // 获取临时文件路径
  39. const fs = wx.getFileSystemManager(); // 获取文件系统管理器
  40. wx.uploadFile({
  41. url: baseUrl + '/common/upload', // 服务器接口地址
  42. filePath: tempFilePath, // 本地文件路径(通过 wx.chooseImage 获取)
  43. name: 'file', // 文件对应的 key(服务器接收字段名)
  44. formData: { // 额外表单数据
  45. file: res.tempFiles[0],
  46. },
  47. header: {
  48. 'Authorization': 'Bearer ' + getToken(), // 自定义请求头
  49. },
  50. success(res) {
  51. const data = JSON.parse(res.data);
  52. file.value = data.url;
  53. emit('onSubmit', { key: props.data.key, url: data.url })
  54. fs.readFile({
  55. filePath: tempFilePath,
  56. encoding: 'base64', // 指定编码格式
  57. success: (fileRes) => {
  58. const base64Data = 'data:image/png;base64,' + fileRes.data; // 拼接 Base64 前缀
  59. img.value = base64Data;
  60. uni.showToast({
  61. title: '上传成功',
  62. icon: 'none'
  63. })
  64. },
  65. fail: (err) => {
  66. uni.showToast({
  67. title: '上传失败',
  68. icon: 'none'
  69. })
  70. console.error(' 转换失败:', err);
  71. }
  72. });
  73. },
  74. fail(error) {
  75. console.log(' 上传失败', error);
  76. uni.showToast({
  77. title: '上传失败',
  78. icon: 'none'
  79. })
  80. }
  81. });
  82. }
  83. }
  84. });
  85. }
  86. function getFile() {
  87. return toRaw(file.value);
  88. }
  89. defineExpose({
  90. getFile,
  91. });
  92. </script>
  93. <style lang="scss" scoped>
  94. .updata-img {
  95. border-radius: 8px;
  96. background: rgba(255, 255, 255, 1);
  97. height: 100px;
  98. padding: 16px;
  99. display: flex;
  100. align-content: center;
  101. justify-content: space-between;
  102. .updata-title-box {
  103. display: flex;
  104. align-content: center;
  105. flex-direction: column;
  106. justify-content: space-evenly;
  107. }
  108. }
  109. </style>