Network.vue 8.5 KB

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