index.vue 4.0 KB

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