index.vue 13 KB

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