index.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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;" v-if="data.required">*</span> </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. const data = JSON.parse(res.data);
  54. file.value = data.url;
  55. emit('onSubmit',{key: props.data.key,url: data.url})
  56. fs.readFile({
  57. filePath: tempFilePath,
  58. encoding: 'base64', // 指定编码格式
  59. success: (fileRes) => {
  60. const base64Data = 'data:image/png;base64,' + fileRes.data; // 拼接 Base64 前缀
  61. img.value = base64Data;
  62. uni.showToast({
  63. title: '上传成功',
  64. icon: 'none'
  65. })
  66. },
  67. fail: (err) => {
  68. uni.showToast({
  69. title: '上传失败',
  70. icon: 'none'
  71. })
  72. console.error(' 转换失败:', err);
  73. }
  74. });
  75. },
  76. fail(error) {
  77. console.log(' 上传失败', error);
  78. uni.showToast({
  79. title: '上传失败',
  80. icon: 'none'
  81. })
  82. }
  83. });
  84. }
  85. }
  86. });
  87. }
  88. function getFile() {
  89. return toRaw(file.value);
  90. }
  91. defineExpose({
  92. getFile,
  93. });
  94. </script>
  95. <style lang="scss" scoped>
  96. .updata-img {
  97. border-radius: 8px;
  98. background: rgba(255, 255, 255, 1);
  99. height: 100px;
  100. padding: 16px;
  101. display: flex;
  102. align-content: center;
  103. justify-content: space-between;
  104. .updata-title-box {
  105. display: flex;
  106. align-content: center;
  107. flex-direction: column;
  108. justify-content: space-evenly;
  109. }
  110. }
  111. </style>