pref: 优化插件store

This commit is contained in:
xiaojunnuo
2025-04-12 23:59:03 +08:00
parent 965dc2cb47
commit cc0657aaa8
71 changed files with 764 additions and 767 deletions
@@ -1,9 +0,0 @@
import { createPinia } from "pinia";
const store = createPinia();
export default {
install(app: any) {
app.use(store);
}
};
export { store };
@@ -0,0 +1,82 @@
import { request } from "/src/api/service";
import * as _ from "lodash-es";
import { PluginConfigBean, PluginSysSetting } from "/@/views/sys/plugin/api";
const apiPrefix = "/pi/plugin";
const defaultInputDefine = {
component: {
name: "a-input",
vModel: "modelValue",
},
};
function initPlugins(plugins: any) {
const checkedComponents = ["a-checkbox", "a-radio", "a-switch"];
for (const plugin of plugins) {
for (const key in plugin.input) {
const field = _.merge({}, defaultInputDefine, plugin.input[key]);
const componentName = field.component.name;
if (componentName.startsWith("a-")) {
if (checkedComponents.includes(componentName)) {
field.component.vModel = "checked";
} else {
field.component.vModel = "value";
}
}
//嵌套对象
field.key = ["input", key];
if (field.required) {
// delete field.required;
if (field.rules == null) {
field.rules = [];
}
field.rules.push({ required: true, message: "此项必填" });
}
plugin.input[key] = field;
}
}
}
export async function GetList(query: any) {
const plugins = await request({
url: apiPrefix + "/list",
method: "post",
params: query,
});
initPlugins(plugins);
return plugins;
}
export async function GetGroups(query: any) {
const groups = await request({
url: apiPrefix + "/groups",
method: "post",
params: query,
});
const plugins: any = [];
for (const groupKey in groups) {
plugins.push(...groups[groupKey].plugins);
}
initPlugins(plugins);
return groups;
}
export async function GetPluginDefine(type: string) {
const define = await request({
url: apiPrefix + "/getDefineByType",
method: "post",
data: {
type,
},
});
initPlugins([define]);
return define;
}
export async function GetPluginConfig(req: { id?: number; name: string; type: string }): Promise<PluginConfigBean> {
return await request({
url: apiPrefix + "/config",
method: "post",
data: req,
});
}
@@ -0,0 +1,161 @@
import { defineStore } from "pinia";
import * as api from "./api.plugin";
import { DynamicType, FormItemProps } from "@fast-crud/fast-crud";
interface PluginState {
group?: PluginGroups;
}
export type PluginGroup = {
key: string;
title: string;
desc?: string;
order: number;
icon: string;
plugins: any[];
};
export type PluginDefine = {
name: string;
title: string;
desc?: string;
shortcut: any;
input: {
[key: string]: DynamicType<FormItemProps>;
};
output: {
[key: string]: any;
};
};
export class PluginGroups {
groups!: { [key: string]: PluginGroup };
map!: { [key: string]: PluginDefine };
constructor(groups: { [key: string]: PluginGroup }) {
this.groups = groups;
this.initGroup(groups);
this.initMap();
}
private initGroup(groups: { [p: string]: PluginGroup }) {
const all: PluginGroup = {
key: "all",
title: "全部",
order: 0,
plugins: [],
icon: "material-symbols:border-all-rounded",
};
for (const key in groups) {
all.plugins.push(...groups[key].plugins);
}
this.groups = {
all,
...groups,
};
}
initMap() {
const map: { [key: string]: PluginDefine } = {};
for (const key in this.groups) {
const group = this.groups[key];
for (const plugin of group.plugins) {
map[plugin.name] = plugin;
}
}
this.map = map;
}
getGroups() {
return this.groups;
}
get(name: string) {
return this.map[name];
}
getPreStepOutputOptions({ pipeline, currentStageIndex, currentTaskIndex, currentStepIndex, currentTask }: any) {
const steps = this.collectionPreStepOutputs({
pipeline,
currentStageIndex,
currentTaskIndex,
currentStepIndex,
currentTask,
});
const options: any[] = [];
for (const step of steps) {
const stepDefine = this.get(step.type);
for (const key in stepDefine?.output) {
options.push({
value: `step.${step.id}.${key}`,
label: `${stepDefine.output[key].title}【from${step.title}`,
type: step.type,
});
}
}
return options;
}
collectionPreStepOutputs({ pipeline, currentStageIndex, currentTaskIndex, currentStepIndex, currentTask }: any) {
const steps: any[] = [];
// 开始放step
for (let i = 0; i < currentStageIndex; i++) {
const stage = pipeline.stages[i];
for (const task of stage.tasks) {
for (const step of task.steps) {
steps.push(step);
}
}
}
//当前阶段之前的task
const currentStage = pipeline.stages[currentStageIndex];
for (let i = 0; i < currentTaskIndex; i++) {
const task = currentStage.tasks[i];
for (const step of task.steps) {
steps.push(step);
}
}
//放当前任务下的step
for (let i = 0; i < currentStepIndex; i++) {
const step = currentTask.steps[i];
steps.push(step);
}
return steps;
}
}
export const usePluginStore = defineStore({
id: "app.plugin",
state: (): PluginState => ({
group: null,
}),
actions: {
async reload() {
const groups = await api.GetGroups({});
this.group = new PluginGroups(groups);
},
async init() {
if (!this.group) {
await this.reload();
}
return this.group;
},
async getGroups(): Promise<PluginGroups> {
await this.init();
return this.group as PluginGroups;
},
async clear() {
this.group = null;
},
async getList(): Promise<PluginDefine[]> {
await this.init();
return this.group.groups.all.plugins;
},
async getPluginDefine(name: string): Promise<PluginDefine> {
await this.init();
return this.group.get(name);
},
async getPluginConfig(query: any) {
return await api.GetPluginConfig(query);
},
},
});
@@ -0,0 +1,111 @@
import { request } from "/src/api/service";
export type SiteEnv = {
agent?: {
enabled?: boolean;
contactText?: string;
contactLink?: string;
};
};
export type AppInfo = {
version?: string;
time?: number;
deltaTime?: number;
};
export type SiteInfo = {
title?: string;
slogan?: string;
logo?: string;
loginLogo?: string;
icpNo?: string;
licenseTo?: string;
licenseToUrl?: string;
};
export type PlusInfo = {
vipType?: string;
expireTime?: number;
isPlus: boolean;
isComm?: boolean;
};
export type SysPublicSetting = {
registerEnabled?: boolean;
usernameRegisterEnabled?: boolean;
mobileRegisterEnabled?: boolean;
emailRegisterEnabled?: boolean;
passwordLoginEnabled?: boolean;
smsLoginEnabled?: boolean;
limitUserPipelineCount?: number;
managerOtherUserPipeline?: boolean;
icpNo?: string;
robots?: boolean;
};
export type SuiteSetting = {
enabled?: boolean;
};
export type SysPrivateSetting = {
httpProxy?: string;
httpsProxy?: string;
dnsResultOrder?: string;
commonCnameEnabled?: boolean;
sms?: {
type?: string;
config?: any;
};
};
export type SysInstallInfo = {
siteId: string;
};
export type MenuItem = {
id: string;
title: string;
icon?: string;
path?: string;
children?: MenuItem[];
};
export type HeaderMenus = {
menus: MenuItem[];
};
export type AllSettings = {
sysPublic: SysPublicSetting;
installInfo: SysInstallInfo;
plusInfo: PlusInfo;
siteInfo: SiteInfo;
siteEnv: SiteEnv;
headerMenus: HeaderMenus;
suiteSetting: SuiteSetting;
app: AppInfo;
};
export async function loadAllSettings(): Promise<AllSettings> {
return await request({
url: "/basic/settings/all",
method: "get",
});
}
export async function bindUrl(data: any): Promise<any> {
return await request({
url: "/sys/plus/bindUrl",
method: "post",
data,
});
}
export async function sendSmsCode(data: any): Promise<any> {
return await request({
url: "/basic/code/sendSmsCode",
method: "post",
data,
});
}
export async function sendEmailCode(data: any): Promise<any> {
return await request({
url: "/basic/code/sendEmailCode",
method: "post",
data,
});
}
@@ -1,9 +1,9 @@
import { defineStore } from "pinia";
import { Modal, notification } from "ant-design-vue";
import * as _ from "lodash-es";
import * as basicApi from "/@/api/modules/api.basic";
import { AppInfo, HeaderMenus, PlusInfo, SiteEnv, SiteInfo, SuiteSetting, SysInstallInfo, SysPublicSetting } from "/@/api/modules/api.basic";
import { useUserStore } from "/@/store/modules/user";
import * as basicApi from "./api.basic";
import { AppInfo, HeaderMenus, PlusInfo, SiteEnv, SiteInfo, SuiteSetting, SysInstallInfo, SysPublicSetting } from "./api.basic";
import { useUserStore } from "../user";
import { mitter } from "/@/utils/util.mitt";
import { env } from "/@/utils/util.env";
import { updatePreferences } from "/@/vben/preferences";
@@ -0,0 +1,68 @@
import { request } from "/src/api/service";
export interface RegisterReq {
username: string;
password: string;
confirmPassword: string;
}
/**
* @description: Login interface parameters
*/
export interface LoginReq {
username: string;
password: string;
}
export interface SmsLoginReq {
mobile: string;
phoneCode: string;
smsCode: string;
randomStr: string;
}
export interface UserInfoRes {
id: string | number;
username: string;
nickName: string;
avatar?: string;
roleIds: number[];
isWeak?: boolean;
}
export interface LoginRes {
token: string;
expire: number;
}
export async function register(user: RegisterReq): Promise<UserInfoRes> {
return await request({
url: "/register",
method: "post",
data: user,
});
}
export async function login(data: LoginReq): Promise<LoginRes> {
//如果开启了登录与权限模块,则真实登录
return await request({
url: "/login",
method: "post",
data,
});
}
export async function loginBySms(data: SmsLoginReq): Promise<LoginRes> {
//如果开启了登录与权限模块,则真实登录
return await request({
url: "/loginBySms",
method: "post",
data,
});
}
export async function mine(): Promise<UserInfoRes> {
return await request({
url: "/mine/info",
method: "post",
});
}
@@ -3,8 +3,8 @@ import router from "../../router";
// @ts-ignore
import { LocalStorage } from "/src/utils/util.storage";
// @ts-ignore
import * as UserApi from "/src/api/modules/api.user";
import { RegisterReq, SmsLoginReq } from "/src/api/modules/api.user";
import * as UserApi from "./api.user";
import { RegisterReq, SmsLoginReq } from "./api.user";
// @ts-ignore
import { LoginReq, UserInfoRes } from "/@/api/modules/api.user";
import { message, Modal, notification } from "ant-design-vue";
@@ -28,7 +28,7 @@ export const useUserStore = defineStore({
// user info
userInfo: null,
// token
token: undefined
token: undefined,
}),
getters: {
getUserInfo(): UserInfoRes {
@@ -39,7 +39,7 @@ export const useUserStore = defineStore({
},
isAdmin(): boolean {
return this.getUserInfo.roleIds?.includes(1) || this.getUserInfo.id === 1;
}
},
},
actions: {
setToken(token: string, expire: number) {
@@ -63,7 +63,7 @@ export const useUserStore = defineStore({
async register(user: RegisterReq) {
await UserApi.register(user);
notification.success({
message: "注册成功,请登录"
message: "注册成功,请登录",
});
await router.replace("/login");
},
@@ -127,8 +127,8 @@ export const useUserStore = defineStore({
content: t("app.login.logoutMessage"),
onOk: async () => {
await this.logout(true);
}
},
});
}
}
},
},
});