index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 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="info" 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 { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
  137. import EditDic from './component/edit.vue';
  138. import ExcelDic from './component/excel.vue';
  139. import api from '/@/api/device';
  140. import { ArrowDown } from '@element-plus/icons-vue'
  141. import { useRoute } from 'vue-router'
  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 route = useRoute()
  174. const productKey = route.query.productKey
  175. const addDicRef = ref();
  176. const editDicRef = ref();
  177. const excelDicRef = ref();
  178. const detailRef = ref();
  179. const queryRef = ref();
  180. const batchLoading = ref(false);
  181. const state = reactive<TableDataState>({
  182. keys: [],
  183. productData: [],
  184. tableData: {
  185. data: [],
  186. total: 0,
  187. loading: false,
  188. param: {
  189. pageNum: 1,
  190. pageSize: 20,
  191. name: '',
  192. key: '',
  193. productKey: '',
  194. status: '',
  195. dateRange: [],
  196. },
  197. },
  198. });
  199. // 初始化表格数据
  200. const initTableData = () => {
  201. typeList();
  202. getProductList()
  203. };
  204. const typeList = () => {
  205. state.tableData.loading = true;
  206. api.instance.getList(state.tableData.param).then((res: any) => {
  207. state.tableData.data = res.device;
  208. state.tableData.total = res.total;
  209. }).finally(() => (state.tableData.loading = false));
  210. };
  211. const getProductList = () => {
  212. const query = { productKey }
  213. api.product.getLists(query).then((res: any) => {
  214. state.productData = res.product
  215. })
  216. }
  217. //查看详情
  218. const onOpenDetail = (row: TableDataRow) => {
  219. detailRef.value.openDialog(row);
  220. }
  221. // 打开新增产品弹窗
  222. const onOpenAddDic = () => {
  223. editDicRef.value.openDialog();
  224. };
  225. // 打开修改产品弹窗
  226. const onOpenEditDic = (row: TableDataRow) => {
  227. editDicRef.value.openDialog(row);
  228. };
  229. const onOpenexcelDic = (type: string) => {
  230. excelDicRef.value.openDialog(type);
  231. };
  232. //批量启用
  233. const setDeviceStatus1 = (row?: TableDataRow) => {
  234. let keys: string[] = [];
  235. if (row) {
  236. keys = [row.key];
  237. } else {
  238. keys = state.keys;
  239. }
  240. if (keys.length === 0) {
  241. ElMessage.error('请选择要操作的数据。');
  242. return;
  243. }
  244. ElMessageBox.confirm("确认要批量启用这些设备吗?", '提示', {
  245. confirmButtonText: '确认',
  246. cancelButtonText: '取消',
  247. type: 'warning',
  248. })
  249. .then(() => {
  250. batchLoading.value = true
  251. api.device.setDeviceStatus({ ids: keys, status: 1 }).then(() => {
  252. ElMessage.success('启用成功');
  253. typeList();
  254. }).finally(() => batchLoading.value = false)
  255. })
  256. .catch(() => { });
  257. }
  258. //批量禁用
  259. const setDeviceStatus0 = (row?: TableDataRow) => {
  260. let keys: string[] = [];
  261. if (row) {
  262. keys = [row.key];
  263. } else {
  264. keys = state.keys;
  265. }
  266. if (keys.length === 0) {
  267. ElMessage.error('请选择要操作的数据。');
  268. return;
  269. }
  270. ElMessageBox.confirm("确认要批量禁用这些设备吗?", '提示', {
  271. confirmButtonText: '确认',
  272. cancelButtonText: '取消',
  273. type: 'warning',
  274. })
  275. .then(() => {
  276. batchLoading.value = true
  277. api.device.setDeviceStatus({ ids: keys, status: 0 }).then(() => {
  278. ElMessage.success('禁用成功');
  279. typeList();
  280. }).finally(() => batchLoading.value = false)
  281. })
  282. .catch(() => { });
  283. }
  284. // 删除产品
  285. const onRowDel = (row?: TableDataRow) => {
  286. let msg = '你确定要删除所选数据?';
  287. let keys: string[] = [];
  288. if (row) {
  289. msg = `此操作将永久删除设备:“${row.name}”,是否继续?`;
  290. keys = [row.key];
  291. } else {
  292. keys = state.keys;
  293. }
  294. if (keys.length === 0) {
  295. ElMessage.error('请选择要删除的数据。');
  296. return;
  297. }
  298. ElMessageBox.confirm(msg, '提示', {
  299. confirmButtonText: '确认',
  300. cancelButtonText: '取消',
  301. type: 'warning',
  302. })
  303. .then(() => {
  304. api.instance.del(keys).then(() => {
  305. ElMessage.success('删除成功');
  306. typeList();
  307. });
  308. })
  309. .catch(() => { });
  310. };
  311. // 页面加载时
  312. onMounted(() => {
  313. initTableData();
  314. });
  315. /** 重置按钮操作 */
  316. const resetQuery = (formEl: FormInstance | undefined) => {
  317. if (!formEl) return;
  318. formEl.resetFields();
  319. typeList();
  320. };
  321. // 多选框选中数据
  322. const handleSelectionChange = (selection: TableDataRow[]) => {
  323. state.keys = selection.map((item) => item.key);
  324. };
  325. const onActionStatus = (item: TableDataRow) => {
  326. if (item.status == 0) {
  327. api.instance.devdeploy(item.key).then((res: any) => {
  328. typeList();
  329. ElMessage.success(res.message || '操作成功');
  330. });
  331. } else {
  332. api.instance.devundeploy(item.key).then((res: any) => {
  333. typeList();
  334. ElMessage.success(res.message || '操作成功');
  335. });
  336. }
  337. }
  338. return {
  339. addDicRef,
  340. excelDicRef,
  341. onOpenexcelDic,
  342. editDicRef,
  343. detailRef,
  344. queryRef,
  345. onActionStatus,
  346. batchLoading,
  347. setDeviceStatus1,
  348. setDeviceStatus0,
  349. onOpenDetail,
  350. onOpenAddDic,
  351. onOpenEditDic,
  352. onRowDel,
  353. typeList,
  354. resetQuery,
  355. handleSelectionChange,
  356. ...toRefs(state),
  357. };
  358. },
  359. });
  360. </script>