utils.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { FormItem, Pen, Meta2d, Meta2dData } from "@meta2d/core";
  2. import { MessagePlugin, NotifyPlugin, Button } from "tdesign-vue-next";
  3. import { h, ref } from "vue";
  4. import { cdn } from "./api";
  5. const market = import.meta.env.VITE_MARKET;
  6. export const noLoginTip = "请先登录,否则无法保存!";
  7. export const localMeta2dDataName = "meta2dData";
  8. export interface Meta2dBackData extends Meta2dData {
  9. id?: string;
  10. name?: string;
  11. userId?: string;
  12. image?: string;
  13. component?: boolean;
  14. componentDatas?: Pen[];
  15. version?: string;
  16. folder?: string;
  17. shared?: boolean; // 是否分享
  18. class?: string; // 分类,架构拓扑图那些
  19. // 组合为状态,组件保存使用,无需存储到后端
  20. showChild?: number;
  21. _id?: string;
  22. owner?: {
  23. id?: string;
  24. };
  25. folderId?: string;
  26. editor?: {
  27. id?: string;
  28. username?: string;
  29. };
  30. username?: string;
  31. editorId?: string;
  32. editorName?: string;
  33. teams?: { id?: string; name?: string }[];
  34. tags?: string[]; //标签数组
  35. }
  36. const notification = ref<any>(null);
  37. export function showNotification(title: string): Promise<boolean> {
  38. return new Promise<boolean>((resolve) => {
  39. const btnClick = () => {
  40. NotifyPlugin.close(notification.value);
  41. notification.value = null;
  42. resolve(true);
  43. };
  44. if (!notification.value) {
  45. notification.value = NotifyPlugin.info({
  46. title: "提示",
  47. content: title,
  48. closeBtn: true,
  49. onCloseBtnClick: () => {
  50. //关闭按钮
  51. notification.value = null;
  52. resolve(false);
  53. },
  54. // duration: 1000000,
  55. footer: h(
  56. Button,
  57. {
  58. theme: "primary",
  59. size: "small",
  60. style: {
  61. "margin-top": "16px",
  62. "margin-left": "256px",
  63. },
  64. onClick: btnClick,
  65. },
  66. "确定"
  67. ),
  68. });
  69. }
  70. });
  71. }
  72. export async function dealwithFormatbeforeOpen(data: Meta2dBackData) {
  73. if (!data) {
  74. return;
  75. }
  76. if ((!data.https || data.https?.length == 0) && data.http) {
  77. data.https = [
  78. {
  79. http: data.http,
  80. httpTimeInterval: data.httpTimeInterval,
  81. httpHeaders: data.httpHeaders,
  82. },
  83. ];
  84. delete data.http;
  85. delete data.httpHeaders;
  86. delete data.httpTimeInterval;
  87. }
  88. //新版渐进色
  89. data.pens &&
  90. data.pens.forEach((pen) => {
  91. if (pen.lineGradientFromColor && pen.lineGradientToColor) {
  92. pen.lineGradientColors = `linear-gradient(${
  93. pen.lineGradientAngle ? Number(pen.lineGradientAngle) + 90 : 0
  94. }deg,${pen.lineGradientFromColor} 0%,${pen.lineGradientToColor} 100%)`;
  95. }
  96. if (pen.gradientFromColor && pen.gradientToColor) {
  97. pen.gradientColors = `linear-gradient(${
  98. pen.gradientAngle ? Number(pen.gradientAngle) + 90 : 0
  99. }deg,${pen.gradientFromColor} 0%,${pen.gradientToColor} 100%)`;
  100. }
  101. });
  102. }
  103. export function gotoAccount() {
  104. MessagePlugin.info({
  105. content: "请开通vip,即将跳转到开通页面...",
  106. duration: 3,
  107. });
  108. setTimeout(() => {
  109. if (market) {
  110. window.open("/account?unVip=true");
  111. } else {
  112. let arr = location.host.split(".");
  113. arr[0] = "http://account";
  114. let accountUrl = arr.join(".");
  115. window.open(`${accountUrl}?unVip=true`);
  116. }
  117. }, 3000);
  118. }
  119. export function isGif(url: string): boolean {
  120. return url.endsWith(".gif");
  121. }
  122. /**
  123. * 正常的 assign 操作,是存在弊端的,
  124. * 若源对象存在该属性,但目标对象不存在该属性(不存在并非 undefined),则会导致无法覆盖
  125. * 该方法会把源对象的属性全部清空,然后再把目标对象的属性覆盖到源对象上
  126. * source 可能是个监听的对象,所有最后一步再更改它的属性值
  127. * @param source 原对象
  128. * @param target 目标对象
  129. */
  130. export function strictAssign(
  131. source: Record<string, any>,
  132. target: Record<string, any>
  133. ) {
  134. // source 的全部属性都是 undefined 的对象,而非没有这个属性
  135. const undefinedSource: Record<string, any> = {};
  136. Object.keys(source).forEach((key) => {
  137. undefinedSource[key] = undefined;
  138. });
  139. Object.assign(undefinedSource, target);
  140. Object.assign(source, undefinedSource);
  141. }
  142. export function checkData(data: Meta2dData) {
  143. const pens: Pen[] = data.pens || [];
  144. for (let i = 0; i < pens.length; i++) {
  145. const pen: any = pens[i];
  146. pen.events?.forEach((event: any) => {
  147. delete event.setProps;
  148. });
  149. //处理画笔是脏数据的情况
  150. if (
  151. !(
  152. pen.x > -Infinity &&
  153. pen.x < Infinity &&
  154. pen.y > -Infinity &&
  155. pen.y < Infinity &&
  156. pen.width > -Infinity &&
  157. pen.width < Infinity &&
  158. pen.height > -Infinity &&
  159. pen.height < Infinity
  160. )
  161. ) {
  162. pens.splice(i, 1);
  163. --i;
  164. } else if (
  165. pen.x == null ||
  166. pen.y == null ||
  167. pen.width == null ||
  168. pen.height == null
  169. ) {
  170. pens.splice(i, 1);
  171. --i;
  172. }
  173. }
  174. if (Array.isArray(data.mqttOptions)) {
  175. // mqttOptions 是数组则认为是脏数据,删掉
  176. data.mqttOptions = {};
  177. }
  178. }