123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- <template>
- <hc-sys class="h-full hc-uni-app-form-upload" :isNavBar="false">
- <uni-section class="mb-1" title="文件列表" sub-title="允许格式:jpg/png/pdf/excel/word, 文件大小 小于 60MB"/>
- <template v-if='fileListData.length > 0'>
- <view v-for="item in fileListData" class="hc-file-item">
- <view class="name" @click="fileTap(item)">{{item.name}}</view>
- <view class="action">
- <uni-tag :inverted="true" text="删除" type="error" @click="delFileClick(item)"/>
- </view>
- </view>
- </template>
- <view class="action-bar-block"/>
- <view class="action-bar">
- <button type="primary" class="action-bar-btn" @click="selectImageTap">选择图片</button>
- <button type="primary" class="action-bar-btn" @click="selectFileTap">选择文件</button>
- </view>
- <hc-select-file ref="selectFileRef" @change="selectFileChange" v-if="isFileBody"/>
- </hc-sys>
- </template>
- <script setup>
- import {ref} from "vue";
- import {onLoad} from '@dcloudio/uni-app'
- import {useAppStore} from "@/store";
- import wbsApi from '~api/data-fill/wbs';
- import {errorToast, openDocFile, successToast, toPdfPreview} from "@/utils/tools";
- import {getArrValue, getObjValue} from "js-fast-way";
- import {getTokenHeader} from "@/httpApi/request/header";
- import {getUploadApi} from "@/httpApi/modules/upload";
- const store = useAppStore()
- //基础变量
- const projectId = ref(store.projectId);
- const contractId = ref(store.contractId);
- const pageNode = ref({});
- const isFileBody = ref(false);
- //页面启动
- onLoad(({node}) => {
- if (node) {
- const res = JSON.parse(decodeURIComponent(node));
- pageNode.value = {
- projectId: projectId.value,
- contractId: contractId.value,
- classify: res.classify,
- pkeyId: res.pkeyId,
- nodeId: res.treeId,
- }
- getBussFileList()
- isFileBody.value = true
- } else {
- errorToast('参数错误');
- setTimeout(() => {
- uni.navigateBack()
- },1500)
- }
- })
- //获取已上传的文件列表
- const fileListData = ref([])
- const getBussFileList = async () => {
- const { pkeyId } = pageNode.value
- if (!pkeyId) {
- errorToast('pkeyId为空');
- return
- }
- const { data } = await wbsApi.getBussFileList({
- pkeyid: pkeyId,
- })
- fileListData.value = getArrValue(data)
- }
- //选择图片上传
- const selectImageTap = () => {
- uni.chooseImage({
- count: 3,
- sizeType: ['original'],
- success: (res)=> {
- setUploadFile(res.tempFilePaths)
- }
- });
- }
- //选择文件上传
- const selectFileRef = ref(null)
- const selectFileTap = () => {
- selectFileRef.value?.selectFile()
- }
- //文件选择完成
- const selectFileChange = async (files) => {
- if (files && files.length > 0) {
- let isUnsupported = false
- uni.showLoading({title: '上传文件中...', mask: true})
- const fileType = ['doc', 'docx', 'pdf', 'xls', 'xlsx', 'gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp']
- for (let i = 0; i < files.length; i++) {
- const extension = files[i].fileExtension.toLowerCase()
- if (fileType.indexOf(extension) > -1) {
- await uploadFileApi(files[i].filePath)
- getBussFileList().then()
- } else {
- isUnsupported = true
- }
- }
- uni.hideLoading()
- if (isUnsupported) {
- errorToast('有不支持的文件格式,已过滤上传');
- }
- }
- }
- //处理文件上传
- const setUploadFile = async (arr) => {
- if (arr && arr.length > 0) {
- uni.showLoading({title: '上传文件中...', mask: true})
- for (let i = 0; i < arr.length; i++) {
- await uploadFileApi(arr[i])
- getBussFileList().then()
- }
- uni.hideLoading()
- }
- }
- //上传文件
- const uploadFileApi = async (file) => {
- return new Promise((resolve) => {
- uni.uploadFile({
- url: getUploadApi() + 'blade-manager/exceltab/add-buss-file',
- name: 'file',
- filePath: file,
- formData: pageNode.value,
- header: getTokenHeader(),
- timeout: 300000,
- success: (res) => {
- resolve(getObjValue(res.data));
- },
- fail: () => {
- resolve({});
- }
- });
- })
- }
- //删除文件
- const delFileClick = async(item) => {
- uni.showLoading({title: '删除文件中...', mask: true})
- const {error, code, msg} = await wbsApi.removeBussFile({
- ids: item.id
- })
- uni.hideLoading()
- if (!error && code === 200) {
- successToast('删除成功');
- getBussFileList().then()
- } else {
- errorToast('删除失败' + msg);
- }
- }
- //查看文件
- const fileTap = async({url, extension}) => {
- const word = ['doc', 'docx', 'pdf', 'xls', 'xlsx']
- const img = ['gif', 'jpg', 'jpeg', 'png', 'bmp', 'webp']
- if(word.indexOf(extension) !== -1) {
- if(extension === 'pdf') {
- toPdfPreview(url).then()
- } else {
- openDocFile(url, extension)
- }
- } else if(img.indexOf(extension) !== -1) {
- uni.previewImage({
- current: 1,
- urls: [url]
- });
- } else {
- errorToast('当前' + extension + '文件格式暂不支持预览');
- }
- }
- </script>
- <style scoped lang="scss">
- page {
- height: 100%;
- background: #FAFBFE;
- }
- </style>
- <style lang="scss">
- @import "@/style/data-fill/uploadFile.scss";
- </style>
|