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