多租户平台
This commit is contained in:
60
src/plugins/auth.ts
Normal file
60
src/plugins/auth.ts
Normal file
@ -0,0 +1,60 @@
|
||||
import { useUserStore } from '@/store/modules/user';
|
||||
|
||||
const authPermission = (permission: string): boolean => {
|
||||
const all_permission = '*:*:*';
|
||||
const permissions: string[] = useUserStore().permissions;
|
||||
if (permission && permission.length > 0) {
|
||||
return permissions.some((v) => {
|
||||
return all_permission === v || v === permission;
|
||||
});
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const authRole = (role: string): boolean => {
|
||||
const super_admin = 'admin';
|
||||
const roles = useUserStore().roles;
|
||||
if (role && role.length > 0) {
|
||||
return roles.some((v) => {
|
||||
return super_admin === v || v === role;
|
||||
});
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
// 验证用户是否具备某权限
|
||||
hasPermi(permission: string): boolean {
|
||||
return authPermission(permission);
|
||||
},
|
||||
// 验证用户是否含有指定权限,只需包含其中一个
|
||||
hasPermiOr(permissions: string[]): boolean {
|
||||
return permissions.some((item) => {
|
||||
return authPermission(item);
|
||||
});
|
||||
},
|
||||
// 验证用户是否含有指定权限,必须全部拥有
|
||||
hasPermiAnd(permissions: string[]): boolean {
|
||||
return permissions.every((item) => {
|
||||
return authPermission(item);
|
||||
});
|
||||
},
|
||||
// 验证用户是否具备某角色
|
||||
hasRole(role: string): boolean {
|
||||
return authRole(role);
|
||||
},
|
||||
// 验证用户是否含有指定角色,只需包含其中一个
|
||||
hasRoleOr(roles: string[]): boolean {
|
||||
return roles.some((item) => {
|
||||
return authRole(item);
|
||||
});
|
||||
},
|
||||
// 验证用户是否含有指定角色,必须全部拥有
|
||||
hasRoleAnd(roles: string[]): boolean {
|
||||
return roles.every((item) => {
|
||||
return authRole(item);
|
||||
});
|
||||
}
|
||||
};
|
79
src/plugins/cache.ts
Normal file
79
src/plugins/cache.ts
Normal file
@ -0,0 +1,79 @@
|
||||
const sessionCache = {
|
||||
set(key: string, value: any) {
|
||||
if (!sessionStorage) {
|
||||
return;
|
||||
}
|
||||
if (key != null && value != null) {
|
||||
sessionStorage.setItem(key, value);
|
||||
}
|
||||
},
|
||||
get(key: string) {
|
||||
if (!sessionStorage) {
|
||||
return null;
|
||||
}
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
return sessionStorage.getItem(key);
|
||||
},
|
||||
setJSON(key: string, jsonValue: any) {
|
||||
if (jsonValue != null) {
|
||||
this.set(key, JSON.stringify(jsonValue));
|
||||
}
|
||||
},
|
||||
getJSON(key: string) {
|
||||
const value = this.get(key);
|
||||
if (value != null) {
|
||||
return JSON.parse(value);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
remove(key: string) {
|
||||
sessionStorage.removeItem(key);
|
||||
}
|
||||
};
|
||||
const localCache = {
|
||||
set(key: string, value: any) {
|
||||
if (!localStorage) {
|
||||
return;
|
||||
}
|
||||
if (key != null && value != null) {
|
||||
localStorage.setItem(key, value);
|
||||
}
|
||||
},
|
||||
get(key: string) {
|
||||
if (!localStorage) {
|
||||
return null;
|
||||
}
|
||||
if (key == null) {
|
||||
return null;
|
||||
}
|
||||
return localStorage.getItem(key);
|
||||
},
|
||||
setJSON(key: string, jsonValue: any) {
|
||||
if (jsonValue != null) {
|
||||
this.set(key, JSON.stringify(jsonValue));
|
||||
}
|
||||
},
|
||||
getJSON(key: string) {
|
||||
const value = this.get(key);
|
||||
if (value != null) {
|
||||
return JSON.parse(value);
|
||||
}
|
||||
return null;
|
||||
},
|
||||
remove(key: string) {
|
||||
localStorage.removeItem(key);
|
||||
}
|
||||
};
|
||||
|
||||
export default {
|
||||
/**
|
||||
* 会话级缓存
|
||||
*/
|
||||
session: sessionCache,
|
||||
/**
|
||||
* 本地缓存
|
||||
*/
|
||||
local: localCache
|
||||
};
|
65
src/plugins/download.ts
Normal file
65
src/plugins/download.ts
Normal file
@ -0,0 +1,65 @@
|
||||
import axios from 'axios';
|
||||
import FileSaver from 'file-saver';
|
||||
import errorCode from '@/utils/errorCode';
|
||||
import { blobValidate } from '@/utils/ruoyi';
|
||||
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading';
|
||||
import { globalHeaders } from '@/utils/request';
|
||||
|
||||
const baseURL = import.meta.env.VITE_APP_BASE_API;
|
||||
let downloadLoadingInstance: LoadingInstance;
|
||||
export default {
|
||||
async oss(ossId: string | number) {
|
||||
const url = baseURL + '/resource/oss/download/' + ossId;
|
||||
downloadLoadingInstance = ElLoading.service({ text: '正在下载数据,请稍候', background: 'rgba(0, 0, 0, 0.7)' });
|
||||
try {
|
||||
const res = await axios({
|
||||
method: 'get',
|
||||
url: url,
|
||||
responseType: 'blob',
|
||||
headers: globalHeaders()
|
||||
});
|
||||
const isBlob = blobValidate(res.data);
|
||||
if (isBlob) {
|
||||
const blob = new Blob([res.data], { type: 'application/octet-stream' });
|
||||
FileSaver.saveAs(blob, decodeURIComponent(res.headers['download-filename'] as string));
|
||||
} else {
|
||||
this.printErrMsg(res.data);
|
||||
}
|
||||
downloadLoadingInstance.close();
|
||||
} catch (r) {
|
||||
console.error(r);
|
||||
ElMessage.error('下载文件出现错误,请联系管理员!');
|
||||
downloadLoadingInstance.close();
|
||||
}
|
||||
},
|
||||
async zip(url: string, name: string) {
|
||||
url = baseURL + url;
|
||||
downloadLoadingInstance = ElLoading.service({ text: '正在下载数据,请稍候', background: 'rgba(0, 0, 0, 0.7)' });
|
||||
try {
|
||||
const res = await axios({
|
||||
method: 'get',
|
||||
url: url,
|
||||
responseType: 'blob',
|
||||
headers: globalHeaders()
|
||||
});
|
||||
const isBlob = blobValidate(res.data);
|
||||
if (isBlob) {
|
||||
const blob = new Blob([res.data], { type: 'application/zip' });
|
||||
FileSaver.saveAs(blob, name);
|
||||
} else {
|
||||
this.printErrMsg(res.data);
|
||||
}
|
||||
downloadLoadingInstance.close();
|
||||
} catch (r) {
|
||||
console.error(r);
|
||||
ElMessage.error('下载文件出现错误,请联系管理员!');
|
||||
downloadLoadingInstance.close();
|
||||
}
|
||||
},
|
||||
async printErrMsg(data: any) {
|
||||
const resText = await data.text();
|
||||
const rspObj = JSON.parse(resText);
|
||||
const errMsg = errorCode[rspObj.code] || rspObj.msg || errorCode['default'];
|
||||
ElMessage.error(errMsg);
|
||||
}
|
||||
};
|
43
src/plugins/index.ts
Normal file
43
src/plugins/index.ts
Normal file
@ -0,0 +1,43 @@
|
||||
import modal from './modal';
|
||||
import tab from './tab';
|
||||
import download from './download';
|
||||
import cache from './cache';
|
||||
import auth from './auth';
|
||||
// 预设动画
|
||||
import animate from '@/animate';
|
||||
|
||||
import { download as dl } from '@/utils/request';
|
||||
import { useDict } from '@/utils/dict';
|
||||
import { getConfigKey, updateConfigByKey } from '@/api/system/config';
|
||||
import { parseTime, addDateRange, handleTree, selectDictLabel, selectDictLabels } from '@/utils/ruoyi';
|
||||
|
||||
import { App } from 'vue';
|
||||
|
||||
export default function installPlugin(app: App) {
|
||||
// 页签操作
|
||||
app.config.globalProperties.$tab = tab;
|
||||
|
||||
// 模态框对象
|
||||
app.config.globalProperties.$modal = modal;
|
||||
|
||||
// 缓存对象
|
||||
app.config.globalProperties.$cache = cache;
|
||||
|
||||
// 下载文件
|
||||
app.config.globalProperties.$download = download;
|
||||
|
||||
// 认证对象
|
||||
app.config.globalProperties.$auth = auth;
|
||||
|
||||
// 全局方法挂载
|
||||
app.config.globalProperties.useDict = useDict;
|
||||
app.config.globalProperties.getConfigKey = getConfigKey;
|
||||
app.config.globalProperties.updateConfigByKey = updateConfigByKey;
|
||||
app.config.globalProperties.download = dl;
|
||||
app.config.globalProperties.parseTime = parseTime;
|
||||
app.config.globalProperties.handleTree = handleTree;
|
||||
app.config.globalProperties.addDateRange = addDateRange;
|
||||
app.config.globalProperties.selectDictLabel = selectDictLabel;
|
||||
app.config.globalProperties.selectDictLabels = selectDictLabels;
|
||||
app.config.globalProperties.animate = animate;
|
||||
}
|
81
src/plugins/modal.ts
Normal file
81
src/plugins/modal.ts
Normal file
@ -0,0 +1,81 @@
|
||||
import { MessageBoxData } from 'element-plus';
|
||||
import { LoadingInstance } from 'element-plus/es/components/loading/src/loading';
|
||||
let loadingInstance: LoadingInstance;
|
||||
export default {
|
||||
// 消息提示
|
||||
msg(content: any) {
|
||||
ElMessage.info(content);
|
||||
},
|
||||
// 错误消息
|
||||
msgError(content: any) {
|
||||
ElMessage.error(content);
|
||||
},
|
||||
// 成功消息
|
||||
msgSuccess(content: any) {
|
||||
ElMessage.success(content);
|
||||
},
|
||||
// 警告消息
|
||||
msgWarning(content: any) {
|
||||
ElMessage.warning(content);
|
||||
},
|
||||
// 弹出提示
|
||||
alert(content: any) {
|
||||
ElMessageBox.alert(content, '系统提示');
|
||||
},
|
||||
// 错误提示
|
||||
alertError(content: any) {
|
||||
ElMessageBox.alert(content, '系统提示', { type: 'error' });
|
||||
},
|
||||
// 成功提示
|
||||
alertSuccess(content: any) {
|
||||
ElMessageBox.alert(content, '系统提示', { type: 'success' });
|
||||
},
|
||||
// 警告提示
|
||||
alertWarning(content: any) {
|
||||
ElMessageBox.alert(content, '系统提示', { type: 'warning' });
|
||||
},
|
||||
// 通知提示
|
||||
notify(content: any) {
|
||||
ElNotification.info(content);
|
||||
},
|
||||
// 错误通知
|
||||
notifyError(content: any) {
|
||||
ElNotification.error(content);
|
||||
},
|
||||
// 成功通知
|
||||
notifySuccess(content: any) {
|
||||
ElNotification.success(content);
|
||||
},
|
||||
// 警告通知
|
||||
notifyWarning(content: any) {
|
||||
ElNotification.warning(content);
|
||||
},
|
||||
// 确认窗体
|
||||
confirm(content: any): Promise<MessageBoxData> {
|
||||
return ElMessageBox.confirm(content, '系统提示', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
});
|
||||
},
|
||||
// 提交内容
|
||||
prompt(content: any) {
|
||||
return ElMessageBox.prompt(content, '系统提示', {
|
||||
confirmButtonText: '确定',
|
||||
cancelButtonText: '取消',
|
||||
type: 'warning'
|
||||
});
|
||||
},
|
||||
// 打开遮罩层
|
||||
loading(content: string) {
|
||||
loadingInstance = ElLoading.service({
|
||||
lock: true,
|
||||
text: content,
|
||||
background: 'rgba(0, 0, 0, 0.7)'
|
||||
});
|
||||
},
|
||||
// 关闭遮罩层
|
||||
closeLoading() {
|
||||
loadingInstance.close();
|
||||
}
|
||||
};
|
10
src/plugins/svgicon.ts
Normal file
10
src/plugins/svgicon.ts
Normal file
@ -0,0 +1,10 @@
|
||||
import * as ElementPlusIconsVue from '@element-plus/icons-vue';
|
||||
import { App } from 'vue';
|
||||
|
||||
export default {
|
||||
install: (app: App) => {
|
||||
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
|
||||
app.component(key, component);
|
||||
}
|
||||
}
|
||||
};
|
96
src/plugins/tab.ts
Normal file
96
src/plugins/tab.ts
Normal file
@ -0,0 +1,96 @@
|
||||
import router from '@/router';
|
||||
import { RouteLocationMatched, RouteLocationNormalized, RouteLocationRaw } from 'vue-router';
|
||||
import { useTagsViewStore } from '@/store/modules/tagsView';
|
||||
|
||||
export default {
|
||||
/**
|
||||
* 刷新当前tab页签
|
||||
* @param obj 标签对象
|
||||
*/
|
||||
async refreshPage(obj?: RouteLocationNormalized): Promise<void> {
|
||||
const { path, query, matched } = router.currentRoute.value;
|
||||
if (obj === undefined) {
|
||||
matched.forEach((m: RouteLocationMatched) => {
|
||||
if (m.components && m.components.default && m.components.default.name) {
|
||||
if (!['Layout', 'ParentView'].includes(m.components.default.name)) {
|
||||
obj = {
|
||||
name: m.components.default.name,
|
||||
path: path,
|
||||
query: query,
|
||||
matched: undefined,
|
||||
fullPath: undefined,
|
||||
hash: undefined,
|
||||
params: undefined,
|
||||
redirectedFrom: undefined,
|
||||
meta: undefined
|
||||
};
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
let query1: undefined | {} = {};
|
||||
let path1: undefined | string = '';
|
||||
if (obj) {
|
||||
query1 = obj.query;
|
||||
path1 = obj.path;
|
||||
}
|
||||
await useTagsViewStore().delCachedView(obj);
|
||||
await router.replace({
|
||||
path: '/redirect' + path1,
|
||||
query: query1
|
||||
});
|
||||
},
|
||||
// 关闭当前tab页签,打开新页签
|
||||
closeOpenPage(obj: RouteLocationRaw): void {
|
||||
useTagsViewStore().delView(router.currentRoute.value);
|
||||
if (obj !== undefined) {
|
||||
router.push(obj);
|
||||
}
|
||||
},
|
||||
// 关闭指定tab页签
|
||||
async closePage(obj?: RouteLocationNormalized): Promise<{ visitedViews: RouteLocationNormalized[]; cachedViews: string[] } | any> {
|
||||
if (obj === undefined) {
|
||||
// prettier-ignore
|
||||
const { visitedViews } = await useTagsViewStore().delView(router.currentRoute.value)
|
||||
const latestView = visitedViews.slice(-1)[0];
|
||||
if (latestView) {
|
||||
return router.push(latestView.fullPath);
|
||||
}
|
||||
return router.push('/');
|
||||
}
|
||||
return useTagsViewStore().delView(obj);
|
||||
},
|
||||
// 关闭所有tab页签
|
||||
closeAllPage() {
|
||||
return useTagsViewStore().delAllViews();
|
||||
},
|
||||
// 关闭左侧tab页签
|
||||
closeLeftPage(obj?: RouteLocationNormalized) {
|
||||
return useTagsViewStore().delLeftTags(obj || router.currentRoute.value);
|
||||
},
|
||||
// 关闭右侧tab页签
|
||||
closeRightPage(obj?: RouteLocationNormalized) {
|
||||
return useTagsViewStore().delRightTags(obj || router.currentRoute.value);
|
||||
},
|
||||
// 关闭其他tab页签
|
||||
closeOtherPage(obj?: RouteLocationNormalized) {
|
||||
return useTagsViewStore().delOthersViews(obj || router.currentRoute.value);
|
||||
},
|
||||
/**
|
||||
* 打开tab页签
|
||||
* @param url 路由地址
|
||||
* @param title 标题
|
||||
* @param query 参数
|
||||
*/
|
||||
openPage(url: string, title?: string, query?: any) {
|
||||
const obj = { path: url, query: { ...query, title } };
|
||||
return router.push(obj);
|
||||
},
|
||||
/**
|
||||
* 修改tab页签
|
||||
* @param obj 标签对象
|
||||
*/
|
||||
updatePage(obj: RouteLocationNormalized) {
|
||||
return useTagsViewStore().updateVisitedView(obj);
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user