Network.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. >
  19. <template #panel>
  20. <ul style="padding: 4px">
  21. <li
  22. class="hover-background"
  23. style="line-height: 1.5; padding: 8px; border-radius: 2px"
  24. v-for="item in networkList"
  25. :key="item.url"
  26. @click="() => onSelect(item)"
  27. >
  28. 名称: {{ item.name }}
  29. <div class="desc">地址: {{ item.url }}</div>
  30. </li>
  31. <li
  32. v-if="!networkList.length"
  33. style="line-height: 1.5; padding: 8px; border-radius: 2px"
  34. :key="-1"
  35. >
  36. <div class="desc">暂无数据</div>
  37. </li>
  38. </ul>
  39. </template>
  40. </t-select-input>
  41. <t-input v-else v-model="modelValue.name" placeholder="名称" />
  42. </div>
  43. <div class="form-item mt-8">
  44. <label>通信方式</label>
  45. <t-select
  46. v-model="modelValue.protocol"
  47. placeholder="MQTT"
  48. @change="protocolChange"
  49. >
  50. <t-option key="mqtt" value="mqtt" label="MQTT" />
  51. <t-option key="websocket" value="websocket" label="Websocket" />
  52. <t-option key="http" value="http" label="HTTP" />
  53. </t-select>
  54. </div>
  55. <div class="form-item mt-8">
  56. <label>URL地址</label>
  57. <t-input v-model="modelValue.url" />
  58. </div>
  59. <template v-if="modelValue.protocol === 'websocket'"> </template>
  60. <template v-else-if="modelValue.protocol === 'http'">
  61. <div class="form-item mt-8">
  62. <label>请求方式</label>
  63. <t-select v-model="modelValue.method" @change="httpMethodChange">
  64. <t-option key="GET" value="GET" label="GET" />
  65. <t-option key="POST" value="POST" label="POST" />
  66. </t-select>
  67. </div>
  68. <div class="form-item mt-8">
  69. <label>请求头</label>
  70. <t-textarea
  71. v-model="modelValue.headers"
  72. :autosize="{ minRows: 3, maxRows: 5 }"
  73. placeholder="请输入"
  74. />
  75. </div>
  76. <div v-if="modelValue.method === 'POST'" class="form-item mt-8">
  77. <label>请求体</label>
  78. <t-textarea
  79. v-model="modelValue.body"
  80. :autosize="{ minRows: 3, maxRows: 5 }"
  81. placeholder="请输入"
  82. />
  83. </div>
  84. </template>
  85. <template v-else>
  86. <div class="form-item mt-8">
  87. <label>Client Id</label>
  88. <t-input v-model="modelValue.options.clientId" />
  89. </div>
  90. <div class="form-item mt-8">
  91. <label>自动生成</label>
  92. <t-switch
  93. class="mt-8 ml-8"
  94. v-model="modelValue.options.customClientId"
  95. size="small"
  96. />
  97. </div>
  98. <div class="form-item mt-8">
  99. <label>用户名</label>
  100. <t-input v-model="modelValue.options.username" />
  101. </div>
  102. <div class="form-item mt-8">
  103. <label>密码</label>
  104. <t-input v-model="modelValue.options.password" />
  105. </div>
  106. <div class="form-item mt-8">
  107. <label>Topics</label>
  108. <t-input v-model="modelValue.topics" />
  109. </div>
  110. </template>
  111. <div class="form-item mt-8" v-if="mode">
  112. <label> </label>
  113. <div>
  114. <t-button @click="onSave">保存到我的数据发送</t-button>
  115. </div>
  116. </div>
  117. </div>
  118. </template>
  119. <script lang="ts" setup>
  120. import { onBeforeMount, ref } from 'vue';
  121. import axios from 'axios';
  122. import { debounce } from '@/services/debouce';
  123. const { modelValue, mode } = defineProps<{
  124. modelValue: any;
  125. mode?: any;
  126. }>();
  127. const emit = defineEmits(['update:modelValue', 'change']);
  128. const popupVisible = ref<boolean>(false);
  129. const networkList = ref<any[]>([]);
  130. onBeforeMount(() => {
  131. console.log(123123, mode);
  132. if (mode) {
  133. getNetworks();
  134. }
  135. });
  136. const protocolChange = (protocol: string) => {
  137. if (protocol === 'http') {
  138. Object.assign(modelValue, {
  139. httpTimeInterval: 1000,
  140. headers: '',
  141. method: 'GET',
  142. body: '',
  143. });
  144. } else if (protocol === 'websocket') {
  145. // modelValue.url = '';
  146. } else {
  147. Object.assign(modelValue, {
  148. options: {
  149. clientId: '',
  150. username: '',
  151. password: '',
  152. customClientId: false,
  153. },
  154. });
  155. }
  156. };
  157. const httpMethodChange = (method: string) => {
  158. if (method === 'GET') {
  159. modelValue.body = undefined;
  160. }
  161. };
  162. const onSave = async () => {
  163. emit('update:modelValue', modelValue);
  164. emit('change', modelValue);
  165. const id = modelValue._id || modelValue.id;
  166. // 保存到我的数据源
  167. if (id) {
  168. modelValue._id = id;
  169. await axios.post(`/api/data/datasources/update`, modelValue);
  170. } else {
  171. await axios.post(`/api/data/datasources/add`, modelValue);
  172. }
  173. };
  174. const onInput = (text: string) => {
  175. debounce(getNetworks, 300);
  176. };
  177. // 请求我的数据源接口
  178. const getNetworks = async () => {
  179. const body: any = {
  180. query: {
  181. type: modelValue.type,
  182. },
  183. projection: { updatedAt: 0 },
  184. };
  185. if (modelValue.name) {
  186. body.q = {
  187. name: modelValue.name,
  188. };
  189. }
  190. const ret: any = await axios.post(`/api/data/datasources/list`, body);
  191. if (ret) {
  192. networkList.value = ret.list;
  193. }
  194. };
  195. const onSelect = (item: any) => {
  196. Object.assign(modelValue, item);
  197. popupVisible.value = false;
  198. };
  199. </script>
  200. <style lang="postcss" scoped>
  201. .network-component {
  202. }
  203. </style>