index.ts 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
  2. import type { App, Plugin } from 'vue';
  3. import { unref } from 'vue';
  4. import { isObject } from '/@/utils/is';
  5. // update-begin--author:sunjianlei---date:20220408---for: 【VUEN-656】配置外部网址打不开,原因是带了#号,需要替换一下
  6. export const URL_HASH_TAB = `__AGWE4H__HASH__TAG__PWHRG__`;
  7. // update-end--author:sunjianlei---date:20220408---for: 【VUEN-656】配置外部网址打不开,原因是带了#号,需要替换一下
  8. export const noop = () => {};
  9. /**
  10. * @description: Set ui mount node
  11. */
  12. export function getPopupContainer(node?: HTMLElement): HTMLElement {
  13. return (node?.parentNode as HTMLElement) ?? document.body;
  14. }
  15. /**
  16. * Add the object as a parameter to the URL
  17. * @param baseUrl url
  18. * @param obj
  19. * @returns {string}
  20. * eg:
  21. * let obj = {a: '3', b: '4'}
  22. * setObjToUrlParams('www.baidu.com', obj)
  23. * ==>www.baidu.com?a=3&b=4
  24. */
  25. export function setObjToUrlParams(baseUrl: string, obj: any): string {
  26. let parameters = '';
  27. for (const key in obj) {
  28. parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
  29. }
  30. parameters = parameters.replace(/&$/, '');
  31. return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
  32. }
  33. export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  34. let key: string;
  35. for (key in target) {
  36. src[key] = isObject(src[key]) ? deepMerge(src[key], target[key]) : (src[key] = target[key]);
  37. }
  38. return src;
  39. }
  40. export function openWindow(url: string, opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean }) {
  41. const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
  42. const feature: string[] = [];
  43. noopener && feature.push('noopener=yes');
  44. noreferrer && feature.push('noreferrer=yes');
  45. window.open(url, target, feature.join(','));
  46. }
  47. // dynamic use hook props
  48. export function getDynamicProps<T, U>(props: T): Partial<U> {
  49. const ret: Recordable = {};
  50. Object.keys(props).map((key) => {
  51. ret[key] = unref((props as Recordable)[key]);
  52. });
  53. return ret as Partial<U>;
  54. }
  55. /**
  56. * 获取表单字段值数据类型
  57. * @param props
  58. * @param field
  59. * @updateBy:zyf
  60. */
  61. export function getValueType(props, field) {
  62. const formSchema = unref(unref(props)?.schemas);
  63. let valueType = 'string';
  64. if (formSchema) {
  65. const schema = formSchema.filter((item) => item.field === field)[0];
  66. valueType = schema.componentProps && schema.componentProps.valueType ? schema.componentProps.valueType : valueType;
  67. }
  68. return valueType;
  69. }
  70. export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
  71. if (!route) return route;
  72. const { matched, ...opt } = route;
  73. return {
  74. ...opt,
  75. matched: (matched
  76. ? matched.map((item) => ({
  77. meta: item.meta,
  78. name: item.name,
  79. path: item.path,
  80. }))
  81. : undefined) as RouteRecordNormalized[],
  82. };
  83. }
  84. /**
  85. * 深度克隆对象、数组
  86. * @param obj 被克隆的对象
  87. * @return 克隆后的对象
  88. */
  89. export function cloneObject(obj) {
  90. return JSON.parse(JSON.stringify(obj));
  91. }
  92. export const withInstall = <T>(component: T, alias?: string) => {
  93. console.log('---初始化---', component);
  94. const comp = component as any;
  95. comp.install = (app: App) => {
  96. app.component(comp.name || comp.displayName, component);
  97. if (alias) {
  98. app.config.globalProperties[alias] = component;
  99. }
  100. };
  101. return component as T & Plugin;
  102. };
  103. /**
  104. * 获取url地址参数
  105. * @param paraName
  106. */
  107. export function getUrlParam(paraName) {
  108. const url = document.location.toString();
  109. const arrObj = url.split('?');
  110. if (arrObj.length > 1) {
  111. const arrPara = arrObj[1].split('&');
  112. let arr;
  113. for (let i = 0; i < arrPara.length; i++) {
  114. arr = arrPara[i].split('=');
  115. if (arr != null && arr[0] == paraName) {
  116. return arr[1];
  117. }
  118. }
  119. return '';
  120. } else {
  121. return '';
  122. }
  123. }
  124. /**
  125. * 休眠(setTimeout的promise版)
  126. * @param ms 要休眠的时间,单位:毫秒
  127. * @param fn callback,可空
  128. * @return Promise
  129. */
  130. export function sleep(ms: number, fn?: Fn) {
  131. return new Promise<void>((resolve) =>
  132. setTimeout(() => {
  133. fn && fn();
  134. resolve();
  135. }, ms)
  136. );
  137. }
  138. /**
  139. * 不用正则的方式替换所有值
  140. * @param text 被替换的字符串
  141. * @param checker 替换前的内容
  142. * @param replacer 替换后的内容
  143. * @returns {String} 替换后的字符串
  144. */
  145. export function replaceAll(text, checker, replacer) {
  146. const lastText = text;
  147. text = text.replace(checker, replacer);
  148. if (lastText !== text) {
  149. return replaceAll(text, checker, replacer);
  150. }
  151. return text;
  152. }
  153. /**
  154. * 获取URL上参数
  155. * @param url
  156. */
  157. export function getQueryVariable(url) {
  158. if (!url) return;
  159. let t,
  160. n,
  161. r,
  162. i = url.split('?')[1],
  163. s = {};
  164. (t = i.split('&')), (r = null), (n = null);
  165. for (const o in t) {
  166. const u = t[o].indexOf('=');
  167. u !== -1 && ((r = t[o].substr(0, u)), (n = t[o].substr(u + 1)), (s[r] = n));
  168. }
  169. return s;
  170. }
  171. /**
  172. * 判断是否显示办理按钮
  173. * @param bpmStatus
  174. * @returns {*}
  175. */
  176. export function showDealBtn(bpmStatus) {
  177. if (bpmStatus != '1' && bpmStatus != '3' && bpmStatus != '4') {
  178. return true;
  179. }
  180. return false;
  181. }
  182. /**
  183. * 数字转大写
  184. * @param value
  185. * @returns {*}
  186. */
  187. export function numToUpper(value) {
  188. if (value != '') {
  189. const unit = ['仟', '佰', '拾', '', '仟', '佰', '拾', '', '角', '分'];
  190. const toDx = (n) => {
  191. switch (n) {
  192. case '0':
  193. return '零';
  194. case '1':
  195. return '壹';
  196. case '2':
  197. return '贰';
  198. case '3':
  199. return '叁';
  200. case '4':
  201. return '肆';
  202. case '5':
  203. return '伍';
  204. case '6':
  205. return '陆';
  206. case '7':
  207. return '柒';
  208. case '8':
  209. return '捌';
  210. case '9':
  211. return '玖';
  212. }
  213. };
  214. const lth = value.toString().length;
  215. value *= 100;
  216. value += '';
  217. const length = value.length;
  218. if (lth <= 8) {
  219. let result = '';
  220. for (let i = 0; i < length; i++) {
  221. if (i == 2) {
  222. result = '元' + result;
  223. } else if (i == 6) {
  224. result = '万' + result;
  225. }
  226. if (value.charAt(length - i - 1) == 0) {
  227. if (i != 0 && i != 1) {
  228. if (result.charAt(0) != '零' && result.charAt(0) != '元' && result.charAt(0) != '万') {
  229. result = '零' + result;
  230. }
  231. }
  232. continue;
  233. }
  234. result = toDx(value.charAt(length - i - 1)) + unit[unit.length - i - 1] + result;
  235. }
  236. result += result.charAt(result.length - 1) == '元' ? '整' : '';
  237. return result;
  238. } else {
  239. return null;
  240. }
  241. }
  242. return null;
  243. }
  244. //update-begin-author:taoyan date:2022-6-8 for:解决老的vue2动态导入文件语法 vite不支持的问题
  245. const allModules = import.meta.glob('../views/**/*.vue');
  246. export function importViewsFile(path): Promise<any> {
  247. if (path.startsWith('/')) {
  248. path = path.substring(1);
  249. }
  250. let page = '';
  251. if (path.endsWith('.vue')) {
  252. page = `../views/${path}`;
  253. } else {
  254. page = `../views/${path}.vue`;
  255. }
  256. return new Promise((resolve, reject) => {
  257. let flag = true;
  258. for (const path in allModules) {
  259. if (path == page) {
  260. flag = false;
  261. allModules[path]().then((mod) => {
  262. console.log(path, mod);
  263. resolve(mod);
  264. });
  265. }
  266. }
  267. if (flag) {
  268. reject('该文件不存在:' + page);
  269. }
  270. });
  271. }
  272. //update-end-author:taoyan date:2022-6-8 for:解决老的vue2动态导入文件语法 vite不支持的问题
  273. /**
  274. * 跳转至积木报表的 预览页面
  275. * @param url
  276. * @param id
  277. * @param token
  278. */
  279. export function goJmReportViewPage(url, id, token) {
  280. // update-begin--author:liaozhiyang---date:20230904---for:【QQYUN-6390】eval替换成new Function,解决build警告
  281. // URL支持{{ window.xxx }}占位符变量
  282. url = url.replace(/{{([^}]+)?}}/g, (_s1, s2) => _eval(s2));
  283. // update-end--author:liaozhiyang---date:20230904---for:【QQYUN-6390】eval替换成new Function,解决build警告
  284. if (url.includes('?')) {
  285. url += '&';
  286. } else {
  287. url += '?';
  288. }
  289. url += `id=${id}`;
  290. url += `&token=${token}`;
  291. window.open(url);
  292. }
  293. // 防抖截流
  294. export function debounce(delay, callback) {
  295. let task;
  296. return function () {
  297. clearTimeout(task);
  298. task = setTimeout(() => {
  299. callback.apply(this, arguments);
  300. }, delay);
  301. };
  302. }
  303. export function setRem() {
  304. // 默认使用100px作为基准大小
  305. const baseSize = 100;
  306. const baseVal = baseSize / 1920;
  307. const vW = window.innerWidth; // 当前窗口的宽度
  308. const rem = vW * baseVal; // 以默认比例值乘以当前窗口宽度,得到该宽度下的相应font-size值
  309. window.$size = rem / 100;
  310. document.documentElement.style.fontSize = rem + 'px';
  311. }
  312. /**
  313. * 获取随机颜色
  314. */
  315. export function getRandomColor(index?) {
  316. const colors = [
  317. 'rgb(100, 181, 246)',
  318. 'rgb(77, 182, 172)',
  319. 'rgb(255, 183, 77)',
  320. 'rgb(229, 115, 115)',
  321. 'rgb(149, 117, 205)',
  322. 'rgb(161, 136, 127)',
  323. 'rgb(144, 164, 174)',
  324. 'rgb(77, 208, 225)',
  325. 'rgb(129, 199, 132)',
  326. 'rgb(255, 138, 101)',
  327. 'rgb(133, 202, 205)',
  328. 'rgb(167, 214, 118)',
  329. 'rgb(254, 225, 89)',
  330. 'rgb(251, 199, 142)',
  331. 'rgb(239, 145, 139)',
  332. 'rgb(169, 181, 255)',
  333. 'rgb(231, 218, 202)',
  334. 'rgb(252, 128, 58)',
  335. 'rgb(254, 161, 172)',
  336. 'rgb(194, 163, 205)',
  337. ];
  338. return index && index < 19 ? colors[index] : colors[Math.floor(Math.random() * (colors.length - 1))];
  339. }
  340. export function getRefPromise(componentRef) {
  341. return new Promise((resolve) => {
  342. (function next() {
  343. const ref = componentRef.value;
  344. if (ref) {
  345. resolve(ref);
  346. } else {
  347. setTimeout(() => {
  348. next();
  349. }, 100);
  350. }
  351. })();
  352. });
  353. }
  354. /**
  355. * 2023-09-04
  356. * liaozhiyang
  357. * 用new Function替换eval
  358. */
  359. export function _eval(str: string) {
  360. return new Function(`return ${str}`)();
  361. }