View.vue 50 KB

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