index.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. import { cloneDeep } from 'lodash-es';
  6. export const noop = () => {};
  7. /**
  8. * @description: Set ui mount node
  9. */
  10. export function getPopupContainer(node?: HTMLElement): HTMLElement {
  11. return (node?.parentNode as HTMLElement) ?? document.body;
  12. }
  13. /**
  14. * Add the object as a parameter to the URL
  15. * @param baseUrl url
  16. * @param obj
  17. * @returns {string}
  18. * eg:
  19. * let obj = {a: '3', b: '4'}
  20. * setObjToUrlParams('www.baidu.com', obj)
  21. * ==>www.baidu.com?a=3&b=4
  22. */
  23. export function setObjToUrlParams(baseUrl: string, obj: any): string {
  24. let parameters = '';
  25. for (const key in obj) {
  26. parameters += key + '=' + encodeURIComponent(obj[key]) + '&';
  27. }
  28. parameters = parameters.replace(/&$/, '');
  29. return /\?$/.test(baseUrl) ? baseUrl + parameters : baseUrl.replace(/\/?$/, '?') + parameters;
  30. }
  31. // 深度合并
  32. export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  33. let key: string;
  34. const res: any = cloneDeep(src);
  35. for (key in target) {
  36. res[key] = isObject(res[key]) ? deepMerge(res[key], target[key]) : (res[key] = target[key]);
  37. }
  38. return res;
  39. }
  40. export function openWindow(
  41. url: string,
  42. opt?: { target?: TargetContext | string; noopener?: boolean; noreferrer?: boolean },
  43. ) {
  44. const { target = '__blank', noopener = true, noreferrer = true } = opt || {};
  45. const feature: string[] = [];
  46. noopener && feature.push('noopener=yes');
  47. noreferrer && feature.push('noreferrer=yes');
  48. window.open(url, target, feature.join(','));
  49. }
  50. // dynamic use hook props
  51. export function getDynamicProps<T, U>(props: T): Partial<U> {
  52. const ret: Recordable = {};
  53. Object.keys(props).map(key => {
  54. ret[key] = unref((props as Recordable)[key]);
  55. });
  56. return ret as Partial<U>;
  57. }
  58. export function getRawRoute(route: RouteLocationNormalized): RouteLocationNormalized {
  59. if (!route) return route;
  60. const { matched, ...opt } = route;
  61. return {
  62. ...opt,
  63. matched: (matched
  64. ? matched.map((item: any) => ({
  65. meta: item.meta,
  66. name: item.name,
  67. path: item.path,
  68. }))
  69. : undefined) as RouteRecordNormalized[],
  70. };
  71. }
  72. export const withInstall = <T>(component: T, alias?: string) => {
  73. const comp = component as any;
  74. comp.install = (app: App) => {
  75. // console.log('🚀 ~ file: index.ts ~ line 88 ~ withInstall ~ app', app);
  76. app.component(comp.name || comp.displayName, component);
  77. if (alias) {
  78. app.config.globalProperties[alias] = component;
  79. }
  80. };
  81. return component as T & Plugin;
  82. };
  83. export function deleteEmptyChildren(treeData: Array<any>) {
  84. treeData.forEach(item => {
  85. if (item.children && item.children.length > 0) {
  86. item = deleteEmptyChildren(item.children);
  87. } else {
  88. delete item.children;
  89. }
  90. return item;
  91. });
  92. return treeData;
  93. }
  94. export function getAllParentKeys(treeData: Array<any>, key: string) {
  95. const getParentIds = function (list, id) {
  96. for (const i in list) {
  97. if (list[i].id == id) {
  98. return [];
  99. }
  100. if (list[i].children) {
  101. const ro = getParentIds(list[i].children, id);
  102. if (ro !== undefined) return ro.concat(list[i].id);
  103. }
  104. }
  105. };
  106. return getParentIds(treeData, key) || [];
  107. }
  108. // 获取字典值
  109. export function formatDictValue(options: Array<any>, value: string) {
  110. if (!value) {
  111. if (value != '0') {
  112. return '';
  113. }
  114. }
  115. if (!options) {
  116. return '';
  117. }
  118. let matchItem = '';
  119. if (typeof value == 'boolean') {
  120. matchItem = value ? '是' : '否';
  121. return matchItem;
  122. }
  123. matchItem = options.find(item => (item['value'] as string) == value);
  124. return matchItem && matchItem['label'] ? matchItem['label'] : '';
  125. }
  126. // 获取字典颜色
  127. export function formatDictColor(options: Array<any>, value: string) {
  128. if (!value) {
  129. if (value != '0') {
  130. return '';
  131. }
  132. }
  133. if (!options) {
  134. return '';
  135. }
  136. let matchItem = '';
  137. matchItem = options.find(item => item['value'] == value);
  138. return matchItem && matchItem['color'] ? matchItem['color'] : '';
  139. }
  140. // 转换字典
  141. export function transformDict(data) {
  142. return data.map(ele => {
  143. return {
  144. label: ele.dictItemName,
  145. value: ele.dictItemCode,
  146. };
  147. });
  148. }
  149. // 常规状态转换
  150. export function commonDict(data: number | boolean | string, str?: number | null) {
  151. const val = data == 1 || data == true || data == 'true';
  152. const map = [
  153. { str: 0, val: ['是', '否'] },
  154. { str: 1, val: ['停用', '正常'] },
  155. { str: 2, val: ['显示', '隐藏'] },
  156. { str: 3, val: ['可见', '不可见'] },
  157. { str: 4, val: ['成功', '失败'] },
  158. ];
  159. return map[str == null || str == undefined ? 0 : str].val[val ? 0 : 1];
  160. }
  161. // 设置api传参数
  162. export function setParams(params: any) {
  163. const page = {
  164. current: 1,
  165. size: 10,
  166. orders: [{ field: 'createTime', direction: 'DESC' }],
  167. };
  168. if (params?.pageNum && params.pageNum !== undefined) {
  169. page.current = params.pageNum;
  170. }
  171. if (params?.pageSize && params.pageSize !== undefined) {
  172. page.size = params.pageSize;
  173. }
  174. if (params?.orders && params.orders !== undefined) {
  175. page.orders = params.orders;
  176. }
  177. delete params?.pageNum;
  178. delete params?.pageSize;
  179. delete params?.orders;
  180. params['page'] = page;
  181. console.log('🚀 ~ file: index.ts:194 ~ setParams ~ params', params);
  182. return params;
  183. }