Network.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <template>
  2. <div class="network-component">
  3. <div class="form-item mt-8">
  4. <label>
  5. {{ modelValue.type === 'subscribe' ? $t('数据获取') : $t('发送到') }}
  6. </label>
  7. <t-select-input
  8. v-if="mode"
  9. v-model:inputValue="modelValue.name"
  10. :value="modelValue.name"
  11. :placeholder="$t('我的数据发送')"
  12. allow-input
  13. clearable
  14. v-model:popup-visible="popupVisible"
  15. @focus="popupVisible = true"
  16. @blur="popupVisible = false"
  17. @input-change="onInput"
  18. @clear="
  19. modelValue.name = undefined;
  20. "
  21. >
  22. <template #panel>
  23. <ul style="padding: 4px">
  24. <li class="hover-background item" @click="() => {onSelect({protocol:'http',name:'HTTP'}); protocolChange('http') }">
  25. HTTP
  26. </li>
  27. <li class="hover-background item" @click="() => {onSelect({protocol:'websocket',name:'Websocket'}); protocolChange('websocket')}">
  28. Websocket
  29. </li>
  30. <li class="hover-background item" @click="() => {onSelect({protocol:'mqtt',name:'MQTT'});protocolChange('mqtt')}">
  31. MQTT
  32. </li>
  33. <t-divider style="border-top: 1px solid var(--color-border-input);"></t-divider>
  34. <li v-if="hasIot" class="hover-background item" @click="() => onSelect({protocol:'iot',name:'物联网平台'})">
  35. 物联网平台
  36. </li>
  37. <li
  38. class="hover-background item"
  39. style="line-height: 1.5; padding: 8px; border-radius: 2px"
  40. v-for="(item, i) in networkList"
  41. :key="item.url"
  42. @click="() => onSelect(item)"
  43. >
  44. 名称: {{ item.name }}
  45. <div v-if="item.url" class="desc">地址: {{ item.url }}</div>
  46. <!-- <span class="del" @click.stop="onDelNetWork(item, i)">
  47. <delete-icon />
  48. </span> -->
  49. </li>
  50. <li
  51. v-if="networkList.length >= 10"
  52. style="line-height: 1.5; padding: 8px; border-radius: 2px"
  53. :key="-1"
  54. >
  55. <div class="desc">...</div>
  56. </li>
  57. <li v-if="!networkList.length" style="line-height: 1.5; padding: 8px; border-radius: 2px" :key="-1">
  58. <div class="desc">{{$t('暂无数据')}}</div>
  59. </li>
  60. </ul>
  61. </template>
  62. </t-select-input>
  63. <t-input v-else v-model="modelValue.name" :placeholder="$t('名称')"></t-input>
  64. </div>
  65. <template v-if="modelValue.protocol&&modelValue.protocol!=='iot'">
  66. <!-- <div v-if="!modelValue.unmodifiable" class="form-item mt-8">
  67. <label>通信方式</label>
  68. <t-select
  69. v-model="modelValue.protocol"
  70. placeholder="MQTT"
  71. @change="protocolChange"
  72. >
  73. <t-option key="mqtt" value="mqtt" label="MQTT" />
  74. <t-option key="websocket" value="websocket" label="Websocket" />
  75. <t-option key="http" value="http" label="HTTP" />
  76. </t-select>
  77. </div> -->
  78. <div v-if="!modelValue.unmodifiable" class="form-item mt-8">
  79. <label>{{$t('URL地址')}}</label>
  80. <t-input
  81. :format="urlFormat"
  82. :placeholder="
  83. modelValue.protocol !== 'http'
  84. ? isSafeProtocol()
  85. ? $t('必须是wss协议')
  86. : $t('必须是ws协议')
  87. : $t('请输入')
  88. " v-model="modelValue.url"></t-input>
  89. </div>
  90. <template v-if="modelValue.protocol === 'websocket'&&modelValue.options">
  91. <div v-if="!modelValue.unmodifiable" class="form-item mt-8">
  92. <label>protocols</label>
  93. <t-input v-model="modelValue.options.protocols" />
  94. </div>
  95. </template>
  96. <template v-else-if="modelValue.protocol === 'http'">
  97. <div class="form-item mt-8">
  98. <label>{{$t('请求方式')}}</label>
  99. <t-select :disabled="modelValue.unmodifiable" v-model="modelValue.method" @change="httpMethodChange">
  100. <t-option key="GET" value="GET" label="GET" />
  101. <t-option key="POST" value="POST" label="POST" />
  102. </t-select>
  103. </div>
  104. <div v-if="modelValue.type === 'subscribe'" class="form-item mt-8">
  105. <label>{{$t('请求间隔')}}</label>
  106. <t-input-number theme="column" v-model="modelValue.interval" :placeholder="$t('默认1000 ms')"></t-input-number>
  107. </div>
  108. <div class="form-item mt-8">
  109. <label>{{$t('请求头')}}</label>
  110. <!-- <t-textarea
  111. v-model="modelValue.headers"
  112. :autosize="{ minRows: 3, maxRows: 5 }"
  113. placeholder="请输入"
  114. /> -->
  115. <!-- <CodeEditor
  116. :json="true"
  117. :language="'json'"
  118. v-model="modelValue.headers"
  119. class="mt-4"
  120. style="height: 50px"
  121. /> -->
  122. <t-button
  123. class="ml-8"
  124. shape="square"
  125. variant="outline"
  126. @click="showJsonModal()"
  127. >
  128. <ellipsis-icon slot="icon" />
  129. </t-button>
  130. </div>
  131. <!-- <div class="form-item mt-8 desc">
  132. <label></label>
  133. 支持设置动态参数,例如:{"Authorization": "Bearer ${token}"}
  134. </div> -->
  135. <div v-if="!mode && modelValue.method === 'POST'" class="form-item mt-8">
  136. <label>{{$t('请求体')}}</label>
  137. <!-- <t-textarea
  138. v-model="modelValue.body"
  139. :autosize="{ minRows: 3, maxRows: 5 }"
  140. placeholder="请输入"
  141. /> -->
  142. <CodeEditor
  143. :json="true"
  144. :language="'json'"
  145. v-model="modelValue.body"
  146. class="mt-4"
  147. style="height: 50px"
  148. />
  149. </div>
  150. <div
  151. v-if="!mode && modelValue.method === 'POST'"
  152. class="form-item mt-8 desc"
  153. >
  154. <label></label>
  155. {{$t('支持设置动态参数')}},{{$t('例如')}}:{"value": "${value}"}
  156. </div>
  157. </template>
  158. <template v-else-if="modelValue.protocol === 'mqtt'">
  159. <template v-if="modelValue.options&&!modelValue.unmodifiable">
  160. <div class="form-item mt-8">
  161. <label>Client Id</label>
  162. <t-input v-model="modelValue.options.clientId" />
  163. </div>
  164. <div class="form-item mt-8">
  165. <label>{{$t('自动生成')}}</label>
  166. <t-switch class="mt-8 ml-8" v-model="modelValue.options.customClientId" size="small"></t-switch>
  167. </div>
  168. <div class="form-item mt-8">
  169. <label>{{$t('用户名')}}</label>
  170. <t-input v-model="modelValue.options.username"></t-input>
  171. </div>
  172. <div class="form-item mt-8">
  173. <label>{{$t('密码')}}</label>
  174. <t-input v-model="modelValue.options.password"></t-input>
  175. </div>
  176. </template>
  177. <div class="form-item mt-8">
  178. <label>Topics</label>
  179. <t-input v-model="modelValue.topics" />
  180. </div>
  181. </template>
  182. </template>
  183. <!-- <div class="form-item mt-8" v-if="mode">
  184. <label> </label>
  185. <div>
  186. <t-button @click="onSave">{{$t('保存到我的数据发送')}}</t-button>
  187. </div>
  188. </div> -->
  189. </div>
  190. <JsonModal v-model:visible="codeDialog.visible" v-model:data="codeDialog.data" :options="headersOptions" tips='支持设置动态参数,例如:{"Authorization": "Bearer ${token}"}' @change="getJsonData" />
  191. </template>
  192. <script lang="ts" setup>
  193. import { onBeforeMount, ref, getCurrentInstance, reactive } from 'vue';
  194. import axios from 'axios';
  195. import { debounce } from '@/services/debouce';
  196. import { MessagePlugin } from 'tdesign-vue-next';
  197. import CodeEditor from '@/views/components/common/CodeEditor.vue';
  198. import { DeleteIcon } from 'tdesign-icons-vue-next';
  199. import { transformData } from '@/services/utils';
  200. import { deepClone } from '@meta2d/core';
  201. import { EllipsisIcon } from 'tdesign-icons-vue-next';
  202. import JsonModal from '@/views/components/common/JsonModal.vue';
  203. const { modelValue, mode } = defineProps<{
  204. modelValue: any;
  205. mode?: any;
  206. }>();
  207. const { proxy } = getCurrentInstance();
  208. const $t = proxy.$t
  209. const emit = defineEmits(['update:modelValue', 'change']);
  210. const popupVisible = ref<boolean>(false);
  211. const networkList = ref<any[]>([]);
  212. onBeforeMount(() => {
  213. modelValue.id = modelValue.id || modelValue._id;
  214. if (mode) {
  215. getNetworks();
  216. }
  217. });
  218. const protocolChange = (protocol: string) => {
  219. if (protocol === 'http') {
  220. Object.assign(modelValue, {
  221. httpTimeInterval: 1000,
  222. headers: '',
  223. method: 'GET',
  224. body: '',
  225. });
  226. } else if (protocol === 'websocket') {
  227. // modelValue.url = '';
  228. } else {
  229. Object.assign(modelValue, {
  230. options: {
  231. clientId: '',
  232. username: '',
  233. password: '',
  234. customClientId: false,
  235. },
  236. });
  237. }
  238. };
  239. const httpMethodChange = (method: string) => {
  240. if (method === 'GET') {
  241. modelValue.body = undefined;
  242. }
  243. };
  244. const onSave = async () => {
  245. emit('update:modelValue', modelValue);
  246. emit('change', modelValue);
  247. let ret: any;
  248. const data = transformData(modelValue,'toNetwork');
  249. // 保存到我的数据源
  250. if (modelValue.id) {
  251. ret = await axios.post(`/api/data/datasource/update`, data);
  252. } else {
  253. ret = await axios.post(`/api/data/datasource/add`, data);
  254. }
  255. if (ret) {
  256. MessagePlugin.success($t('保存成功!'));
  257. }
  258. };
  259. const onInput = (text: string) => {
  260. debounce(getNetworks, 300);
  261. };
  262. const hasIot = ref(false);
  263. // 请求我的数据源接口
  264. const getNetworks = async () => {
  265. let arr = [];
  266. meta2d.store.data.networks?.forEach((network)=>{
  267. if(['mqtt','websocket','http'].includes(network.protocol)&&network.method!=="POST"){
  268. let net:any = deepClone(network);
  269. if(net.protocol === 'http'){
  270. net.method = 'POST'
  271. }
  272. net.unmodifiable = true;
  273. net.type = '';
  274. net.value ='';
  275. net.tem_index='';
  276. net.label = '';
  277. arr.push(net);
  278. }
  279. });
  280. if(meta2d.store.data.iot?.tree?.length){
  281. hasIot.value = true;
  282. }
  283. networkList.value =arr;// deepClone(meta2d.store.data.networks);
  284. return;
  285. const body: any = {
  286. type: modelValue.type,
  287. projection: "id,data,name,type",
  288. };
  289. if (modelValue.name) {
  290. body.name = modelValue.name;
  291. }
  292. const ret: any = await axios.post(`/api/data/datasource/list`, body, {
  293. params: {
  294. current: 1,
  295. pageSize: 10,
  296. },
  297. });
  298. if (ret?.list) {
  299. const list = [];
  300. for (const item of ret.list) {
  301. item.id = item.id || item._id;
  302. list.push(transformData(item,'toMetaNetwork'));
  303. }
  304. networkList.value = list;
  305. }
  306. };
  307. const onSelect = (item: any) => {
  308. for (let key in modelValue) {
  309. if (modelValue.hasOwnProperty(key)) {
  310. delete modelValue[key];
  311. }
  312. }
  313. Object.assign(modelValue, item);
  314. popupVisible.value = false;
  315. };
  316. const onDelNetWork = async (item: any, i: number) => {
  317. const ret: any = await axios.post(`/api/data/datasource/delete`, {
  318. id: item._id || item.id,
  319. });
  320. if (ret) {
  321. networkList.value.splice(i, 1);
  322. }
  323. };
  324. const isSafeProtocol = () => {
  325. return document.location.protocol === 'https:' ? true : false;
  326. };
  327. const urlFormat = (val) => {
  328. if (modelValue.protocol === 'mqtt') {
  329. let reg = /^ws\:.*/;
  330. if (isSafeProtocol()) {
  331. reg = /^wss\:.*/;
  332. }
  333. if (reg.test(val)) {
  334. return val;
  335. } else {
  336. return '';
  337. }
  338. } else {
  339. return val;
  340. }
  341. };
  342. const codeDialog = reactive({
  343. visible:false,
  344. data:null
  345. });
  346. const showJsonModal = () => {
  347. codeDialog.visible = true;
  348. codeDialog.data = modelValue.headers;
  349. };
  350. const getJsonData = (data) => {
  351. modelValue.headers = data;
  352. codeDialog.visible = false;
  353. };
  354. const headersOptions = [{
  355. label: 'Authorization',
  356. value: 'Authorization',
  357. }]
  358. </script>
  359. <style lang="postcss" scoped>
  360. .network-component {
  361. }
  362. </style>