attachmentList.vue 3.6 KB

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