attachmentList.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <template>
  2. <view class="message_attachment">
  3. <uni-row :gutter="0">
  4. <uni-col v-for="(attachment, index) in attachments" :key="index" :xs="{span: 24}" :sm="6">
  5. <view class="attachment">
  6. <text @click="seeAttachment(attachment.path)" class="file_name uni-ellipsis">{{ attachment.fileName || '附件名为空' }}</text>
  7. <view class="icon-container">
  8. <uni-icons @click="shareAttachment(attachment.path)" :size="14" color="#2196f3" type="redo-filled"
  9. style="margin-left:10px;">分享</uni-icons>
  10. <uni-icons @click="copyAttachment(attachment.path)" :size="14" color="#2196f3" type="download-filled"
  11. style="margin-left:10px;">复制链接</uni-icons>
  12. </view>
  13. </view>
  14. </uni-col>
  15. </uni-row>
  16. </view>
  17. </template>
  18. <script setup lang="ts">
  19. import config from '@/config';
  20. import $tab from '@/plugins/tab';
  21. import { onMounted, ref } from 'vue';
  22. import $modal from '@/plugins/modal.js'
  23. onMounted(() => {
  24. startAttachmentCheck();
  25. })
  26. const intervalId = ref(null); // 定时器ID
  27. // TODO: 根据接口返回的数据动态生成process表单
  28. // const props = defineProps(['attachments'])
  29. const props = defineProps({
  30. attachments: {
  31. type: Array,
  32. required: true,
  33. default: []
  34. }
  35. })
  36. //检查附件列表是否有数据
  37. function startAttachmentCheck() {
  38. intervalId.value = setInterval(() => {
  39. if (props.attachments) {
  40. // console.log('props', props.attachments);
  41. clearInterval(intervalId.value); // 清除定时器
  42. }
  43. }, 100); // 每0.1秒检查一次
  44. }
  45. //预览文件
  46. function seeAttachment(path) {
  47. console.log('path',path);
  48. // 下载文件
  49. wx.downloadFile({
  50. url: config.baseUrlPre + path, // 需要下载的文件地址
  51. success: (res)=> {
  52. if (res.statusCode === 200) {
  53. // 下载成功,打开文档(临时文件路径res.tempFilePath)
  54. wx.openDocument({
  55. filePath: res.tempFilePath,
  56. success: function (res) {
  57. console.log('文档打开成功');
  58. },
  59. fail: function (err) {
  60. // $modal.alert('err', JSON.stringify(err))
  61. console.error('打开文档失败:', err);
  62. }
  63. });
  64. } else {
  65. // $modal.alert('err1')
  66. console.error('下载文件失败,状态码:', res.statusCode);
  67. }
  68. },
  69. fail: (err)=> {
  70. console.error('下载文件失败:', err);
  71. // $modal.alert('err2', JSON.stringify(err))
  72. }
  73. });
  74. }
  75. //文件分享
  76. function shareAttachment(path) {
  77. uni.downloadFile({
  78. url: config.baseUrlPre + path,
  79. success: (res) => {
  80. if (res.statusCode === 200) {
  81. wx.shareFileMessage({
  82. filePath: res.tempFilePath,
  83. fileName: path.split('/').pop(),
  84. success() { },
  85. fail: console.error,
  86. })
  87. }
  88. }
  89. });
  90. }
  91. //复制下载链接
  92. function copyAttachment(path) {
  93. wx.setClipboardData({
  94. data: config.baseUrlPre + path,
  95. success: (res)=> {
  96. $modal.msgSuccess('下载链接已复制')
  97. }
  98. });
  99. }
  100. </script>
  101. <style lang="scss">
  102. .attachment {
  103. color: #2196f3;
  104. margin-left: 5px;
  105. display: flex;
  106. /* 使用 flexbox 布局 */
  107. justify-content: space-between;
  108. /* 在空余空间时,图标靠右 */
  109. align-items: center;
  110. /* 垂直居中对齐 */
  111. }
  112. .icon-container {
  113. display: flex;
  114. /* 图标使用 flexbox 布局 */
  115. justify-content: flex-end;
  116. /* 使图标靠右 */
  117. }
  118. .file_name {
  119. max-width: 22vh;
  120. }
  121. </style>