attachmentList.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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)"
  7. class="file_name uni-ellipsis">{{ attachment.fileName || '附件名为空' }}</text>
  8. <view class="icon-container">
  9. <view>
  10. <uni-icons @click="shareAttachment(attachment.path)" :size="14" color="#2196f3" type="redo-filled" style="margin-left:10px;">
  11. 分享
  12. </uni-icons>
  13. <uni-icons @click="copyAttachment(attachment.path)" :size="14" color="#2196f3" type="download-filled" style="margin-left:10px;">
  14. 复制链接
  15. </uni-icons>
  16. </view>
  17. <!-- <view v-else>
  18. <uni-icons @click="deleteAttachment(attachment)" :size="14" color="#ff0000" type="close" style="margin-left:10px;">
  19. 删除
  20. </uni-icons>
  21. </view> -->
  22. </view>
  23. </view>
  24. </uni-col>
  25. </uni-row>
  26. </view>
  27. </template>
  28. <script setup lang="ts">
  29. import config from '@/config';
  30. import $tab from '@/plugins/tab';
  31. import { onMounted, ref } from 'vue';
  32. import $modal from '@/plugins/modal.js'
  33. onMounted(() => {
  34. startAttachmentCheck();
  35. })
  36. const intervalId = ref(null); // 定时器ID
  37. // TODO: 根据接口返回的数据动态生成process表单
  38. // const props = defineProps(['attachments'])
  39. const props = defineProps({
  40. attachments: {
  41. type: Array,
  42. required: true,
  43. default: []
  44. },
  45. canEdit:{
  46. type: Boolean,
  47. default: false
  48. }
  49. })
  50. const emits = defineEmits([
  51. 'clickDelete', // 点击分段器
  52. ])
  53. //检查附件列表是否有数据
  54. function startAttachmentCheck() {
  55. intervalId.value = setInterval(() => {
  56. if (props.attachments) {
  57. // console.log('props', props.attachments);
  58. clearInterval(intervalId.value); // 清除定时器
  59. }
  60. }, 100); // 每0.1秒检查一次
  61. }
  62. //预览文件
  63. function seeAttachment(path) {
  64. const isIOS = () => {
  65. const systemInfo = uni.getSystemInfoSync();
  66. return /iOS/i.test(systemInfo.system);
  67. }
  68. // console.log('====',isIOS());
  69. // 下载文件
  70. wx.downloadFile({
  71. url: config.baseUrlPre + path, // 需要下载的文件地址
  72. success: (res) => {
  73. if (res.statusCode === 200) {
  74. // 下载成功,打开文档(临时文件路径res.tempFilePath)
  75. //文件拓展名
  76. const extension = res.tempFilePath.split('.').pop().toLowerCase();
  77. const supportedTypes = ['jpg', 'jpeg', 'png'];
  78. if (isIOS() && supportedTypes.includes(extension)) {
  79. uni.previewImage({
  80. urls: [res.tempFilePath],
  81. });
  82. }else{
  83. wx.openDocument({
  84. filePath: res.tempFilePath,
  85. success: (res)=> {
  86. console.log('文档打开成功');
  87. },
  88. fail: (err)=> {
  89. console.error('打开文档失败:', err);
  90. }
  91. });
  92. }
  93. } else {
  94. // $modal.alert('err1')
  95. console.error('下载文件失败,状态码:', res.statusCode);
  96. }
  97. },
  98. fail: (err) => {
  99. console.error('下载文件失败:', err);
  100. // $modal.alert('err2', JSON.stringify(err))
  101. }
  102. });
  103. // uni.downloadFile({
  104. // url: config.baseUrlPre + path, // 需要下载的文件地址
  105. // success: (res) => {
  106. // if (res.statusCode === 200) {
  107. // // 下载成功,打开文档(临时文件路径 res.tempFilePath)
  108. // uni.openDocument({
  109. // filePath: res.tempFilePath,
  110. // fileType: getFileExtension(res.tempFilePath), // 明确指定文件扩展名
  111. // success: () => {
  112. // console.log('文档打开成功');
  113. // },
  114. // fail: (err) => {
  115. // console.error('打开文档失败:', err);
  116. // // 可以选择向用户显示错误信息
  117. // $modal.showToast('打开文档失败')
  118. // }
  119. // });
  120. // } else {
  121. // console.error('下载文件失败,状态码:', res.statusCode);
  122. // // 向用户显示下载失败的信息
  123. // $modal.showToast('下载文件失败')
  124. // }
  125. // },
  126. // fail: (err) => {
  127. // console.error('下载文件失败:', err);
  128. // // 向用户显示下载失败的信息
  129. // $modal.showToast('下载文件失败')
  130. // }
  131. // });
  132. // uni.downloadFile({
  133. // url: config.baseUrlPre + path, // 需要下载的文件地址
  134. // success: (res) => {
  135. // if (res.statusCode === 200) {
  136. // const tempFilePath = res.tempFilePath;
  137. // const extension = getFileExtension(tempFilePath);
  138. // if(isIOS) {
  139. // // 检查文件类型是否受支持
  140. // const supportedTypes = ['pdf', 'jpg', 'jpeg', 'png', 'mp4']; // 根据需要添加更多支持的类型
  141. // if (!supportedTypes.includes(extension)) {
  142. // console.warn('文件类型不受支持:', extension);
  143. // uni.showToast({ title: '文件类型不受支持', icon: 'none' });
  144. // return;
  145. // }
  146. // }
  147. // uni.openDocument({
  148. // filePath: tempFilePath,
  149. // fileType: extension, // 明确指定文件扩展名
  150. // success: () => {
  151. // console.log('文档打开成功');
  152. // },
  153. // fail: (err) => {
  154. // console.error('打开文档失败:', JSON.stringify(err));
  155. // uni.showToast({ title: '打开文档失败', icon: 'none' });
  156. // }
  157. // });
  158. // } else {
  159. // console.error('下载文件失败,状态码:', res.statusCode);
  160. // uni.showToast({ title: '下载文件失败', icon: 'none' });
  161. // }
  162. // },
  163. // fail: (err) => {
  164. // uni.hideLoading();
  165. // console.error('下载文件失败:', JSON.stringify(err));
  166. // uni.showToast({ title: '下载文件失败', icon: 'none' });
  167. // }
  168. // });
  169. }
  170. //文件分享
  171. function shareAttachment(path) {
  172. uni.downloadFile({
  173. url: config.baseUrlPre + path,
  174. success: (res) => {
  175. if (res.statusCode === 200) {
  176. wx.shareFileMessage({
  177. filePath: res.tempFilePath,
  178. fileName: path.split('/').pop(),
  179. success() { },
  180. fail: console.error,
  181. })
  182. }
  183. }
  184. });
  185. }
  186. //复制下载链接
  187. function copyAttachment(path) {
  188. wx.setClipboardData({
  189. data: config.baseUrlPre + path,
  190. success: (res) => {
  191. $modal.msgSuccess('下载链接已复制')
  192. }
  193. });
  194. }
  195. function deleteAttachment(file) {
  196. // console.log('delAttachment: ',file);
  197. emits('clickDelete', file)
  198. }
  199. </script>
  200. <style lang="scss">
  201. .attachment {
  202. color: #2196f3;
  203. margin-left: 5px;
  204. display: flex;
  205. /* 使用 flexbox 布局 */
  206. justify-content: space-between;
  207. /* 在空余空间时,图标靠右 */
  208. align-items: center;
  209. /* 垂直居中对齐 */
  210. }
  211. .icon-container {
  212. display: flex;
  213. /* 图标使用 flexbox 布局 */
  214. justify-content: flex-end;
  215. /* 使图标靠右 */
  216. }
  217. .file_name {
  218. max-width: 22vh;
  219. }
  220. </style>