index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <template>
  2. <div class="h-full overflow-hidden">
  3. <n-card title="认证鉴权" :bordered="false" class="rounded-16px shadow-sm">
  4. <n-space class="pb-12px" justify="space-between">
  5. <n-space>
  6. <n-button type="primary" @click="handleAddTable">
  7. <icon-ic-round-plus class="mr-4px text-20px" />
  8. 新增
  9. </n-button>
  10. </n-space>
  11. <n-space align="center" :size="18">
  12. <n-input-group>
  13. <n-input v-model:value="formValue.subscribe_topic_name" placeholder="输入订阅主题名称" />
  14. <n-input v-model:value="formValue.publish_topic_name" placeholder="输入发布主题名称" />
  15. <n-input v-model:value="formValue.user_name" placeholder="输入用户姓名" />
  16. <n-input v-model:value="formValue.pasture_name" placeholder="输入牧场名称" />
  17. <n-button type="primary" @click="handleSearch">搜索</n-button>
  18. </n-input-group>
  19. <n-button size="small" type="primary" @click="getTableData">
  20. <icon-mdi-refresh class="mr-4px text-16px" :class="{ 'animate-spin': loading }" />
  21. 刷新表格
  22. </n-button>
  23. </n-space>
  24. </n-space>
  25. <n-data-table :columns="columns" :data="tableData" :loading="loading" :pagination="pagination" />
  26. <table-action-modal
  27. v-model:visible="visible"
  28. :type="modalType"
  29. :edit-data="editData"
  30. :group-enum-list-data="groupEnumListData"
  31. @update:visible="handleModalVisibilityChange"
  32. />
  33. </n-card>
  34. </div>
  35. </template>
  36. <script setup lang="tsx">
  37. import { reactive, ref } from 'vue';
  38. import type { Ref } from 'vue';
  39. import { NButton, NSpace, NPopconfirm } from 'naive-ui';
  40. import type { DataTableColumns, PaginationProps, SelectOption } from 'naive-ui';
  41. import { useBoolean, useLoading } from '@/hooks';
  42. import { fetchMqttAuthList, groupEnumList, mqttAuthDelete } from '@/service/api/mqtt';
  43. import TableActionModal from '../authentication/components/table-action-modal.vue';
  44. import type { ModalType } from '../authentication/components/table-action-modal.vue';
  45. const { loading, startLoading, endLoading } = useLoading(false);
  46. const { bool: visible, setTrue: openModal } = useBoolean();
  47. const formValue = ref<Mqtt.SearchAuth>({
  48. publish_topic_name: '',
  49. subscribe_topic_name: '',
  50. pasture_name: '',
  51. user_name: ''
  52. });
  53. const tableData = ref<ApiMqttAuth.Auth[]>([]);
  54. const editData = ref<Mqtt.Auth | null>(null);
  55. const modalType = ref<ModalType>('add');
  56. const groupEnumListData = ref<SelectOption[]>([]);
  57. function setGroupList(data: SelectOption[]) {
  58. groupEnumListData.value = data;
  59. }
  60. function setEditData(data: Mqtt.Auth | null) {
  61. editData.value = data;
  62. }
  63. function setTableData(data: ApiMqttAuth.Auth[]) {
  64. tableData.value = data;
  65. }
  66. function setModalType(type: ModalType) {
  67. modalType.value = type;
  68. }
  69. async function getTableData() {
  70. startLoading();
  71. // eslint-disable-next-line @typescript-eslint/no-use-before-define
  72. const { data } = await fetchMqttAuthList(pagination.page, pagination.pageSize, formValue.value);
  73. if (data) {
  74. setTableData(data);
  75. endLoading();
  76. } else {
  77. endLoading();
  78. }
  79. }
  80. async function getGroupEnumList() {
  81. const { data } = await groupEnumList();
  82. if (data) {
  83. setTimeout(() => {
  84. setGroupList(data);
  85. }, 100);
  86. }
  87. }
  88. const columns: Ref<DataTableColumns<ApiMqttAuth.Auth>> = ref([
  89. {
  90. type: 'selection',
  91. align: 'center'
  92. },
  93. {
  94. key: 'index',
  95. title: '序号',
  96. align: 'center'
  97. },
  98. {
  99. key: 'pasture_name',
  100. title: '牧场名称',
  101. align: 'center'
  102. },
  103. {
  104. key: 'group_name',
  105. title: '集团名称',
  106. align: 'center'
  107. },
  108. {
  109. key: 'mount_point',
  110. title: '挂载点',
  111. align: 'center'
  112. },
  113. {
  114. key: 'client_id',
  115. title: '客户端ID',
  116. align: 'center'
  117. },
  118. {
  119. key: 'user_name',
  120. title: '用户名称',
  121. align: 'center'
  122. },
  123. {
  124. key: 'subscribe_topic_name_stings',
  125. title: '订阅topic名称',
  126. align: 'center'
  127. },
  128. {
  129. key: 'subscribe_acl',
  130. title: '订阅topic',
  131. align: 'center'
  132. },
  133. {
  134. key: 'publish_topic_name_stings',
  135. title: '发布topic名称',
  136. align: 'center'
  137. },
  138. {
  139. key: 'publish_acl',
  140. title: '发布topic',
  141. align: 'center'
  142. },
  143. {
  144. key: 'actions',
  145. title: '操作',
  146. align: 'center',
  147. render: row => {
  148. return (
  149. <NSpace justify={'center'}>
  150. <NButton type="info" size={'small'} onClick={() => handleEditTable(row.id)}>
  151. 编辑
  152. </NButton>
  153. <NPopconfirm onPositiveClick={() => handleDeleteTable(row.id)}>
  154. {{
  155. default: () => '确认删除',
  156. trigger: () => (
  157. <NButton type="error" size={'small'}>
  158. 删除
  159. </NButton>
  160. )
  161. }}
  162. </NPopconfirm>
  163. </NSpace>
  164. );
  165. }
  166. }
  167. ]) as Ref<DataTableColumns<ApiMqttAuth.Auth>>;
  168. const pagination: PaginationProps = reactive({
  169. page: 1,
  170. pageSize: 10,
  171. showSizePicker: true,
  172. pageSizes: [10, 15, 20, 25, 30],
  173. onChange: (page: number) => {
  174. pagination.page = page;
  175. },
  176. onUpdatePageSize: (pageSize: number) => {
  177. pagination.pageSize = pageSize;
  178. pagination.page = 1;
  179. }
  180. });
  181. async function handleSearch() {
  182. if (!formValue.value) {
  183. window.$message?.warning('请输入相关名称');
  184. } else {
  185. startLoading();
  186. const { data } = await fetchMqttAuthList(pagination.page, pagination.pageSize, formValue.value);
  187. if (data) {
  188. setTableData(data);
  189. endLoading();
  190. } else {
  191. endLoading();
  192. }
  193. }
  194. }
  195. function handleEditTable(rowId: number) {
  196. const findItem = tableData.value.find(item => item.id === rowId);
  197. if (findItem) {
  198. setEditData(findItem);
  199. }
  200. setModalType('edit');
  201. openModal();
  202. }
  203. function handleDeleteTable(rowId: number) {
  204. const data = mqttAuthDelete(rowId);
  205. data.then(res => {
  206. if (res.data) {
  207. window.$message?.success('删除成功!');
  208. }
  209. });
  210. init();
  211. }
  212. function handleModalVisibilityChange() {
  213. setTimeout(() => {
  214. getTableData();
  215. }, 200);
  216. }
  217. function handleAddTable() {
  218. getGroupEnumList();
  219. openModal();
  220. setModalType('add');
  221. }
  222. function init() {
  223. getTableData();
  224. }
  225. // 初始化
  226. init();
  227. </script>
  228. <style scoped></style>