index.vue 13 KB

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