utils.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { Pen, Meta2dData } from '@meta2d/core';
  2. import { MessagePlugin, NotifyPlugin, Button } from 'tdesign-vue-next';
  3. import { h, ref } from 'vue';
  4. import { upCdn } from './api';
  5. export const noLoginTip = '请先登录,否则无法保存!';
  6. export const localStorageName = 'le5leV';
  7. export interface Meta2dBackData extends Meta2dData {
  8. id?: string;
  9. name?: string;
  10. userId?: string;
  11. image?: string;
  12. component?: boolean;
  13. componentDatas?: Pen[];
  14. version?: string;
  15. folder?: string;
  16. shared?: boolean; // 是否分享
  17. class?: string; // 分类,架构拓扑图那些
  18. // 组合为状态,组件保存使用,无需存储到后端
  19. showChild?: number;
  20. _id?: string;
  21. owner?: {
  22. id?: string;
  23. };
  24. folderId?: string;
  25. editor?: {
  26. id?: string;
  27. username?: string;
  28. };
  29. username?: string;
  30. editorId?: string;
  31. editorName?: string;
  32. teams?: { id?: string; name?: string }[];
  33. tags?: string[]; //标签数组
  34. }
  35. const notification = ref<any>(null);
  36. export function showNotification(title: string): Promise<boolean> {
  37. return new Promise<boolean>((resolve) => {
  38. const btnClick = () => {
  39. NotifyPlugin.close(notification.value);
  40. notification.value = null;
  41. resolve(true);
  42. };
  43. if (!notification.value) {
  44. notification.value = NotifyPlugin.info({
  45. title: '提示',
  46. content: title,
  47. closeBtn: true,
  48. onCloseBtnClick: () => {
  49. //关闭按钮
  50. notification.value = null;
  51. resolve(false);
  52. },
  53. // duration: 1000000,
  54. // @ts-ignore
  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. if (
  102. pen.image &&
  103. pen.image.startsWith('/') &&
  104. !pen.image.startsWith('/v/') &&
  105. !pen.image.startsWith('/png/')
  106. ) {
  107. pen.image = upCdn + pen.image;
  108. }
  109. });
  110. }
  111. export function gotoAccount() {
  112. MessagePlugin.info({
  113. content: '请开通vip,即将跳转到开通页面...',
  114. duration: 3,
  115. });
  116. setTimeout(() => {
  117. if (import.meta.env.BASE_URL[0] === '/') {
  118. window.open('/account?unVip=true');
  119. } else {
  120. let arr = location.host.split('.');
  121. arr[0] = 'http://account';
  122. let accountUrl = arr.join('.');
  123. window.open(`${accountUrl}?unVip=true`);
  124. }
  125. }, 3000);
  126. }
  127. export function isGif(url: string): boolean {
  128. return url.endsWith('.gif');
  129. }
  130. /**
  131. * 正常的 assign 操作,是存在弊端的,
  132. * 若源对象存在该属性,但目标对象不存在该属性(不存在并非 undefined),则会导致无法覆盖
  133. * 该方法会把源对象的属性全部清空,然后再把目标对象的属性覆盖到源对象上
  134. * source 可能是个监听的对象,所有最后一步再更改它的属性值
  135. * @param source 原对象
  136. * @param target 目标对象
  137. */
  138. export function strictAssign(
  139. source: Record<string, any>,
  140. target: Record<string, any>
  141. ) {
  142. // source 的全部属性都是 undefined 的对象,而非没有这个属性
  143. const undefinedSource: Record<string, any> = {};
  144. Object.keys(source).forEach((key) => {
  145. undefinedSource[key] = undefined;
  146. });
  147. Object.assign(undefinedSource, target);
  148. Object.assign(source, undefinedSource);
  149. }
  150. export function checkData(data: Meta2dData) {
  151. const pens: Pen[] = data.pens || [];
  152. for (let i = 0; i < pens.length; i++) {
  153. const pen: any = pens[i];
  154. pen.events?.forEach((event: any) => {
  155. delete event.setProps;
  156. });
  157. //处理画笔是脏数据的情况
  158. if (
  159. !(
  160. pen.x > -Infinity &&
  161. pen.x < Infinity &&
  162. pen.y > -Infinity &&
  163. pen.y < Infinity &&
  164. pen.width > -Infinity &&
  165. pen.width < Infinity &&
  166. pen.height > -Infinity &&
  167. pen.height < Infinity
  168. )
  169. ) {
  170. pens.splice(i, 1);
  171. --i;
  172. } else if (
  173. pen.x == null ||
  174. pen.y == null ||
  175. pen.width == null ||
  176. pen.height == null
  177. ) {
  178. pens.splice(i, 1);
  179. --i;
  180. }
  181. }
  182. if (Array.isArray(data.mqttOptions)) {
  183. // mqttOptions 是数组则认为是脏数据,删掉
  184. data.mqttOptions = {};
  185. }
  186. }