View.vue 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770
  1. <template>
  2. <div class="meta2d">
  3. <div class="tools">
  4. <t-tooltip content="新建" placement="bottom">
  5. <a><t-icon name="add" @click="newFile" /></a>
  6. </t-tooltip>
  7. <t-tooltip content="保存为方案" placement="bottom">
  8. <a>
  9. <t-badge
  10. :class="{ gray: route.query.c }"
  11. dot
  12. :showZero="false"
  13. :count="dot ? 1 : 0"
  14. >
  15. <t-icon
  16. name="save"
  17. @click="!route.query.c && save(SaveType.Save, '', true)"
  18. />
  19. </t-badge>
  20. </a>
  21. </t-tooltip>
  22. <t-tooltip content="保存为模板" placement="bottom">
  23. <a>
  24. <t-badge
  25. :class="{ gray: route.query.c }"
  26. dot
  27. :showZero="false"
  28. :count="dot ? 1 : 0"
  29. >
  30. <t-icon
  31. name="view-module"
  32. @click="
  33. !route.query.c && save(SaveType.Save, 'le5leV-template', true)
  34. "
  35. />
  36. </t-badge>
  37. </a>
  38. </t-tooltip>
  39. <t-tooltip content="保存为我的组件" placement="bottom">
  40. <a :class="{ gray: route.query.id && !route.query.c }">
  41. <t-icon
  42. name="layers"
  43. @click="
  44. (!route.query.id || route.query.c) &&
  45. save(SaveType.Save, 'le5leV-components', true)
  46. "
  47. />
  48. </a>
  49. </t-tooltip>
  50. <t-tooltip content="格式化(双击可连续使用)" placement="bottom">
  51. <a
  52. @click="oneFormat"
  53. @dblclick="alwaysFormat"
  54. :style="{
  55. color: one || always ? ' #1677ff' : '',
  56. }"
  57. >
  58. <svg
  59. width="1em"
  60. height="1em"
  61. viewBox="0 0 256 256"
  62. xmlns="http://www.w3.org/2000/svg"
  63. xmlns:xlink="http://www.w3.org/1999/xlink"
  64. >
  65. <defs>
  66. <path id="77456364" d="M0 0h256v256H0z"></path>
  67. </defs>
  68. <g fill="none" fill-rule="evenodd">
  69. <mask id="3804093554b" fill="#fff">
  70. <use xlink:href="#77456364"></use>
  71. </mask>
  72. <path
  73. d="M213 77c0 14.36-11.64 26-26 26H69c-14.36 0-26-11.64-26-26V51c0-14.36 11.64-26 26-26h118c14.36 0 26 11.64 26 26v2h9.125c9.83 0 17.82 7.88 17.997 17.67l.003.33v50c0 9.83-7.88 17.82-17.67 17.997l-.33.003h-84.626v18H139c9.83 0 17.82 7.88 17.997 17.67l.003.33v35c0 16.016-12.984 29-29 29-15.856 0-28.74-12.725-28.996-28.52L99 210v-35c0-9.83 7.88-17.82 17.67-17.997L117 157h.499l.001-20c0-9.83 7.88-17.82 17.67-17.997l.33-.003h84.625V73H213Zm-76 100h-18v33a9 9 0 0 0 8.471 8.985l.264.011.265.004a9 9 0 0 0 8.996-8.735L137 210v-33Zm50-132H69a6 6 0 0 0-6 6v26a6 6 0 0 0 6 6h118a6 6 0 0 0 6-6V51a6 6 0 0 0-6-6Z"
  74. fill="currentColor"
  75. fill-rule="nonzero"
  76. mask="url(#3804093554b)"
  77. ></path>
  78. </g>
  79. </svg>
  80. </a>
  81. </t-tooltip>
  82. <t-tooltip content="清除格式" placement="bottom">
  83. <a @click="clearFormat">
  84. <svg
  85. width="1em"
  86. height="1em"
  87. viewBox="0 0 1024 1024"
  88. xmlns="http://www.w3.org/2000/svg"
  89. >
  90. <path
  91. d="M889.186 384.07 677.671 172.56c-53.063-53.063-139.094-53.063-192.157 0L134.617 523.457c-53.063 53.063-53.063 139.099 0 192.158l170.196 170.2a41.354 41.354 0 0 0 29.243 12.11h215.001a41.354 41.354 0 0 0 29.184-12.05L889.155 576.26c53.094-53.09 53.094-139.126.031-192.19zM830.7 442.558c20.48 20.472 20.764 53.492.855 74.319l-.961.984-298.618 297.358H351.185l-158.09-158.086c-20.76-20.763-20.76-54.43 0-75.193l350.901-350.897c20.764-20.764 54.43-20.764 75.193 0l211.515 211.511z"
  92. fill="currentColor"
  93. ></path>
  94. <path
  95. d="m685.505 678.754-58.19 58.77-317.587-314.43 58.191-58.774zm197.55 136.508c23.46 0 42.483 18.514 42.483 41.353 0 22.45-18.38 40.724-41.294 41.338l-1.19.016h-454.6c-23.462 0-42.485-18.514-42.485-41.354 0-22.449 18.381-40.723 41.295-41.338l1.19-.015h454.6z"
  96. fill="currentColor"
  97. ></path>
  98. </svg>
  99. </a>
  100. </t-tooltip>
  101. <div class="flex-grow"></div>
  102. <t-tooltip content="直线" placement="bottom">
  103. <a
  104. :draggable="true"
  105. @dragstart="onAddShape($event, 'line')"
  106. @click="onAddShape($event, 'line')"
  107. ><t-icon name="slash"
  108. /></a>
  109. </t-tooltip>
  110. <t-tooltip content="文字" placement="bottom">
  111. <a
  112. :draggable="true"
  113. @dragstart="onAddShape($event, 'text')"
  114. @click="onAddShape($event, 'text')"
  115. >T</a
  116. >
  117. </t-tooltip>
  118. <t-tooltip content="连线(双击可连续使用)" placement="bottom">
  119. <a
  120. @click="oneDraw"
  121. @dblclick="alwaysDraw"
  122. :style="{
  123. color: oneD || alwaysD ? ' #1677ff' : '',
  124. }"
  125. >
  126. <svg
  127. width="1em"
  128. height="1em"
  129. viewBox="0 0 1024 1024"
  130. xmlns="http://www.w3.org/2000/svg"
  131. >
  132. <path
  133. d="M192 64a128 128 0 0 1 123.968 96H384a160 160 0 0 1 159.68 149.504L544 320v384a96 96 0 0 0 86.784 95.552L640 800h68.032a128 128 0 1 1 0 64.064L640 864a160 160 0 0 1-159.68-149.504L480 704V320a96 96 0 0 0-86.784-95.552L384 224l-68.032 0.064A128 128 0 1 1 192 64z m640 704a64 64 0 1 0 0 128 64 64 0 0 0 0-128zM192 128a64 64 0 1 0 0 128 64 64 0 0 0 0-128z"
  134. fill="currentColor"
  135. ></path>
  136. </svg>
  137. </a>
  138. </t-tooltip>
  139. <t-dropdown
  140. :minColumnWidth="200"
  141. :maxHeight="560"
  142. :delay2="[10, 150]"
  143. overlayClassName="header-dropdown"
  144. >
  145. <a>
  146. <svg class="l-icon" aria-hidden="true">
  147. <use
  148. :xlink:href="
  149. lineTypes.find((item) => item.value === currentLineType)?.icon
  150. "
  151. ></use>
  152. </svg>
  153. </a>
  154. <t-dropdown-menu>
  155. <t-dropdown-item v-for="item in lineTypes">
  156. <div class="flex middle" @click="changeLineType(item.value)">
  157. {{ item.name }} <span class="flex-grow"></span>
  158. <svg class="l-icon" aria-hidden="true">
  159. <use :xlink:href="item.icon"></use>
  160. </svg>
  161. </div>
  162. </t-dropdown-item>
  163. </t-dropdown-menu>
  164. </t-dropdown>
  165. <t-dropdown
  166. :minColumnWidth="200"
  167. :maxHeight="560"
  168. :delay2="[10, 150]"
  169. overlayClassName="header-dropdown"
  170. >
  171. <a>
  172. <svg class="l-icon" aria-hidden="true">
  173. <use
  174. :xlink:href="
  175. fromArrows.find((item) => item.value === fromArrow)?.icon
  176. "
  177. ></use>
  178. </svg>
  179. </a>
  180. <t-dropdown-menu>
  181. <t-dropdown-item v-for="item in fromArrows">
  182. <div
  183. class="flex middle"
  184. style="height: 30px"
  185. @click="changeFromArrow(item.value)"
  186. >
  187. <svg class="l-icon" aria-hidden="true">
  188. <use :xlink:href="item.icon"></use>
  189. </svg>
  190. </div>
  191. </t-dropdown-item>
  192. </t-dropdown-menu>
  193. </t-dropdown>
  194. <t-dropdown
  195. :minColumnWidth="200"
  196. :maxHeight="560"
  197. :delay2="[10, 150]"
  198. overlayClassName="header-dropdown"
  199. >
  200. <a>
  201. <svg class="l-icon" aria-hidden="true">
  202. <use
  203. :xlink:href="
  204. toArrows.find((item) => item.value === toArrow)?.icon
  205. "
  206. ></use>
  207. </svg>
  208. </a>
  209. <t-dropdown-menu>
  210. <t-dropdown-item v-for="item in toArrows">
  211. <div
  212. class="flex middle"
  213. style="height: 30px"
  214. @click="changeToArrow(item.value)"
  215. >
  216. <svg class="l-icon" aria-hidden="true">
  217. <use :xlink:href="item.icon"></use>
  218. </svg>
  219. </div>
  220. </t-dropdown-item>
  221. </t-dropdown-menu>
  222. </t-dropdown>
  223. <!-- </t-tooltip> -->
  224. <t-tooltip content="视图大小" placement="bottom">
  225. <div style="line-height: 40px; margin-left: 8px">{{ scale }}%</div>
  226. </t-tooltip>
  227. <t-tooltip content="100%视图" placement="bottom">
  228. <a @click="onScaleFull"><t-icon name="refresh" /></a>
  229. </t-tooltip>
  230. <t-tooltip content="窗口大小" placement="bottom">
  231. <a @click="onScaleWindow"><t-icon name="fullscreen-exit" /></a>
  232. </t-tooltip>
  233. <t-tooltip content="数据源" placement="bottom">
  234. <a @click="onShowDataDialog"><t-icon name="server" /></a>
  235. </t-tooltip>
  236. <div class="flex-grow"></div>
  237. <t-tooltip
  238. :content="canScale ? '允许缩放' : '禁止缩放'"
  239. placement="bottom"
  240. >
  241. <t-icon v-if="canScale" name="unfold-more" @click="onCanScale(true)" />
  242. <t-icon v-else name="unfold-less" @click="onCanScale(false)" />
  243. </t-tooltip>
  244. <t-tooltip :content="isLock ? '浏览模式' : '编辑模式'" placement="bottom">
  245. <a>
  246. <svg
  247. v-if="isLock"
  248. class="l-icon"
  249. aria-hidden="true"
  250. @click="onLock(0)"
  251. >
  252. <use xlink:href="#l-lock"></use>
  253. </svg>
  254. <svg v-else class="l-icon" aria-hidden="true" @click="onLock(1)">
  255. <use xlink:href="#l-unlock"></use>
  256. </svg>
  257. </a>
  258. </t-tooltip>
  259. <t-tooltip content="运行(查看)" placement="bottom">
  260. <a @click="preview"><t-icon name="caret-right" /></a>
  261. </t-tooltip>
  262. <t-tooltip content="分享" placement="bottom">
  263. <a @click="share">
  264. <t-icon name="share" :class="{ primary: shared }" />
  265. </a>
  266. </t-tooltip>
  267. <t-popup placement="bottom" v-model="qrcode.visible">
  268. <a><t-icon name="qrcode" /></a>
  269. <template #content>
  270. <div style="padding: 12px 12px 6px 12px">
  271. <img v-if="route.query.id && !route.query.c" :src="qrcode.url" />
  272. <div
  273. v-else-if="!route.query.id"
  274. class="gray center mb-4"
  275. style="font-size: 10px; margin-bottom: 6px"
  276. >
  277. 当前项目为本地编辑模式<br />
  278. 请保存为大屏项目后访问
  279. </div>
  280. <div
  281. v-else
  282. class="gray center mb-4"
  283. style="font-size: 10px; margin-bottom: 6px"
  284. >
  285. 仅支持手机访问大屏项目
  286. </div>
  287. </div>
  288. </template>
  289. </t-popup>
  290. <t-tooltip content="云发布" placement="bottom">
  291. <a @click="onShowPublish"><t-icon name="cloud" /></a>
  292. </t-tooltip>
  293. </div>
  294. <div id="meta2d"></div>
  295. <ContextMenu
  296. v-if="contextmenu.visible"
  297. :type="contextmenu.type"
  298. :style="contextmenu.style"
  299. @changeVisible="changeContextMenuVisible"
  300. />
  301. <t-dialog
  302. v-if="dataDialog.show"
  303. width="800px"
  304. header="数据源管理"
  305. :visible="true"
  306. @close="dataDialog.show = false"
  307. >
  308. <t-tabs v-model="dataDialog.tab" class="body">
  309. <t-tab-panel :value="1" label="数据通信" :destroy-on-hide="false">
  310. <template #panel>
  311. <div v-if="!dataDialog.editNetwork">
  312. <div class="mt-16 flex between middle">
  313. <div>
  314. <t-checkbox
  315. style="display: inline"
  316. v-model="dataDialog.enableMock"
  317. @change="onChangeMock"
  318. >
  319. 开启模拟数据
  320. </t-checkbox>
  321. <label class="ml-8"> 轮询间隔 </label>
  322. <t-input-number
  323. style="width: 100px"
  324. v-model="dataDialog.networkInterval"
  325. theme="normal"
  326. :min="30"
  327. placeholder="毫秒"
  328. suffix="ms"
  329. />
  330. </div>
  331. <div>
  332. <t-select-input
  333. v-model:inputValue="dataDialog.input"
  334. placeholder="搜索我的实时数据"
  335. allow-input
  336. clearable
  337. :popup-visible="dataDialog.popupVisible"
  338. @focus="dataDialog.popupVisible = true"
  339. @blur="dataDialog.popupVisible = undefined"
  340. @input-change="onInputNetwork"
  341. style="width: 240px"
  342. >
  343. <template #panel>
  344. <ul style="padding: 4px">
  345. <li
  346. class="hover-background item"
  347. v-for="(item, i) in dataDialog.networkList"
  348. :key="item.url"
  349. @click="onSelectNetWork(item)"
  350. >
  351. 名称: {{ item.name }}
  352. <div class="desc">地址: {{ item.url }}</div>
  353. <span class="del" @click.stop="onDelNetWork(item, i)">
  354. <t-icon name="delete" />
  355. </span>
  356. </li>
  357. <li
  358. v-if="dataDialog.networkList.length >= 10"
  359. style="
  360. line-height: 1.5;
  361. padding: 8px;
  362. border-radius: 2px;
  363. "
  364. :key="-1"
  365. >
  366. <div class="desc">...</div>
  367. </li>
  368. <li
  369. v-if="!dataDialog.networkList.length"
  370. style="
  371. line-height: 1.5;
  372. padding: 8px;
  373. border-radius: 2px;
  374. "
  375. :key="-1"
  376. >
  377. <div class="desc">暂无数据</div>
  378. </li>
  379. </ul>
  380. </template>
  381. <template #suffixIcon>
  382. <t-icon name="search" class="hover" />
  383. </template>
  384. </t-select-input>
  385. <t-button
  386. class="ml-12"
  387. style="height: 30px"
  388. @click="addNetwork"
  389. >
  390. 添加实时数据
  391. </t-button>
  392. </div>
  393. </div>
  394. <t-table
  395. class="mt-12"
  396. row-key="id"
  397. :data="dataDialog.networks"
  398. :columns="networkColumns"
  399. :max-height="280"
  400. size="small"
  401. >
  402. <template #protocol="{ row, rowIndex }">
  403. {{ row.protocol || 'MQTT' }}
  404. </template>
  405. <template #actions="{ row, rowIndex }">
  406. <a @click="editNetwork(row, rowIndex)"> 编辑 </a>
  407. <a class="ml-12" @click="deleteNetwork(rowIndex)"> 删除 </a>
  408. </template>
  409. <template #empty>
  410. <div class="center">
  411. <div>暂无数据</div>
  412. <div class="mt-8">
  413. <a @click="addNetwork"> + 添加实时数据</a>
  414. </div>
  415. </div>
  416. </template>
  417. </t-table>
  418. </div>
  419. <div v-else>
  420. <div class="mt-8">
  421. <a
  422. @click="
  423. dataDialog.network = dataDialog.networkBak;
  424. dataDialog.editNetwork = false;
  425. "
  426. class="flex middle"
  427. style="width: 44px"
  428. >
  429. <t-icon name="rollback" class="mr-4" /> 返回
  430. </a>
  431. </div>
  432. <div style="height: 300px; overflow-y: auto">
  433. <Network v-model="dataDialog.network" />
  434. </div>
  435. </div>
  436. </template>
  437. </t-tab-panel>
  438. <t-tab-panel :value="2" :destroy-on-hide="false">
  439. <template #label> 数据模型 </template>
  440. <template #panel>
  441. <template v-if="!dataDialog.editDataset">
  442. <div class="form-item mt-16">
  443. <label style="width: 100px"> 当前数据模型 </label>
  444. <div class="flex w-full">
  445. <t-select
  446. class="flex-grow"
  447. v-model="dataDialog.datasetId"
  448. filterable
  449. placeholder="选择数据模型"
  450. :on-search="onInputDatasets"
  451. :popup-props="{ overlayClassName: 'select-options' }"
  452. @change="onSelDataset()"
  453. >
  454. <t-option
  455. v-for="(item, i) in dataDialog.datasetList"
  456. :key="item.id"
  457. :value="item.id"
  458. :label="item.name"
  459. >
  460. <div class="hover-background item">
  461. 名称: {{ item.name }}
  462. <div v-if="item.url" class="desc">
  463. URL: {{ item.url }}
  464. </div>
  465. <div v-else class="desc">自定义</div>
  466. <span class="del" @click.stop="onDelDataset(item, i)">
  467. <t-icon name="delete" />
  468. </span>
  469. </div>
  470. </t-option>
  471. <t-option
  472. v-if="dataDialog.datasetList.length >= 10"
  473. :disabled="true"
  474. >
  475. <div class="ml-8 gray">...</div>
  476. </t-option>
  477. </t-select>
  478. <t-button
  479. class="ml-12 shrink-0"
  480. style="height: 30px"
  481. @click="addDataset"
  482. >
  483. 添加数据模型
  484. </t-button>
  485. </div>
  486. </div>
  487. <t-table
  488. class="mt-12"
  489. row-key="id"
  490. :data="dataDialog.dataset.data"
  491. :columns="datasetColumns"
  492. size="small"
  493. :max-height="280"
  494. >
  495. <template #type="{ row }">
  496. {{ row.type || 'string' }}
  497. </template>
  498. </t-table>
  499. </template>
  500. <div v-else>
  501. <div class="mt-8">
  502. <a
  503. @click="
  504. dataDialog.datasetBak &&
  505. (dataDialog.dataset = dataDialog.datasetBak);
  506. dataDialog.dataset &&
  507. (dataDialog.datasetId = dataDialog.dataset.id);
  508. dataDialog.editDataset = 0;
  509. "
  510. class="flex middle"
  511. style="width: 44px"
  512. >
  513. <t-icon name="rollback" class="mr-4" /> 返回
  514. </a>
  515. </div>
  516. <div style="height: 300px; overflow-y: auto">
  517. <Dataset v-model="dataDialog.dataset" />
  518. </div>
  519. </div>
  520. </template>
  521. </t-tab-panel>
  522. </t-tabs>
  523. <template #footer>
  524. <div
  525. v-if="dataDialog.tab === 1 && dataDialog.editNetwork"
  526. class="flex middle"
  527. >
  528. <div class="flex-grow"></div>
  529. <t-checkbox v-model="dataDialog.save" class="mr-12">
  530. 同时保存到我的实时数据
  531. </t-checkbox>
  532. <t-button @click="onOkNetwork">确定</t-button>
  533. </div>
  534. <div v-else-if="dataDialog.tab === 2" class="flex middle">
  535. <template v-if="dataDialog.editDataset === 1">
  536. <div class="flex-grow"></div>
  537. <t-checkbox v-model="dataDialog.save" class="mr-12">
  538. 同时保存为我的数据模型
  539. </t-checkbox>
  540. <t-button @click="onOkDataset()">保存</t-button>
  541. </template>
  542. <template v-else-if="dataDialog.editDataset === 2">
  543. <div class="flex-grow"></div>
  544. <t-button @click="onOkDataset(true)"> 另外为新数据模型 </t-button>
  545. <t-button @click="onOkDataset()">保存</t-button>
  546. </template>
  547. <template v-else>
  548. <a
  549. v-if="dataDialog.dataset && dataDialog.dataset.id"
  550. @click="editDataset"
  551. >
  552. 编辑当前数据模型
  553. </a>
  554. <div class="flex-grow"></div>
  555. <t-button @click="dataDialog.show = false"> 完成 </t-button>
  556. </template>
  557. </div>
  558. <div v-else class="flex middle">
  559. <div class="flex-grow"></div>
  560. <t-button @click="onFinishDataDialog"> 完成 </t-button>
  561. </div>
  562. </template>
  563. </t-dialog>
  564. <t-dialog
  565. v-if="publishDialog.show"
  566. width="700px"
  567. header="云发布"
  568. :visible="true"
  569. :cancel-btn="null"
  570. :confirm-btn="'完成'"
  571. @close="publishDialog.show = false"
  572. @confirm="publishDialog.show = false"
  573. >
  574. <div class="body" style="height: 350px">
  575. <div class="form-item mt-8">
  576. <label>项目名称</label>
  577. <div class="flex middle">
  578. <t-icon name="laptop" class="mr-8" /> {{ publishDialog.name }}
  579. </div>
  580. </div>
  581. <template v-if="publishDialog.data">
  582. <div class="form-item mt-8">
  583. <label>发布状态</label>
  584. <div v-if="publishDialog.data.id" class="flex middle">
  585. <div v-if="publishDialog.data.isExpired" class="warning">
  586. <t-icon name="stop-circle-1" class="mr-4" />
  587. 已过期
  588. </div>
  589. <div v-else class="primary">
  590. <t-icon name="play-circle" class="mr-4" />
  591. 正在运行
  592. </div>
  593. <div class="gray" style="margin-left: 32px; margin-top: -2px">
  594. 有效期至:{{ publishDialog.data.expired }}
  595. </div>
  596. <a class="bland ml-12" @click="publishChargeDialog.show = true">
  597. 续费
  598. </a>
  599. </div>
  600. <div v-else class="flex middle">
  601. <t-icon name="play-circle-stroke" class="mr-4" />
  602. 未发布
  603. </div>
  604. </div>
  605. <div class="form-item mt-16">
  606. <label>乐吾乐域名</label>
  607. <div class="flex w-full">
  608. <t-input
  609. v-model="publishDialog.data.subDomain"
  610. :maxlength="16"
  611. :status="publishDialog.status"
  612. :tips="publishDialog.tips"
  613. class="mr-4"
  614. style="width: 350px"
  615. placeholder="子域名"
  616. @change="onChangeSubdomain"
  617. />
  618. <t-tooltip content="随机生成">
  619. <t-icon
  620. name="refresh"
  621. class="hover"
  622. @click="refreshSubdomain"
  623. />
  624. </t-tooltip>
  625. <div class="mt-4 ml-16">v.le5le.com</div>
  626. </div>
  627. </div>
  628. <div class="form-item mt-24">
  629. <label>访问域名</label>
  630. <div class="w-full">
  631. <div class="flex middle">
  632. <t-input
  633. v-model="publishDialog.data.domain"
  634. style="width: 350px"
  635. placeholder="支持自定义域名"
  636. />
  637. <template v-if="publishDialog.data.domain">
  638. <a
  639. :href="`http://${publishDialog.data.domain}`"
  640. target="_blank"
  641. class="hover"
  642. style="margin-left: 36px; text-decoration: underline"
  643. >
  644. 点击访问
  645. </a>
  646. <t-popup
  647. placement="bottom"
  648. v-model="publishDialog.qrcodeVisible"
  649. >
  650. <a class="hover" style="margin-left: 32px">
  651. <t-icon name="qrcode" />
  652. <span
  653. style="margin-left: 2px; position: relative; top: 1px"
  654. >
  655. 手机访问
  656. </span>
  657. </a>
  658. <template #content>
  659. <div style="padding: 12px 12px 6px 12px">
  660. <img :src="publishDialog.url" />
  661. </div>
  662. </template>
  663. </t-popup>
  664. </template>
  665. </div>
  666. <div class="desc mt-12">
  667. <div class="title desc">- 注意</div>
  668. <div>仅支持48小时免费试用,到期后需要续费使用。</div>
  669. </div>
  670. <div class="desc mt-8">
  671. <div class="title desc">- 自定义域名</div>
  672. <div>
  673. 在域名服务商的DNS控制台设置好对应的CNAME,值为乐吾乐域名。
  674. <a
  675. href="https://doc.le5le.com/document/126505408"
  676. target="_blank"
  677. >
  678. 更多帮助
  679. </a>
  680. </div>
  681. </div>
  682. </div>
  683. </div>
  684. <div class="form-item mt-16">
  685. <label></label>
  686. <div class="w-full">
  687. <t-button @click="onPublish">发布</t-button>
  688. </div>
  689. </div>
  690. </template>
  691. <div v-else class="flex center" style="margin-top: 32px">
  692. <t-loading text="加载中..." size="small"></t-loading>
  693. </div>
  694. </div>
  695. </t-dialog>
  696. <t-dialog
  697. v-if="publishChargeDialog.show"
  698. v-model:visible="publishChargeDialog.show"
  699. header="云发布续费"
  700. :close-on-overlay-click="false"
  701. :width="700"
  702. :footer="false"
  703. >
  704. <ChargeCloudPublish
  705. :projectId="publishDialog.id"
  706. @success="onSuccessChargeCloud"
  707. />
  708. </t-dialog>
  709. </div>
  710. </template>
  711. <script lang="ts" setup>
  712. import { onMounted, onUnmounted, watch, ref, reactive } from 'vue';
  713. import { useRouter, useRoute } from 'vue-router';
  714. import {
  715. Meta2d,
  716. Options,
  717. Pen,
  718. deepClone,
  719. PenType,
  720. HoverType,
  721. } from '@meta2d/core';
  722. import localforage from 'localforage';
  723. import dayjs from 'dayjs';
  724. import QRCode from 'qrcode';
  725. import axios from 'axios';
  726. import { MessagePlugin } from 'tdesign-vue-next';
  727. import { registerBasicDiagram } from '@/services/register';
  728. import { useUser } from '@/services/user';
  729. import {
  730. cdn,
  731. getComponents,
  732. getLe5leV,
  733. updateCollection,
  734. } from '@/services/api';
  735. import {
  736. save,
  737. autoSave,
  738. newFile,
  739. SaveType,
  740. onScaleFull,
  741. onScaleWindow,
  742. useDot,
  743. } from '@/services/common';
  744. import { useSelection } from '@/services/selections';
  745. import { defaultFormat } from '@/services/defaults';
  746. import { checkData, localStorageName, Meta2dBackData } from '@/services/utils';
  747. import { debounce } from '@/services/debouce';
  748. import { s8 } from '@/services/random';
  749. import { setCookie } from '@/services/cookie';
  750. import ContextMenu from './ContextMenu.vue';
  751. import Network from './Network.vue';
  752. import Dataset from './Dataset.vue';
  753. import ChargeCloudPublish from './ChargeCloudPublish.vue';
  754. const router = useRouter();
  755. const route = useRoute();
  756. const { user, getUser } = useUser();
  757. const { dot, setDot } = useDot();
  758. const { select } = useSelection();
  759. const meta2dOptions: Options = {
  760. cdn,
  761. rule: true,
  762. background: '#1e2430',
  763. x: 32,
  764. y: 32,
  765. width: 1920,
  766. height: 1080,
  767. color: '#bdc7db',
  768. disableAnchor: true,
  769. defaultFormat: { ...defaultFormat },
  770. };
  771. let timer: any = 0;
  772. const shared = ref<boolean>(false);
  773. const qrcode = reactive({
  774. visible: false,
  775. url: '',
  776. });
  777. const publishDialog = reactive<any>({});
  778. const publishChargeDialog = reactive<any>({});
  779. onMounted(() => {
  780. meta2d = new Meta2d('meta2d', meta2dOptions);
  781. registerBasicDiagram();
  782. open(true);
  783. meta2d.on('active', active);
  784. meta2d.on('inactive', inactive);
  785. meta2d.on('scale', scaleSubscriber);
  786. meta2d.on('add', lineAdd);
  787. meta2d.on('opened', openedListener);
  788. meta2d.on('undo', patchFlag);
  789. meta2d.on('redo', patchFlag);
  790. meta2d.on('add', patchFlag);
  791. meta2d.on('delete', patchFlag);
  792. meta2d.on('rotatePens', patchFlag);
  793. meta2d.on('translatePens', patchFlag);
  794. // 所有编辑栏所做修改
  795. meta2d.on('components-update-value', patchFlag);
  796. meta2d.on('contextmenu', onContextmenu);
  797. meta2d.on('click', canvasClick);
  798. timer = setInterval(autoSave, 60000);
  799. window.onbeforeunload = () => {
  800. autoSave();
  801. };
  802. window.addEventListener('message', function (e) {
  803. if (typeof e.data !== 'string') {
  804. return;
  805. }
  806. let data = JSON.parse(e.data);
  807. if (typeof data === 'object') {
  808. meta2d.emit(data.name);
  809. } else {
  810. meta2d.emit(data);
  811. }
  812. });
  813. });
  814. const watcher = watch(
  815. () => route.query,
  816. async () => {
  817. open();
  818. }
  819. );
  820. const open = async (flag: boolean = false) => {
  821. if (route.query.token) {
  822. localStorage.setItem('token', route.query.token + '');
  823. setCookie('token', route.query.token + '');
  824. getUser();
  825. const newQuery = { ...route.query };
  826. delete newQuery.token;
  827. router.replace({
  828. path: '/',
  829. query: newQuery,
  830. });
  831. return;
  832. }
  833. if (route.query.id) {
  834. let ret: any;
  835. if (route.query.c) {
  836. ret = await getComponents(route.query.id + '');
  837. } else {
  838. ret = await getLe5leV(route.query.id + '');
  839. }
  840. if (ret) {
  841. meta2d.open(ret);
  842. if (!route.query.c) {
  843. shared.value = ret.shared;
  844. const qr: any = await QRCode.toDataURL(
  845. `https://view2d.le5le.com/?id=${route.query.id + ''}`
  846. );
  847. qrcode.url = qr;
  848. }
  849. }
  850. } else if (flag) {
  851. const data: string = await localforage.getItem(localStorageName);
  852. if (data) {
  853. meta2d.open(JSON.parse(data));
  854. }
  855. } else if (!sessionStorage.getItem('opening')) {
  856. meta2d.open({
  857. name: '新建项目',
  858. pens: [],
  859. enableMock: true,
  860. folder: route.query.folder,
  861. tags: [route.query.tags],
  862. } as any);
  863. }
  864. sessionStorage.removeItem('opening');
  865. !meta2d.store.data.x && (meta2d.store.data.x = meta2d.store.options.x || 0);
  866. !meta2d.store.data.y && (meta2d.store.data.y = meta2d.store.options.y || 0);
  867. };
  868. const openedListener = () => {
  869. const {
  870. locked,
  871. scale: canvasScale,
  872. fromArrow: canvasFromArrow,
  873. toArrow: canvasToArrow,
  874. } = meta2d.store.data;
  875. isLock.value = locked || 0;
  876. scale.value = Math.round(canvasScale * 100);
  877. fromArrow.value = canvasFromArrow || '';
  878. toArrow.value = canvasToArrow || '';
  879. };
  880. const patchFlag = () => {
  881. setDot();
  882. };
  883. onUnmounted(() => {
  884. clearInterval(timer);
  885. watcher();
  886. if (meta2d) {
  887. meta2d.destroy();
  888. }
  889. });
  890. const inactive = () => {
  891. select();
  892. };
  893. const active = (pens: Pen[]) => {
  894. select(pens);
  895. //格式刷处理
  896. if (one.value || always.value) {
  897. meta2d.formatPainter();
  898. one.value = false;
  899. }
  900. };
  901. const one = ref(false);
  902. const always = ref(false);
  903. const oneFormat = () => {
  904. if (one.value) {
  905. one.value = false;
  906. } else {
  907. one.value = true;
  908. meta2d.setFormatPainter();
  909. }
  910. if (always.value) {
  911. always.value = false;
  912. one.value = false;
  913. }
  914. };
  915. const alwaysFormat = () => {
  916. always.value = true;
  917. };
  918. const clearFormat = () => {
  919. always.value = false;
  920. one.value = false;
  921. meta2d.clearFormatPainter();
  922. };
  923. const scale = ref(100);
  924. const scaleSubscriber = (val: number) => {
  925. scale.value = Math.round(val * 100);
  926. };
  927. const dataDialog = reactive<any>({
  928. tab: 1,
  929. });
  930. const currentLineType = ref('curve');
  931. const lineTypes = reactive([
  932. { name: '曲线', icon: '#l-curve2', value: 'curve' },
  933. { name: '线段', icon: '#l-polyline', value: 'polyline' },
  934. { name: '直线', icon: '#l-line', value: 'line' },
  935. { name: '脑图曲线', icon: '#l-mind', value: 'mind' },
  936. ]);
  937. const changeLineType = (value: string) => {
  938. currentLineType.value = value;
  939. if (meta2d) {
  940. meta2d.store.options.drawingLineName = value;
  941. meta2d.canvas.drawingLineName && (meta2d.canvas.drawingLineName = value);
  942. meta2d.store.active?.forEach((pen) => {
  943. meta2d.updateLineType(pen, value);
  944. });
  945. }
  946. };
  947. const fromArrow = ref('');
  948. const fromArrows = [
  949. { icon: '#l-line', value: '' },
  950. { icon: '#l-from-triangle', value: 'triangle' },
  951. { icon: '#l-from-diamond', value: 'diamond' },
  952. { icon: '#l-from-circle', value: 'circle' },
  953. { icon: '#l-from-lineDown', value: 'lineDown' },
  954. { icon: '#l-from-lineUp', value: 'lineUp' },
  955. { icon: '#l-from-triangleSolid', value: 'triangleSolid' },
  956. { icon: '#l-from-diamondSolid', value: 'diamondSolid' },
  957. { icon: '#l-from-circleSolid', value: 'circleSolid' },
  958. { icon: '#l-from-line', value: 'line' },
  959. ];
  960. const toArrow = ref('');
  961. const toArrows = [
  962. { icon: '#l-line', value: '' },
  963. { icon: '#l-to-triangle', value: 'triangle' },
  964. { icon: '#l-to-diamond', value: 'diamond' },
  965. { icon: '#l-to-circle', value: 'circle' },
  966. { icon: '#l-to-lineDown', value: 'lineDown' },
  967. { icon: '#l-to-lineUp', value: 'lineUp' },
  968. { icon: '#l-to-triangleSolid', value: 'triangleSolid' },
  969. { icon: '#l-to-diamondSolid', value: 'diamondSolid' },
  970. { icon: '#l-to-circleSolid', value: 'circleSolid' },
  971. { icon: '#l-to-line', value: 'line' },
  972. ];
  973. const changeFromArrow = (value: string) => {
  974. fromArrow.value = value;
  975. // 画布默认值
  976. meta2d.store.data.fromArrow = value;
  977. // 活动层的箭头都变化
  978. if (meta2d.store.active) {
  979. meta2d.store.active.forEach((pen: Pen) => {
  980. if (pen.type === PenType.Line) {
  981. pen.fromArrow = value;
  982. meta2d.setValue(
  983. {
  984. id: pen.id,
  985. fromArrow: pen.fromArrow,
  986. },
  987. {
  988. render: false,
  989. }
  990. );
  991. }
  992. });
  993. meta2d.render();
  994. }
  995. };
  996. const changeToArrow = (value: string) => {
  997. toArrow.value = value;
  998. // 画布默认值
  999. meta2d.store.data.toArrow = value;
  1000. // 活动层的箭头都变化
  1001. if (meta2d.store.active) {
  1002. meta2d.store.active.forEach((pen: Pen) => {
  1003. if (pen.type === PenType.Line) {
  1004. pen.toArrow = value;
  1005. meta2d.setValue(
  1006. {
  1007. id: pen.id,
  1008. toArrow: pen.toArrow,
  1009. },
  1010. {
  1011. render: false,
  1012. }
  1013. );
  1014. }
  1015. });
  1016. meta2d.render();
  1017. }
  1018. };
  1019. const oneD = ref<boolean>(false);
  1020. const alwaysD = ref<boolean>(false);
  1021. const oneDraw = () => {
  1022. if (oneD.value) {
  1023. oneD.value = false;
  1024. if (!alwaysD.value) {
  1025. meta2d.finishDrawLine();
  1026. meta2d.drawLine();
  1027. meta2d.store.options.disableAnchor = true;
  1028. }
  1029. } else {
  1030. oneD.value = true;
  1031. meta2d.drawLine(meta2d.store.options.drawingLineName);
  1032. meta2d.store.options.disableAnchor = false;
  1033. }
  1034. if (alwaysD.value) {
  1035. meta2d.finishDrawLine();
  1036. meta2d.drawLine();
  1037. oneD.value = false;
  1038. alwaysD.value = false;
  1039. meta2d.store.options.disableAnchor = true;
  1040. }
  1041. };
  1042. const alwaysDraw = () => {
  1043. alwaysD.value = true;
  1044. meta2d.drawLine(meta2d.store.options.drawingLineName);
  1045. meta2d.store.options.disableAnchor = false;
  1046. };
  1047. const lineAdd = (pens: Pen[]) => {
  1048. if (pens.length === 1 && pens[0].name === 'line') {
  1049. //连线类型
  1050. if (oneD.value && !alwaysD.value) {
  1051. if (meta2d.canvas.drawingLineName) {
  1052. oneD.value = false;
  1053. setTimeout(() => {
  1054. meta2d.finishDrawLine();
  1055. meta2d.drawLine();
  1056. meta2d.store.options.disableAnchor = true;
  1057. }, 100);
  1058. }
  1059. }
  1060. }
  1061. };
  1062. const onAddShape = (event: DragEvent | MouseEvent, name: string) => {
  1063. event.stopPropagation();
  1064. let data: any;
  1065. if (name === 'text') {
  1066. data = {
  1067. text: 'text',
  1068. width: 100,
  1069. height: 20,
  1070. name: 'text',
  1071. };
  1072. } else if (name === 'line') {
  1073. data = {
  1074. anchors: [
  1075. { id: '0', x: 1, y: 0 },
  1076. { id: '1', x: 0, y: 1 },
  1077. ],
  1078. width: 100,
  1079. height: 100,
  1080. name: 'line',
  1081. lineName: 'line',
  1082. type: 1,
  1083. };
  1084. }
  1085. if (!(event as DragEvent).dataTransfer) {
  1086. meta2d.canvas.addCaches = deepClone([data]);
  1087. } else {
  1088. (event as DragEvent).dataTransfer?.setData('Meta2d', JSON.stringify(data));
  1089. }
  1090. };
  1091. const isLock = ref(0);
  1092. function onLock(lock: number) {
  1093. isLock.value = lock;
  1094. meta2d.lock(lock);
  1095. meta2d.hideInput();
  1096. }
  1097. const canScale = ref(true);
  1098. function onCanScale(can: boolean) {
  1099. canScale.value = can;
  1100. meta2d.setOptions({
  1101. disableScale: can,
  1102. });
  1103. }
  1104. const preview = async () => {
  1105. meta2d.stopAnimate();
  1106. // @ts-ignore
  1107. const data: Meta2dBackData = meta2d.data();
  1108. checkData(data);
  1109. if (dot && user && data._id) {
  1110. // 有 id ,是修改后保存
  1111. await save(SaveType.Save);
  1112. }
  1113. if (!data._id) {
  1114. await localforage.setItem(localStorageName, JSON.stringify(data));
  1115. }
  1116. // router.push({
  1117. // path: '/preview',
  1118. // query: {
  1119. // r: Date.now() + '',
  1120. // id: data._id,
  1121. // },
  1122. // });
  1123. let screenWidth = window.screen.width;
  1124. let screenHeight = window.screen.height;
  1125. let url = `/preview?r=${Date.now() + ''}`;
  1126. if (data._id) {
  1127. url += `&id=${data._id}`;
  1128. }
  1129. window.open(
  1130. url,
  1131. '',
  1132. `height=${screenHeight},width=${screenWidth},top=0,left=0,toolbar=no,menubar=no, scrollbars=no,resizable=no,location=no, status=no`
  1133. );
  1134. };
  1135. const contextmenu = reactive<any>({
  1136. visible: false,
  1137. type: '',
  1138. style: {},
  1139. });
  1140. const onContextmenu = ({ e, rect }: { e: any; rect: any }) => {
  1141. contextmenu.type = '';
  1142. contextmenu.style = {
  1143. left: e.clientX + 'px',
  1144. top: e.clientY + 'px',
  1145. };
  1146. if (
  1147. meta2d.store.hoverAnchor &&
  1148. meta2d.canvas.hoverType === HoverType.LineAnchor
  1149. ) {
  1150. contextmenu.type = 'anchor';
  1151. if (document.body.clientHeight - e.clientY < 128) {
  1152. contextmenu.style = {
  1153. left: e.clientX + 'px',
  1154. bottom: '4px',
  1155. };
  1156. }
  1157. } else {
  1158. contextmenu.type = 'pen';
  1159. if (document.body.clientHeight - e.clientY < 450) {
  1160. contextmenu.style = {
  1161. left: e.clientX + 'px',
  1162. bottom: '4px',
  1163. };
  1164. }
  1165. }
  1166. if (contextmenu.type) {
  1167. contextmenu.visible = true;
  1168. } else {
  1169. contextmenu.visible = false;
  1170. }
  1171. };
  1172. const canvasClick = () => {
  1173. contextmenu.visible = false;
  1174. };
  1175. const changeContextMenuVisible = (e: boolean) => {
  1176. contextmenu.visible = e;
  1177. };
  1178. const networkColumns = ref([
  1179. {
  1180. colKey: 'name',
  1181. title: '名称',
  1182. ellipsis: true,
  1183. width: 160,
  1184. },
  1185. {
  1186. colKey: 'protocol',
  1187. title: '通信方式',
  1188. width: 120,
  1189. },
  1190. {
  1191. colKey: 'url',
  1192. title: 'URL地址',
  1193. ellipsis: true,
  1194. },
  1195. { colKey: 'actions', title: '操作', width: 100 },
  1196. ]);
  1197. const onShowDataDialog = async () => {
  1198. dataDialog.input = '';
  1199. dataDialog.networks = meta2d.store.data.networks || [];
  1200. // @ts-ignore
  1201. dataDialog.dataset = meta2d.store.data.dataset || {};
  1202. dataDialog.datasetId = dataDialog.dataset.id;
  1203. // @ts-ignore
  1204. dataDialog.enableMock = meta2d.store.data.enableMock;
  1205. dataDialog.networkInterval = meta2d.store.data.networkInterval;
  1206. dataDialog.networkList = [];
  1207. dataDialog.datasetList = [];
  1208. dataDialog.editNetwork = false;
  1209. dataDialog.save = true;
  1210. dataDialog.show = true;
  1211. getNetworks();
  1212. await getDatasets();
  1213. onSelDataset(true);
  1214. };
  1215. const onFinishDataDialog = () => {
  1216. dataDialog.show = false;
  1217. if (dataDialog.networkInterval) {
  1218. meta2d.store.data.networkInterval = dataDialog.networkInterval;
  1219. }
  1220. meta2d.connectNetwork();
  1221. };
  1222. const onChangeMock = () => {
  1223. // @ts-ignore
  1224. meta2d.store.data.enableMock = dataDialog.enableMock;
  1225. };
  1226. const onSelectNetWork = (item: any) => {
  1227. const network: any = dataDialog.networks.find(
  1228. (elem: any) => item.id === elem.id
  1229. );
  1230. if (!network) {
  1231. dataDialog.networks.push(item);
  1232. meta2d.store.data.networks = dataDialog.networks;
  1233. meta2d.connectNetwork();
  1234. setDot(true);
  1235. }
  1236. dataDialog.popupVisible = false;
  1237. };
  1238. const onDelNetWork = async (item: any, i: number) => {
  1239. const ret: any = await axios.post(`/api/data/datasources/delete`, {
  1240. id: item._id || item.id,
  1241. });
  1242. if (ret) {
  1243. dataDialog.networkList.splice(i, 1);
  1244. }
  1245. };
  1246. const onInputNetwork = () => {
  1247. debounce(getNetworks, 300);
  1248. };
  1249. // 请求我的实时数据
  1250. const getNetworks = async () => {
  1251. const ret: any = await axios.post(
  1252. `/api/data/datasources/list`,
  1253. {
  1254. q: {
  1255. name: dataDialog.input,
  1256. },
  1257. query: {
  1258. type: 'subscribe',
  1259. },
  1260. projection: { updatedAt: 0 },
  1261. },
  1262. {
  1263. params: {
  1264. current: 1,
  1265. pageSize: 10,
  1266. },
  1267. }
  1268. );
  1269. if (ret?.list) {
  1270. for (const item of ret.list) {
  1271. item.id = item.id || item._id;
  1272. }
  1273. dataDialog.networkList = ret.list;
  1274. }
  1275. };
  1276. const onInputDatasets = (name: string) => {
  1277. debounce(getDatasets, 300, name);
  1278. };
  1279. // 请求我的数据模型
  1280. const getDatasets = async (name?: string) => {
  1281. const body: any = {
  1282. query: {
  1283. type: 'dataset',
  1284. },
  1285. projection: { updatedAt: 0 },
  1286. };
  1287. if (name) {
  1288. body.q = { name };
  1289. }
  1290. const ret: any = await axios.post(`/api/data/datasources/list`, body, {
  1291. params: {
  1292. current: 1,
  1293. pageSize: 10,
  1294. },
  1295. });
  1296. if (ret?.list) {
  1297. let found = false;
  1298. for (const item of ret.list) {
  1299. item.id = item.id || item._id;
  1300. if (dataDialog.dataset?.id === item.id) {
  1301. found = true;
  1302. }
  1303. }
  1304. if (dataDialog.dataset?.id && !found) {
  1305. ret.list.push(dataDialog.dataset);
  1306. }
  1307. dataDialog.datasetList = ret.list;
  1308. }
  1309. };
  1310. const addNetwork = () => {
  1311. dataDialog.network = {
  1312. name: '',
  1313. type: 'subscribe',
  1314. protocol: 'mqtt',
  1315. url: '',
  1316. options: {
  1317. clientId: '',
  1318. username: '',
  1319. password: '',
  1320. customClientId: false,
  1321. },
  1322. };
  1323. dataDialog.editNetwork = 1;
  1324. };
  1325. const editNetwork = (data: any, index: number) => {
  1326. dataDialog.networkBak = data;
  1327. dataDialog.network = JSON.parse(JSON.stringify(data));
  1328. dataDialog.editNetwork = 2;
  1329. dataDialog.editNetworkIndex = index;
  1330. };
  1331. const deleteNetwork = (index: number) => {
  1332. dataDialog.networks.splice(index, 1);
  1333. meta2d.store.data.networks = dataDialog.networks;
  1334. meta2d.connectNetwork();
  1335. setDot(true);
  1336. };
  1337. const onOkNetwork = async () => {
  1338. if (dataDialog.editNetwork === 1) {
  1339. if (dataDialog.save) {
  1340. const ret: any = await axios.post(
  1341. `/api/data/datasources/add`,
  1342. dataDialog.network
  1343. );
  1344. if (!ret) {
  1345. return;
  1346. }
  1347. ret.id = ret.id || ret._id;
  1348. dataDialog.network._id = dataDialog.network.id = ret.id;
  1349. }
  1350. dataDialog.networks.push(dataDialog.network);
  1351. dataDialog.networkList.push(dataDialog.network);
  1352. } else if (dataDialog.editNetwork === 2) {
  1353. if (dataDialog.save) {
  1354. const ret: any = await axios.post(
  1355. `/api/data/datasources/update`,
  1356. dataDialog.network
  1357. );
  1358. if (!ret) {
  1359. return;
  1360. }
  1361. }
  1362. //替换
  1363. let index = dataDialog.editNetworkIndex;
  1364. if (index !== undefined) {
  1365. dataDialog.networks.splice(index, 1, dataDialog.network);
  1366. dataDialog.networkList.splice(index, 1, dataDialog.network);
  1367. }
  1368. }
  1369. dataDialog.editNetwork = false;
  1370. meta2d.store.data.networks = dataDialog.networks;
  1371. meta2d.connectNetwork();
  1372. setDot(true);
  1373. delete dataDialog.networkBak;
  1374. };
  1375. const datasetColumns = ref([
  1376. {
  1377. colKey: 'device',
  1378. title: '设备',
  1379. ellipsis: true,
  1380. },
  1381. {
  1382. colKey: 'label',
  1383. title: '数据点名称',
  1384. ellipsis: true,
  1385. },
  1386. {
  1387. colKey: 'id',
  1388. title: '数据点ID',
  1389. ellipsis: true,
  1390. },
  1391. {
  1392. colKey: 'type',
  1393. title: '类型',
  1394. ellipsis: true,
  1395. },
  1396. {
  1397. colKey: 'mock',
  1398. title: '值范围',
  1399. ellipsis: true,
  1400. },
  1401. ]);
  1402. const addDataset = () => {
  1403. dataDialog.dataset = {
  1404. name: '',
  1405. type: 'dataset',
  1406. mode: 'api',
  1407. url: '',
  1408. data: [],
  1409. };
  1410. dataDialog.editDataset = 1;
  1411. };
  1412. const editDataset = () => {
  1413. dataDialog.datasetBak = dataDialog.dataset;
  1414. dataDialog.dataset = JSON.parse(JSON.stringify(dataDialog.dataset));
  1415. dataDialog.editDataset = 2;
  1416. };
  1417. const onOkDataset = async (saveas = false) => {
  1418. if (!dataDialog.dataset.name) {
  1419. MessagePlugin.error('名称不能为空');
  1420. return;
  1421. }
  1422. const dataset = JSON.parse(JSON.stringify(dataDialog.dataset));
  1423. // 保存到我的数据源
  1424. if (saveas || dataDialog.editDataset === 1) {
  1425. delete dataset.id;
  1426. delete dataset._id;
  1427. const ret: any = await axios.post(`/api/data/datasources/add`, dataset);
  1428. if (!ret) {
  1429. return;
  1430. }
  1431. ret.id = ret.id || ret._id;
  1432. dataDialog.datasetId = ret.id;
  1433. dataset._id = dataset.id = ret.id;
  1434. dataDialog.datasetList.push(dataset);
  1435. } else {
  1436. const ret: any = await axios.post(`/api/data/datasources/update`, dataset);
  1437. if (!ret) {
  1438. return;
  1439. }
  1440. }
  1441. delete dataset.data;
  1442. // @ts-ignore
  1443. meta2d.store.data.dataset = dataset;
  1444. setDot(true);
  1445. dataDialog.editDataset = false;
  1446. delete dataDialog.datesetBak;
  1447. };
  1448. const onDelDataset = async (item: any, i: number) => {
  1449. const ret: any = await axios.post(`/api/data/datasources/delete`, {
  1450. id: item.id,
  1451. });
  1452. if (ret) {
  1453. dataDialog.datasetList.splice(i, 1);
  1454. }
  1455. };
  1456. const onSelDataset = async (init = false) => {
  1457. if (dataDialog.datasetId) {
  1458. const dataset = dataDialog.datasetList.find((item: any) => {
  1459. return item.id === dataDialog.datasetId;
  1460. });
  1461. if (!dataset) {
  1462. return;
  1463. }
  1464. if (dataset.url) {
  1465. const ret = await axios.get(dataset.url);
  1466. if (ret) {
  1467. dataset.data = ret;
  1468. }
  1469. } else {
  1470. const ret = await axios.post(`/api/data/datasources/get`, {
  1471. id: dataset.id,
  1472. });
  1473. if (ret?.data) {
  1474. dataset.data = ret.data;
  1475. }
  1476. }
  1477. dataDialog.dataset = dataset;
  1478. if (!init) {
  1479. const d = JSON.parse(JSON.stringify(dataset));
  1480. delete d.data;
  1481. // @ts-ignore
  1482. meta2d.store.data.dataset = d;
  1483. setDot(true);
  1484. }
  1485. }
  1486. };
  1487. const share = async () => {
  1488. if (!route.query.id) {
  1489. MessagePlugin.error('请先保存!');
  1490. return;
  1491. }
  1492. const ret: any = await updateCollection('le5leV', {
  1493. _id: route.query.id,
  1494. shared: !shared.value,
  1495. });
  1496. if (ret) {
  1497. shared.value = ret.shared;
  1498. }
  1499. };
  1500. let qrTimer: any;
  1501. const onEnterQrcode = () => {
  1502. if (!qrcode.visible && !qrTimer) {
  1503. qrTimer = setTimeout(() => {
  1504. qrTimer = undefined;
  1505. qrcode.visible = true;
  1506. }, 300);
  1507. }
  1508. };
  1509. const onLeaveQrcode = () => {
  1510. if (qrTimer) {
  1511. clearTimeout(qrTimer);
  1512. qrTimer = undefined;
  1513. }
  1514. };
  1515. const onShowPublish = async () => {
  1516. // @ts-ignore
  1517. publishDialog.name = meta2d.store.data.name;
  1518. publishDialog.status = '';
  1519. publishDialog.tips = '';
  1520. publishDialog.id = route.query.id + '';
  1521. publishDialog.show = true;
  1522. const ret: any = await axios.post(`/api/domain/get`, {
  1523. id: publishDialog.id,
  1524. });
  1525. if (ret) {
  1526. if (ret.expired) {
  1527. const expired = new Date(ret.expired);
  1528. if (expired > new Date()) {
  1529. ret.isExpired = false;
  1530. } else if (expired > new Date('2023-01-17T08:00:00+08:00')) {
  1531. ret.isExpired = true;
  1532. } else {
  1533. ret.expired = undefined;
  1534. }
  1535. if (ret.expired) {
  1536. ret.expired = dayjs(ret.expired).format('YYYY-MM-DD HH:mm:ss');
  1537. }
  1538. }
  1539. publishDialog.data = ret;
  1540. makePublishQrcode();
  1541. }
  1542. };
  1543. const refreshSubdomain = async () => {
  1544. publishDialog.data.subDomain = s8();
  1545. onChangeSubdomain();
  1546. };
  1547. const onChangeSubdomain = async () => {
  1548. if (!publishDialog.data.subDomain) {
  1549. publishDialog.status = 'error';
  1550. publishDialog.tips = '请填写子域名';
  1551. }
  1552. const ret: any = await axios.post(`/api/domain/exists`, {
  1553. subDomain: publishDialog.data.subDomain,
  1554. });
  1555. if (ret && ret.id) {
  1556. publishDialog.status = 'error';
  1557. publishDialog.tips = '已经存在相同子域名';
  1558. } else {
  1559. publishDialog.status = '';
  1560. publishDialog.tips = '';
  1561. }
  1562. if (
  1563. !publishDialog.data.domain ||
  1564. publishDialog.data.domain.indexOf('.v.le5le.com') >= 0
  1565. ) {
  1566. publishDialog.data.domain = publishDialog.data.subDomain + '.v.le5le.com';
  1567. }
  1568. makePublishQrcode();
  1569. };
  1570. const onPublish = async () => {
  1571. if (publishDialog.status) {
  1572. return;
  1573. }
  1574. if (!publishDialog.data.subDomain) {
  1575. publishDialog.status = 'error';
  1576. publishDialog.tips = '请填写子域名';
  1577. }
  1578. if (!publishDialog.data.domain) {
  1579. publishDialog.data.domain = publishDialog.data.subDomain + '.v.le5le.com';
  1580. }
  1581. const ret: any = await axios.post(`/api/domain/set`, {
  1582. id: route.query.id,
  1583. subDomain: publishDialog.data.subDomain,
  1584. domain: publishDialog.data.domain,
  1585. });
  1586. if (ret) {
  1587. if (ret.expired) {
  1588. const expired = new Date(ret.expired);
  1589. if (expired > new Date()) {
  1590. ret.isExpired = false;
  1591. } else if (expired > new Date('2023-01-17T08:00:00+08:00')) {
  1592. ret.isExpired = true;
  1593. } else {
  1594. ret.expired = undefined;
  1595. }
  1596. if (ret.expired) {
  1597. ret.expired = dayjs(ret.expired).format('YYYY-MM-DD HH:mm:ss');
  1598. }
  1599. }
  1600. publishDialog.data = ret;
  1601. makePublishQrcode();
  1602. }
  1603. };
  1604. const makePublishQrcode = async () => {
  1605. const qr: any = await QRCode.toDataURL(`http://${publishDialog.data.domain}`);
  1606. publishDialog.url = qr;
  1607. };
  1608. const onSuccessChargeCloud = () => {
  1609. publishChargeDialog.show = false;
  1610. onShowPublish();
  1611. };
  1612. </script>
  1613. <style lang="postcss" scoped>
  1614. .meta2d {
  1615. display: flex;
  1616. flex-direction: column;
  1617. background-color: var(--color-background-editor);
  1618. border-left: 1px solid var(--color-border);
  1619. .tools {
  1620. display: flex;
  1621. font-size: 12px;
  1622. background-color: var(--color-background);
  1623. height: 40px;
  1624. flex-shrink: 0;
  1625. padding: 0 12px;
  1626. z-index: 2;
  1627. a {
  1628. display: flex;
  1629. align-items: center;
  1630. height: 100%;
  1631. padding: 0 10px;
  1632. color: var(--color);
  1633. text-decoration: none;
  1634. .l-icon {
  1635. width: 16px;
  1636. height: 16px;
  1637. }
  1638. &:hover {
  1639. color: var(--color-primary);
  1640. }
  1641. }
  1642. .t-icon {
  1643. font-size: 16px;
  1644. }
  1645. }
  1646. #meta2d {
  1647. border-top: 1px solid var(--color-background-input);
  1648. height: calc(100vh - 81px);
  1649. z-index: 1;
  1650. :deep(.meta2d-map) {
  1651. background: var(--color-background);
  1652. }
  1653. }
  1654. }
  1655. </style>