From 6c823457b9624c47e2e74def55af900bcc7cff13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Wed, 15 Mar 2023 22:36:41 +0800 Subject: [PATCH] finish filelist analyze --- frontend/vben/.env.development | 2 +- frontend/vben/mock/page/filelist.ts | 32 +++++++++++++++++++ frontend/vben/src/api/page/index.ts | 12 +++++-- .../vben/src/api/page/model/fileListModel.ts | 4 +++ .../vben/src/views/page/filelist/data.tsx | 19 ++++++----- .../vben/src/views/page/filelist/index.vue | 28 ++++++++++++++-- 6 files changed, 84 insertions(+), 13 deletions(-) diff --git a/frontend/vben/.env.development b/frontend/vben/.env.development index 9236123..85af123 100644 --- a/frontend/vben/.env.development +++ b/frontend/vben/.env.development @@ -6,7 +6,7 @@ VITE_PUBLIC_PATH = / # Cross-domain proxy, you can configure multiple # Please note that no line breaks -VITE_PROXY = [["/basic-api","http://localhost:3000"],["/upload","http://localhost:3300/upload"]] +VITE_PROXY = [["/basic-api","http://localhost:3000"],["/upload","http://localhost:3000/upload"]] # VITE_PROXY=[["/api","https://vvbin.cn/test"]] # Delete console diff --git a/frontend/vben/mock/page/filelist.ts b/frontend/vben/mock/page/filelist.ts index 4138675..989500f 100644 --- a/frontend/vben/mock/page/filelist.ts +++ b/frontend/vben/mock/page/filelist.ts @@ -3,6 +3,8 @@ import { resultError, resultSuccess, getRequestToken, requestParams } from '../_ const deletedIDs: number[] = [] +const analyzingIDs: { id: number; per: number }[] = [] + function createFileList() { const lst: any[] = [] for (let i = 100; i > 0; i--) { @@ -48,6 +50,21 @@ export default [ if (!token) return resultError('Invalid token') const id = request.query.id if (!id || id < 0) return resultError('Invalid id') + let p = 0 + analyzingIDs.map((value: { id: number; per: number }, index: number) => { + if (!p && value.id == id) { + value.per += 10 + if (value.per >= 100) { + analyzingIDs.splice(index, 1) + p = 100 + } + p = value.per + } + }) + if (p > 0) + return resultSuccess({ + percent: p, + }) return resultSuccess({ percent: 100, }) @@ -68,4 +85,19 @@ export default [ }) }, }, + { + url: '/basic-api/analyzeFile', + timeout: 1000, + method: 'get', + response: (request: requestParams) => { + const token = getRequestToken(request) + if (!token) return resultError('Invalid token') + const id = Number(request.query.id) + if (!id || id < 0) return resultError('Invalid id') + analyzingIDs.push({ id: id, per: 1 }) + return resultSuccess({ + msg: '正在分析' + id + ', 请耐心等待...', + }) + }, + }, ] as MockMethod[] diff --git a/frontend/vben/src/api/page/index.ts b/frontend/vben/src/api/page/index.ts index 8c47866..87edb12 100644 --- a/frontend/vben/src/api/page/index.ts +++ b/frontend/vben/src/api/page/index.ts @@ -1,10 +1,11 @@ import { defHttp } from '/@/utils/http/axios' -import { getFileListModel, FilePercent, DelFile } from './model/fileListModel' +import { getFileListModel, FilePercent, DelFile, AnalyzeFile } from './model/fileListModel' enum Api { GetFileList = '/getFileList', GetFilePercent = '/getFilePercent', DelFile = '/delFile', + AnalyzeFile = '/analyzeFile', } /** @@ -22,8 +23,15 @@ export const getFilePercent = (id: number) => { } /** - * @description: Get file percant + * @description: Get file percent */ export const delFile = (id: number) => { return defHttp.get({ url: Api.DelFile, params: { id: id } }) } + +/** + * @description: Analyze file + */ +export const analyzeFile = (id: number) => { + return defHttp.get({ url: Api.AnalyzeFile, params: { id: id } }) +} diff --git a/frontend/vben/src/api/page/model/fileListModel.ts b/frontend/vben/src/api/page/model/fileListModel.ts index 91b756c..abfa831 100644 --- a/frontend/vben/src/api/page/model/fileListModel.ts +++ b/frontend/vben/src/api/page/model/fileListModel.ts @@ -21,3 +21,7 @@ export interface FilePercent { export interface DelFile { msg: string } + +export interface AnalyzeFile { + msg: string +} diff --git a/frontend/vben/src/views/page/filelist/data.tsx b/frontend/vben/src/views/page/filelist/data.tsx index 71b2fcb..ef0a8af 100644 --- a/frontend/vben/src/views/page/filelist/data.tsx +++ b/frontend/vben/src/views/page/filelist/data.tsx @@ -2,15 +2,18 @@ import { reactive } from 'vue' import { getFileList, getFilePercent } from '/@/api/page' import { getFileListModel } from '/@/api/page/model/fileListModel' -const random = (min: number, max: number) => Math.floor(Math.random() * (max - min + 1) + min) +export const random = (min: number, max: number) => + Math.floor(Math.random() * (max - min + 1) + min) -export function refreshFilePercent(arr: any[], i: number) { +export function refreshFilePercent(item: any) { return async () => { - const p = await getFilePercent(arr[i].id) - arr[i].percent = p.percent - if (p.percent < 100) { - setTimeout(refreshFilePercent(arr, i), 1000) - } + const p = await getFilePercent(item.id) + if (p.percent) { + item.percent = p.percent + if (p.percent < 100) { + setTimeout(refreshFilePercent(item), 1000) + } + } else item.hassettimeout = false } } @@ -22,7 +25,7 @@ export function getListOfPage(pageSize: number, page: number): any[] { else lst = reactive(cardList._cardList.slice((cardList._cardList.length / pageSize) * pageSize)) for (let i = 0; i < lst.length; i++) { if (!lst[i].hassettimeout && lst[i].percent > 0 && lst[i].percent < 100) { - setTimeout(refreshFilePercent(lst, i), 1000 + random(0, 1000)) + setTimeout(refreshFilePercent(lst[i]), 1000 + random(0, 1000)) lst[i].hassettimeout = true } } diff --git a/frontend/vben/src/views/page/filelist/index.vue b/frontend/vben/src/views/page/filelist/index.vue index bd38f3e..0ba4cf0 100644 --- a/frontend/vben/src/views/page/filelist/index.vue +++ b/frontend/vben/src/views/page/filelist/index.vue @@ -47,6 +47,7 @@ color="warning" v-if="hasPermission([RoleEnum.SUPER, RoleEnum.FILE_MANAGER])" :disabled="item.percent != 0" + @click="analyzeFile(item)" > 解析 @@ -94,7 +95,14 @@ import { defineComponent } from 'vue' import { Icon } from '/@/components/Icon' import { BasicUpload } from '/@/components/Upload' - import { cardList, getListOfPage, deleteFileByID, pagination } from './data' + import { + cardList, + getListOfPage, + deleteFileByID, + pagination, + refreshFilePercent, + random, + } from './data' import { PageWrapper } from '/@/components/Page' import { useMessage } from '/@/hooks/web/useMessage' import { usePermission } from '/@/hooks/web/usePermission' @@ -102,7 +110,7 @@ import { List } from 'ant-design-vue' import { uploadApi } from '/@/api/sys/upload' import { useI18n } from '/@/hooks/web/useI18n' - import { delFile } from '/@/api/page' + import { delFile, analyzeFile } from '/@/api/page' const { t } = useI18n() const { createMessage } = useMessage() @@ -126,6 +134,21 @@ } } + async function analFile(item: any) { + try { + const msg = await analyzeFile(item.id) + if (msg) { + createMessage.success(msg.msg) + if (!item.hassettimeout && item.percent == 0) { + setTimeout(refreshFilePercent(item), 1000 + random(0, 1000)) + item.hassettimeout = true + } + } + } catch (error) { + createMessage.error((error as unknown as Error).message) + } + } + export default defineComponent({ components: { BasicUpload, @@ -152,6 +175,7 @@ prefixCls: 'list-basic', getListOfPage, deleteFile, + analyzeFile: analFile, cardList, pagination, }