index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <template>
  2. <ContentWrap>
  3. <!-- 搜索工作栏 -->
  4. <el-form
  5. class="-mb-15px"
  6. :model="queryParams"
  7. ref="queryFormRef"
  8. :inline="true"
  9. label-width="68px"
  10. >
  11. <el-form-item label="产品" prop="productId">
  12. <el-select
  13. v-model="queryParams.productId"
  14. placeholder="请选择产品"
  15. clearable
  16. class="!w-240px"
  17. >
  18. <el-option
  19. v-for="product in products"
  20. :key="product.id"
  21. :label="product.name"
  22. :value="product.id"
  23. />
  24. </el-select>
  25. </el-form-item>
  26. <el-form-item label="DeviceName" prop="deviceName">
  27. <el-input
  28. v-model="queryParams.deviceName"
  29. placeholder="请输入 DeviceName"
  30. clearable
  31. @keyup.enter="handleQuery"
  32. class="!w-240px"
  33. />
  34. </el-form-item>
  35. <el-form-item label="备注名称" prop="nickname">
  36. <el-input
  37. v-model="queryParams.nickname"
  38. placeholder="请输入备注名称"
  39. clearable
  40. @keyup.enter="handleQuery"
  41. class="!w-240px"
  42. />
  43. </el-form-item>
  44. <el-form-item label="设备类型" prop="deviceType">
  45. <el-select
  46. v-model="queryParams.deviceType"
  47. placeholder="请选择设备类型"
  48. clearable
  49. class="!w-240px"
  50. >
  51. <el-option
  52. v-for="dict in getIntDictOptions(DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE)"
  53. :key="dict.value"
  54. :label="dict.label"
  55. :value="dict.value"
  56. />
  57. </el-select>
  58. </el-form-item>
  59. <el-form-item label="设备状态" prop="status">
  60. <el-select
  61. v-model="queryParams.status"
  62. placeholder="请选择设备状态"
  63. clearable
  64. class="!w-240px"
  65. >
  66. <el-option
  67. v-for="dict in getIntDictOptions(DICT_TYPE.IOT_DEVICE_STATUS)"
  68. :key="dict.value"
  69. :label="dict.label"
  70. :value="dict.value"
  71. />
  72. </el-select>
  73. </el-form-item>
  74. <el-form-item>
  75. <el-button @click="handleQuery">
  76. <Icon icon="ep:search" class="mr-5px" />
  77. 搜索
  78. </el-button>
  79. <el-button @click="resetQuery">
  80. <Icon icon="ep:refresh" class="mr-5px" />
  81. 重置
  82. </el-button>
  83. <el-button
  84. type="primary"
  85. plain
  86. @click="openForm('create')"
  87. v-hasPermi="['iot:device:create']"
  88. >
  89. <Icon icon="ep:plus" class="mr-5px" />
  90. 新增
  91. </el-button>
  92. </el-form-item>
  93. </el-form>
  94. </ContentWrap>
  95. <!-- 列表 -->
  96. <ContentWrap>
  97. <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
  98. <el-table-column label="DeviceName" align="center" prop="deviceName">
  99. <template #default="scope">
  100. <el-link @click="openDetail(scope.row.id)">{{ scope.row.deviceName }}</el-link>
  101. </template>
  102. </el-table-column>
  103. <el-table-column label="备注名称" align="center" prop="nickname" />
  104. <el-table-column label="设备所属产品" align="center" prop="productId">
  105. <template #default="scope">
  106. {{ productMap[scope.row.productId] }}
  107. </template>
  108. </el-table-column>
  109. <el-table-column label="设备类型" align="center" prop="deviceType">
  110. <template #default="scope">
  111. <dict-tag :type="DICT_TYPE.IOT_PRODUCT_DEVICE_TYPE" :value="scope.row.deviceType" />
  112. </template>
  113. </el-table-column>
  114. <el-table-column label="设备状态" align="center" prop="status">
  115. <template #default="scope">
  116. <dict-tag :type="DICT_TYPE.IOT_DEVICE_STATUS" :value="scope.row.status" />
  117. </template>
  118. </el-table-column>
  119. <el-table-column
  120. label="最后上线时间"
  121. align="center"
  122. prop="lastOnlineTime"
  123. :formatter="dateFormatter"
  124. width="180px"
  125. />
  126. <el-table-column label="操作" align="center" min-width="120px">
  127. <template #default="scope">
  128. <el-button
  129. link
  130. type="primary"
  131. @click="openDetail(scope.row.id)"
  132. v-hasPermi="['iot:product:query']"
  133. >
  134. 查看
  135. </el-button>
  136. <el-button
  137. link
  138. type="primary"
  139. @click="openForm('update', scope.row.id)"
  140. v-hasPermi="['iot:device:update']"
  141. >
  142. 编辑
  143. </el-button>
  144. <el-button
  145. link
  146. type="danger"
  147. @click="handleDelete(scope.row.id)"
  148. v-hasPermi="['iot:device:delete']"
  149. >
  150. 删除
  151. </el-button>
  152. </template>
  153. </el-table-column>
  154. </el-table>
  155. <!-- 分页 -->
  156. <Pagination
  157. :total="total"
  158. v-model:page="queryParams.pageNo"
  159. v-model:limit="queryParams.pageSize"
  160. @pagination="getList"
  161. />
  162. </ContentWrap>
  163. <!-- 表单弹窗:添加/修改 -->
  164. <DeviceForm ref="formRef" @success="getList" />
  165. </template>
  166. <script setup lang="ts">
  167. import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
  168. import { dateFormatter } from '@/utils/formatTime'
  169. import { DeviceApi, DeviceVO } from '@/api/iot/device'
  170. import DeviceForm from './DeviceForm.vue'
  171. import { ProductApi } from '@/api/iot/product'
  172. /** IoT 设备 列表 */
  173. defineOptions({ name: 'IoTDevice' })
  174. const message = useMessage() // 消息弹窗
  175. const { t } = useI18n() // 国际化
  176. const loading = ref(true) // 列表的加载中
  177. const list = ref<DeviceVO[]>([]) // 列表的数据
  178. const total = ref(0) // 列表的总页数
  179. const queryParams = reactive({
  180. pageNo: 1,
  181. pageSize: 10,
  182. deviceName: undefined,
  183. productId: undefined,
  184. deviceType: undefined,
  185. nickname: undefined,
  186. status: undefined
  187. })
  188. const queryFormRef = ref() // 搜索的表单
  189. /** 产品标号和名称的映射 */
  190. const productMap = reactive({})
  191. /** 查询列表 */
  192. const getList = async () => {
  193. loading.value = true
  194. try {
  195. const data = await DeviceApi.getDevicePage(queryParams)
  196. list.value = data.list
  197. total.value = data.total
  198. // 获取产品ID列表
  199. const productIds = [...new Set(data.list.map((device) => device.productId))]
  200. // 获取产品名称
  201. // TODO @haohao:最好后端拼接哈
  202. const products = await Promise.all(productIds.map((id) => ProductApi.getProduct(id)))
  203. products.forEach((product) => {
  204. productMap[product.id] = product.name
  205. })
  206. } finally {
  207. loading.value = false
  208. }
  209. }
  210. /** 搜索按钮操作 */
  211. const handleQuery = () => {
  212. queryParams.pageNo = 1
  213. getList()
  214. }
  215. /** 重置按钮操作 */
  216. const resetQuery = () => {
  217. queryFormRef.value.resetFields()
  218. handleQuery()
  219. }
  220. /** 添加/修改操作 */
  221. const formRef = ref()
  222. const openForm = (type: string, id?: number) => {
  223. formRef.value.open(type, id)
  224. }
  225. /** 打开详情 */
  226. const { push } = useRouter()
  227. const openDetail = (id: number) => {
  228. push({ name: 'IoTDeviceDetail', params: { id } })
  229. }
  230. /** 删除按钮操作 */
  231. const handleDelete = async (id: number) => {
  232. try {
  233. // 删除的二次确认
  234. await message.delConfirm()
  235. // 发起删除
  236. await DeviceApi.deleteDevice(id)
  237. message.success(t('common.delSuccess'))
  238. // 刷新列表
  239. await getList()
  240. } catch {}
  241. }
  242. /** 查询字典下拉列表 */
  243. const products = ref()
  244. const getProducts = async () => {
  245. products.value = await ProductApi.getSimpleProductList()
  246. }
  247. /** 初始化 **/
  248. onMounted(() => {
  249. getList()
  250. getProducts()
  251. })
  252. </script>