index.vue 6.4 KB

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