diff --git a/src/api/device/index.ts b/src/api/device/index.ts index 907c79fa..0fa0bdd4 100644 --- a/src/api/device/index.ts +++ b/src/api/device/index.ts @@ -60,5 +60,10 @@ export const DeviceApi = { //根据设备ID更新设备状态 updateDeviceStatus: async (devicecode: number, devicestatus: number) => { return await request.put({ url: `/system/device/updateDeviceStatus?devicecode=` + devicecode + `&devicestatus=` + devicestatus }) - } + }, + + //查询没有绑定过的设备 + getDeviceNotBind: async (params: any) => { + return await request.get({ url: `/system/device/getDeviceNotBind`, params }) + }, } diff --git a/src/api/deviceuser/index.ts b/src/api/deviceuser/index.ts index 1e3f8c5d..64c9faa5 100644 --- a/src/api/deviceuser/index.ts +++ b/src/api/deviceuser/index.ts @@ -53,4 +53,9 @@ export const DeviceuserApi = { getDeviceuserByDeviceId: async (deviceid: number) => { return await request.get({ url: `/system/deviceuser/getDeviceuserByDeviceId?deviceid=` + deviceid }) }, + + //根据用户ID查询设备人员关联 + getDeviceuserByUserId: async (userid: number) => { + return await request.get({ url: `/system/deviceuser/getDeviceuserByUserId?userid=` + userid }) + } } diff --git a/src/views/person/devicebind.vue b/src/views/person/devicebind.vue index cf9d555a..164ca3e6 100644 --- a/src/views/person/devicebind.vue +++ b/src/views/person/devicebind.vue @@ -2,107 +2,211 @@ - - - - - - + +
+
+

已绑定设备

+
+ - - - - - 搜索 - 重置 - - - -
- - - - - - - - - - - - -
+ + + + + 搜索 + 重置 + + -
- +
+ + + + + + + + + + + + +
+
+ + +
+
+

可绑定设备

+
+ + + + + + + + + + + 搜索 + 重置 + + + +
+ + + + + + + + + + + + +
+ +
+ +
+
@@ -122,30 +226,36 @@ const userProfile = ref() const message = useMessage() const dialogVisible = ref(false) const loading = ref(false) +const boundLoading = ref(false) const total = ref(0) const list = ref([]) +const boundList = ref([]) const personId = ref() const personName = ref() const queryParams = reactive({ pageNo: 1, pageSize: 10, - devicename: undefined, - devicecode: undefined, - devicetype: undefined, - devicestatus: 0, orgid: undefined }) const queryFormRef = ref() +const boundQueryParams = reactive({ + devicecode: undefined, + devicetype: undefined +}) + +const boundQueryFormRef = ref() + /** 查询列表 */ const getList = async () => { loading.value = true try { userProfile.value = await getUserProfile() queryParams.orgid = userProfile.value.dept.id - const data = await DeviceApi.getDevicePage(queryParams) + console.log(queryParams) + const data = await DeviceApi.getDeviceNotBind(queryParams) list.value = data.list total.value = data.total } finally { @@ -153,6 +263,26 @@ const getList = async () => { } } +/** 查询已绑定设备列表 */ +const getBoundList = async () => { + if (!personId.value) return + boundLoading.value = true + try { + const bindData = await DeviceuserApi.getDeviceuserByUserId(personId.value) + // 获取每个设备的详细信息 + const devicePromises = bindData.map(async (item: DeviceuserVO) => { + const deviceInfo = await DeviceApi.getDeviceId(item.deviceid) + return { + ...deviceInfo, + devicecode: item.deviceid // 确保devicecode字段存在 + } + }) + boundList.value = await Promise.all(devicePromises) + } finally { + boundLoading.value = false + } +} + /** 搜索按钮操作 */ const handleQuery = () => { queryParams.pageNo = 1 @@ -162,10 +292,25 @@ const handleQuery = () => { /** 重置按钮操作 */ const resetQuery = () => { queryFormRef.value?.resetFields() - queryParams.devicestatus = 0 handleQuery() } +/** 搜索已绑定设备 */ +const handleBoundQuery = () => { + const { devicecode, devicetype } = boundQueryParams + boundList.value = boundList.value.filter(item => { + const matchCode = !devicecode || item.devicecode.toString().includes(devicecode) + const matchType = !devicetype || item.devicetype === devicetype + return matchCode && matchType + }) +} + +/** 重置已绑定设备搜索 */ +const resetBoundQuery = () => { + boundQueryFormRef.value?.resetFields() + getBoundList() +} + /** 获取设备状态标签 */ const getDeviceStatusLabel = (status: number) => { const statusMap = { @@ -223,12 +368,38 @@ const handleBind = async (row: DeviceVO) => { } } +/** 解绑设备 */ +const handleUnbind = async (row: DeviceVO) => { + try { + await ElMessageBox.confirm('确认要解绑该设备吗?', '提示', { + confirmButtonText: '确定', + cancelButtonText: '取消', + type: 'warning' + }) + + const bindData = await DeviceuserApi.getDeviceuserByDeviceId(row.id) + const bindRecord = bindData.find((item: DeviceuserVO) => item.userid === personId.value) + if (bindRecord) { + await DeviceuserApi.deleteDeviceuser(bindRecord.id) + await DeviceApi.updateDeviceStatus(row.devicecode, 0) + message.success('解绑成功') + getBoundList() + getList() + } + } catch (error) { + if (error !== 'cancel') { + message.error('解绑失败') + } + } +} + /** 打开弹窗 */ const open = (id: number, name: string) => { personId.value = id personName.value = name dialogVisible.value = true getList() + getBoundList() } defineExpose({