This commit is contained in:
dhr
2025-09-05 15:11:21 +08:00
parent c35a75d030
commit eb895793dd
4 changed files with 150 additions and 133 deletions

View File

@ -94,3 +94,10 @@ export function getRemoveBind(data: { id: number; clientId: string }) {
data: data
});
}
export function getProjectId() {
return request({
url: 'gps/equipment/getProjectList',
method: 'get'
});
}

View File

@ -117,6 +117,10 @@ export interface EquipmentQuery extends PageQuery {
* 项目ID
*/
projectId?: string | number;
/**
* 是否绑定
*/
type?: string | number;
/**
* 用户id

View File

@ -0,0 +1,18 @@
<template>
<div class="text-center py-10">
<el-button type="primary" @click="handleGoBack">返回上一页</el-button>
</div>
</template>
<script setup name="EmptyPage" lang="ts">
import { useRouter } from 'vue-router';
const router = useRouter();
// 返回上一页
const handleGoBack = () => {
router.go(-1);
};
</script>
<style scoped></style>

View File

@ -41,6 +41,12 @@
<el-col :span="2">
<el-button type="primary" plain @click="handleViewAll" v-hasPermi="['system:equipment:view']">{{ viewAllButtonText }}</el-button>
</el-col>
<!-- 新增跳转空页面按钮 -->
<el-col :span="2">
<el-button type="primary" plain icon="international" @click="handleGoToEmptyPage"> GPS定位 </el-button>
</el-col>
<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
</el-row>
</template>
@ -48,11 +54,20 @@
<el-table v-loading="loading" :data="equipmentList" @selection-change="handleSelectionChange">
<el-table-column type="selection" width="55" align="center" />
<el-table-column label="项目ID" align="center" prop="projectId" />
<el-table-column label="用户id" align="center" prop="userId" />
<el-table-column label="项目名称" align="center" prop="projectName" />
<el-table-column label="用户名称" align="center" prop="userName" />
<el-table-column label="设备标识" align="center" prop="clientId" />
<!-- 设备名称列点击触发历史用户弹窗传入项目ID+当前用户ID -->
<!-- 绑定状态列 -->
<el-table-column label="绑定状态" align="center">
<template #default="scope">
<el-tag :type="scope.row.type === 1 ? 'success' : 'warning'" size="small">
{{ scope.row.type === 1 ? '已绑定' : '未绑定' }}
</el-tag>
</template>
</el-table-column>
<!-- 设备名称列 -->
<el-table-column label="设备名称" align="center">
<template #default="scope">
<el-button
@ -85,12 +100,12 @@
<el-tooltip content="删除" placement="top">
<el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['gps:equipment:remove']"></el-button>
</el-tooltip>
<el-tooltip :content="scope.row.userId ? '取消绑定' : '绑定用户'" placement="top">
<el-tooltip :content="scope.row.type === 1 ? '取消绑定' : '绑定用户'" placement="top">
<el-button
link
type="primary"
icon="User"
@click="scope.row.userId ? handleUnbindUser(scope.row) : handleBindUser(scope.row)"
@click="scope.row.type === 1 ? handleUnbindUser(scope.row) : handleBindUser(scope.row)"
v-hasPermi="['gps:equipment:bindUser']"
>
</el-button>
@ -120,9 +135,10 @@
<!-- 绑定用户对话框 -->
<el-dialog title="绑定用户" v-model="bindDialogVisible" width="500px" append-to-body>
<el-form ref="bindUserFormRef" :model="bindForm" :rules="bindRules" label-width="80px">
<!-- 绑定用户对话框 - 项目选择下拉框 -->
<el-form-item label="项目选择" prop="projectId" required>
<el-select v-model="bindForm.projectId" placeholder="请选择项目" clearable @change="handleProjectChange">
<el-option v-for="project in projectList" :key="project.id" :label="project.name" :value="project.id" />
<el-option v-for="project in projectList" :key="project.projectId" :label="project.projectName" :value="project.projectId" />
</el-select>
</el-form-item>
<el-form-item label="设备名称" prop="deviceName">
@ -143,20 +159,8 @@
</el-dialog>
<!-- 历史用户弹窗 -->
<el-dialog
title="设备历史用户"
v-model="historyUserDialogVisible"
width="600px"
append-to-body
@close="
() => {
historyUserList.splice(0);
}
"
>
<!-- 加载状态 -->
<el-dialog title="设备历史用户" v-model="historyUserDialogVisible" width="600px" append-to-body @close="() => historyUserList.splice(0)">
<div v-loading="historyUserLoading" style="min-height: 200px; padding: 16px">
<!-- 历史用户列表 -->
<el-table
:data="historyUserList"
stripe
@ -168,15 +172,13 @@
<el-table-column label="用户名" align="center" prop="userName" />
<el-table-column label="状态" align="center" width="120">
<template #default="scope">
<!-- 通过scope.row.type获取当前行的状态值判断后显示对应内容 -->
<el-tag :type="scope.row.type === 1 ? 'success' : 'info'" size="small">
{{ scope.row.type === 1 ? '当前绑定' : '历史绑定' }}
<el-tag :type="scope.row.type === 0 ? 'success' : 'info'" size="small">
{{ scope.row.type === 0 ? '当前绑定' : '历史绑定' }}
</el-tag>
</template>
</el-table-column>
</el-table>
<!-- 历史用户弹窗-无数据提示 -->
<div v-if="!historyUserLoading && historyUserList.length === 0" style="text-align: center; padding: 50px 0">
<el-empty description="暂无该设备的历史用户数据"></el-empty>
</div>
@ -190,7 +192,6 @@
</template>
<script setup name="Equipment" lang="ts">
// 导入历史用户接口确保API文件中存在该接口
import {
listEquipment,
getEquipment,
@ -200,17 +201,24 @@ import {
bindUser,
getUserId,
gethistroyUser,
getRemoveBind
getRemoveBind,
getProjectId
} from '@/api/equipment/index';
import { listProject } from '@/api/project/project/index';
import { EquipmentVO, EquipmentQuery, EquipmentForm } from '@/api/equipment/types';
import { getCurrentInstance, ComponentInternalInstance, onMounted, ref, reactive, toRefs, watch, computed } from 'vue';
import { useUserStoreHook } from '@/store/modules/user';
import { Row } from 'element-plus/es/components/table-v2/src/components/index.mjs';
import { useRouter } from 'vue-router'; // 新增:导入路由钩子
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const router = useRouter(); // 新增:初始化路由实例
// 扩展EquipmentVO类型添加type字段
interface ExtendedEquipmentVO extends EquipmentVO {
type: 1 | 2; // 1=已绑定2=未绑定
}
// 设备列表相关
const equipmentList = ref<EquipmentVO[]>([]);
const equipmentList = ref<ExtendedEquipmentVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
@ -218,7 +226,6 @@ const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const isViewAll = ref(false); // 标记是否处于查看全部模式
// 表单相关引用
const queryFormRef = ref<ElFormInstance>();
@ -228,12 +235,12 @@ const bindUserFormRef = ref<ElFormInstance>();
// 项目和用户相关
const userStore = useUserStoreHook();
const currentProject = computed(() => userStore.selectedProject);
const userList = ref<any[]>([]); // 存储获取到的用户列表
const projectList = ref<any[]>([]); // 存储获取到的项目列表
const projectLoading = ref(false); // 项目列表加载状态
const userLoading = ref(false); // 用户列表加载状态
const userList = ref<any[]>([]);
const projectList = ref<any[]>([]);
const projectLoading = ref(false);
const userLoading = ref(false);
const viewAllButtonText = computed(() => {
return isViewAll.value ? '查看绑定设备' : '查看绑定设备';
return queryParams.value.type === 1 ? '查看绑定设备' : '查看绑定设备';
});
// 对话框相关
@ -244,10 +251,10 @@ const dialog = reactive<DialogOption>({
const bindDialogVisible = ref(false);
const bindButtonLoading = ref(false);
// 历史用户弹窗相关状态移除无用的currentDeviceId
const historyUserDialogVisible = ref(false); // 历史用户弹窗可见性
const historyUserList = ref<HistoryUserVO[]>([]); // 历史用户列表
const historyUserLoading = ref(false); // 历史用户接口加载状态
// 历史用户弹窗相关
const historyUserDialogVisible = ref(false);
const historyUserList = ref<HistoryUserVO[]>([]);
const historyUserLoading = ref(false);
// 绑定用户表单数据
const bindForm = reactive({
@ -268,7 +275,6 @@ const initFormData: EquipmentForm = {
id: undefined,
projectId: undefined,
userId: undefined,
clientId: undefined,
deviceName: undefined,
udp: undefined,
remoteAddressStr: undefined,
@ -282,6 +288,7 @@ const initFormData: EquipmentForm = {
const data = reactive<PageData<EquipmentForm, EquipmentQuery>>({
form: { ...initFormData },
queryParams: {
type: 1, // 默认查询已绑定设备
pageNum: 1,
pageSize: 10,
projectId: undefined,
@ -302,11 +309,7 @@ const data = reactive<PageData<EquipmentForm, EquipmentQuery>>({
const { queryParams, form, rules } = toRefs(data);
/**
* 格式化日期时间
* @param timestamp 时间戳、日期字符串或自定义的 yyyyMMddHHmmss 格式数字
* @returns 格式化后的日期时间字符串yyyy-MM-dd HH:mm:ss
*/
/** 格式化日期时间 */
const formatDateTime = (timestamp: any): string => {
if (!timestamp) return '-';
@ -365,21 +368,20 @@ const formatDateTime = (timestamp: any): string => {
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
};
/** 获取设备列表 */
const getList = async () => {
loading.value = true;
try {
// 仅当projectId有实际值非undefined、非null才退出未绑定模式
if (queryParams.value.projectId !== undefined && queryParams.value.projectId !== null) {
isViewAll.value = false;
}
// 已绑定模式下:若未选项目,自动用当前项目补全筛选
if (!isViewAll.value && currentProject.value?.id && queryParams.value.projectId === undefined) {
// 已绑定设备默认关联当前项目
if (queryParams.value.type === 1 && currentProject.value?.id && !queryParams.value.projectId) {
queryParams.value.projectId = currentProject.value.id;
} else if (queryParams.value.type === 2) {
// 未绑定设备清空项目筛选
queryParams.value.projectId = undefined;
}
const res = await listEquipment(queryParams.value);
equipmentList.value = res.rows;
equipmentList.value = res.rows as ExtendedEquipmentVO[];
total.value = res.total;
} catch (error) {
console.error('获取设备列表失败:', error);
@ -392,10 +394,31 @@ const getList = async () => {
const getProjects = async () => {
projectLoading.value = true;
try {
const res = await listProject();
projectList.value = res.rows || [];
const res = await getProjectId();
console.log('getProjectId接口原始返回:', res);
// 从res.data中获取数组适配外层包裹的接口格式
const rawProjects = res?.data || [];
// 验证数据类型,确保是数组
if (!Array.isArray(rawProjects)) {
console.warn('接口返回的项目数据不是数组,已自动修正为空数组');
projectList.value = [];
return;
}
// 处理项目数据使用projectId和projectName
projectList.value = rawProjects
.map((project) => ({
projectId: project.projectId,
projectName: project.projectName || '未命名项目'
}))
.filter((project) => project.projectId);
console.log('处理后项目列表:', projectList.value);
} catch (error) {
console.error('获取项目列表失败:', error);
proxy?.$modal.msgError('获取项目列表失败,请重试');
} finally {
projectLoading.value = false;
}
@ -422,30 +445,40 @@ const getUsersByProjectId = async (projectId: any) => {
/** 项目选择变化时触发 */
const handleProjectChange = (projectId: any) => {
isViewAll.value = false;
bindForm.userId = undefined;
getUsersByProjectId(projectId);
queryParams.value.projectId = projectId;
getList();
// 仅已绑定设备需要更新项目筛选
if (queryParams.value.type === 1) {
queryParams.value.projectId = projectId;
getList();
}
};
/** 切换查看绑定设备无projectId / 查看已绑定设备有projectId */
/** 切换查看绑定/未绑定设备 */
const handleViewAll = () => {
queryParams.value.pageNum = 1;
if (!isViewAll.value) {
queryParams.value.projectId = null;
isViewAll.value = true;
proxy?.$modal.msgSuccess('已切换到查看未绑定设备模式(仅显示无项目设备)');
if (queryParams.value.type === 1) {
// 切换到未绑定设备
queryParams.value.type = 2;
proxy?.$modal.msgSuccess('已切换到查看未绑定设备模式');
} else {
// 切换到已绑定设备
queryParams.value.type = 1;
queryParams.value.projectId = currentProject.value?.id;
isViewAll.value = false;
proxy?.$modal.msgSuccess('已切换到查看已绑定设备模式(仅显示当前项目设备)');
proxy?.$modal.msgSuccess('已切换到查看已绑定设备模式');
}
getList();
};
const handleGoToEmptyPage = () => {
router.push({
path: './equipmentGPS'
});
};
/** 取消按钮 */
const cancel = () => {
reset();
@ -460,28 +493,27 @@ const reset = () => {
/** 搜索按钮操作 */
const handleQuery = () => {
isViewAll.value = false;
queryParams.value.pageNum = 1;
getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
isViewAll.value = false;
queryFormRef.value?.resetFields();
queryParams.value.type = 1;
queryParams.value.projectId = currentProject.value?.id;
handleQuery();
};
/** 多选框选中数据 */
const handleSelectionChange = (selection: EquipmentVO[]) => {
const handleSelectionChange = (selection: ExtendedEquipmentVO[]) => {
ids.value = selection.map((item) => item.id);
single.value = selection.length != 1;
multiple.value = !selection.length;
};
/** 修改按钮操作 */
const handleUpdate = async (row?: EquipmentVO) => {
const handleUpdate = async (row?: ExtendedEquipmentVO) => {
reset();
const _id = row?.id || ids.value[0];
try {
@ -495,6 +527,7 @@ const handleUpdate = async (row?: EquipmentVO) => {
}
};
/** 提交表单 */
const submitForm = () => {
equipmentFormRef.value?.validate(async (valid: boolean) => {
if (valid) {
@ -519,7 +552,7 @@ const submitForm = () => {
};
/** 删除按钮操作 */
const handleDelete = async (row?: EquipmentVO) => {
const handleDelete = async (row?: ExtendedEquipmentVO) => {
const _ids = row?.id || ids.value;
try {
await proxy?.$modal.confirm('是否确认删除GPS设备详细编号为"' + _ids + '"的数据项');
@ -540,7 +573,7 @@ const handleBindUser = async (row?: EquipmentVO) => {
id: undefined,
projectId: undefined,
userId: undefined,
clientId: row?.clientId, // 从行数据中获取clientId并保存到bindForm
clientId: row?.clientId,
deviceName: undefined
});
bindUserFormRef.value?.resetFields();
@ -550,10 +583,8 @@ const handleBindUser = async (row?: EquipmentVO) => {
try {
const res = await getEquipment(_id);
const equipmentData = res.data;
bindForm.id = equipmentData.id;
bindForm.deviceName = equipmentData.deviceName;
// 确保clientId正确赋值优先使用行数据中的值
bindForm.clientId = row?.clientId || equipmentData.clientId;
if (projectList.value.length === 0) {
@ -577,10 +608,8 @@ const handleBindUser = async (row?: EquipmentVO) => {
/** 提交绑定用户 */
const submitBindUser = () => {
// 移除多余的row参数因为方法没有接收该参数
bindUserFormRef.value?.validate(async (valid: boolean) => {
if (valid) {
// 验证clientId是否存在
if (!bindForm.clientId) {
proxy?.$modal.msgWarning('设备标识clientId不存在无法完成绑定');
bindButtonLoading.value = false;
@ -593,11 +622,10 @@ const submitBindUser = () => {
id: bindForm.id,
projectId: bindForm.projectId,
userId: bindForm.userId,
clientId: bindForm.clientId, // 从bindForm获取clientId
clientId: bindForm.clientId,
deviceName: bindForm.deviceName
};
// 打印参数日志确认clientId已正确传递
console.log('提交绑定用户参数:', bindData);
await bindUser(bindData);
@ -614,29 +642,19 @@ const submitBindUser = () => {
});
};
/** 取消绑定用户 */
/** 取消绑定用户对话框 */
const cancelBindUser = () => {
bindDialogVisible.value = false;
};
/** 取消用户绑定操作 */
const handleUnbindUser = async (row: EquipmentVO) => {
const handleUnbindUser = async (row: ExtendedEquipmentVO) => {
try {
// 1. 打印完整行数据用于调试
console.log('解除绑定 - 原始行数据:', {
rowId: row.id,
rowIdType: typeof row.id,
clientId: row.clientId,
clientIdType: typeof row.clientId
});
// 2. 严格验证参数
if (!row) {
proxy?.$modal.msgWarning('未获取到设备信息');
return;
}
// 验证id
if (row.id === undefined || row.id === null || row.id === '') {
proxy?.$modal.msgWarning('设备ID不能为空');
return;
@ -647,66 +665,41 @@ const handleUnbindUser = async (row: EquipmentVO) => {
return;
}
// 验证clientId
if (!row.clientId || typeof row.clientId !== 'string' || row.clientId.trim() === '') {
proxy?.$modal.msgWarning(`设备标识clientId格式错误必须是非空字符串当前值: ${row.clientId}`);
return;
}
const clientId = row.clientId.trim();
// 3. 构建符合要求的参数对象
const unbindParams = {
id: deviceId, // 确保是数字类型
clientId: clientId // 确保是字符串类型
id: deviceId,
clientId: clientId
};
console.log('解除绑定 - 发送参数:', unbindParams);
console.log('解除绑定 - 参数类型:', {
idType: typeof unbindParams.id,
clientIdType: typeof unbindParams.clientId
});
// 4. 确认对话框
await proxy?.$modal.confirm('是否确认解除该设备的用户绑定');
// 5. 调用接口并记录请求详情
console.log('解除绑定 - 开始调用接口:', '/equipment/removeBind');
const response = await getRemoveBind(unbindParams);
// 6. 记录接口响应
console.log('解除绑定 - 接口响应:', response);
// 7. 验证响应是否成功
if (response && response.code === 200) {
// 根据实际接口成功标识调整
proxy?.$modal.msgSuccess('解除绑定成功');
await getList();
} else {
throw new Error(`接口返回非成功状态: ${JSON.stringify(response)}`);
}
} catch (error) {
// 详细错误信息记录
console.error('解除绑定 - 错误详情:', error);
console.error('解除绑定 - 错误堆栈:', error instanceof Error ? error.stack : '无堆栈信息');
// 区分不同错误类型
if (error === 'cancel' || (error instanceof Error && error.message.includes('cancel'))) {
console.log('用户取消了解除绑定操作');
return;
}
// 显示更具体的错误信息
const errorMsg = error instanceof Error ? error.message : typeof error === 'string' ? error : '未知错误';
proxy?.$modal.msgError(`解除绑定失败: ${errorMsg}`);
}
};
/**
* 打开历史用户弹窗修改后传入clientId和当前用户ID
* @param clientId 设备标识原deviceId参数替换为clientId
* @param currentUserId 当前绑定的用户ID
*/
/** 打开历史用户弹窗 */
const handleOpenHistoryUser = async (clientId: string | number | undefined, currentUserId: string | number | undefined) => {
// 边界校验改为校验clientId是否存在而非原deviceId
if (!clientId) {
proxy?.$modal.msgWarning('设备标识clientId不存在无法获取历史用户');
return;
@ -717,16 +710,13 @@ const handleOpenHistoryUser = async (clientId: string | number | undefined, curr
historyUserList.value = [];
try {
// 关键修改:接口调用时传递 clientId而非原deviceId
// 注意:需确认后端接口接收「设备标识」的参数名(此处假设为 clientId若后端仍用 id 则改为 id: clientId
const res = await gethistroyUser({
clientId: clientId, // 传递设备标识参数名需与后端接口一致如后端用id则改为 id: clientId
userId: currentUserId || '' // 保持当前用户ID不变
clientId: clientId,
userId: currentUserId || ''
});
const rawUserList = res.data || [];
if (currentUserId && rawUserList.length > 0) {
// 分离当前用户和历史用户(当前用户置顶)
const currentUser = rawUserList.find((user: HistoryUserVO) => user.sysUserId === currentUserId);
const historyUsers = rawUserList.filter((user: HistoryUserVO) => user.sysUserId !== currentUserId);
historyUserList.value = currentUser ? [currentUser, ...historyUsers] : historyUsers;
@ -742,17 +732,16 @@ const handleOpenHistoryUser = async (clientId: string | number | undefined, curr
}
};
/** 页面挂载时初始化 */
onMounted(() => {
getProjects().then(() => {
getList();
});
getList();
});
// 监听项目id刷新数据
/** 监听项目变化 */
const listeningProject = watch(
() => currentProject.value?.id,
(nid) => {
if (!isViewAll.value) {
if (queryParams.value.type === 1) {
queryParams.value.projectId = nid;
form.value.projectId = nid;
getList();
@ -760,11 +749,12 @@ const listeningProject = watch(
}
);
/** 页面卸载时清理 */
onUnmounted(() => {
listeningProject();
});
// 类型补充
// 类型定义
interface DialogOption {
visible: boolean;
title: string;
@ -776,13 +766,11 @@ interface PageData<T, Q> {
rules: Record<string, any[]>;
}
// 历史用户类型定义(根据后端返回字段调整)
interface HistoryUserVO {
sysUserId: string | number; // 用户ID与绑定用户的userId字段一致
userName: string; // 用户名
lastUseTime?: number | string; // 最后使用时间
bindTime?: number | string; // 绑定时间
sysUserId: string | number;
userName: string;
lastUseTime?: number | string;
bindTime?: number | string;
type?: number; // 1:当前绑定,其他:历史绑定
// 其他后端返回字段...
}
</script>