Net.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. <template>
  2. <div class="network-component">
  3. <div class="form-item mt-8">
  4. <label>
  5. {{$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.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. {{$t('名称:')}} {{ item.name }}
  33. <div class="desc">{{$t('地址:')}}' {{ 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">{{$t('暂无数据')}}</div>
  52. </li>
  53. </ul>
  54. </template>
  55. </t-select-input>
  56. <t-input v-else v-model="modelValue.name" :placeholder="$t('名称')" />
  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>{{$t('URL地址')}}</label>
  72. <t-input
  73. :format="urlFormat"
  74. :placeholder="
  75. (modelValue.protocol !== 'http'&&modelValue.protocol !== 'SSE')
  76. ? isSafeProtocol()
  77. ? $t('必须是wss协议')
  78. : $t('必须是ws协议')
  79. : $t('请输入')
  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>{{$t('请求方式')}}</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>{{$t('请求间隔')}}</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>{{$t('请求头')}}</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. {{$t('支持设置动态参数,例如:')}}{"Authorization": "Bearer ${token}"}
  124. </div>
  125. <div v-if="!mode && modelValue.method === 'POST'" class="form-item mt-8">
  126. <label>{{$t('请求体')}}</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. {{$t('支持设置动态参数,例如:')}}{"value": "${value}"}
  146. </div>
  147. </template>
  148. <template v-else-if="modelValue.protocol === 'SSE'">
  149. <div class="form-item mt-8">
  150. <label>{{$t('跨域凭据')}}</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>{{$t('关闭自动生成')}}
  165. <t-tooltip :content="$t('是否关闭Client Id自动生成')" placement="top">
  166. <HelpCircleIcon style="font-size: 12px" class="ml-2"/>
  167. </t-tooltip>
  168. </label>
  169. <t-switch
  170. class="mt-8 ml-8"
  171. v-model="modelValue.options.customClientId"
  172. size="small"
  173. />
  174. </div>
  175. <div class="form-item mt-8">
  176. <label>{{$t('用户名')}}</label>
  177. <t-input v-model="modelValue.options.username" />
  178. </div>
  179. <div class="form-item mt-8">
  180. <label>{{$t('密码')}}</label>
  181. <t-input v-model="modelValue.options.password" />
  182. </div>
  183. <div class="form-item mt-8">
  184. <label>Topics</label>
  185. <t-input v-model="modelValue.topics" />
  186. </div>
  187. </template>
  188. <div class="form-item mt-8" v-if="mode">
  189. <label> </label>
  190. <div>
  191. <t-button @click="onSave">{{$t('保存到我的数据发送')}}</t-button>
  192. </div>
  193. </div>
  194. </div>
  195. </template>
  196. <script lang="ts" setup>
  197. import { onBeforeMount, ref } from 'vue';
  198. import axios from 'axios';
  199. import { debounce } from '@/services/debouce';
  200. import { MessagePlugin } from 'tdesign-vue-next';
  201. import CodeEditor from '@/views/components/common/CodeEditor.vue';
  202. import { DeleteIcon, HelpCircleIcon } from 'tdesign-icons-vue-next';
  203. import { transformData } from '@/services/utils';
  204. const { modelValue, mode } = defineProps<{
  205. modelValue: any;
  206. mode?: any;
  207. }>();
  208. const emit = defineEmits(['update:modelValue', 'change']);
  209. const popupVisible = ref<boolean>(false);
  210. const networkList = ref<any[]>([]);
  211. onBeforeMount(() => {
  212. modelValue.id = modelValue.id || modelValue._id;
  213. if (mode) {
  214. getNetworks();
  215. }
  216. });
  217. const protocolChange = (protocol: string) => {
  218. if (protocol === 'http') {
  219. Object.assign(modelValue, {
  220. httpTimeInterval: 1000,
  221. headers: '',
  222. method: 'GET',
  223. body: '',
  224. });
  225. } else if (protocol === 'websocket') {
  226. // modelValue.url = '';
  227. } else {
  228. Object.assign(modelValue, {
  229. options: {
  230. clientId: '',
  231. username: '',
  232. password: '',
  233. customClientId: false,
  234. },
  235. });
  236. }
  237. };
  238. const httpMethodChange = (method: string) => {
  239. if (method === 'GET') {
  240. modelValue.body = undefined;
  241. }
  242. };
  243. const onSave = async () => {
  244. emit('update:modelValue', modelValue);
  245. emit('change', modelValue);
  246. let ret: any;
  247. const data = transformData(modelValue,'toNetwork');
  248. // 保存到我的数据源
  249. if (modelValue.id) {
  250. ret = await axios.post(`/api/data/datasource/update`, data);
  251. } else {
  252. ret = await axios.post(`/api/data/datasource/add`, data);
  253. }
  254. if (ret) {
  255. MessagePlugin.success('保存成功!');
  256. }
  257. };
  258. const onInput = (text: string) => {
  259. debounce(getNetworks, 300);
  260. };
  261. // 请求我的数据源接口
  262. const getNetworks = async () => {
  263. const body: any = {
  264. type: modelValue.type,
  265. projection: "id,data,name,type",
  266. };
  267. if (modelValue.name) {
  268. body.name = modelValue.name;
  269. }
  270. const ret: any = await axios.post(`/api/data/datasource/list`, body, {
  271. params: {
  272. current: 1,
  273. pageSize: 10,
  274. },
  275. });
  276. if (ret?.list) {
  277. const list = [];
  278. for (const item of ret.list) {
  279. item.id = item.id || item._id;
  280. list.push(transformData(item,'toMetaNetwork'));
  281. }
  282. networkList.value = list;
  283. }
  284. };
  285. const onSelect = (item: any) => {
  286. Object.assign(modelValue, item);
  287. popupVisible.value = false;
  288. };
  289. const onDelNetWork = async (item: any, i: number) => {
  290. const ret: any = await axios.post(`/api/data/datasource/delete`, {
  291. id: item._id || item.id,
  292. });
  293. if (ret) {
  294. networkList.value.splice(i, 1);
  295. }
  296. };
  297. const isSafeProtocol = () => {
  298. return document.location.protocol === 'https:' ? true : false;
  299. };
  300. const urlFormat = (val) => {
  301. if (modelValue.protocol === 'mqtt') {
  302. let reg = /^ws\:.*/;
  303. if (isSafeProtocol()) {
  304. reg = /^wss\:.*/;
  305. }
  306. if (reg.test(val)) {
  307. return val;
  308. } else {
  309. return '';
  310. }
  311. } else {
  312. return val;
  313. }
  314. };
  315. </script>
  316. <style lang="postcss" scoped>
  317. .form-item{
  318. label{
  319. width: 100px;
  320. }
  321. }
  322. .network-component {
  323. }
  324. </style>