index.ts 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import type { DataSource } from "../types";
  2. import { defaultsDeep } from "lodash-es";
  3. import { containerDefaultConfig, chartDefaultConfig } from "../config";
  4. import { EChartsOption } from "echarts";
  5. import { PropType } from "vue";
  6. import { DataSourceType } from "../chartEnum";
  7. /**
  8. * 获取容器组件属性
  9. * @param {Record<string, any>} config - 传入属性
  10. * @returns {Record<string, any>} - 返回与默认配置合并后的属性
  11. */
  12. export function getNormalizedContainer(config: Record<string, any>) {
  13. return defaultsDeep(config, containerDefaultConfig);
  14. }
  15. /**
  16. * 获取图表组件属性
  17. * @param {Record<string, any>} config - 传入属性
  18. * @returns {Record<string, any>} - 返回与默认配置合并后的属性
  19. */
  20. export function getNormalizedChart(config: EChartsOption) {
  21. return defaultsDeep(config, chartDefaultConfig);
  22. }
  23. // 图表组件数据来源prop
  24. export const dataSource = {
  25. type: Object as PropType<DataSource>,
  26. default: () => ({
  27. sourceType: DataSourceType.STATIC,
  28. data: [],
  29. url: "",
  30. method: "GET",
  31. params: {},
  32. headers: {},
  33. refreshTime: 0,
  34. dataProcess: () => [],
  35. }),
  36. }
  37. /**
  38. * 执行动态js
  39. * @param code 代码
  40. * @param param 参数
  41. * @returns promise
  42. */
  43. export const cllJsCode = (code: string, param: string): Promise<any> => {
  44. return new Promise((resove, reject) => {
  45. // 生成一份new webwork
  46. const blob = new Blob([`
  47. self.onmessage = function(e) {
  48. self.postMessage((${code}).call(null, e.data));
  49. }
  50. `], { type: 'application/javascript' });
  51. const worker = new Worker(URL.createObjectURL(blob));
  52. // 向webwork发送消息
  53. worker.postMessage(JSON.parse(param));
  54. worker.onmessage = (e) => {
  55. worker.terminate();
  56. resove(e.data);
  57. }
  58. worker.onerror = (e) => {
  59. worker.terminate();
  60. reject(e);
  61. }
  62. });
  63. }