fileUp.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <hc-sys class="h-full hc-uni-app-form-upload" :isNavBar="false">
  3. <uni-section class="mb-1" title="文件列表" sub-title="允许格式:jpg/png/pdf/excel/word, 文件大小 小于 60MB"/>
  4. <template v-if='fileListData.length > 0'>
  5. <view v-for="item in fileListData" class="hc-file-item">
  6. <view class="name" @click="fileTap(item)">{{item.name}}</view>
  7. <view class="action">
  8. <uni-tag :inverted="true" text="删除" type="error" @click="delFileClick(item)"/>
  9. </view>
  10. </view>
  11. </template>
  12. <view class="action-bar-block"/>
  13. <view class="action-bar">
  14. <button type="primary" class="action-bar-btn" @click="selectImageTap">选择图片</button>
  15. <button type="primary" class="action-bar-btn" @click="selectFileTap">选择文件</button>
  16. </view>
  17. <hc-select-file ref="selectFileRef" @change="selectFileChange"/>
  18. </hc-sys>
  19. </template>
  20. <script setup>
  21. import {ref, onMounted} from "vue";
  22. import {onLoad} from '@dcloudio/uni-app'
  23. import {useAppStore} from "@/store";
  24. import wbsApi from '~api/data-fill/wbs';
  25. import {errorToast, openDocFile, successToast, toPdfPreview} from "@/utils/tools";
  26. import {getArrValue, getObjValue} from "js-fast-way";
  27. import {getTokenHeader} from "@/httpApi/request/header";
  28. const store = useAppStore()
  29. //基础变量
  30. const projectId = ref(store.projectId);
  31. const contractId = ref(store.contractId);
  32. const pageNode = ref({});
  33. //页面启动
  34. onLoad((option) => {
  35. if (option.node) {
  36. const res = JSON.parse(decodeURIComponent(option.node));
  37. pageNode.value = {
  38. projectId: projectId.value,
  39. contractId: contractId.value,
  40. classify: res.classify,
  41. pkeyId: res.pkeyId,
  42. nodeId: res.treeId,
  43. }
  44. }
  45. })
  46. //渲染完成
  47. onMounted(() => {
  48. getBussFileList()
  49. })
  50. //获取已上传的文件列表
  51. const fileListData = ref([])
  52. const getBussFileList = async () => {
  53. const { pkeyId } = pageNode.value
  54. if (!pkeyId) {
  55. errorToast('pkeyId为空');
  56. return
  57. }
  58. const { data } = await wbsApi.getBussFileList({
  59. pkeyid: pkeyId,
  60. })
  61. fileListData.value = getArrValue(data)
  62. }
  63. //选择图片上传
  64. const selectImageTap = () => {
  65. uni.chooseImage({
  66. count: 3,
  67. sizeType: ['original'],
  68. success: (res)=> {
  69. setUploadFile(res.tempFilePaths)
  70. }
  71. });
  72. }
  73. //选择文件上传
  74. const selectFileRef = ref(null)
  75. const selectFileTap = () => {
  76. selectFileRef.value.selectFile()
  77. }
  78. //文件选择完成
  79. const selectFileChange = ({file, path}) => {
  80. if(file) {
  81. uni.showLoading({title: '上传文件中...', mask: true})
  82. uni.uploadFile({
  83. url: '/api/blade-manager/exceltab/add-buss-file',
  84. name: 'file',
  85. file: file,
  86. filePath: path,
  87. formData: pageNode.value,
  88. header: getTokenHeader(),
  89. timeout: 300000,
  90. success: (res) => {
  91. uni.hideLoading()
  92. getBussFileList()
  93. },
  94. fail: () => {
  95. uni.hideLoading()
  96. }
  97. });
  98. }
  99. }
  100. //处理文件上传
  101. const setUploadFile = async (arr) => {
  102. if (arr && arr.length > 0) {
  103. uni.showLoading({title: '上传文件中...', mask: true})
  104. for (let i = 0; i < arr.length; i++) {
  105. await uploadFileApi(arr[i])
  106. getBussFileList().then()
  107. }
  108. uni.hideLoading()
  109. }
  110. }
  111. //上传文件
  112. const uploadFileApi = async (file) => {
  113. return new Promise((resolve) => {
  114. uni.uploadFile({
  115. url: '/api/blade-manager/exceltab/add-buss-file',
  116. name: 'file',
  117. filePath: file,
  118. formData: pageNode.value,
  119. header: getTokenHeader(),
  120. timeout: 300000,
  121. success: (res) => {
  122. resolve(getObjValue(res.data));
  123. },
  124. fail: () => {
  125. resolve({});
  126. }
  127. });
  128. })
  129. }
  130. //删除文件
  131. const delFileClick = async(item) => {
  132. uni.showLoading({title: '删除文件中...', mask: true})
  133. const {error, code, msg} = await wbsApi.removeBussFile({
  134. ids: item.id
  135. })
  136. uni.hideLoading()
  137. if (!error && code === 200) {
  138. successToast('删除成功');
  139. getBussFileList().then()
  140. } else {
  141. errorToast('删除失败' + msg);
  142. }
  143. }
  144. //查看文件
  145. const fileTap = async({url, extension}) => {
  146. const word = ['doc', 'docx', 'pdf', 'xls', 'xlsx']
  147. const img = ['gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp']
  148. if(word.indexOf(extension) !== -1) {
  149. if(extension === 'pdf') {
  150. toPdfPreview(url)
  151. } else {
  152. openDocFile(url, extension)
  153. }
  154. } else if(img.indexOf(extension) !== -1) {
  155. uni.previewImage({
  156. current: 1,
  157. urls: [url]
  158. });
  159. } else {
  160. errorToast('当前' + extension + '文件格式暂不支持预览');
  161. }
  162. }
  163. </script>
  164. <style scoped lang="scss">
  165. page {
  166. height: 100%;
  167. background: #FAFBFE;
  168. }
  169. </style>
  170. <style lang="scss">
  171. @import "@/style/data-fill/uploadFile.scss";
  172. </style>