utils.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. import { IFunction, IObject } from "@/types/interface";
  2. import { debounce, DebouncedFunc, DebounceSettings } from "lodash";
  3. import type { App, Plugin } from "vue";
  4. /**
  5. * 获取对象下的字段值
  6. * @param record {}
  7. * @param key 'a.b.c'
  8. * @param defaultValue
  9. * @returns
  10. */
  11. export const getValueByKeys = (record: IObject = {}, key: string, defaultValue?: unknown): any => {
  12. const keys = key.split(".");
  13. for (let i = 0; i < keys.length; i++) {
  14. record = record[keys[i]] || (i === keys.length - 1 ? defaultValue : {});
  15. }
  16. return record || defaultValue;
  17. };
  18. /**
  19. * 数组转对象
  20. * @param {*} rs 集合
  21. * @param {*} key 需要转换目标key的名称
  22. */
  23. export const arrayToObject = (
  24. rs: any[] = [],
  25. key: string | IFunction,
  26. render?: IFunction
  27. ): IObject => {
  28. const o: IObject = {};
  29. rs.forEach((x) => {
  30. o[typeof key === "function" ? key(x) : x[key]] = render ? render(x) : x;
  31. });
  32. return o;
  33. };
  34. /**
  35. * 数组转换格式
  36. * @param rs
  37. * @param render
  38. * @returns
  39. */
  40. export const arrayToKeyValueArray = (rs: any[] = [], render?: IFunction): any[] => {
  41. return rs.map((x) => (render ? render(x) : typeof x === "object" ? x : { label: x, value: x }));
  42. };
  43. /**
  44. * 是否只null和undefined值
  45. * @param vl
  46. * @returns
  47. */
  48. export const isNullOrUndefined = (vl: unknown): boolean => {
  49. return vl === null || typeof vl === "undefined";
  50. };
  51. /**
  52. * 是否外链
  53. * @param path
  54. * @returns
  55. */
  56. export const isExternalLink = (path: string): boolean => {
  57. return /^(https?:|\/\/|mailto:|tel:|^{{\s?ApiUrl\s?}})/.test(path);
  58. };
  59. /**
  60. * 复制
  61. * @param value
  62. */
  63. export const copyToClipboard = (value: string): void => {
  64. const transfer = document.createElement("textarea");
  65. document.body.appendChild(transfer);
  66. transfer.value = value;
  67. transfer.focus();
  68. transfer.select();
  69. if (document.execCommand("copy")) {
  70. document.execCommand("copy");
  71. }
  72. transfer.blur();
  73. document.body.removeChild(transfer);
  74. };
  75. /**
  76. * 检查是否有权限
  77. * @param permission
  78. * @param key
  79. * @returns
  80. */
  81. export const checkPermission = (permission: string[], key: string): boolean => {
  82. return permission.includes(key);
  83. };
  84. /**
  85. * 获取uuid
  86. */
  87. export const getUuid = (): string => {
  88. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
  89. const r = (Math.random() * 16) | 0,
  90. v = c == "x" ? r : (r & 0x3) | 0x8;
  91. return v.toString(16);
  92. });
  93. };
  94. /**
  95. * 邮箱
  96. * @param {*} s
  97. */
  98. export const isEmail = (s: string): boolean => {
  99. return /^([a-zA-Z0-9._-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(s);
  100. };
  101. /**
  102. * 手机号码
  103. * @param {*} s
  104. */
  105. export const isMobile = (s: string): boolean => {
  106. return /^1[0-9]{10}$/.test(s);
  107. };
  108. /**
  109. * 电话号码
  110. * @param {*} s
  111. */
  112. export const isPhone = (s: string): boolean => {
  113. return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(s);
  114. };
  115. /**
  116. * URL地址
  117. * @param {*} s
  118. */
  119. export const isURL = (s: string): boolean => {
  120. return /^http[s]?:\/\/.*/.test(s);
  121. };
  122. /**
  123. * 正整数
  124. * @param {*} s
  125. */
  126. export const isNumber = (s: string): boolean => {
  127. return /^\+?[1-9][0-9]*$/.test(s);
  128. };
  129. /**
  130. * 获取字典数据列表
  131. * @param dictType 字典类型
  132. */
  133. export const getDictDataList = (list: IObject[], dictType?: string): IObject[] => {
  134. const type = list.find((element: IObject) => element.dictType === dictType);
  135. if (type) {
  136. return type.dataList;
  137. } else {
  138. return [];
  139. }
  140. };
  141. /**
  142. * 获取字典名称
  143. * @param dictType 字典类型
  144. * @param dictValue 字典值
  145. */
  146. export const getDictLabel = (
  147. list: IObject[],
  148. dictType: string,
  149. dictValue: number
  150. ): string | number => {
  151. const type = list.find((element: IObject) => element.dictType === dictType);
  152. if (type) {
  153. const val = type.dataList.find((element: IObject) => element.dictValue === dictValue + "");
  154. if (val) {
  155. return val.dictLabel;
  156. } else {
  157. return dictValue;
  158. }
  159. } else {
  160. return dictValue;
  161. }
  162. };
  163. /**
  164. * 获取svg图标(id)列表
  165. */
  166. export const getIconList = (): string[] => {
  167. const rs: string[] = [];
  168. const list = document.querySelectorAll("svg symbol");
  169. for (let i = 0; i < list.length; i++) {
  170. rs.push(list[i].id);
  171. }
  172. return rs;
  173. };
  174. /**
  175. * 树形数据转换
  176. * @param {*} data
  177. * @param {*} id
  178. * @param {*} pid
  179. */
  180. export const treeDataTranslate = (data: IObject[], id?: string, pid?: string): IObject[] => {
  181. const res: IObject[] = [];
  182. const temp: IObject = {};
  183. id = id || "id";
  184. pid = pid || "pid";
  185. for (let i = 0; i < data.length; i++) {
  186. temp[data[i][id]] = data[i];
  187. }
  188. for (let k = 0; k < data.length; k++) {
  189. if (!temp[data[k][pid]] || data[k][id] === data[k][pid]) {
  190. res.push(data[k]);
  191. continue;
  192. }
  193. if (!temp[data[k][pid]]["children"]) {
  194. temp[data[k][pid]]["children"] = [];
  195. }
  196. temp[data[k][pid]]["children"].push(data[k]);
  197. data[k]["_level"] = (temp[data[k][pid]]._level || 0) + 1;
  198. }
  199. return res;
  200. };
  201. /**
  202. * 全局组件安装
  203. * @param component
  204. * @param alias
  205. * @returns
  206. */
  207. export const withInstall = <T extends object>(component: T, alias?: string): T & Plugin => {
  208. const comp = component as any;
  209. comp.install = (app: App) => {
  210. app.component(comp.name || comp.displayName, component);
  211. if (alias) {
  212. app.config.globalProperties[alias] = component;
  213. }
  214. };
  215. return component as T & Plugin;
  216. };
  217. /**
  218. * 节流函数 `created() {this.dataFormSubmitHandle = useDebounce(this.dataFormSubmitHandle);}`
  219. * @param fn
  220. * @param wait
  221. * @returns
  222. */
  223. export const useDebounce = (
  224. fn: (e?: any) => any,
  225. wait?: number,
  226. options?: DebounceSettings
  227. ): DebouncedFunc<any> => {
  228. return debounce(fn, wait ?? 1000, {
  229. leading: true,
  230. trailing: false,
  231. ...options
  232. });
  233. };