index.ts 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. let formSchema = unref(unref(props)?.schemas);
  63. let valueType = 'string';
  64. if (formSchema) {
  65. let 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. const comp = component as any;
  94. comp.install = (app: App) => {
  95. app.component(comp.name || comp.displayName, component);
  96. if (alias) {
  97. app.config.globalProperties[alias] = component;
  98. }
  99. };
  100. return component as T & Plugin;
  101. };
  102. /**
  103. * 获取url地址参数
  104. * @param paraName
  105. */
  106. export function getUrlParam(paraName) {
  107. let url = document.location.toString();
  108. let arrObj = url.split('?');
  109. if (arrObj.length > 1) {
  110. let arrPara = arrObj[1].split('&');
  111. let arr;
  112. for (let i = 0; i < arrPara.length; i++) {
  113. arr = arrPara[i].split('=');
  114. if (arr != null && arr[0] == paraName) {
  115. return arr[1];
  116. }
  117. }
  118. return '';
  119. } else {
  120. return '';
  121. }
  122. }
  123. /**
  124. * 休眠(setTimeout的promise版)
  125. * @param ms 要休眠的时间,单位:毫秒
  126. * @param fn callback,可空
  127. * @return Promise
  128. */
  129. export function sleep(ms: number, fn?: Fn) {
  130. return new Promise<void>((resolve) =>
  131. setTimeout(() => {
  132. fn && fn();
  133. resolve();
  134. }, ms)
  135. );
  136. }
  137. /**
  138. * 不用正则的方式替换所有值
  139. * @param text 被替换的字符串
  140. * @param checker 替换前的内容
  141. * @param replacer 替换后的内容
  142. * @returns {String} 替换后的字符串
  143. */
  144. export function replaceAll(text, checker, replacer) {
  145. let lastText = text;
  146. text = text.replace(checker, replacer);
  147. if (lastText !== text) {
  148. return replaceAll(text, checker, replacer);
  149. }
  150. return text;
  151. }
  152. /**
  153. * 获取URL上参数
  154. * @param url
  155. */
  156. export function getQueryVariable(url) {
  157. if (!url) return;
  158. var t,
  159. n,
  160. r,
  161. i = url.split('?')[1],
  162. s = {};
  163. (t = i.split('&')), (r = null), (n = null);
  164. for (var o in t) {
  165. var u = t[o].indexOf('=');
  166. u !== -1 && ((r = t[o].substr(0, u)), (n = t[o].substr(u + 1)), (s[r] = n));
  167. }
  168. return s;
  169. }
  170. /**
  171. * 判断是否显示办理按钮
  172. * @param bpmStatus
  173. * @returns {*}
  174. */
  175. export function showDealBtn(bpmStatus) {
  176. if (bpmStatus != '1' && bpmStatus != '3' && bpmStatus != '4') {
  177. return true;
  178. }
  179. return false;
  180. }
  181. /**
  182. * 数字转大写
  183. * @param value
  184. * @returns {*}
  185. */
  186. export function numToUpper(value) {
  187. if (value != '') {
  188. let unit = new Array('仟', '佰', '拾', '', '仟', '佰', '拾', '', '角', '分');
  189. const toDx = (n) => {
  190. switch (n) {
  191. case '0':
  192. return '零';
  193. case '1':
  194. return '壹';
  195. case '2':
  196. return '贰';
  197. case '3':
  198. return '叁';
  199. case '4':
  200. return '肆';
  201. case '5':
  202. return '伍';
  203. case '6':
  204. return '陆';
  205. case '7':
  206. return '柒';
  207. case '8':
  208. return '捌';
  209. case '9':
  210. return '玖';
  211. }
  212. };
  213. let lth = value.toString().length;
  214. value *= 100;
  215. value += '';
  216. let length = value.length;
  217. if (lth <= 8) {
  218. let result = '';
  219. for (let i = 0; i < length; i++) {
  220. if (i == 2) {
  221. result = '元' + result;
  222. } else if (i == 6) {
  223. result = '万' + result;
  224. }
  225. if (value.charAt(length - i - 1) == 0) {
  226. if (i != 0 && i != 1) {
  227. if (result.charAt(0) != '零' && result.charAt(0) != '元' && result.charAt(0) != '万') {
  228. result = '零' + result;
  229. }
  230. }
  231. continue;
  232. }
  233. result = toDx(value.charAt(length - i - 1)) + unit[unit.length - i - 1] + result;
  234. }
  235. result += result.charAt(result.length - 1) == '元' ? '整' : '';
  236. return result;
  237. } else {
  238. return null;
  239. }
  240. }
  241. return null;
  242. }
  243. //update-begin-author:taoyan date:2022-6-8 for:解决老的vue2动态导入文件语法 vite不支持的问题
  244. const allModules = import.meta.glob('../views/**/*.vue');
  245. export function importViewsFile(path): Promise<any> {
  246. if (path.startsWith('/')) {
  247. path = path.substring(1);
  248. }
  249. let page = '';
  250. if (path.endsWith('.vue')) {
  251. page = `../views/${path}`;
  252. } else {
  253. page = `../views/${path}.vue`;
  254. }
  255. return new Promise((resolve, reject) => {
  256. let flag = true;
  257. for (const path in allModules) {
  258. if (path == page) {
  259. flag = false;
  260. allModules[path]().then((mod) => {
  261. console.log(path, mod);
  262. resolve(mod);
  263. });
  264. }
  265. }
  266. if (flag) {
  267. reject('该文件不存在:' + page);
  268. }
  269. });
  270. }
  271. //update-end-author:taoyan date:2022-6-8 for:解决老的vue2动态导入文件语法 vite不支持的问题
  272. /**
  273. * 跳转至积木报表的 预览页面
  274. * @param url
  275. * @param id
  276. * @param token
  277. */
  278. export function goJmReportViewPage(url, id, token) {
  279. // URL支持{{ window.xxx }}占位符变量
  280. url = url.replace(/{{([^}]+)?}}/g, (_s1, s2) => eval(s2))
  281. if (url.includes('?')) {
  282. url += '&'
  283. } else {
  284. url += '?'
  285. }
  286. url += `id=${id}`
  287. url += `&token=${token}`
  288. window.open(url)
  289. }