Net.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. <template>
  2. <div class="network-component">
  3. <div class="form-item mt-8">
  4. <label>
  5. 数据源名称
  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'&&modelValue.protocol !== 'SSE')
  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 v-if="modelValue.type === 'subscribe'" class="form-item mt-8">
  99. <label>请求间隔</label>
  100. <t-input-number
  101. theme="column"
  102. v-model="modelValue.interval"
  103. placeholder="默认1000 ms"
  104. />
  105. </div>
  106. <div class="form-item mt-8">
  107. <label>请求头</label>
  108. <!-- <t-textarea
  109. v-model="modelValue.headers"
  110. :autosize="{ minRows: 3, maxRows: 5 }"
  111. placeholder="请输入"
  112. /> -->
  113. <CodeEditor
  114. :json="true"
  115. :language="'json'"
  116. v-model="modelValue.headers"
  117. class="mt-4"
  118. style="height: 50px"
  119. />
  120. </div>
  121. <div class="form-item mt-8 desc">
  122. <label></label>
  123. 支持设置动态参数,例如:{"Authorization": "Bearer ${token}"}
  124. </div>
  125. <div v-if="!mode && modelValue.method === 'POST'" class="form-item mt-8">
  126. <label>请求体</label>
  127. <!-- <t-textarea
  128. v-model="modelValue.body"
  129. :autosize="{ minRows: 3, maxRows: 5 }"
  130. placeholder="请输入"
  131. /> -->
  132. <CodeEditor
  133. :json="true"
  134. :language="'json'"
  135. v-model="modelValue.body"
  136. class="mt-4"
  137. style="height: 50px"
  138. />
  139. </div>
  140. <div
  141. v-if="!mode && modelValue.method === 'POST'"
  142. class="form-item mt-8 desc"
  143. >
  144. <label></label>
  145. 支持设置动态参数,例如:{"value": "${value}"}
  146. </div>
  147. </template>
  148. <template v-else-if="modelValue.protocol === 'SSE'">
  149. <div class="form-item mt-8">
  150. <label>跨域凭据</label>
  151. <t-switch
  152. class="mt-8 ml-8"
  153. v-model="modelValue.withCredentials"
  154. size="small"
  155. />
  156. </div>
  157. </template>
  158. <template v-else>
  159. <div class="form-item mt-8">
  160. <label>Client Id</label>
  161. <t-input v-model="modelValue.options.clientId" />
  162. </div>
  163. <div class="form-item mt-8">
  164. <label>自动生成</label>
  165. <t-switch
  166. class="mt-8 ml-8"
  167. v-model="modelValue.options.customClientId"
  168. size="small"
  169. />
  170. </div>
  171. <div class="form-item mt-8">
  172. <label>用户名</label>
  173. <t-input v-model="modelValue.options.username" />
  174. </div>
  175. <div class="form-item mt-8">
  176. <label>密码</label>
  177. <t-input v-model="modelValue.options.password" />
  178. </div>
  179. <div class="form-item mt-8">
  180. <label>Topics</label>
  181. <t-input v-model="modelValue.topics" />
  182. </div>
  183. </template>
  184. <div class="form-item mt-8" v-if="mode">
  185. <label> </label>
  186. <div>
  187. <t-button @click="onSave">保存到我的数据发送</t-button>
  188. </div>
  189. </div>
  190. </div>
  191. </template>
  192. <script lang="ts" setup>
  193. import { onBeforeMount, ref } 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. const { modelValue, mode } = defineProps<{
  201. modelValue: any;
  202. mode?: any;
  203. }>();
  204. const emit = defineEmits(['update:modelValue', 'change']);
  205. const popupVisible = ref<boolean>(false);
  206. const networkList = ref<any[]>([]);
  207. onBeforeMount(() => {
  208. modelValue.id = modelValue.id || modelValue._id;
  209. if (mode) {
  210. getNetworks();
  211. }
  212. });
  213. const protocolChange = (protocol: string) => {
  214. if (protocol === 'http') {
  215. Object.assign(modelValue, {
  216. httpTimeInterval: 1000,
  217. headers: '',
  218. method: 'GET',
  219. body: '',
  220. });
  221. } else if (protocol === 'websocket') {
  222. // modelValue.url = '';
  223. } else {
  224. Object.assign(modelValue, {
  225. options: {
  226. clientId: '',
  227. username: '',
  228. password: '',
  229. customClientId: false,
  230. },
  231. });
  232. }
  233. };
  234. const httpMethodChange = (method: string) => {
  235. if (method === 'GET') {
  236. modelValue.body = undefined;
  237. }
  238. };
  239. const onSave = async () => {
  240. emit('update:modelValue', modelValue);
  241. emit('change', modelValue);
  242. let ret: any;
  243. const data = transformData(modelValue,'toNetwork');
  244. // 保存到我的数据源
  245. if (modelValue.id) {
  246. ret = await axios.post(`/api/data/datasource/update`, data);
  247. } else {
  248. ret = await axios.post(`/api/data/datasource/add`, data);
  249. }
  250. if (ret) {
  251. MessagePlugin.success('保存成功!');
  252. }
  253. };
  254. const onInput = (text: string) => {
  255. debounce(getNetworks, 300);
  256. };
  257. // 请求我的数据源接口
  258. const getNetworks = async () => {
  259. const body: any = {
  260. type: modelValue.type,
  261. projection: "id,data,name,type",
  262. };
  263. if (modelValue.name) {
  264. body.name = modelValue.name;
  265. }
  266. const ret: any = await axios.post(`/api/data/datasource/list`, body, {
  267. params: {
  268. current: 1,
  269. pageSize: 10,
  270. },
  271. });
  272. if (ret?.list) {
  273. const list = [];
  274. for (const item of ret.list) {
  275. item.id = item.id || item._id;
  276. list.push(transformData(item,'toMetaNetwork'));
  277. }
  278. networkList.value = list;
  279. }
  280. };
  281. const onSelect = (item: any) => {
  282. Object.assign(modelValue, item);
  283. popupVisible.value = false;
  284. };
  285. const onDelNetWork = async (item: any, i: number) => {
  286. const ret: any = await axios.post(`/api/data/datasource/delete`, {
  287. id: item._id || item.id,
  288. });
  289. if (ret) {
  290. networkList.value.splice(i, 1);
  291. }
  292. };
  293. const isSafeProtocol = () => {
  294. return document.location.protocol === 'https:' ? true : false;
  295. };
  296. const urlFormat = (val) => {
  297. if (modelValue.protocol === 'mqtt') {
  298. let reg = /^ws\:.*/;
  299. if (isSafeProtocol()) {
  300. reg = /^wss\:.*/;
  301. }
  302. if (reg.test(val)) {
  303. return val;
  304. } else {
  305. return '';
  306. }
  307. } else {
  308. return val;
  309. }
  310. };
  311. </script>
  312. <style lang="postcss" scoped>
  313. .network-component {
  314. }
  315. </style>