Network.vue 5.4 KB

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