Network.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <div class="network-component">
  3. <div class="form-item mt-8">
  4. <label>
  5. {{ modelValue.type === 'subscribe' ? '数据通信' : '数据发送' }}
  6. </label>
  7. <t-select-input
  8. v-if="mode"
  9. v-model:inputValue="modelValue.name"
  10. :value="modelValue.name"
  11. placeholder="我的数据发送"
  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.id = undefined;
  20. modelValue._id = undefined;
  21. "
  22. >
  23. <template #panel>
  24. <ul style="padding: 4px">
  25. <li
  26. class="hover-background item"
  27. style="line-height: 1.5; padding: 8px; border-radius: 2px"
  28. v-for="(item, i) in networkList"
  29. :key="item.url"
  30. @click="() => onSelect(item)"
  31. >
  32. 名称: {{ item.name }}
  33. <div class="desc">地址: {{ item.url }}</div>
  34. <span class="del" @click.stop="onDelNetWork(item, i)">
  35. <delete-icon />
  36. <!-- <t-icon name="delete" /> -->
  37. </span>
  38. </li>
  39. <li
  40. v-if="networkList.length >= 10"
  41. style="line-height: 1.5; padding: 8px; border-radius: 2px"
  42. :key="-1"
  43. >
  44. <div class="desc">...</div>
  45. </li>
  46. <li
  47. v-if="!networkList.length"
  48. style="line-height: 1.5; padding: 8px; border-radius: 2px"
  49. :key="-1"
  50. >
  51. <div class="desc">暂无数据</div>
  52. </li>
  53. </ul>
  54. </template>
  55. </t-select-input>
  56. <t-input v-else v-model="modelValue.name" placeholder="名称" />
  57. </div>
  58. <div class="form-item mt-8">
  59. <label>通信方式</label>
  60. <t-select
  61. v-model="modelValue.protocol"
  62. placeholder="MQTT"
  63. @change="protocolChange"
  64. >
  65. <t-option key="mqtt" value="mqtt" label="MQTT" />
  66. <t-option key="websocket" value="websocket" label="Websocket" />
  67. <t-option key="http" value="http" label="HTTP" />
  68. </t-select>
  69. </div>
  70. <div class="form-item mt-8">
  71. <label>URL地址</label>
  72. <t-input
  73. :format="urlFormat"
  74. :placeholder="
  75. modelValue.protocol !== 'http'
  76. ? isSafeProtocol()
  77. ? '必须是wss协议'
  78. : '必须是ws协议'
  79. : '请输入'
  80. "
  81. v-model="modelValue.url"
  82. />
  83. </div>
  84. <template v-if="modelValue.protocol === 'websocket'">
  85. <div class="form-item mt-8">
  86. <label>protocols</label>
  87. <t-input v-model="modelValue.options.protocols" />
  88. </div>
  89. </template>
  90. <template v-else-if="modelValue.protocol === 'http'">
  91. <div class="form-item mt-8">
  92. <label>请求方式</label>
  93. <t-select v-model="modelValue.method" @change="httpMethodChange">
  94. <t-option key="GET" value="GET" label="GET" />
  95. <t-option key="POST" value="POST" label="POST" />
  96. </t-select>
  97. </div>
  98. <div class="form-item mt-8">
  99. <label>请求头</label>
  100. <!-- <t-textarea
  101. v-model="modelValue.headers"
  102. :autosize="{ minRows: 3, maxRows: 5 }"
  103. placeholder="请输入"
  104. /> -->
  105. <CodeEditor
  106. :json="true"
  107. v-model="modelValue.headers"
  108. class="mt-4"
  109. style="height: 50px"
  110. />
  111. </div>
  112. <div class="form-item mt-8 desc">
  113. <label></label>
  114. 支持设置动态参数,例如:{"Authorization": "Bearer ${token}"}
  115. </div>
  116. <div v-if="!mode && modelValue.method === 'POST'" class="form-item mt-8">
  117. <label>请求体</label>
  118. <!-- <t-textarea
  119. v-model="modelValue.body"
  120. :autosize="{ minRows: 3, maxRows: 5 }"
  121. placeholder="请输入"
  122. /> -->
  123. <CodeEditor
  124. :json="true"
  125. v-model="modelValue.body"
  126. class="mt-4"
  127. style="height: 50px"
  128. />
  129. </div>
  130. <div
  131. v-if="!mode && modelValue.method === 'POST'"
  132. class="form-item mt-8 desc"
  133. >
  134. <label></label>
  135. 支持设置动态参数,例如:{"value": "${value}"}
  136. </div>
  137. </template>
  138. <template v-else>
  139. <div class="form-item mt-8">
  140. <label>Client Id</label>
  141. <t-input v-model="modelValue.options.clientId" />
  142. </div>
  143. <div class="form-item mt-8">
  144. <label>自动生成</label>
  145. <t-switch
  146. class="mt-8 ml-8"
  147. v-model="modelValue.options.customClientId"
  148. size="small"
  149. />
  150. </div>
  151. <div class="form-item mt-8">
  152. <label>用户名</label>
  153. <t-input v-model="modelValue.options.username" />
  154. </div>
  155. <div class="form-item mt-8">
  156. <label>密码</label>
  157. <t-input v-model="modelValue.options.password" />
  158. </div>
  159. <div class="form-item mt-8">
  160. <label>Topics</label>
  161. <t-input v-model="modelValue.topics" />
  162. </div>
  163. </template>
  164. <div class="form-item mt-8" v-if="mode">
  165. <label> </label>
  166. <div>
  167. <t-button @click="onSave">保存到我的数据发送</t-button>
  168. </div>
  169. </div>
  170. </div>
  171. </template>
  172. <script lang="ts" setup>
  173. import { onBeforeMount, ref } from 'vue';
  174. import axios from 'axios';
  175. import { debounce } from '@/services/debouce';
  176. import { MessagePlugin } from 'tdesign-vue-next';
  177. import CodeEditor from '@/views/components/common/CodeEditor.vue';
  178. import { DeleteIcon } from 'tdesign-icons-vue-next';
  179. import { transformData } from '@/services/utils';
  180. const { modelValue, mode } = defineProps<{
  181. modelValue: any;
  182. mode?: any;
  183. }>();
  184. const emit = defineEmits(['update:modelValue', 'change']);
  185. const popupVisible = ref<boolean>(false);
  186. const networkList = ref<any[]>([]);
  187. onBeforeMount(() => {
  188. modelValue.id = modelValue.id || modelValue._id;
  189. if (mode) {
  190. getNetworks();
  191. }
  192. });
  193. const protocolChange = (protocol: string) => {
  194. if (protocol === 'http') {
  195. Object.assign(modelValue, {
  196. httpTimeInterval: 1000,
  197. headers: '',
  198. method: 'GET',
  199. body: '',
  200. });
  201. } else if (protocol === 'websocket') {
  202. // modelValue.url = '';
  203. } else {
  204. Object.assign(modelValue, {
  205. options: {
  206. clientId: '',
  207. username: '',
  208. password: '',
  209. customClientId: false,
  210. },
  211. });
  212. }
  213. };
  214. const httpMethodChange = (method: string) => {
  215. if (method === 'GET') {
  216. modelValue.body = undefined;
  217. }
  218. };
  219. const onSave = async () => {
  220. emit('update:modelValue', modelValue);
  221. emit('change', modelValue);
  222. let ret: any;
  223. const data = transformData(modelValue,'toNetwork');
  224. // 保存到我的数据源
  225. if (modelValue.id) {
  226. ret = await axios.post(`/api/data/datasource/update`, data);
  227. } else {
  228. ret = await axios.post(`/api/data/datasource/add`, data);
  229. }
  230. if (ret) {
  231. MessagePlugin.success('保存成功!');
  232. }
  233. };
  234. const onInput = (text: string) => {
  235. debounce(getNetworks, 300);
  236. };
  237. // 请求我的数据源接口
  238. const getNetworks = async () => {
  239. const body: any = {
  240. type: modelValue.type,
  241. projection: "id,data,name,type",
  242. };
  243. if (modelValue.name) {
  244. body.name = modelValue.name;
  245. }
  246. const ret: any = await axios.post(`/api/data/datasource/list`, body, {
  247. params: {
  248. current: 1,
  249. pageSize: 10,
  250. },
  251. });
  252. if (ret?.list) {
  253. const list = [];
  254. for (const item of ret.list) {
  255. item.id = item.id || item._id;
  256. list.push(transformData(item,'toMetaNetwork'));
  257. }
  258. networkList.value = list;
  259. }
  260. };
  261. const onSelect = (item: any) => {
  262. Object.assign(modelValue, item);
  263. popupVisible.value = false;
  264. };
  265. const onDelNetWork = async (item: any, i: number) => {
  266. const ret: any = await axios.post(`/api/data/datasource/delete`, {
  267. id: item._id || item.id,
  268. });
  269. if (ret) {
  270. networkList.value.splice(i, 1);
  271. }
  272. };
  273. const isSafeProtocol = () => {
  274. return document.location.protocol === 'https:' ? true : false;
  275. };
  276. const urlFormat = (val) => {
  277. if (modelValue.protocol === 'mqtt') {
  278. let reg = /^ws\:.*/;
  279. if (isSafeProtocol()) {
  280. reg = /^wss\:.*/;
  281. }
  282. if (reg.test(val)) {
  283. return val;
  284. } else {
  285. return '';
  286. }
  287. } else {
  288. return val;
  289. }
  290. };
  291. </script>
  292. <style lang="postcss" scoped>
  293. .network-component {
  294. }
  295. </style>