feat: 1.插件加载时会有全局提示。2.重命名miaoMessageProvider为miaoAlertTipProvider
This commit is contained in:
1 parent
3a18bb3cbd
commit
ec4ed10afd
16 files changed
+481
-465
No files matched your search
+3
-2
@@ -1,5 +1,6 @@
|
||||
import { alertTipType } from "@/types/type";
|
||||
import initPlugin from "./init/initPlugin";
|
||||
|
||||
export default async function init() {
|
||||
await initPlugin()
|
||||
export default async function init(globalAlertTip: alertTipType) {
|
||||
await initPlugin(globalAlertTip)
|
||||
}
|
||||
@@ -1,16 +1,23 @@
|
||||
import usePluginCenter, { initPluginCenter } from "@/hooks/usePluginCenter";
|
||||
import { registerComponentOption } from "@/class/PluginCenter";
|
||||
import { alertTipType } from "@/types/type";
|
||||
|
||||
import usePluginCenter from '@/hooks/usePluginCenter'
|
||||
import { registerComponentOption } from '@/class/PluginCenter'
|
||||
const initPlugin = async (globalAlertTip: alertTipType) => {
|
||||
initPluginCenter(globalAlertTip)
|
||||
const modulesFiles = import.meta.glob("@/plugins/**/export.ts");
|
||||
const pluginCenter = usePluginCenter();
|
||||
const promiseArray = [];
|
||||
for (let pluginConfig of Object.values(modulesFiles)) {
|
||||
promiseArray.push(
|
||||
new Promise(async resolve => {
|
||||
const _config = pluginConfig as () => Promise<{
|
||||
default: registerComponentOption;
|
||||
}>;
|
||||
resolve(pluginCenter.registerComponent((await _config()).default));
|
||||
})
|
||||
);
|
||||
}
|
||||
await Promise.all(promiseArray);
|
||||
};
|
||||
|
||||
const initPlugin = async () => {
|
||||
const modulesFiles = import.meta.glob('@/plugins/**/export.ts')
|
||||
const pluginCenter = usePluginCenter()
|
||||
for (let pluginConfig of Object.values(modulesFiles)) {
|
||||
const _config = pluginConfig as () => Promise<{
|
||||
default: registerComponentOption
|
||||
}>
|
||||
pluginCenter.registerComponent((await _config()).default)
|
||||
}
|
||||
}
|
||||
|
||||
export default initPlugin
|
||||
export default initPlugin;
|
||||
+252
-269
@@ -1,291 +1,274 @@
|
||||
import VirtualDirectory, { VirtualFile } from '@/class/VirtualDirectory'
|
||||
import VirtualDirectory, { VirtualFile } from "@/class/VirtualDirectory";
|
||||
import {
|
||||
getOption,
|
||||
getResponse,
|
||||
miaoFetchConfig,
|
||||
deleteOption,
|
||||
uploadOption,
|
||||
queryOption,
|
||||
renameOption,
|
||||
cutOption,
|
||||
mkdirOption
|
||||
} from '@/types/fetch'
|
||||
import Config from '@/config'
|
||||
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios'
|
||||
getOption,
|
||||
getResponse,
|
||||
miaoFetchConfig,
|
||||
deleteOption,
|
||||
uploadOption,
|
||||
queryOption,
|
||||
renameOption,
|
||||
cutOption,
|
||||
mkdirOption,
|
||||
getFileOption
|
||||
} from "@/types/fetch";
|
||||
import Config from "@/config";
|
||||
import axios, { AxiosRequestConfig, AxiosResponse } from "axios";
|
||||
|
||||
export const useMiaoFetch = (option?: miaoFetchConfig) => {
|
||||
const retry = option?.retry ?? 0
|
||||
const controller = new AbortController()
|
||||
let flag = true
|
||||
return {
|
||||
miaoFetch: async (
|
||||
config: AxiosRequestConfig<any>
|
||||
): Promise<AxiosResponse<any, any>> => {
|
||||
let counter = 0
|
||||
let response: any
|
||||
let errList = []
|
||||
while (flag && counter <= retry) {
|
||||
try {
|
||||
response = await axios({
|
||||
...config,
|
||||
signal: controller.signal
|
||||
})
|
||||
return response
|
||||
} catch (_e) {
|
||||
errList.push(_e)
|
||||
counter += 1
|
||||
}
|
||||
}
|
||||
throw errList
|
||||
},
|
||||
/**
|
||||
* 强制终止miaoFetch
|
||||
*/
|
||||
abortMiaoFetch: () => {
|
||||
flag = false
|
||||
controller.abort()
|
||||
}
|
||||
}
|
||||
}
|
||||
const retry = option?.retry ?? 0;
|
||||
const controller = new AbortController();
|
||||
let flag = true;
|
||||
return {
|
||||
miaoFetch: async (config: AxiosRequestConfig<any>): Promise<AxiosResponse<any, any>> => {
|
||||
let counter = 0;
|
||||
let response: any;
|
||||
let errList = [];
|
||||
while (flag && counter <= retry) {
|
||||
try {
|
||||
response = await axios({
|
||||
...config,
|
||||
signal: controller.signal
|
||||
});
|
||||
return response;
|
||||
} catch (_e) {
|
||||
errList.push(_e);
|
||||
counter += 1;
|
||||
}
|
||||
}
|
||||
throw errList;
|
||||
},
|
||||
/**
|
||||
* 强制终止miaoFetch
|
||||
*/
|
||||
abortMiaoFetch: () => {
|
||||
flag = false;
|
||||
controller.abort();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
const miaoFetchApi = {
|
||||
/**
|
||||
* 获取对应文件夹下的数据
|
||||
*/
|
||||
async get(
|
||||
vDirectory: VirtualDirectory,
|
||||
option?: getOption
|
||||
): Promise<getResponse> {
|
||||
const { baseUrl, api } = Config
|
||||
const url = `${baseUrl}${api.get}${vDirectory.path}`
|
||||
const retry = option?.retry ?? 0
|
||||
/**
|
||||
* 获取对应文件夹下的数据
|
||||
*/
|
||||
async get(vDirectory: VirtualDirectory, option?: getOption): Promise<getResponse> {
|
||||
const { baseUrl, api } = Config;
|
||||
const url = `${baseUrl}${api.get}${vDirectory.path}`;
|
||||
const retry = option?.retry ?? 0;
|
||||
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
})
|
||||
const response = await miaoFetch({
|
||||
url
|
||||
})
|
||||
return response.data
|
||||
},
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
});
|
||||
const response = await miaoFetch({
|
||||
url
|
||||
});
|
||||
return response.data;
|
||||
},
|
||||
|
||||
/**
|
||||
* 获取对应文件下的数据
|
||||
*/
|
||||
async getFile(
|
||||
vFile: VirtualFile,
|
||||
option?: getOption
|
||||
) {
|
||||
// const { baseUrl, api } = Config
|
||||
const url = vFile.url
|
||||
const retry = option?.retry ?? 0
|
||||
const axiosOption = option?.axiosOption ?? {}
|
||||
/**
|
||||
* 获取对应文件下的数据
|
||||
*/
|
||||
getFile(vFile: VirtualFile, option?: getFileOption) {
|
||||
// const { baseUrl, api } = Config
|
||||
const url = vFile.url;
|
||||
const retry = option?.retry ?? 0;
|
||||
const axiosOption = option?.axiosOption ?? {};
|
||||
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
})
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
responseType: 'arraybuffer',
|
||||
...axiosOption
|
||||
})
|
||||
return new Uint8Array(response.data)
|
||||
},
|
||||
const { miaoFetch, abortMiaoFetch: abort } = useMiaoFetch({
|
||||
retry
|
||||
});
|
||||
const response = miaoFetch({
|
||||
url,
|
||||
responseType: "arraybuffer",
|
||||
onDownloadProgress(progressEvent) {
|
||||
option && option.onProcess && option.onProcess(progressEvent.total ? progressEvent.loaded / progressEvent.total : -1);
|
||||
},
|
||||
...axiosOption
|
||||
});
|
||||
return {
|
||||
response: new Promise<Uint8Array>(async resolve => {
|
||||
resolve(new Uint8Array((await response).data));
|
||||
}),
|
||||
abort
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* 上传单个文件到对应文件夹, 后续要将data类型改成buffer以支持分片上传
|
||||
* 正常上传文件请使用uploadQueue
|
||||
*/
|
||||
upload(data: File, targetVDir: VirtualDirectory, option?: uploadOption) {
|
||||
const { baseUrl, api } = Config
|
||||
const url = `${baseUrl}${api.upload}${targetVDir.path}`
|
||||
const retry = option?.retry ?? 0
|
||||
const { miaoFetch, abortMiaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
})
|
||||
const sizeTotal = data.size
|
||||
/**
|
||||
* 上传单个文件到对应文件夹, 后续要将data类型改成buffer以支持分片上传
|
||||
* 正常上传文件请使用uploadQueue
|
||||
*/
|
||||
upload(data: File, targetVDir: VirtualDirectory, option?: uploadOption) {
|
||||
const { baseUrl, api } = Config;
|
||||
const url = `${baseUrl}${api.upload}${targetVDir.path}`;
|
||||
const retry = option?.retry ?? 0;
|
||||
const { miaoFetch, abortMiaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
});
|
||||
const sizeTotal = data.size;
|
||||
|
||||
const opType = option?.type ?? 'write'
|
||||
const opType = option?.type ?? "write";
|
||||
|
||||
const formData = new FormData()
|
||||
const formData = new FormData();
|
||||
|
||||
formData.append('file', data)
|
||||
// 必须这样传,不然中文乱码
|
||||
formData.append('fileName', data.name)
|
||||
formData.append('operateType', opType)
|
||||
formData.append("file", data);
|
||||
// 必须这样传,不然中文乱码
|
||||
formData.append("fileName", data.name);
|
||||
formData.append("operateType", opType);
|
||||
|
||||
return {
|
||||
abort: () => abortMiaoFetch(),
|
||||
response: new Promise<{
|
||||
message: 'success' | 'failed'
|
||||
error?: string
|
||||
}>(async (resolve) => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: 'post',
|
||||
data: formData,
|
||||
onUploadProgress(progressEvent) {
|
||||
option &&
|
||||
option.onProcess &&
|
||||
option.onProcess(progressEvent.loaded / sizeTotal)
|
||||
}
|
||||
})
|
||||
resolve(response.data)
|
||||
})
|
||||
}
|
||||
},
|
||||
return {
|
||||
abort: () => abortMiaoFetch(),
|
||||
response: new Promise<{
|
||||
message: "success" | "failed";
|
||||
error?: string;
|
||||
}>(async resolve => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: "post",
|
||||
data: formData,
|
||||
onUploadProgress(progressEvent) {
|
||||
option && option.onProcess && option.onProcess(progressEvent.loaded / sizeTotal);
|
||||
}
|
||||
});
|
||||
resolve(response.data);
|
||||
})
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* 在目标文件夹下创建新的文件夹
|
||||
*/
|
||||
mkdir(
|
||||
targetVDir: VirtualDirectory,
|
||||
newFolderName: string,
|
||||
option?: mkdirOption
|
||||
) {
|
||||
const { baseUrl, api } = Config
|
||||
const url = `${baseUrl}${api.mkdir}${targetVDir.path}`
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry: option?.retry ?? 0
|
||||
})
|
||||
/**
|
||||
* 在目标文件夹下创建新的文件夹
|
||||
*/
|
||||
mkdir(targetVDir: VirtualDirectory, newFolderName: string, option?: mkdirOption) {
|
||||
const { baseUrl, api } = Config;
|
||||
const url = `${baseUrl}${api.mkdir}${targetVDir.path}`;
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry: option?.retry ?? 0
|
||||
});
|
||||
|
||||
return new Promise<{
|
||||
message: 'success' | 'failed'
|
||||
error?: string
|
||||
}>(async (resolve) => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: 'post',
|
||||
data: {
|
||||
folderName: newFolderName
|
||||
}
|
||||
})
|
||||
resolve(response.data)
|
||||
})
|
||||
},
|
||||
/**
|
||||
* 在目标文件夹下创建新的空文件,实际上是通过upload空文件实现
|
||||
*/
|
||||
mkFile(
|
||||
targetVDir: VirtualDirectory,
|
||||
newFileName: string,
|
||||
option?: uploadOption
|
||||
) {
|
||||
const emptyBlob = new Blob([])
|
||||
const emptyFile = new File([emptyBlob], newFileName, {
|
||||
type: 'text/plain'
|
||||
})
|
||||
return this.upload(emptyFile, targetVDir, option)
|
||||
},
|
||||
return new Promise<{
|
||||
message: "success" | "failed";
|
||||
error?: string;
|
||||
}>(async resolve => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: "post",
|
||||
data: {
|
||||
folderName: newFolderName
|
||||
}
|
||||
});
|
||||
resolve(response.data);
|
||||
});
|
||||
},
|
||||
/**
|
||||
* 在目标文件夹下创建新的空文件,实际上是通过upload空文件实现
|
||||
*/
|
||||
mkFile(targetVDir: VirtualDirectory, newFileName: string, option?: uploadOption) {
|
||||
const emptyBlob = new Blob([]);
|
||||
const emptyFile = new File([emptyBlob], newFileName, {
|
||||
type: "text/plain"
|
||||
});
|
||||
return this.upload(emptyFile, targetVDir, option);
|
||||
},
|
||||
|
||||
/**
|
||||
* 删除对应的文件或文件夹,若为文件夹会递归删除
|
||||
*/
|
||||
delete(target: VirtualFile | VirtualDirectory, option?: deleteOption) {
|
||||
const { baseUrl, api } = Config
|
||||
const url = `${baseUrl}${api.delete}${target.path}`
|
||||
const retry = option?.retry ?? 0
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
})
|
||||
/**
|
||||
* 删除对应的文件或文件夹,若为文件夹会递归删除
|
||||
*/
|
||||
delete(target: VirtualFile | VirtualDirectory, option?: deleteOption) {
|
||||
const { baseUrl, api } = Config;
|
||||
const url = `${baseUrl}${api.delete}${target.path}`;
|
||||
const retry = option?.retry ?? 0;
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
});
|
||||
|
||||
return {
|
||||
response: new Promise<{
|
||||
eventId: number
|
||||
}>(async (resolve) => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: 'post'
|
||||
})
|
||||
resolve(response.data)
|
||||
})
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 重命名对应的文件或文件夹
|
||||
*/
|
||||
rename(
|
||||
target: VirtualFile | VirtualDirectory,
|
||||
newName: string,
|
||||
option?: renameOption
|
||||
) {
|
||||
const { baseUrl, api } = Config
|
||||
const url = `${baseUrl}${api.rename}${target.path}`
|
||||
const retry = option?.retry ?? 0
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
})
|
||||
return {
|
||||
response: new Promise<{
|
||||
eventId: number;
|
||||
}>(async resolve => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: "post"
|
||||
});
|
||||
resolve(response.data);
|
||||
})
|
||||
};
|
||||
},
|
||||
/**
|
||||
* 重命名对应的文件或文件夹
|
||||
*/
|
||||
rename(target: VirtualFile | VirtualDirectory, newName: string, option?: renameOption) {
|
||||
const { baseUrl, api } = Config;
|
||||
const url = `${baseUrl}${api.rename}${target.path}`;
|
||||
const retry = option?.retry ?? 0;
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
});
|
||||
|
||||
return {
|
||||
response: new Promise<{
|
||||
eventId: number
|
||||
}>(async (resolve) => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: 'post',
|
||||
data: { newName }
|
||||
})
|
||||
resolve(response.data)
|
||||
})
|
||||
}
|
||||
},
|
||||
/**
|
||||
* 剪切文件或文件夹
|
||||
*/
|
||||
cut(
|
||||
target: VirtualFile | VirtualDirectory,
|
||||
to: VirtualDirectory,
|
||||
option?: cutOption
|
||||
) {
|
||||
const newPath = to.path
|
||||
const { baseUrl, api } = Config
|
||||
const url = `${baseUrl}${api.cut}${target.path}`
|
||||
const retry = option?.retry ?? 0
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
})
|
||||
return {
|
||||
response: new Promise<{
|
||||
eventId: number
|
||||
}>(async (resolve) => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: 'post',
|
||||
data: { newPath }
|
||||
})
|
||||
resolve(response.data)
|
||||
})
|
||||
}
|
||||
},
|
||||
return {
|
||||
response: new Promise<{
|
||||
eventId: number;
|
||||
}>(async resolve => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: "post",
|
||||
data: { newName }
|
||||
});
|
||||
resolve(response.data);
|
||||
})
|
||||
};
|
||||
},
|
||||
/**
|
||||
* 剪切文件或文件夹
|
||||
*/
|
||||
cut(target: VirtualFile | VirtualDirectory, to: VirtualDirectory, option?: cutOption) {
|
||||
const newPath = to.path;
|
||||
const { baseUrl, api } = Config;
|
||||
const url = `${baseUrl}${api.cut}${target.path}`;
|
||||
const retry = option?.retry ?? 0;
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
});
|
||||
return {
|
||||
response: new Promise<{
|
||||
eventId: number;
|
||||
}>(async resolve => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: "post",
|
||||
data: { newPath }
|
||||
});
|
||||
resolve(response.data);
|
||||
})
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* 查询服务端操作事件的进展
|
||||
*/
|
||||
query(eventId: number, option?: queryOption) {
|
||||
const { baseUrl, api } = Config
|
||||
const url = `${baseUrl}${api.query}`
|
||||
const retry = option?.retry ?? 0
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
})
|
||||
return {
|
||||
response: new Promise<{
|
||||
status: 'process' | 'success' | 'failed'
|
||||
error?: unknown
|
||||
}>(async (resolve) => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: 'get',
|
||||
params: {
|
||||
id: eventId
|
||||
}
|
||||
})
|
||||
resolve(response.data)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
/**
|
||||
* 查询服务端操作事件的进展
|
||||
*/
|
||||
query(eventId: number, option?: queryOption) {
|
||||
const { baseUrl, api } = Config;
|
||||
const url = `${baseUrl}${api.query}`;
|
||||
const retry = option?.retry ?? 0;
|
||||
const { miaoFetch } = useMiaoFetch({
|
||||
retry
|
||||
});
|
||||
return {
|
||||
response: new Promise<{
|
||||
status: "process" | "success" | "failed";
|
||||
error?: unknown;
|
||||
}>(async resolve => {
|
||||
const response = await miaoFetch({
|
||||
url,
|
||||
method: "get",
|
||||
params: {
|
||||
id: eventId
|
||||
}
|
||||
});
|
||||
resolve(response.data);
|
||||
})
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
export default function useMiaoFetchApi() {
|
||||
return miaoFetchApi
|
||||
return miaoFetchApi;
|
||||
}
|
||||
@@ -1,11 +1,15 @@
|
||||
import PluginCenter from "@/class/PluginCenter";
|
||||
import { alertTipType } from "@/types/type";
|
||||
import { reactive } from "vue";
|
||||
|
||||
let pluginCenter: PluginCenter
|
||||
|
||||
export default function usePluginCenter(){
|
||||
export const initPluginCenter = (globalAlertTip: alertTipType) => {
|
||||
if(!pluginCenter) {
|
||||
pluginCenter = reactive(new PluginCenter())
|
||||
pluginCenter = reactive(new PluginCenter(globalAlertTip))
|
||||
}
|
||||
}
|
||||
|
||||
export default function usePluginCenter(){
|
||||
return pluginCenter
|
||||
}
|
||||
Reference in new issue
Block a user