fileUp.vue 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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" v-if="isFileBody"/>
  18. </hc-sys>
  19. </template>
  20. <script setup>
  21. import {ref} 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. import {getUploadApi} from "@/httpApi/modules/upload";
  29. const store = useAppStore()
  30. //基础变量
  31. const projectId = ref(store.projectId);
  32. const contractId = ref(store.contractId);
  33. const pageNode = ref({});
  34. const isFileBody = ref(false);
  35. //页面启动
  36. onLoad(({node}) => {
  37. if (node) {
  38. const res = JSON.parse(decodeURIComponent(node));
  39. pageNode.value = {
  40. projectId: projectId.value,
  41. contractId: contractId.value,
  42. classify: res.classify,
  43. pkeyId: res.pkeyId,
  44. nodeId: res.treeId,
  45. }
  46. getBussFileList()
  47. isFileBody.value = true
  48. } else {
  49. errorToast('参数错误');
  50. setTimeout(() => {
  51. uni.navigateBack()
  52. },1500)
  53. }
  54. })
  55. //获取已上传的文件列表
  56. const fileListData = ref([])
  57. const getBussFileList = async () => {
  58. const { pkeyId } = pageNode.value
  59. if (!pkeyId) {
  60. errorToast('pkeyId为空');
  61. return
  62. }
  63. const { data } = await wbsApi.getBussFileList({
  64. pkeyid: pkeyId,
  65. })
  66. fileListData.value = getArrValue(data)
  67. }
  68. //选择图片上传
  69. const selectImageTap = () => {
  70. uni.chooseImage({
  71. count: 3,
  72. sizeType: ['original'],
  73. success: (res)=> {
  74. setUploadFile(res.tempFilePaths)
  75. }
  76. });
  77. }
  78. //选择文件上传
  79. const selectFileRef = ref(null)
  80. const selectFileTap = () => {
  81. selectFileRef.value?.selectFile()
  82. }
  83. //文件选择完成
  84. const selectFileChange = async (files) => {
  85. if (files && files.length > 0) {
  86. let isUnsupported = false
  87. uni.showLoading({title: '上传文件中...', mask: true})
  88. const fileType = ['doc', 'docx', 'pdf', 'xls', 'xlsx', 'gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp']
  89. for (let i = 0; i < files.length; i++) {
  90. const extension = files[i].fileExtension.toLowerCase()
  91. if (fileType.indexOf(extension) > -1) {
  92. await uploadFileApi(files[i].filePath)
  93. getBussFileList().then()
  94. } else {
  95. isUnsupported = true
  96. }
  97. }
  98. uni.hideLoading()
  99. if (isUnsupported) {
  100. errorToast('有不支持的文件格式,已过滤上传');
  101. }
  102. }
  103. }
  104. //处理文件上传
  105. const setUploadFile = async (arr) => {
  106. if (arr && arr.length > 0) {
  107. uni.showLoading({title: '上传文件中...', mask: true})
  108. for (let i = 0; i < arr.length; i++) {
  109. await uploadFileApi(arr[i])
  110. getBussFileList().then()
  111. }
  112. uni.hideLoading()
  113. }
  114. }
  115. //上传文件
  116. const uploadFileApi = async (file) => {
  117. return new Promise((resolve) => {
  118. uni.uploadFile({
  119. url: getUploadApi() + 'blade-manager/exceltab/add-buss-file',
  120. name: 'file',
  121. filePath: file,
  122. formData: pageNode.value,
  123. header: getTokenHeader(),
  124. timeout: 300000,
  125. success: (res) => {
  126. resolve(getObjValue(res.data));
  127. },
  128. fail: () => {
  129. resolve({});
  130. }
  131. });
  132. })
  133. }
  134. //删除文件
  135. const delFileClick = async(item) => {
  136. uni.showLoading({title: '删除文件中...', mask: true})
  137. const {error, code, msg} = await wbsApi.removeBussFile({
  138. ids: item.id
  139. })
  140. uni.hideLoading()
  141. if (!error && code === 200) {
  142. successToast('删除成功');
  143. getBussFileList().then()
  144. } else {
  145. errorToast('删除失败' + msg);
  146. }
  147. }
  148. //查看文件
  149. const fileTap = async({url, extension}) => {
  150. const word = ['doc', 'docx', 'pdf', 'xls', 'xlsx']
  151. const img = ['gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp']
  152. if(word.indexOf(extension) !== -1) {
  153. if(extension === 'pdf') {
  154. toPdfPreview(url).then()
  155. } else {
  156. openDocFile(url, extension)
  157. }
  158. } else if(img.indexOf(extension) !== -1) {
  159. uni.previewImage({
  160. current: 1,
  161. urls: [url]
  162. });
  163. } else {
  164. errorToast('当前' + extension + '文件格式暂不支持预览');
  165. }
  166. }
  167. </script>
  168. <style scoped lang="scss">
  169. page {
  170. height: 100%;
  171. background: #FAFBFE;
  172. }
  173. </style>
  174. <style lang="scss">
  175. @import "@/style/data-fill/uploadFile.scss";
  176. </style>