diff --git a/src/views/system/device/index.vue b/src/views/system/device/index.vue index 01cc990..3fca87a 100644 --- a/src/views/system/device/index.vue +++ b/src/views/system/device/index.vue @@ -123,7 +123,7 @@ - + {{ scope.row.sn }} @@ -333,145 +333,111 @@ - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - + + + + + + + + + + + - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + - - - - - - {{ versionDescription || '暂无说明' }} - - - - - + + + + + + {{ versionDescription || '暂无说明' }} + + + + + - - + + @@ -541,7 +507,7 @@ @select-changed="onSelectChange" @map-geo="onMapGeo" :init-lat="form.latitude" :init-lng="form.longitude" :status="form.status" :online-status="form.onlineStatus" :device-sn="form.sn" :areaId="form.areaId" :trip-route-str="tripRouteStr" /> --> - + @@ -840,100 +806,100 @@ export default { } }, /** 当选择代理商时调用 */ -handleDeptChange(val) { - if (!this.isUpdating) { - if (val) { - this.isUpdating = true; - selectAreaListByDeptId(val).then((response) => { - // 更新运营区选项 - this.areaOptions = response.data.areaList || []; - this.modelOptions = response.data.modelList || []; - - // 清空现有值 - this.form.areaId = null; - this.form.modelId = null; - - // 只有在有选项时才设置默认值 - if (this.areaOptions.length > 0) { - this.form.areaId = this.areaOptions[0].areaId; - } - if (this.modelOptions.length > 0) { - this.form.modelId = this.modelOptions[0].modelId; - } + handleDeptChange(val) { + if (!this.isUpdating) { + if (val) { + this.isUpdating = true; + selectAreaListByDeptId(val).then((response) => { + // 更新运营区选项 + this.areaOptions = response.data.areaList || []; + this.modelOptions = response.data.modelList || []; - // 如果没有选项,显示提示信息 - if (this.areaOptions.length === 0) { - this.$message.warning('该代理商暂无运营区'); - } - if (this.modelOptions.length === 0) { - this.$message.warning('该代理商暂无可用车型'); - } - }).finally(() => { - this.isUpdating = false; - }); - } else { - // 当代理商为空时,加载所有运营区 - this.isUpdating = true; - listArea(this.queryParams2).then((response) => { - this.areaOptions = response.rows || []; - this.form.areaId = null; - this.form.modelId = null; - this.modelOptions = []; - }).finally(() => { - this.isUpdating = false; - }); - } - } -}, + // 清空现有值 + this.form.areaId = null; + this.form.modelId = null; -/** 当选择运营区时调用 */ -handleAreaChange(val) { - if (!this.isUpdating) { - if (val) { - this.isUpdating = true; - selectDeptByAreaId(val).then((response) => { - // 只有在代理商为空时,才设置代理商 - if (!this.form.deptId && response.data.sysDept) { - this.form.deptId = response.data.sysDept.deptId; - - // 获取该代理商下的车型列表 - selectAreaListByDeptId(this.form.deptId).then((deptResponse) => { - this.modelOptions = deptResponse.data.modelList || []; + // 只有在有选项时才设置默认值 + if (this.areaOptions.length > 0) { + this.form.areaId = this.areaOptions[0].areaId; + } if (this.modelOptions.length > 0) { this.form.modelId = this.modelOptions[0].modelId; - } else { - this.form.modelId = null; + } + + // 如果没有选项,显示提示信息 + if (this.areaOptions.length === 0) { + this.$message.warning('该代理商暂无运营区'); + } + if (this.modelOptions.length === 0) { this.$message.warning('该代理商暂无可用车型'); } + }).finally(() => { + this.isUpdating = false; }); } else { - // 如果已有代理商,则使用现有的车型列表 - this.modelOptions = response.data.modelList || []; - if (!this.form.modelId && this.modelOptions.length > 0) { - this.form.modelId = this.modelOptions[0].modelId; - } else if (this.modelOptions.length === 0) { + // 当代理商为空时,加载所有运营区 + this.isUpdating = true; + listArea(this.queryParams2).then((response) => { + this.areaOptions = response.rows || []; + this.form.areaId = null; this.form.modelId = null; - this.$message.warning('该运营区暂无可用车型'); - } + this.modelOptions = []; + }).finally(() => { + this.isUpdating = false; + }); } - }).finally(() => { - this.isUpdating = false; - }); - } else { - // 清空车型,但保留代理商 - this.form.modelId = null; - this.modelOptions = []; - } - } -}, + } + }, - /** 当选择车型时调用 */ - handleModelChange(val) { - // 移除车型变化时对其他字段的影响 - if (!val) { - this.form.modelId = null; - } - }, + /** 当选择运营区时调用 */ + handleAreaChange(val) { + if (!this.isUpdating) { + if (val) { + this.isUpdating = true; + selectDeptByAreaId(val).then((response) => { + // 只有在代理商为空时,才设置代理商 + if (!this.form.deptId && response.data.sysDept) { + this.form.deptId = response.data.sysDept.deptId; + + // 获取该代理商下的车型列表 + selectAreaListByDeptId(this.form.deptId).then((deptResponse) => { + this.modelOptions = deptResponse.data.modelList || []; + if (this.modelOptions.length > 0) { + this.form.modelId = this.modelOptions[0].modelId; + } else { + this.form.modelId = null; + this.$message.warning('该代理商暂无可用车型'); + } + }); + } else { + // 如果已有代理商,则使用现有的车型列表 + this.modelOptions = response.data.modelList || []; + if (!this.form.modelId && this.modelOptions.length > 0) { + this.form.modelId = this.modelOptions[0].modelId; + } else if (this.modelOptions.length === 0) { + this.form.modelId = null; + this.$message.warning('该运营区暂无可用车型'); + } + } + }).finally(() => { + this.isUpdating = false; + }); + } else { + // 清空车型,但保留代理商 + this.form.modelId = null; + this.modelOptions = []; + } + } + }, + + /** 当选择车型时调用 */ + handleModelChange(val) { + // 移除车型变化时对其他字段的影响 + if (!val) { + this.form.modelId = null; + } + }, // 处理硬件版本变化 handleHardwareVersionChange(val) { @@ -1012,8 +978,8 @@ handleAreaChange(val) { // } }, - - + + /** 排序触发事件 */ handleSortChange(column, prop, order) { this.queryParams.orderByColumn = column.prop; @@ -1159,19 +1125,19 @@ handleAreaChange(val) { this.total = response.total; this.loading = false; }); - if(this.queryParams2.deptId){ + if (this.queryParams2.deptId) { selectAreaListByDeptId(this.queryParams2.deptId).then((deptResponse) => { - this.modelOptions = deptResponse.data.modelList || []; - - }); - }else{ + this.modelOptions = deptResponse.data.modelList || []; + + }); + } else { listModel(this.queryParams2).then((response) => { - - - this.modelOptions = response.rows; - }); + + + this.modelOptions = response.rows; + }); } - + }, // 取消按钮 cancel() { @@ -1215,7 +1181,7 @@ handleAreaChange(val) { } ); } - + listModel(this.queryParams2).then((response) => { this.modelOptions = response.rows; }); @@ -1224,24 +1190,24 @@ handleAreaChange(val) { }); }, reset2() { - this.form.areaId = null; - this.form.deptId = null; - this.form.modelId = null; - this.areaOptions = []; - this.modelOptions = []; - - // 重新加载代理商列表 - if (this.userName === "admin") { - listDept2({ status: "0", pageNum: 1, pageSize: 999 }).then((response) => { - this.deptOptions = response.rows; + this.form.areaId = null; + this.form.deptId = null; + this.form.modelId = null; + this.areaOptions = []; + this.modelOptions = []; + + // 重新加载代理商列表 + if (this.userName === "admin") { + listDept2({ status: "0", pageNum: 1, pageSize: 999 }).then((response) => { + this.deptOptions = response.rows; + }); + } + + // 重新加载运营区列表 + listArea(this.queryParams2).then((response) => { + this.areaOptions = response.rows; }); - } - - // 重新加载运营区列表 - listArea(this.queryParams2).then((response) => { - this.areaOptions = response.rows; - }); - }, + }, /** 搜索按钮操作 */ handleQuery() { this.queryParams.pageNum = 1; @@ -1271,7 +1237,7 @@ handleAreaChange(val) { }, /** 详情按钮 */ handleView(row) { - const deviceId = row.deviceId + const deviceId = row.deviceId this.$router.push(`/system/deviceDetail/index/${deviceId}`) // this.$router.push({ // path: '/system/deviceDetail/index', @@ -1349,33 +1315,45 @@ handleAreaChange(val) { // this.fetchData(response.data.deptId) // this.open = true; // this.title = "修改设备1"; - - selectAreaListByDeptId(response.data.deptId).then((res) => { - // 方案1: 使用Vue.set确保响应式更新 - setTimeout(() => { - console.log('=调用了11'); - - this.$set(this, 'modelOptions', []); + // this.open = true; + // this.title = "修改设备"; + let deptId = '100'; + if (response.data.deptId == null) { + console.log('判断1'); + + deptId = '100' + } else { + console.log('判断2'); + deptId = response.data.deptId; + } + console.log(deptId, 'deptIddeptIddeptIddeptId'); + selectAreaListByDeptId(deptId).then((res) => { + // 方案1: 使用Vue.set确保响应式更新 + setTimeout(() => { + + + this.$set(this, 'modelOptions', []); this.$set(this, 'areaOptions', []); this.$set(this, 'modelOptions', res.data.modelList); this.$set(this, 'areaOptions', res.data.areaList); this.$forceUpdate() this.open = true; this.title = "修改设备"; - }, 800); - + }, 800); + + + // 方案2: 使用nextTick确保DOM更新 + // this.$nextTick(() => { + // this.modelOptions = res.data.modelList; + // this.areaOptions = res.data.areaList; + // },600); + + // 不再需要setTimeout和$forceUpdate + }).finally(() => { + + this.isUpdating = false; + }); - // 方案2: 使用nextTick确保DOM更新 - // this.$nextTick(() => { - // this.modelOptions = res.data.modelList; - // this.areaOptions = res.data.areaList; - // },600); - - // 不再需要setTimeout和$forceUpdate - }).finally(() => { - this.isUpdating = false; - }); - }); }, handleListing(row) { @@ -1503,20 +1481,59 @@ handleAreaChange(val) { }, /** 提交按钮 */ submitForm() { - this.$refs["form"].validate((valid) => { + this.$refs["form"].validate(async (valid) => { if (valid) { - if (this.form.deviceId != null) { - updateDevice(this.form).then((response) => { + try { + // 1. 如果选择了运营区,验证运营区与代理商的关系 + if (this.form.areaId) { + const areaResponse = await selectDeptByAreaId(this.form.areaId); + const areaDeptId = areaResponse.data.sysDept?.deptId; + + if (this.form.deptId && areaDeptId && this.form.deptId !== areaDeptId) { + this.$modal.msgError("所选运营区不属于当前代理商,请重新选择!"); + return; + } + } + + // 2. 如果选择了代理商,验证车型是否属于该代理商 + if (this.form.deptId && this.form.modelId) { + const deptResponse = await selectAreaListByDeptId(this.form.deptId); + const validModels = deptResponse.data.modelList || []; + const isValidModel = validModels.some(model => model.modelId === this.form.modelId); + + if (!isValidModel) { + this.$modal.msgError("所选车型不属于当前代理商,请重新选择!"); + return; + } + } + + // 3. 如果选择了运营区和车型,验证车型是否属于该运营区 + if (this.form.areaId && this.form.modelId) { + const areaResponse = await selectDeptByAreaId(this.form.areaId); + const validModels = areaResponse.data.modelList || []; + const isValidModel = validModels.some(model => model.modelId === this.form.modelId); + + if (!isValidModel) { + this.$modal.msgError("所选车型不属于当前运营区,请重新选择!"); + return; + } + } + + // 所有验证通过后,执行提交操作 + if (this.form.deviceId != null) { + const response = await updateDevice(this.form); this.$modal.msgSuccess("修改成功"); this.open = false; this.getList(); - }); - } else { - addDevice(this.form).then((response) => { + } else { + const response = await addDevice(this.form); this.$modal.msgSuccess("新增成功"); this.open = false; this.getList(); - }); + } + } catch (error) { + console.error("提交表单时发生错误:", error); + this.$modal.msgError("操作失败:" + (error.message || "未知错误")); } } }); @@ -1548,6 +1565,8 @@ handleAreaChange(val) { }, }; + +