60 lines
1.8 KiB
TypeScript
60 lines
1.8 KiB
TypeScript
|
|
import { shell } from "electron";
|
|||
|
|
import { CheckFileOrDirExist } from "../../../define/Tools/file";
|
|||
|
|
import { errorMessage, successMessage } from "../../Public/generalTools";
|
|||
|
|
import path from 'path';
|
|||
|
|
|
|||
|
|
|
|||
|
|
/** 打开指定的文件夹的方法 */
|
|||
|
|
export type OpenFolderParams = {
|
|||
|
|
/** 是不是基于项目文件,是的话,会在项目文件夹的基础上进行拼接 */
|
|||
|
|
baseProject: boolean;
|
|||
|
|
/** 判断是不是打开父文件夹 */
|
|||
|
|
dirFloder: boolean;
|
|||
|
|
/** 文件路径,baseProject 为false,需要设置完整的文件路径 */
|
|||
|
|
folderPath: string;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/** 一些对electron接口的封装,配合业务逻辑 */
|
|||
|
|
export default class ElectronInterface {
|
|||
|
|
constructor() { }
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 打开指定的文件,试用默认的打开方式
|
|||
|
|
* @param value
|
|||
|
|
* @returns
|
|||
|
|
*/
|
|||
|
|
public async OpenFile(value: string) {
|
|||
|
|
if (await CheckFileOrDirExist(value)) {
|
|||
|
|
return errorMessage('文件不存在', 'SystemIpc_OPEN_FILE')
|
|||
|
|
}
|
|||
|
|
await shell.openPath(value)
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* 打开对应的文件夹
|
|||
|
|
* @param params
|
|||
|
|
* @returns
|
|||
|
|
*/
|
|||
|
|
public async OpenFolder(params: OpenFolderParams) {
|
|||
|
|
try {
|
|||
|
|
let openFolder = null
|
|||
|
|
if (params.baseProject) {
|
|||
|
|
openFolder = path.join(global.config.project_path, params.folderPath)
|
|||
|
|
}
|
|||
|
|
if (params.dirFloder) {
|
|||
|
|
openFolder = path.dirname(params.folderPath)
|
|||
|
|
}
|
|||
|
|
if (!openFolder) {
|
|||
|
|
openFolder = params.folderPath
|
|||
|
|
}
|
|||
|
|
// 判断文件夹是不是存在
|
|||
|
|
let isExist = await CheckFileOrDirExist(openFolder)
|
|||
|
|
if (!isExist) {
|
|||
|
|
throw new Error('文件夹不存在,请检查')
|
|||
|
|
}
|
|||
|
|
shell.openPath(openFolder)
|
|||
|
|
return successMessage(null, '打开成功')
|
|||
|
|
} catch (error) {
|
|||
|
|
return errorMessage('打开文件夹错误,错误信息如下:' + error.message, 'SystemIpc_OPEN_FOLDER')
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|