index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <template>
  2. <div class="devicePage">
  3. <div class="searchBar">
  4. <a-form layout="inline" @keydown.enter="searchHandler">
  5. <a-form-item label="设备ID">
  6. <a-input
  7. v-model:value.trim="searchState.deviceId"
  8. placeholder="设备ID"
  9. :maxlength="12"
  10. show-count
  11. allow-clear
  12. @change="clearHandler"
  13. />
  14. </a-form-item>
  15. <a-form-item label="设备名称">
  16. <a-input
  17. v-model:value.trim="searchState.deviceName"
  18. placeholder="设备名称"
  19. :maxlength="300"
  20. show-count
  21. allow-clear
  22. @change="clearHandler"
  23. />
  24. </a-form-item>
  25. <a-form-item label="设备状态">
  26. <a-select
  27. v-model:value="searchState.deviceStatus"
  28. style="width: 120px"
  29. :options="deviceStatusOptions"
  30. @change="searchHandler"
  31. ></a-select>
  32. </a-form-item>
  33. <a-form-item label="创建日期">
  34. <range-picker
  35. v-model:start="searchState.createTimeStart"
  36. v-model:end="searchState.createTimeEnd"
  37. @change="searchHandler"
  38. />
  39. </a-form-item>
  40. <a-form-item>
  41. <a-space>
  42. <a-button type="primary" @click="searchHandler"> 搜索 </a-button>
  43. <a-button @click="resetHandler"> 重置 </a-button>
  44. </a-space>
  45. </a-form-item>
  46. </a-form>
  47. </div>
  48. <div class="tableCard">
  49. <div class="tableCard-header">
  50. <div class="tableCard-header-title">
  51. <span>设备列表</span>
  52. <span class="subtitle"
  53. >设备在线数量:<span style="color: #389e0d">{{ onlineDeviceTotal }}</span> /
  54. {{ allDeviceTotal }} (台)
  55. </span>
  56. </div>
  57. <div class="tableCard-header-extra">
  58. <a-space>
  59. <a-button @click="addDeviceHandler">添加设备</a-button>
  60. <a-button type="primary" @click="uploadDeviceHandler">批量上传设备</a-button>
  61. </a-space>
  62. </div>
  63. </div>
  64. <a-table
  65. :columns="columns"
  66. :data-source="deviceList"
  67. :loading="loading"
  68. :pagination="false"
  69. :scroll="{ x: 'max-content' }"
  70. >
  71. <template #bodyCell="{ column, record }">
  72. <template v-if="column.key === 'online'">
  73. <a-tag v-if="record.online === -1" :bordered="false" color="gray">未激活</a-tag>
  74. <a-tag v-if="record.online === 0" :bordered="false" color="red">离线</a-tag>
  75. <a-tag v-if="record.online === 1" :bordered="false" color="green">在线</a-tag>
  76. </template>
  77. <template v-if="column.key === 'activeState'">
  78. <a-tag v-if="isToday(record.presenceChangeTime)" :bordered="false" color="green"
  79. >有</a-tag
  80. >
  81. <a-tag v-else :bordered="false" color="#ccc">无</a-tag>
  82. </template>
  83. <template v-if="column.key === 'action'">
  84. <a-button type="link" @click="detailHandler(record.devId, record.clientId)"
  85. >查看详情</a-button
  86. >
  87. <a-button type="link" @click="unbindDeviceHandler(record)">解绑设备</a-button>
  88. </template>
  89. </template>
  90. </a-table>
  91. <base-pagination
  92. v-if="deviceTotal > 0"
  93. v-model:current="current"
  94. v-model:pageSize="pageSize"
  95. :total="deviceTotal"
  96. @change="paginationChange"
  97. @showSizeChange="paginationSizeChange"
  98. ></base-pagination>
  99. </div>
  100. <add-device-modal
  101. v-model:open="addDeviceOpen"
  102. title="添加设备"
  103. :options="tenantOptions"
  104. @success="searchHandler"
  105. ></add-device-modal>
  106. <upload-device-modal
  107. v-model:open="uploadDeviceOpen"
  108. title="批量上传设备"
  109. @success="searchHandler"
  110. ></upload-device-modal>
  111. <baseModal v-model:open="unbindOpen" title="解绑设备">
  112. <a-descriptions title="" bordered :column="1" size="middle">
  113. <a-descriptions-item label="设备ID">{{ unbindDeviceData.devId }}</a-descriptions-item>
  114. <a-descriptions-item label="设备名称">{{ unbindDeviceData.devName }}</a-descriptions-item>
  115. <a-descriptions-item label="设备状态">
  116. <a-tag v-if="unbindDeviceData.online === -1" :bordered="false" color="gray">未激活</a-tag>
  117. <a-tag v-if="unbindDeviceData.online === 0" :bordered="false" color="red">离线</a-tag>
  118. <a-tag v-if="unbindDeviceData.online === 1" :bordered="false" color="green">在线</a-tag>
  119. </a-descriptions-item>
  120. <a-descriptions-item label="绑定用户ID">{{ unbindDeviceData.userId }}</a-descriptions-item>
  121. <a-descriptions-item label="用户手机号">
  122. {{ unbindDeviceData.userPhone }}
  123. </a-descriptions-item>
  124. <a-descriptions-item label="绑定时间">{{ unbindDeviceData.bindTime }}</a-descriptions-item>
  125. </a-descriptions>
  126. <template #footer>
  127. <a-space class="unbindDevice-btn">
  128. <a-button @click="unbindOpen = false">取消</a-button>
  129. <a-popconfirm
  130. title="确认解绑该设备吗?"
  131. ok-text="确认"
  132. cancel-text="取消"
  133. @confirm="confirmUnbindDevice(unbindDeviceData.devId)"
  134. >
  135. <a-button type="primary">解绑</a-button>
  136. </a-popconfirm>
  137. </a-space>
  138. </template>
  139. </baseModal>
  140. </div>
  141. </template>
  142. <script setup lang="ts">
  143. import * as deviceAPI from '@/api/device'
  144. import { ref, onActivated } from 'vue'
  145. import type { Device } from '@/api/device/types'
  146. import { columns, deviceStatusOptions } from './const'
  147. import addDeviceModal from './components/addDevice/index.vue'
  148. import uploadDeviceModal from './components/uploadDevice/index.vue'
  149. import { useSearch } from '@/hooks/useSearch'
  150. import { useRouter } from 'vue-router'
  151. import * as tenantAPI from '@/api/tenant'
  152. import type { TenantItem } from '@/api/tenant/types'
  153. import * as adminAPI from '@/api/admin'
  154. import * as deviceApi from '@/api/device'
  155. const router = useRouter()
  156. interface SearchData {
  157. deviceId: string // 设备ID
  158. deviceName: string // 设备名称
  159. deviceStatus: number | null // 设备状态
  160. createTimeStart: string // 创建时间开始
  161. createTimeEnd: string // 创建时间结束
  162. }
  163. // 默认搜索条件
  164. const defaultSearch: SearchData = {
  165. deviceId: '',
  166. deviceName: '',
  167. deviceStatus: 1,
  168. createTimeStart: '',
  169. createTimeEnd: '',
  170. }
  171. const [searchState, resetHandler] = useSearch(defaultSearch, { afterReset: () => searchHandler() })
  172. defineOptions({
  173. name: 'DeviceList',
  174. })
  175. const deviceList = ref<Device[]>()
  176. const deviceTotal = ref<number>(0)
  177. const current = ref<number>(1)
  178. const pageSize = ref<number>(10)
  179. // 分页变化
  180. const paginationChange = (current: number, pageSize: number) => {
  181. console.log('change', current, pageSize)
  182. fetchList()
  183. }
  184. // 分页大小变化
  185. const paginationSizeChange = (current: number, pageSize: number) => {
  186. console.log('showSizeChange', current, pageSize)
  187. }
  188. const allDeviceTotal = ref(0) // 所以设备数量
  189. const onlineDeviceTotal = ref(0) // 在线设备数量
  190. const offlineDeviceTotal = ref(0) // 离线设备数量
  191. const loading = ref(false)
  192. // 获取设备信息
  193. const fetchList = async () => {
  194. try {
  195. loading.value = true
  196. const res = await deviceAPI.getDeviceList({
  197. pageNo: current.value,
  198. pageSize: pageSize.value,
  199. clientId: searchState.deviceId,
  200. devName: searchState.deviceName,
  201. createTimeStart: searchState.createTimeStart,
  202. createTimeEnd: searchState.createTimeEnd,
  203. online: searchState.deviceStatus,
  204. })
  205. const allDeviceRes = await deviceAPI.getDeviceList({
  206. pageNo: current.value,
  207. pageSize: pageSize.value,
  208. clientId: searchState.deviceId,
  209. devName: searchState.deviceName,
  210. createTimeStart: searchState.createTimeStart,
  211. createTimeEnd: searchState.createTimeEnd,
  212. online: null,
  213. })
  214. const onlineDeviceRes = await deviceAPI.getDeviceList({
  215. pageNo: current.value,
  216. pageSize: pageSize.value,
  217. clientId: searchState.deviceId,
  218. devName: searchState.deviceName,
  219. createTimeStart: searchState.createTimeStart,
  220. createTimeEnd: searchState.createTimeEnd,
  221. online: 1,
  222. })
  223. allDeviceTotal.value = Number(allDeviceRes.data.total) || 0 // 所以设备数量
  224. onlineDeviceTotal.value = Number(onlineDeviceRes.data.total) || 0 // 在线设备数量
  225. offlineDeviceTotal.value = allDeviceTotal.value - onlineDeviceTotal.value // 离线设备数量
  226. console.log('✅获取到设备信息', res, {
  227. allDeviceTotal: allDeviceTotal.value,
  228. onlineDeviceTotal: onlineDeviceTotal.value,
  229. offlineDeviceTotal: offlineDeviceTotal.value,
  230. })
  231. const { rows, total } = res.data
  232. deviceList.value = rows
  233. deviceTotal.value = Number(total)
  234. loading.value = false
  235. } catch (error) {
  236. console.error('❌ 获取设备信息失败', error)
  237. loading.value = false
  238. }
  239. }
  240. const tenantOptions = ref<{ label: string; value: string }[]>([])
  241. // 获取租户列表
  242. const fetchTenantList = async () => {
  243. try {
  244. const res = await tenantAPI.queryTenant({
  245. pageNo: 1,
  246. pageSize: 10000,
  247. })
  248. const { rows } = res.data
  249. tenantOptions.value = rows.map((item: TenantItem) => ({
  250. label: item.tenantName,
  251. value: item?.tenantId as string,
  252. }))
  253. } catch (err) {
  254. console.log('❌ 获取数据失败', err)
  255. }
  256. }
  257. onActivated(() => {
  258. fetchList()
  259. fetchTenantList()
  260. })
  261. // 搜索
  262. const searchHandler = async () => {
  263. console.log('searchState', searchState)
  264. current.value = 1
  265. pageSize.value = 10
  266. await fetchList()
  267. }
  268. // 搜索条件变化时清空搜索结果
  269. const clearHandler = (e: InputEvent) => {
  270. console.log('clearHandler', e, e.target, (e.target as HTMLInputElement)?.value)
  271. if (!(e.target as HTMLInputElement)?.value) {
  272. fetchList()
  273. }
  274. }
  275. // 详情
  276. const detailHandler = (devId: string, clientId: string) => {
  277. console.log('点击详情', { devId, clientId })
  278. router.push({
  279. name: 'deviceDetail',
  280. query: {
  281. devId,
  282. clientId,
  283. },
  284. })
  285. }
  286. const unbindOpen = ref(false)
  287. const unbindModalLoading = ref(false)
  288. const unbindDeviceData = ref<{
  289. devId: string
  290. clientId: string
  291. devName: string
  292. online: number
  293. userId: number
  294. userPhone: string
  295. bindTime: string
  296. }>({
  297. devId: '',
  298. clientId: '',
  299. devName: '',
  300. online: 0,
  301. userId: 0,
  302. userPhone: '',
  303. bindTime: '',
  304. })
  305. // 解绑设备
  306. const unbindDeviceHandler = async (device: Device) => {
  307. console.log('解绑设备')
  308. unbindDeviceData.value = {
  309. devId: device.devId + '',
  310. clientId: device.clientId,
  311. devName: device.devName,
  312. online: device.online,
  313. userId: device.userId,
  314. userPhone: '',
  315. bindTime: '',
  316. }
  317. unbindOpen.value = true
  318. await fetchDeviceBindUser(device.userId)
  319. await fetchDeviceDetail(device.devId)
  320. }
  321. // 确认解绑设备
  322. const confirmUnbindDevice = async (devId: string) => {
  323. console.log('确认解绑设备')
  324. try {
  325. await adminAPI.unbindUser({ devId: devId })
  326. unbindOpen.value = false
  327. } catch (err) {
  328. console.log('解绑设备失败', err)
  329. }
  330. }
  331. // 获取设备绑定用户信息
  332. const fetchDeviceBindUser = async (userId: number) => {
  333. console.log('获取设备绑定用户信息', userId)
  334. if (!userId) return
  335. try {
  336. unbindModalLoading.value = true
  337. const res = await adminAPI.getBindUserInfo({
  338. userId,
  339. })
  340. console.log('获取设备绑定用户信息成功', res)
  341. unbindModalLoading.value = false
  342. const data = res.data
  343. unbindDeviceData.value.userPhone = data.phone
  344. } catch (err) {
  345. console.log('获取设备绑定用户信息失败', err)
  346. unbindModalLoading.value = false
  347. }
  348. }
  349. // 从设备详情获取设备的激活时间
  350. const fetchDeviceDetail = async (devId: number) => {
  351. console.log('fetchDeviceDetail', devId)
  352. if (!devId) return
  353. try {
  354. const res = await deviceApi.getDeviceDetailByDevId({
  355. devId: String(devId),
  356. })
  357. console.log('✅获取到设备详情', res)
  358. const data = res.data
  359. unbindDeviceData.value.bindTime = data.activeTime
  360. } catch (error) {
  361. console.error('❌获取设备详情失败', error)
  362. }
  363. }
  364. const addDeviceOpen = ref(false)
  365. // 添加设备
  366. const addDeviceHandler = () => {
  367. console.log('添加设备')
  368. addDeviceOpen.value = true
  369. }
  370. const uploadDeviceOpen = ref(false)
  371. // 批量上传设备
  372. const uploadDeviceHandler = () => {
  373. console.log('批量上传设备')
  374. uploadDeviceOpen.value = true
  375. }
  376. // 是否为今天
  377. function isToday(dateStr: string): boolean {
  378. const inputDate = new Date(dateStr)
  379. const now = new Date()
  380. return (
  381. inputDate.getFullYear() === now.getFullYear() &&
  382. inputDate.getMonth() === now.getMonth() &&
  383. inputDate.getDate() === now.getDate()
  384. )
  385. }
  386. </script>
  387. <style scoped lang="less">
  388. .devicePage {
  389. .searchBar {
  390. padding: 20px;
  391. background-color: #fff;
  392. margin-bottom: 20px;
  393. display: flex;
  394. justify-content: space-between;
  395. .ant-form {
  396. flex-grow: 1;
  397. }
  398. :deep(.ant-form-inline .ant-form-item) {
  399. margin-bottom: 16px !important;
  400. }
  401. }
  402. .tableCard {
  403. background-color: #fff;
  404. &-header {
  405. display: flex;
  406. justify-content: space-between;
  407. padding: 20px;
  408. &-title {
  409. font-size: 18px;
  410. font-weight: 600;
  411. }
  412. .subtitle {
  413. font-size: 14px;
  414. color: #999;
  415. margin-left: 10px;
  416. }
  417. }
  418. }
  419. }
  420. .unbindDevice-btn {
  421. margin-top: 12px;
  422. }
  423. :deep(.ant-descriptions-item-label) {
  424. width: 150px;
  425. }
  426. </style>