src/pages/equipmentManagement/upkeep/add.vue
@@ -40,6 +40,22 @@
               <u-icon name="arrow-right" @click="showDatePicker" />
            </template>
         </u-form-item>
         <u-form-item label="保养人" prop="maintenancePerson" border-bottom>
            <u-input
               v-model="form.maintenancePerson"
               placeholder="请输入保养人"
               clearable
            />
         </u-form-item>
         <u-form-item label="保养项目" prop="maintenanceItems" border-bottom>
            <u-input
               v-model="form.maintenanceItems"
               placeholder="请输入保养项目"
               clearable
            />
         </u-form-item>
         
         <!-- 提交按钮 -->
         <view class="footer-btns">
@@ -69,12 +85,15 @@
<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue';
import { onShow } from '@dcloudio/uni-app';
import { onShow, onUnload } from '@dcloudio/uni-app';
import PageHeader from '@/components/PageHeader.vue';
import { getDeviceLedger } from '@/api/equipmentManagement/ledger';
import { addUpkeep, editUpkeep, getUpkeepById } from '@/api/equipmentManagement/upkeep';
import useUserStore from '@/store/modules/user';
import dayjs from "dayjs";
import { formatDateToYMD } from '@/utils/ruoyi';
const userStore = useUserStore();
defineOptions({
   name: "设备保养计划表单",
@@ -85,6 +104,13 @@
    icon: 'none'
  })
}
const normalizeId = (raw) => {
   if (raw === null || raw === undefined) return undefined;
   const val = String(raw).trim();
   if (!val || val === 'undefined' || val === 'null') return undefined;
   return val;
};
// 表单引用
const formRef = ref(null);
@@ -122,6 +148,8 @@
   deviceLedgerId: undefined, // 设备ID
   deviceModel: undefined, // 规格型号
   maintenancePlanTime: dayjs().format("YYYY-MM-DD"), // 计划保养日期
   maintenancePerson: userStore.nickName || undefined, // 保养人
   maintenanceItems: undefined, // 保养项目
});
// 加载设备列表
@@ -144,6 +172,8 @@
            form.value.deviceLedgerId = data.deviceLedgerId;
            form.value.deviceModel = data.deviceModel;
            form.value.maintenancePlanTime = dayjs(data.maintenancePlanTime).format("YYYY-MM-DD");
            form.value.maintenancePerson = data.maintenancePerson;
            form.value.maintenanceItems = data.maintenanceItems || data.maintenanceLocation;
            // 设置设备名称显示
            const device = deviceOptions.value.find(item => item.id === data.deviceLedgerId);
            if (device) {
@@ -187,24 +217,25 @@
   }
   
   isScanning.value = true;
   showToast('扫码成功,3秒后自动填充设备信息');
   showToast('扫码成功');
   
   // 3秒后处理扫码结果
   scanTimer.value = setTimeout(() => {
      processScanResult(scanResult);
      isScanning.value = false;
   }, 3000);
   }, 1000);
};
function getDeviceIdByRegExp(url) {
   // 匹配deviceId=后面的数字
   const reg = /deviceId=(\d+)/;
   const match = url.match(reg);
   // 如果匹配到结果,返回数字类型,否则返回null
   return match ? Number(match[1]) : null;
}
// 处理扫码结果并匹配设备
const processScanResult = (scanResult) => {
   // 在设备列表中查找匹配的设备
   // 假设二维码内容是设备名称或设备编号
   const matchedDevice = deviceOptions.value.find(device =>
      device.deviceName === scanResult ||
      device.deviceCode === scanResult ||
      device.id.toString() === scanResult
   );
   const deviceId = getDeviceIdByRegExp(scanResult);
   const matchedDevice = deviceOptions.value.find(item => item.id == deviceId);
   
   if (matchedDevice) {
      // 找到匹配的设备,自动填充
@@ -288,6 +319,7 @@
      if (code == 200) {
         showToast(`${id ? "编辑" : "新增"}计划成功`);
         setTimeout(() => {
            uni.removeStorageSync('repairId');
            uni.navigateBack();
         }, 1500);
      } else {
@@ -301,32 +333,30 @@
// 返回上一页
const goBack = () => {
   // 清除存储的id
   uni.removeStorageSync('repairId');
   uni.navigateBack();
};
// 获取页面参数
const getPageParams = () => {
   const pages = getCurrentPages();
   const currentPage = pages[pages.length - 1];
   const options = currentPage.options;
   // 根据是否有id参数来判断是新增还是编辑
   if (options.id) {
      // 编辑模式,获取详情
      loadForm(options.id);
   } else {
      // 新增模式
      loadForm();
   }
};
// 获取页面ID
const getPageId = () => {
   const pages = getCurrentPages();
   const currentPage = pages[pages.length - 1];
   const options = currentPage.options;
   return options.id;
   return normalizeId(uni.getStorageSync('repairId'));
};
// 获取页面参数
const getPageParams = () => {
   const id = getPageId();
   if (id) {
      loadForm(id);
   } else {
      operationType.value = 'add';
      loadForm();
   }
};
onUnload(() => {
   uni.removeStorageSync('repairId');
});
</script>
<style scoped lang="scss">