ny
22 小时以前 282fbc6488f4e8ceb5fda759f963ee88fbf7b999
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
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;
}