Network.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 = undefined"
  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. <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.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 v-model="modelValue.url" />
  72. </div>
  73. <template v-if="modelValue.protocol === 'websocket'"> </template>
  74. <template v-else-if="modelValue.protocol === 'http'">
  75. <div class="form-item mt-8">
  76. <label>请求方式</label>
  77. <t-select v-model="modelValue.method" @change="httpMethodChange">
  78. <t-option key="GET" value="GET" label="GET" />
  79. <t-option key="POST" value="POST" label="POST" />
  80. </t-select>
  81. </div>
  82. <div class="form-item mt-8">
  83. <label>请求头</label>
  84. <t-textarea
  85. v-model="modelValue.headers"
  86. :autosize="{ minRows: 3, maxRows: 5 }"
  87. placeholder="请输入"
  88. />
  89. </div>
  90. <div v-if="modelValue.method === 'POST'" class="form-item mt-8">
  91. <label>请求体</label>
  92. <t-textarea
  93. v-model="modelValue.body"
  94. :autosize="{ minRows: 3, maxRows: 5 }"
  95. placeholder="请输入"
  96. />
  97. </div>
  98. </template>
  99. <template v-else>
  100. <div class="form-item mt-8">
  101. <label>Client Id</label>
  102. <t-input v-model="modelValue.options.clientId" />
  103. </div>
  104. <div class="form-item mt-8">
  105. <label>自动生成</label>
  106. <t-switch
  107. class="mt-8 ml-8"
  108. v-model="modelValue.options.customClientId"
  109. size="small"
  110. />
  111. </div>
  112. <div class="form-item mt-8">
  113. <label>用户名</label>
  114. <t-input v-model="modelValue.options.username" />
  115. </div>
  116. <div class="form-item mt-8">
  117. <label>密码</label>
  118. <t-input v-model="modelValue.options.password" />
  119. </div>
  120. <div class="form-item mt-8">
  121. <label>Topics</label>
  122. <t-input v-model="modelValue.topics" />
  123. </div>
  124. </template>
  125. <div class="form-item mt-8" v-if="mode">
  126. <label> </label>
  127. <div>
  128. <t-button @click="onSave">保存到我的数据发送</t-button>
  129. </div>
  130. </div>
  131. </div>
  132. </template>
  133. <script lang="ts" setup>
  134. import { onBeforeMount, ref } from 'vue';
  135. import axios from 'axios';
  136. import { debounce } from '@/services/debouce';
  137. import { MessagePlugin } from 'tdesign-vue-next';
  138. const { modelValue, mode } = defineProps<{
  139. modelValue: any;
  140. mode?: any;
  141. }>();
  142. const emit = defineEmits(['update:modelValue', 'change']);
  143. const popupVisible = ref<boolean>(false);
  144. const networkList = ref<any[]>([]);
  145. onBeforeMount(() => {
  146. modelValue.id = modelValue.id || modelValue._id;
  147. if (mode) {
  148. getNetworks();
  149. }
  150. });
  151. const protocolChange = (protocol: string) => {
  152. if (protocol === 'http') {
  153. Object.assign(modelValue, {
  154. httpTimeInterval: 1000,
  155. headers: '',
  156. method: 'GET',
  157. body: '',
  158. });
  159. } else if (protocol === 'websocket') {
  160. // modelValue.url = '';
  161. } else {
  162. Object.assign(modelValue, {
  163. options: {
  164. clientId: '',
  165. username: '',
  166. password: '',
  167. customClientId: false,
  168. },
  169. });
  170. }
  171. };
  172. const httpMethodChange = (method: string) => {
  173. if (method === 'GET') {
  174. modelValue.body = undefined;
  175. }
  176. };
  177. const onSave = async () => {
  178. emit('update:modelValue', modelValue);
  179. emit('change', modelValue);
  180. let ret: any;
  181. // 保存到我的数据源
  182. if (modelValue.id) {
  183. ret = await axios.post(`/api/data/datasources/update`, modelValue);
  184. } else {
  185. ret = await axios.post(`/api/data/datasources/add`, modelValue);
  186. }
  187. if (ret) {
  188. MessagePlugin.success('保存成功!');
  189. }
  190. };
  191. const onInput = (text: string) => {
  192. debounce(getNetworks, 300);
  193. };
  194. // 请求我的数据源接口
  195. const getNetworks = async () => {
  196. const body: any = {
  197. query: {
  198. type: modelValue.type,
  199. },
  200. projection: { updatedAt: 0 },
  201. };
  202. if (modelValue.name) {
  203. body.q = {
  204. name: modelValue.name,
  205. };
  206. }
  207. const ret: any = await axios.post(`/api/data/datasources/list`, body, {
  208. params: {
  209. current: 1,
  210. pageSize: 10,
  211. },
  212. });
  213. if (ret?.list) {
  214. for (const item of ret.list) {
  215. item.id = item.id || item._id;
  216. }
  217. networkList.value = ret.list;
  218. }
  219. };
  220. const onSelect = (item: any) => {
  221. Object.assign(modelValue, item);
  222. popupVisible.value = false;
  223. };
  224. const onDelNetWork = async (item: any, i: number) => {
  225. const ret: any = await axios.post(`/api/data/datasources/delete`, {
  226. id: item._id || item.id,
  227. });
  228. if (ret) {
  229. networkList.value.splice(i, 1);
  230. }
  231. };
  232. </script>
  233. <style lang="postcss" scoped>
  234. .network-component {
  235. }
  236. </style>