index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. <template>
  2. <div class="page">
  3. <el-card shadow="nover" v-loading="batchLoading">
  4. <div class="system-user-search mb15">
  5. <el-form :model="tableData.param" ref="queryRef" inline>
  6. <el-form-item label="设备名称" prop="name">
  7. <el-input v-model="tableData.param.name" placeholder="请输入设备名称" clearable style="width: 180px" @keyup.enter.native="typeList" />
  8. </el-form-item>
  9. <el-form-item label="设备标识" prop="key">
  10. <el-input v-model="tableData.param.key" placeholder="请输入设备标识" clearable style="width: 180px" @keyup.enter.native="typeList" />
  11. </el-form-item>
  12. <el-form-item label="所属产品" prop="productId">
  13. <el-select v-model="tableData.param.productId" style="width: 150px" filterable placeholder="请选择产品">
  14. <el-option v-for="item in productData" :key="item.id" :label="item.name" :value="item.id.toString()" value-key="id"> </el-option>
  15. </el-select>
  16. </el-form-item>
  17. <el-form-item label="状态" prop="status">
  18. <el-select v-model="tableData.param.status" placeholder="状态" clearable style="width: 100px">
  19. <el-option label="在线" :value="2" />
  20. <el-option label="离线" :value="1" />
  21. <el-option label="未启用" :value="0" />
  22. </el-select>
  23. </el-form-item>
  24. <el-form-item label="创建时间" prop="dateRange">
  25. <el-date-picker v-model="tableData.param.dateRange" style="width: 240px" value-format="YYYY-MM-DD" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
  26. </el-form-item>
  27. <el-form-item>
  28. <el-button type="primary" class="ml10" @click="typeList">
  29. <el-icon>
  30. <ele-Search />
  31. </el-icon>
  32. 查询
  33. </el-button>
  34. <el-button @click="resetQuery(queryRef)">
  35. <el-icon>
  36. <ele-Refresh />
  37. </el-icon>
  38. 重置
  39. </el-button>
  40. <el-button type="primary" class="ml10" @click="onOpenAddDic" v-auth="'add'">
  41. <el-icon>
  42. <ele-FolderAdd />
  43. </el-icon>
  44. 新增设备
  45. </el-button>
  46. <el-button type="info" class="ml10" @click="onRowDel(null)" v-auth="'del'">
  47. <el-icon>
  48. <ele-Delete />
  49. </el-icon>
  50. 删除
  51. </el-button>
  52. <el-dropdown>
  53. <el-button type="danger" class="ml10">
  54. 更多操作<el-icon class="el-icon--right"><arrow-down /></el-icon>
  55. </el-button>
  56. <template #dropdown>
  57. <el-dropdown-menu>
  58. <el-dropdown-item> <el-button type="success" @click="setDeviceStatus1(null)">
  59. <el-icon>
  60. <ele-Delete />
  61. </el-icon>
  62. 批量启用
  63. </el-button></el-dropdown-item>
  64. <el-dropdown-item><el-button type="warning" @click="setDeviceStatus0(null)">
  65. <el-icon>
  66. <ele-Delete />
  67. </el-icon>
  68. 批量禁用
  69. </el-button></el-dropdown-item>
  70. <el-dropdown-item> <el-button @click="onOpenexcelDic('upload')">
  71. <el-icon>
  72. <ele-Upload />
  73. </el-icon>
  74. 导入设备
  75. </el-button></el-dropdown-item>
  76. <el-dropdown-item><el-button @click="onOpenexcelDic('down')">
  77. <el-icon>
  78. <ele-Download />
  79. </el-icon>
  80. 导出设备
  81. </el-button></el-dropdown-item>
  82. </el-dropdown-menu>
  83. </template>
  84. </el-dropdown>
  85. </el-form-item>
  86. </el-form>
  87. </div>
  88. <el-table :data="tableData.data" style="width: 100%" @selection-change="handleSelectionChange" v-loading="tableData.loading">
  89. <el-table-column type="selection" width="55" align="center" />
  90. <el-table-column label="标识" prop="key" min-width="150" show-overflow-tooltip v-col="'key'" />
  91. <el-table-column label="设备名称" prop="name" min-width="160" show-overflow-tooltip v-col="'name'" />
  92. <el-table-column label="设备类型" prop="product.deviceType" min-width="120" align="center" show-overflow-tooltip v-col="'deviceType'" />
  93. <el-table-column label="产品名称" prop="productName" min-width="120" align="center" show-overflow-tooltip v-col="'productName'" />
  94. <el-table-column prop="status" label="状态" min-width="80" align="center" v-col="'status'">
  95. <template #default="scope">
  96. <el-tag type="info" size="small" v-if="scope.row.status == 1">离线</el-tag>
  97. <el-tag type="success" size="small" v-if="scope.row.status == 2">在线</el-tag>
  98. <el-tag type="info" size="small" v-if="scope.row.status == 0">未启用</el-tag>
  99. </template>
  100. </el-table-column>
  101. <el-table-column prop="createdAt" label="创建时间" align="center" width="160" v-col="'registryTime'"></el-table-column>
  102. <el-table-column prop="desc" label="说明" show-overflow-tooltip v-col="'desc'"></el-table-column>
  103. <el-table-column label="操作" width="180" align="center" fixed="right">
  104. <template #default="scope">
  105. <router-link :to="'/iotmanager/device/instance/' + scope.row.id" class="link-type" style="padding-right: 12px;font-size: 12px;color: #409eff;" v-auth="'detail'">
  106. <span>详情</span>
  107. </router-link>
  108. <el-button size="small" text type="warning" @click="onOpenEditDic(scope.row)" v-auth="'edit'">修改</el-button>
  109. <el-button size="small" text type="success" @click="onActionStatus(scope.row)" v-if="scope.row.status == 0" v-auth="'status'">启用</el-button>
  110. <el-button size="small" text type="primary" @click="onActionStatus(scope.row)" v-if="scope.row.status > 0" v-auth="'status'">停用</el-button>
  111. <el-button size="small" text type="info" @click="onRowDel(scope.row)" v-auth="'del'">删除</el-button>
  112. </template>
  113. </el-table-column>
  114. </el-table>
  115. <pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNum" v-model:limit="tableData.param.pageSize" @pagination="typeList" />
  116. </el-card>
  117. <EditDic ref="editDicRef" @typeList="typeList" />
  118. <ExcelDic ref="excelDicRef" @typeList="typeList" />
  119. </div>
  120. </template>
  121. <script lang="ts">
  122. import { toRefs, reactive, onMounted, ref, defineComponent } from 'vue';
  123. import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
  124. import EditDic from './component/edit.vue';
  125. import ExcelDic from './component/excel.vue';
  126. import api from '/@/api/device';
  127. import { ArrowDown } from '@element-plus/icons-vue'
  128. // 定义接口来定义对象的类型
  129. interface TableDataRow {
  130. id: number;
  131. name: string;
  132. key: string;
  133. status: number;
  134. desc: string;
  135. createBy: string;
  136. }
  137. interface TableDataState {
  138. ids: number[];
  139. productData: Array<TableDataRow>;
  140. tableData: {
  141. data: Array<TableDataRow>;
  142. total: number;
  143. loading: boolean;
  144. param: {
  145. pageNum: number;
  146. pageSize: number;
  147. name: string;
  148. key: string;
  149. productId: string;
  150. status: string;
  151. dateRange: string[];
  152. };
  153. };
  154. }
  155. export default defineComponent({
  156. name: 'deviceInstance',
  157. components: { EditDic, ExcelDic, ArrowDown },
  158. setup() {
  159. const addDicRef = ref();
  160. const editDicRef = ref();
  161. const excelDicRef = ref();
  162. const detailRef = ref();
  163. const queryRef = ref();
  164. const batchLoading = ref(false);
  165. const state = reactive<TableDataState>({
  166. ids: [],
  167. productData: [],
  168. tableData: {
  169. data: [],
  170. total: 0,
  171. loading: false,
  172. param: {
  173. pageNum: 1,
  174. pageSize: 10,
  175. name: '',
  176. key: '',
  177. status: '',
  178. dateRange: [],
  179. },
  180. },
  181. });
  182. // 初始化表格数据
  183. const initTableData = () => {
  184. typeList();
  185. getProductList()
  186. };
  187. const typeList = () => {
  188. state.tableData.loading = true;
  189. api.instance.getList(state.tableData.param).then((res: any) => {
  190. state.tableData.data = res.device;
  191. state.tableData.total = res.total;
  192. }).finally(() => (state.tableData.loading = false));
  193. };
  194. const getProductList = () => {
  195. api.product.getLists().then((res: any) => {
  196. state.productData = res.product
  197. })
  198. }
  199. //查看详情
  200. const onOpenDetail = (row: TableDataRow) => {
  201. detailRef.value.openDialog(row);
  202. }
  203. // 打开新增产品弹窗
  204. const onOpenAddDic = () => {
  205. editDicRef.value.openDialog();
  206. };
  207. // 打开修改产品弹窗
  208. const onOpenEditDic = (row: TableDataRow) => {
  209. editDicRef.value.openDialog(row);
  210. };
  211. const onOpenexcelDic = (type: string) => {
  212. excelDicRef.value.openDialog(type);
  213. };
  214. //批量启用
  215. const setDeviceStatus1 = (row: TableDataRow) => {
  216. let ids: number[] = [];
  217. if (row) {
  218. ids = [row.id];
  219. } else {
  220. ids = state.ids;
  221. }
  222. if (ids.length === 0) {
  223. ElMessage.error('请选择要操作的数据。');
  224. return;
  225. }
  226. ElMessageBox.confirm("确认要批量启用这些设备吗?", '提示', {
  227. confirmButtonText: '确认',
  228. cancelButtonText: '取消',
  229. type: 'warning',
  230. })
  231. .then(() => {
  232. batchLoading.value = true
  233. api.device.setDeviceStatus({ ids: ids, status: 1 }).then(() => {
  234. ElMessage.success('启用成功');
  235. typeList();
  236. }).finally(() => batchLoading.value = false)
  237. })
  238. .catch(() => { });
  239. }
  240. //批量禁用
  241. const setDeviceStatus0 = (row: TableDataRow) => {
  242. let ids: number[] = [];
  243. if (row) {
  244. ids = [row.id];
  245. } else {
  246. ids = state.ids;
  247. }
  248. if (ids.length === 0) {
  249. ElMessage.error('请选择要操作的数据。');
  250. return;
  251. }
  252. ElMessageBox.confirm("确认要批量禁用这些设备吗?", '提示', {
  253. confirmButtonText: '确认',
  254. cancelButtonText: '取消',
  255. type: 'warning',
  256. })
  257. .then(() => {
  258. batchLoading.value = true
  259. api.device.setDeviceStatus({ ids: ids, status: 0 }).then(() => {
  260. ElMessage.success('禁用成功');
  261. typeList();
  262. }).finally(() => batchLoading.value = false)
  263. })
  264. .catch(() => { });
  265. }
  266. // 删除产品
  267. const onRowDel = (row: TableDataRow) => {
  268. let msg = '你确定要删除所选数据?';
  269. let ids: number[] = [];
  270. if (row) {
  271. msg = `此操作将永久删除设备:“${row.name}”,是否继续?`;
  272. ids = [row.id];
  273. } else {
  274. ids = state.ids;
  275. }
  276. if (ids.length === 0) {
  277. ElMessage.error('请选择要删除的数据。');
  278. return;
  279. }
  280. ElMessageBox.confirm(msg, '提示', {
  281. confirmButtonText: '确认',
  282. cancelButtonText: '取消',
  283. type: 'warning',
  284. })
  285. .then(() => {
  286. api.instance.del(ids).then(() => {
  287. ElMessage.success('删除成功');
  288. typeList();
  289. });
  290. })
  291. .catch(() => { });
  292. };
  293. // 页面加载时
  294. onMounted(() => {
  295. initTableData();
  296. });
  297. /** 重置按钮操作 */
  298. const resetQuery = (formEl: FormInstance | undefined) => {
  299. if (!formEl) return;
  300. formEl.resetFields();
  301. typeList();
  302. };
  303. // 多选框选中数据
  304. const handleSelectionChange = (selection: TableDataRow[]) => {
  305. state.ids = selection.map((item) => item.id);
  306. };
  307. const onActionStatus = (item: TableDataRow[]) => {
  308. if (item.status == 0) {
  309. api.instance.devdeploy({ id: item.id }).then((res: any) => {
  310. typeList();
  311. ElMessage.success(res.message || '操作成功');
  312. });
  313. } else {
  314. api.instance.devundeploy({ id: item.id }).then((res: any) => {
  315. typeList();
  316. ElMessage.success(res.message || '操作成功');
  317. });
  318. }
  319. }
  320. return {
  321. addDicRef,
  322. excelDicRef,
  323. onOpenexcelDic,
  324. editDicRef,
  325. detailRef,
  326. queryRef,
  327. onActionStatus,
  328. batchLoading,
  329. setDeviceStatus1,
  330. setDeviceStatus0,
  331. onOpenDetail,
  332. onOpenAddDic,
  333. onOpenEditDic,
  334. onRowDel,
  335. typeList,
  336. resetQuery,
  337. handleSelectionChange,
  338. ...toRefs(state),
  339. };
  340. },
  341. });
  342. </script>