import type { UserInfo } from '@vben/types';
|
|
import { useGlobSetting, useMessage } from '@jnpf/hooks';
|
import { isNullOrUnDef, isNumber, isString } from '@jnpf/utils';
|
|
import { useAccessStore, useUserStore } from '@vben/stores';
|
|
import { cloneDeep } from 'lodash-es';
|
|
import { defHttp } from '#/api/request';
|
import { router } from '#/router';
|
|
import { APP_BACKEND_PREFIX, APP_PREFIX } from './constants';
|
|
interface OnlineUserInfo extends UserInfo {
|
token?: string;
|
}
|
|
export function getJnpfAppEnCode() {
|
let appEnCode: string = '';
|
if (window.location.pathname?.startsWith(`/${APP_PREFIX}`)) {
|
const list = window.location.pathname.split('/');
|
appEnCode = list[1] ? list[1].replace(APP_PREFIX, '') : '';
|
}
|
if (window.location.pathname?.startsWith(`/${APP_PREFIX}`.toUpperCase())) {
|
const list = window.location.pathname.split('/');
|
appEnCode = list[1] ? list[1].replace(APP_PREFIX.toUpperCase(), '') : '';
|
}
|
return appEnCode;
|
}
|
export function getRealJnpfAppEnCode() {
|
let appEnCode: string = getJnpfAppEnCode();
|
if (!appEnCode) return appEnCode;
|
if (appEnCode.startsWith(`${APP_BACKEND_PREFIX}`)) {
|
appEnCode = appEnCode.replace(APP_BACKEND_PREFIX, '');
|
}
|
if (appEnCode.startsWith(`${APP_BACKEND_PREFIX}`.toUpperCase())) {
|
appEnCode = appEnCode.replace(APP_BACKEND_PREFIX.toUpperCase(), '');
|
}
|
return appEnCode;
|
}
|
export const onlineUtils = {
|
// 获取用户信息
|
getUserInfo() {
|
const accessStore = useAccessStore();
|
const userStore = useUserStore();
|
const userInfo: OnlineUserInfo = userStore.getUserInfo as OnlineUserInfo;
|
userInfo.token = accessStore.accessToken as string;
|
return userInfo;
|
},
|
// 获取设备信息
|
getDeviceInfo() {
|
const deviceInfo = { vueVersion: '3', origin: 'pc' };
|
return deviceInfo;
|
},
|
// 请求
|
request(url: string, method: string, data = {}, headers = {}) {
|
return defHttp[method ? method.toLowerCase() : 'get']({ url, data, headers });
|
},
|
// 路由跳转
|
route(url: string) {
|
if (!url) return;
|
router.push(url);
|
},
|
// 消息提示
|
toast(message: number | string, type: string = 'info', duration: number = 3000) {
|
const { createMessage } = useMessage();
|
if (!isString(message) && !isNumber(message)) return;
|
const newDuration = duration / 1000;
|
const config = { content: message, type, duration: newDuration };
|
createMessage[type] && createMessage[type](config);
|
},
|
};
|
export function getParamList(templateJson, data?, rowKey = 'id') {
|
if (!templateJson?.length) return [];
|
for (const e of templateJson) {
|
if (e.sourceType == 1 && data) {
|
e.defaultValue = data[e.relationField] || data[e.relationField] == 0 || data[e.relationField] == false ? data[e.relationField] : '';
|
}
|
if (e.sourceType == 4 && e.relationField == '@formId') e.defaultValue = data[rowKey] || '';
|
}
|
return templateJson;
|
}
|
export function getLaunchFlowParamList(transferList, data?, rowKey = 'id') {
|
transferList = cloneDeep(transferList);
|
if (!transferList?.length) return [];
|
for (const e of transferList) {
|
if (e.sourceType == 1) {
|
if (e.sourceValue == '@formId') {
|
e.defaultValue = data[rowKey] || '';
|
} else {
|
if (e.sourceValue.includes('-')) {
|
const tableVModel = e.sourceValue.split('-')[0];
|
const childVModel = e.sourceValue.split('-')[1];
|
e.defaultValue = (data[tableVModel] || []).map((o) => o[`${childVModel}_jnpfId`]);
|
} else {
|
const key = `${e.sourceValue}_jnpfId`;
|
e.defaultValue = isNullOrUnDef(data[key]) ? (isNullOrUnDef(data[e.sourceValue]) ? '' : data[e.sourceValue]) : data[key];
|
}
|
}
|
} else {
|
e.defaultValue = e.sourceValue;
|
}
|
}
|
return transferList;
|
}
|
|
// 开始:解决老的vue2动态导入文件语法vite不支持的问题
|
const allModules: any = import.meta.glob('../views/**/*.vue');
|
export function importViewsFile(path): Promise<any> {
|
if (path.startsWith('/')) {
|
path = path.slice(1);
|
}
|
let page = '';
|
let realPage = '';
|
if (path.endsWith('.vue')) {
|
page = `../views/${path}`;
|
realPage = `../views/${path}`;
|
} else {
|
page = `../views/${path}.vue`;
|
realPage = `../views/${path}/index.vue`;
|
}
|
return new Promise((resolve, reject) => {
|
let flag = true;
|
for (const path in allModules) {
|
if (path == page || path == realPage) {
|
flag = false;
|
allModules[path]().then((mod) => {
|
resolve(mod);
|
});
|
}
|
}
|
if (flag) {
|
reject(new Error(`该文件不存在:${page}`));
|
}
|
});
|
}
|
// 结束:解决老的vue2动态导入文件语法 vite不支持的问题
|
|
export function getAuthMediaUrl(url, isRedirect = true) {
|
if (!url) return '';
|
// eslint-disable-next-line regexp/no-unused-capturing-group
|
const base64WithPrefixRegex = /^data:([a-zA-Z0-9]+\/[a-zA-Z0-9-.+]+);base64,([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{4}|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)$/;
|
if (base64WithPrefixRegex.test(url)) return url;
|
const userStore = useUserStore();
|
const userInfo: OnlineUserInfo = userStore.getUserInfo as OnlineUserInfo;
|
const securityKey = userInfo?.securityKey || '';
|
const globSetting = useGlobSetting();
|
if (!securityKey) return globSetting.apiURL + url;
|
const realUrl = `${globSetting.apiURL + url + (url.includes('?') ? '&' : '?')}s=${securityKey}${isRedirect ? '' : '&t=t'}`;
|
return realUrl;
|
}
|