index.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. import { h, unref } from 'vue';
  2. import type { App, Plugin } from 'vue';
  3. import { NIcon, NTag } from 'naive-ui';
  4. import { PageEnum } from '@/enums/pageEnum';
  5. import { isObject } from './is/index';
  6. import { cloneDeep } from 'lodash';
  7. import dayjs from 'dayjs';
  8. import EventEmitter from 'eventemitter3';
  9. export const eventGlobal = new EventEmitter();
  10. /**
  11. * render 图标
  12. * */
  13. export function renderIcon(icon: any) {
  14. return () => h(NIcon, null, { default: () => h(icon) });
  15. }
  16. /**
  17. * font 图标(Font class)
  18. * */
  19. export function renderFontClassIcon(icon: string, iconName = 'iconfont') {
  20. return () => h('span', { class: [iconName, icon] });
  21. }
  22. /**
  23. * font 图标(Unicode)
  24. * */
  25. export function renderUnicodeIcon(icon: string, iconName = 'iconfont') {
  26. return () => h('span', { class: [iconName], innerHTML: icon });
  27. }
  28. /**
  29. * font svg 图标
  30. * */
  31. export function renderfontsvg(icon: any) {
  32. return () =>
  33. h(NIcon, null, {
  34. default: () =>
  35. h(
  36. 'svg',
  37. { class: `icon`, 'aria-hidden': 'true' },
  38. h('use', { 'xlink:href': `#${icon}` })
  39. )
  40. });
  41. }
  42. /**
  43. * render new Tag
  44. * */
  45. const newTagColors = { color: '#f90', textColor: '#fff', borderColor: '#f90' };
  46. export function renderNew(
  47. type = 'warning',
  48. text = 'New',
  49. color: object = newTagColors
  50. ) {
  51. return () =>
  52. h(
  53. NTag as any,
  54. {
  55. type,
  56. round: true,
  57. size: 'small',
  58. color
  59. },
  60. { default: () => text }
  61. );
  62. }
  63. /**
  64. * 递归组装菜单格式
  65. */
  66. export function generatorMenu(routerMap: Array<any>) {
  67. return filterRouter(routerMap).map(item => {
  68. const isRoot = isRootRouter(item);
  69. if (isRoot) {
  70. console.log(item.children[0], 'isRoot');
  71. }
  72. const info = isRoot ? item.children[0] : item;
  73. // console.log(item)
  74. const currentMenu = {
  75. ...info,
  76. ...info.meta,
  77. label: info.meta?.title,
  78. key: info.key,
  79. icon: info.meta?.icon
  80. };
  81. // 是否有子菜单,并递归处理
  82. if (info.children && info.children.length > 0) {
  83. // Recursion
  84. currentMenu.children = generatorMenu(info.children);
  85. }
  86. return currentMenu;
  87. });
  88. }
  89. /**
  90. * 混合菜单
  91. * */
  92. export function generatorMenuMix(
  93. routerMap: Array<any>,
  94. routerName: string,
  95. location: string
  96. ) {
  97. const cloneRouterMap = cloneDeep(routerMap);
  98. const newRouter = filterRouter(cloneRouterMap);
  99. if (location === 'header') {
  100. const firstRouter: any[] = [];
  101. newRouter.forEach(item => {
  102. const isRoot = isRootRouter(item);
  103. const info = isRoot ? item.children[0] : item;
  104. info.children = undefined;
  105. const currentMenu = {
  106. ...info,
  107. ...info.meta,
  108. label: info.meta?.title,
  109. key: info.name
  110. };
  111. firstRouter.push(currentMenu);
  112. });
  113. return firstRouter;
  114. } else {
  115. // 混合菜单
  116. console.log('混合菜单');
  117. return getChildrenRouter(
  118. newRouter.filter(item => item.name === routerName)
  119. );
  120. }
  121. }
  122. /**
  123. * 递归组装子菜单
  124. * */
  125. export function getChildrenRouter(routerMap: Array<any>) {
  126. return filterRouter(routerMap).map(item => {
  127. const isRoot = isRootRouter(item);
  128. const info = isRoot ? item.children[0] : item;
  129. const currentMenu = {
  130. ...info,
  131. ...info.meta,
  132. label: info.meta?.title,
  133. key: info.name
  134. };
  135. // 是否有子菜单,并递归处理
  136. if (info.children && info.children.length > 0) {
  137. // Recursion
  138. currentMenu.children = getChildrenRouter(info.children);
  139. }
  140. return currentMenu;
  141. });
  142. }
  143. /**
  144. * 判断根路由 Router
  145. * */
  146. export function isRootRouter(item: any) {
  147. return item.meta?.isRoot === true;
  148. }
  149. /**
  150. * 排除Router
  151. * */
  152. export function filterRouter(routerMap: Array<any>) {
  153. return routerMap.filter(item => {
  154. // console.log(
  155. // (item.meta?.hidden || false) != true &&
  156. // !['/:path(.*)*', PageEnum.REDIRECT, PageEnum.BASE_LOGIN].includes(item.path),
  157. // '过滤完之后的路由'
  158. // )
  159. return (
  160. (item.meta?.hidden || false) != true &&
  161. !['/:path(.*)*', PageEnum.REDIRECT, PageEnum.BASE_LOGIN].includes(
  162. item.path
  163. )
  164. );
  165. });
  166. }
  167. export const withInstall = <T>(component: T, alias?: string) => {
  168. const comp = component as any;
  169. comp.install = (app: App) => {
  170. app.component(comp.name || comp.displayName, component as any);
  171. if (alias) {
  172. app.config.globalProperties[alias] = component;
  173. }
  174. };
  175. return component as T & Plugin;
  176. };
  177. /**
  178. * 找到对应的节点
  179. * */
  180. let result = null as any;
  181. export function getTreeItem(data: any[], key?: string | number): any {
  182. data.map(item => {
  183. if (item.key === key) {
  184. result = item;
  185. } else {
  186. if (item.children && item.children.length) {
  187. getTreeItem(item.children, key);
  188. }
  189. }
  190. });
  191. return result;
  192. }
  193. /**
  194. * 找到所有节点
  195. * */
  196. const treeAll: any[] = [];
  197. export function getTreeAll(data: any[]): any[] {
  198. data.map(item => {
  199. treeAll.push(item.key);
  200. if (item.children && item.children.length) {
  201. getTreeAll(item.children);
  202. }
  203. });
  204. return treeAll;
  205. }
  206. export function deepMerge<T = any>(src: any = {}, target: any = {}): T {
  207. let key: string;
  208. for (key in target) {
  209. src[key] = isObject(src[key])
  210. ? deepMerge(src[key], target[key])
  211. : (src[key] = target[key]);
  212. }
  213. return src;
  214. }
  215. /**
  216. * Sums the passed percentage to the R, G or B of a HEX color
  217. * @param {string} color The color to change
  218. * @param {number} amount The amount to change the color by
  219. * @returns {string} The processed part of the color
  220. */
  221. function addLight(color: string, amount: number) {
  222. const cc = parseInt(color, 16) + amount;
  223. const c = cc > 255 ? 255 : cc;
  224. return c.toString(16).length > 1 ? c.toString(16) : `0${c.toString(16)}`;
  225. }
  226. /**
  227. * Lightens a 6 char HEX color according to the passed percentage
  228. * @param {string} color The color to change
  229. * @param {number} amount The amount to change the color by
  230. * @returns {string} The processed color represented as HEX
  231. */
  232. export function lighten(color: string, amount: number) {
  233. color = color.indexOf('#') >= 0 ? color.substring(1, color.length) : color;
  234. amount = Math.trunc((255 * amount) / 100);
  235. return `#${addLight(color.substring(0, 2), amount)}${addLight(
  236. color.substring(2, 4),
  237. amount
  238. )}${addLight(color.substring(4, 6), amount)}`;
  239. }
  240. /**
  241. * 判断是否 url
  242. * */
  243. export function isUrl(url: string) {
  244. return /^(http|https):\/\//g.test(url);
  245. }
  246. /** 递归清除空数据 */
  247. export function clearEmtryData(list: any[], key: string) {
  248. for (let i = 0; i < list.length; i++) {
  249. if (Array.isArray(list[i][key]) && !list[i][key].length) {
  250. list[i][key] = '';
  251. } else {
  252. list[i][key] = clearEmtryData(list[i][key], key);
  253. }
  254. }
  255. return list;
  256. }
  257. // 秒转分
  258. export const getSecondRPM = (second: number, type?: string) => {
  259. if (isNaN(second)) return '00:00';
  260. const mm = Math.floor(second / 60)
  261. .toString()
  262. .padStart(2, '0');
  263. const dd = Math.floor(second % 60)
  264. .toString()
  265. .padStart(2, '0');
  266. if (type === 'cn') {
  267. return mm + '分' + dd + '秒';
  268. } else {
  269. return mm + ':' + dd;
  270. }
  271. };
  272. // 秒转分
  273. export const getSecond = (second: number, type?: string) => {
  274. if (isNaN(second)) return '0000';
  275. const mm = Math.floor(second / 60)
  276. .toString()
  277. .padStart(2, '0');
  278. const dd = Math.floor(second % 60)
  279. .toString()
  280. .padStart(2, '0');
  281. return `${mm}${dd}`;
  282. };
  283. /** 滚动到表单填写错误的地方 */
  284. export function scrollToErrorForm() {
  285. const isError =
  286. document.querySelector('.n-input--error-status') ||
  287. document.querySelector('.n-base-selection--error-status');
  288. isError?.scrollIntoView({
  289. block: 'center',
  290. behavior: 'smooth'
  291. });
  292. }
  293. export const getTimes = (
  294. times: any,
  295. keys: Array<string> = [],
  296. format = 'YYYY-MM-DD'
  297. ) => {
  298. if (times && times.length) {
  299. return format == 'YYYY-MM-DD'
  300. ? {
  301. [keys[0] || 'start']: dayjs(times[0]).isValid()
  302. ? dayjs(times[0]).format(format) + ' 00:00:00'
  303. : '',
  304. [keys[1] || 'end']: dayjs(times[1]).isValid()
  305. ? dayjs(times[1]).format(format) + ' 23:59:59'
  306. : ''
  307. }
  308. : {
  309. [keys[0] || 'start']: dayjs(times[0]).isValid()
  310. ? dayjs(times[0]).format(format)
  311. : '',
  312. [keys[1] || 'end']: dayjs(times[1]).isValid()
  313. ? dayjs(times[1]).format(format)
  314. : ''
  315. };
  316. }
  317. return {};
  318. };
  319. export const px2vw = (px: number): string => `${(px / 1920) * 100}vw`;
  320. export const px2vwH = (px: number): string =>
  321. `${(((px / 1920) * 1920) / 1188) * 100}vw`;
  322. export const fscreen = () => {
  323. const el = document.documentElement;
  324. //进入全屏
  325. (el.requestFullscreen && el.requestFullscreen()) ||
  326. (el.mozRequestFullScreen && el.mozRequestFullScreen()) ||
  327. (el.webkitRequestFullscreen && el.webkitRequestFullscreen()) ||
  328. (el.msRequestFullscreen && el.msRequestFullscreen());
  329. //退出全屏
  330. };
  331. export const exitFullscreen = () => {
  332. document.exitFullscreen
  333. ? document.exitFullscreen()
  334. : document.mozCancelFullScreen
  335. ? document.mozCancelFullScreen()
  336. : document.webkitExitFullscreen
  337. ? document.webkitExitFullscreen()
  338. : '';
  339. };
  340. /** 检测链接格式 */
  341. export function checkUrlType(urlType: string) {
  342. const subfix = (urlType || '').split('.').pop();
  343. if (subfix === 'wav' || subfix === 'mp3' || subfix === 'm4a') {
  344. return 'audio';
  345. }
  346. return 'video';
  347. }
  348. const instruments: any = {
  349. 'Acoustic Grand Piano': '大钢琴',
  350. 'Bright Acoustic Piano': '明亮的钢琴',
  351. 'Electric Grand Piano': '电钢琴',
  352. 'Rhodes Piano': '柔和的电钢琴',
  353. 'Chorused Piano': '加合唱效果的电钢琴',
  354. Harpsichord: '羽管键琴',
  355. Clavichord: '科拉维科特琴',
  356. Celesta: '钢片琴',
  357. Glockenspiel: '钢片琴',
  358. 'Music box': '八音盒',
  359. Vibraphone: '颤音琴',
  360. Marimba: '马林巴',
  361. Xylophone: '木琴',
  362. 'Tubular Bells': '管钟',
  363. Dulcimer: '大扬琴',
  364. 'Hammond Organ': '击杆风琴',
  365. 'Percussive Organ': '打击式风琴',
  366. 'Rock Organ': '摇滚风琴',
  367. 'Church Organ': '教堂风琴',
  368. 'Reed Organ': '簧管风琴',
  369. Accordian: '手风琴',
  370. Harmonica: '口琴',
  371. 'Tango Accordian': '探戈手风琴',
  372. 'Acoustic Guitar': '钢弦吉他',
  373. 'Electric Guitar': '闷音电吉他',
  374. 'Overdriven Guitar': '加驱动效果的电吉他',
  375. 'Distortion Guitar': '加失真效果的电吉他',
  376. 'Guitar Harmonics': '吉他和音',
  377. 'Acoustic Bass': '大贝司',
  378. 'Electric Bass': '电贝司',
  379. 'Fretless Bass': '无品贝司',
  380. 'Slap Bass': '掌击',
  381. 'Synth Bass': '电子合成',
  382. Violin: '小提琴',
  383. Viola: '中提琴',
  384. Cello: '大提琴',
  385. Contrabass: '低音大提琴',
  386. 'Tremolo Strings': '弦乐群颤音音色',
  387. 'Pizzicato Strings': '弦乐群拨弦音色',
  388. 'Orchestral Harp': '竖琴',
  389. Timpani: '定音鼓',
  390. 'String Ensemble': '弦乐合奏音色',
  391. 'Synth Strings': '合成弦乐合奏音色',
  392. 'Choir Aahs': '人声合唱',
  393. 'Voice Oohs': '人声',
  394. 'Synth Voice': '合成人声',
  395. 'Orchestra Hit': '管弦乐敲击齐奏',
  396. Trumpet: '小号',
  397. Trombone: '长号',
  398. Tuba: '大号',
  399. 'Muted Trumpet': '加弱音器小号',
  400. 'French Horn': '法国号',
  401. 'Brass Section': '铜管组',
  402. 'Synth Brass': '合成铜管音色',
  403. 'Soprano Sax': '高音萨克斯管',
  404. 'Alto Sax': '中音萨克斯管',
  405. 'Tenor Sax': '次中音萨克斯管',
  406. 'Baritone Sax': '低音萨克斯管',
  407. Oboe: '双簧管',
  408. 'English Horn': '英国管',
  409. Bassoon: '巴松',
  410. Clarinet: '单簧管',
  411. 'Soprano Saxophone': '高音萨克斯管',
  412. 'Alto Saxophone': '中音萨克斯管',
  413. 'Tenor Saxophone': '次中音萨克斯管',
  414. 'Baritone Saxophone': '低音萨克斯管',
  415. Piccolo: '短笛',
  416. Flute: '长笛',
  417. Recorder: '竖笛',
  418. 'Soprano Recorder': '高音竖笛',
  419. 'Pan Flute': '排箫',
  420. 'Bottle Blow': '瓶木管',
  421. Whistle: '口哨声',
  422. Ocarina: '陶笛',
  423. Lead: '合成主音',
  424. 'Lead lead': '合成主音',
  425. 'Pad age': '合成音色',
  426. Pad: '合成音色',
  427. FX: '合成效果 科幻',
  428. Sitar: '西塔尔',
  429. Banjo: '班卓琴',
  430. Shamisen: '三昧线',
  431. Koto: '十三弦筝',
  432. Kalimba: '卡林巴',
  433. Bagpipe: '风笛',
  434. Fiddle: '民族提琴',
  435. Shanai: '山奈',
  436. 'Tinkle Bell': '叮当铃',
  437. Agogos: '阿戈戈铃',
  438. 'Steel Drums': '钢鼓',
  439. 'Taiko Drum': '太鼓',
  440. 'Melodic Toms': '嗵嗵鼓',
  441. 'Synth Drums': '合成鼓',
  442. 'Reverse Cymbals': '反向镲',
  443. 'Agogo Bells': '阿戈戈铃',
  444. 'Taiko Drums': '太鼓',
  445. Bongos: '邦戈鼓',
  446. 'Bongo Bell': '邦戈铃',
  447. Congas: '康加鼓',
  448. Guiro: '刮壶',
  449. 'Guitar Fret Noise': '吉他换把杂音',
  450. 'Breath Noise': '呼吸声',
  451. Seashore: '海浪声',
  452. 'Bird Tweet': '鸟鸣',
  453. 'Telephone Ring': '电话铃',
  454. Helicopter: '直升机',
  455. Applause: '鼓掌声',
  456. Gunshot: '枪声',
  457. 'Acoustic Bass Drum': '大鼓',
  458. 'Bass Drum': '大鼓',
  459. 'Side Drum': '小鼓鼓边',
  460. 'Acoustic Snare': '小鼓',
  461. 'Hand Claps': '拍手',
  462. 'Electric Snare': '小鼓',
  463. 'Low Floor Tom': '低音嗵鼓',
  464. 'Closed Hi-Hat': '闭合踩镲',
  465. 'High Floor Tom': '高音落地嗵鼓',
  466. 'Pedal Hi-Hat': '脚踏踩镲',
  467. 'Low Tom': '低音嗵鼓',
  468. 'Open Hi-Hat': '开音踩镲',
  469. 'Low-Mid Tom': '中低音嗵鼓',
  470. 'Hi Mid Tom': '高音鼓',
  471. 'Crash Cymbals': '对镲',
  472. 'High Tom': '高音嗵鼓',
  473. 'Ride Cymbals': '叮叮镲',
  474. 'Chinese Cymbals': '中国镲',
  475. 'Ride Bell': '圆铃',
  476. Tambourine: '铃鼓',
  477. 'Splash Cymbal': '溅音镲',
  478. Cowbell: '牛铃',
  479. 'Crash Cymbal': '强音钹',
  480. 'Vibra-Slap': '颤音器',
  481. 'Ride Cymbal': '打点钹',
  482. 'Hi Bongo': '高音邦戈鼓',
  483. 'Low Bongo': '低音邦戈鼓',
  484. 'Mute Hi Conga': '弱音高音康加鼓',
  485. 'Open Hi Conga': '强音高音康加鼓',
  486. 'Low Conga': '低音康加鼓',
  487. 'High Timbale': '高音天巴鼓',
  488. 'Low Timbale': '低音天巴鼓',
  489. 'High Agogo': '高音阿戈戈铃',
  490. 'Low Agogo': '低音阿戈戈铃',
  491. Cabasa: '卡巴萨',
  492. Maracas: '沙锤',
  493. 'Short Whistle': '短口哨',
  494. 'Long Whistle': '长口哨',
  495. 'Short Guiro': '短刮壶',
  496. 'Long Guiro': '长刮壶',
  497. Claves: '响棒',
  498. 'Hi Wood Block': '高音木鱼',
  499. 'Low Wood Block': '低音木鱼',
  500. 'Mute Triangle': '弱音三角铁',
  501. 'Open Triangle': '强音三角铁',
  502. 'Drum Set': '架子鼓',
  503. 'Hulusi flute': '葫芦丝',
  504. Melodica: '口风琴',
  505. Nai: '口风琴',
  506. 'Snare Drum': '小军鼓',
  507. Cymbal: '镲',
  508. Cymbals: '镲',
  509. 'Horn in F': '圆号',
  510. Triangle: '三角铁',
  511. Vibrato: '颤音琴',
  512. 'Suspend Cymbals': '吊镲',
  513. 'Suspended Cymbals': '吊镲',
  514. 'Tom-Toms': '嗵嗵鼓',
  515. Bell: '铃铛',
  516. Bells: '铃铛',
  517. 'Alto Clarinet': '中音单簧管',
  518. 'Bass Clarinet': '低音单簧管',
  519. Cornet: '短号',
  520. Euphonium: '上低音号',
  521. 'crash cymbals': '对镲',
  522. Castanets: '响板',
  523. Shaker: '沙锤',
  524. 'Mark tree': '音树',
  525. Chimes: '管钟',
  526. 'Mark Tree': '音树',
  527. 'Tom-toms': '嗵嗵鼓',
  528. 'Hi-Hat': '踩镲',
  529. 'Sleigh Bells': '雪橇铃',
  530. Flexatone: '弹音器',
  531. 'Brake drum': '闸鼓',
  532. Gong: '锣',
  533. 'concert tom': '音乐会嗵嗵鼓',
  534. 'brake drum': '车轮鼓',
  535. 'finger cymbal': '指钹',
  536. 'ride cymbal': '叮叮镲',
  537. 'Concert Toms': '音乐会嗵嗵鼓',
  538. Vibraslap: '弹音器',
  539. 'Wood Blocks': '木鱼',
  540. 'Temple Blocks': '木鱼',
  541. 'Wood Block': '木鱼',
  542. 'Field Drum': '军鼓',
  543. 'Quad-Toms': '筒鼓',
  544. Quads: '筒鼓',
  545. 'Drums set': '架子鼓',
  546. 'High Bongo': '邦戈',
  547. Timbales: '天巴鼓'
  548. };
  549. /** 获取分轨名称 */
  550. export const getInstrumentName = (name = '') => {
  551. name = name.toLocaleLowerCase().replace(/ /g, '');
  552. if (!name) return '';
  553. for (let key in instruments) {
  554. const _key = key.toLocaleLowerCase().replace(/ /g, '');
  555. if (_key.includes(name)) {
  556. return instruments[key];
  557. }
  558. }
  559. for (let key in instruments) {
  560. const _key = key.toLocaleLowerCase().replace(/ /g, '');
  561. if (name.includes(_key)) {
  562. return instruments[key];
  563. }
  564. }
  565. return '';
  566. };
  567. /**
  568. * 乐器排序
  569. * 排序顺序:长笛、单簧管、中音单簧管、低音单簧管、高音萨克斯风、中音萨克斯风、次中音萨克斯风、低音萨克斯风、小号、长号、圆号、大号、上低音号
  570. * */
  571. export const sortMusical = (name: string, index: number) => {
  572. let sortId = 0;
  573. switch (name) {
  574. case '长笛':
  575. sortId = 1;
  576. break;
  577. case '单簧管':
  578. sortId = 2;
  579. break;
  580. case '中音单簧管':
  581. sortId = 3;
  582. break;
  583. case '低音单簧管':
  584. sortId = 4;
  585. break;
  586. case '高音萨克斯风':
  587. sortId = 5;
  588. break;
  589. case '中音萨克斯风':
  590. sortId = 6;
  591. break;
  592. case '次中音萨克斯风':
  593. sortId = 7;
  594. break;
  595. case '低音萨克斯风':
  596. sortId = 8;
  597. break;
  598. case '小号':
  599. sortId = 9;
  600. break;
  601. case '长号':
  602. sortId = 10;
  603. break;
  604. case '圆号':
  605. sortId = 11;
  606. break;
  607. case '大号':
  608. sortId = 12;
  609. break;
  610. case '上低音号':
  611. sortId = 13;
  612. break;
  613. default:
  614. sortId = index + 14;
  615. break;
  616. }
  617. return sortId;
  618. };