puzhibing
6 天以前 0db8c9b94a9ac655549d5eba5c2a6c92ac386c06
culture/src/views/strain-library/production-cell-library/add.vue
@@ -2,8 +2,43 @@
    <Card>
        <el-form :model="form" :rules="rules" ref="strainForm" label-position="top" class="strain-form">
            <div class="form-row three-columns">
                <el-form-item label="菌种编号" prop="strainCode">
                    <el-input v-model="form.strainCode" placeholder="请输入"></el-input>
              <el-form-item label="选择项目组:" prop="projectId">
                <el-select
                    v-model="form.projectId"
                    placeholder="请选择项目组"
                    filterable
                    clearable
                    :key="teamList.length"
                    :loading="teamLoading"
                    @focus="fetchTeamList"
                >
                  <el-option
                      v-for="item in teamList"
                      :key="item.id"
                      :label="item.teamName"
                      :value="item.id"
                  />
                </el-select>
              </el-form-item>
                <el-form-item label="菌种编号" prop="strainCode" class="strain-code-form-item">
<!--                    <el-input v-model="form.strainCode" placeholder="请输入"></el-input>-->
                  <div class="strain-code-wrapper">
                    <el-input
                        v-model="form.strainCode"
                        placeholder="请输入前缀"
                        class="strain-code-input"
                    />
                    <span class="strain-code-separator">-</span>
                    <el-input
                        :value="displayStrainSeq"
                        placeholder="序号"
                        class="strain-seq-input"
                        maxlength="3"
                        @input="handleSeqInput"
                        @blur="formatSeq"
                        @focus="handleFocus"
                    />
                  </div>
                </el-form-item>
                <el-form-item label="菌种名称" prop="strainName">
                    <el-input v-model="form.strainName" placeholder="请输入"></el-input>
@@ -78,33 +113,40 @@
<script>
import SignatureCanvas from '@/components/SignatureCanvas.vue'
import { add, edit, getDetail, addBatch } from './service'
import {add, addBatch, edit, getDetail} from './service'
import {myTeamPageList} from "@/components/chooseProject/servce";
export default {
    name: 'StrainLibraryManageAdd',
    name: 'ProductionCellLibraryAdd',
    components: {
        SignatureCanvas
    },
    data() {
        return {
          teamList: [], // 项目组列表
          teamLoading: false, // 加载状态
            batchAddDialogVisible: false,
            signatureVisible: false,
            currentAction: '', // 'submit' or 'batchAdd'
            batchForm: {
                count: ''
            },
          displayStrainSeq: '', // 显示用的序号(可能包含前导零)
          cacheForm: null,
            form: {
                strainCode: '',
                strainName: '',
                source: '',
                appraisalMethod: '',
                characteristics: '',
              projectId: '',
                storageLocation: '',
                preservationMethod: '',
                remark: ''
            },
            rules: {
                strainCode: [{
              projectId: [{ required: true, message: '请选择所属项目组', trigger: 'change' }],
                strainCode: [{required: true,
                    validator: (rule, value, callback) => {
                        if (this.currentAction === 'submit' && !value) {
                            callback(new Error('请输入菌种编号'));
@@ -123,22 +165,48 @@
            }
        }
    },
    mounted() {
        this.form = {
            strainCode: '',
            strainName: '',
            source: '',
            appraisalMethod: '',
            characteristics: '',
            storageLocation: '',
            preservationMethod: '',
            remark: ''
  deactivated() {
    this.cacheForm = JSON.parse(JSON.stringify(this.form));
    this.cacheForm.displayStrainSeq = this.displayStrainSeq
  },
    async activated() {
      await this.fetchTeamList();
      this.form = {
        strainCode: '',
        strainName: '',
        source: '',
        appraisalMethod: '',
        characteristics: '',
        storageLocation: '',
        preservationMethod: '',
        remark: ''
      }
      if (this.cacheForm) {
        this.form = this.cacheForm;
        if (this.cacheForm.displayStrainSeq) {
          this.displayStrainSeq = this.cacheForm.displayStrainSeq;
        }
        if (this.$route.query.id) {
            getDetail({ id: this.$route.query.id }).then(res => {
                this.form = res
            })
        }
      }
      if (this.$route.query.id && !this.cacheForm) {
        getDetail({id: this.$route.query.id}).then(res => {
          this.form = res
          if (res.strainCode) {
            const parts = res.strainCode.split('-');
            if (parts.length > 1) {
              const seq = parts.pop();
              this.displayStrainSeq = seq.replace(/^0+/, '');
              this.form.strainCode = parts.join('-');
            } else {
              this.form.strainCode = res.strainCode;
            }
          }
        })
      }
    },
    watch: {
        '$route.query.id'() {
@@ -147,6 +215,7 @@
                strainName: '',
                source: '',
                appraisalMethod: '',
              projectId: '',
                characteristics: '',
                storageLocation: '',
                preservationMethod: '',
@@ -155,6 +224,88 @@
        }
    },
    methods: {
      // 处理输入:只允许数字
      handleSeqInput(value) {
        console.log('输入值:', value);
        // 如果是空字符串,清空所有
        if (value === '') {
          this.form.strainSeq = '';
          this.displayStrainSeq = '';
          return;
        }
        // 只保留数字,限制3位
        let numStr = value.replace(/\D/g, '').slice(0, 3);
        if (numStr) {
          const num = parseInt(numStr);
          if (!isNaN(num) && num >= 0 && num <= 999) {
            // 存储时去除前导零
            this.form.strainSeq = String(num);
            // 显示时保留用户输入的内容(不补零,让用户看到自己输入的内容)
            this.displayStrainSeq = numStr;
          }
        } else {
          this.form.strainSeq = '';
          this.displayStrainSeq = '';
        }
        console.log('存储值:', this.form.strainSeq);
        console.log('显示值:', this.displayStrainSeq);
      },
      // 失去焦点时:补零到3位
      formatSeq() {
        console.log('失去焦点, 当前值:', this.form.strainSeq);
        if (this.form.strainSeq) {
          const num = parseInt(this.form.strainSeq);
          if (!isNaN(num) && num >= 0 && num <= 999) {
            // 保持存储值为数字字符串
            this.form.strainSeq = String(num);
            // 显示时补零到3位
            this.displayStrainSeq = String(num).padStart(3, '0');
          }
        }
        console.log('格式化后存储值:', this.form.strainSeq);
        console.log('格式化后显示值:', this.displayStrainSeq);
      },
      // 获取焦点时:显示原始数字(去除前导零,便于编辑)
      handleFocus() {
        console.log('获取焦点, 当前值:', this.form.strainSeq);
        if (this.form.strainSeq) {
          // 显示原始数字,去除前导零
          this.displayStrainSeq = this.form.strainSeq;
        }
      },
      getFullStrainCode() {
        const prefix = this.form.strainCode || '';
        const seq = this.displayStrainSeq || '';
        if (prefix && seq) {
          const paddedSeq = String(seq).padStart(3, '0');
          return `${prefix}-${paddedSeq}`;
        }
        return prefix;
      },
      // 获取项目组列表
      async fetchTeamList() {
        if (this.teamList.length > 0) return // 已有数据不再重复请求
        this.teamLoading = true
        try {
          const res = await myTeamPageList()
          // 根据实际接口返回的数据结构调整
          this.teamList = res.data || res || []
        } catch (error) {
          console.error('获取项目组列表失败:', error)
          this.$message.error('获取项目组列表失败')
        } finally {
          this.teamLoading = false
        }
      },
        handleSubmit(isDraft) {
            this.currentAction = 'submit'
            this.$refs.strainForm.validate((valid) => {
@@ -186,8 +337,10 @@
            })
        },
        async handleSignatureConfirm(signatureImage) {
          const fullStrainCode = this.getFullStrainCode();
            let requestData = {
                strainCode: this.form.strainCode,
              projectId: this.form.projectId,
                strainCode: fullStrainCode,
                strainName: this.form.strainName,
                strainSource: this.form.strainSource,
                appraisalMethod: this.form.appraisalMethod,
@@ -212,8 +365,12 @@
                } else {
                    await add(requestData);
                }
              this.cacheForm = null
              this.displayStrainSeq = null
                this.signatureVisible = false;
                this.$router.push({ path: '/strain-library/production-cell-library' });
                this.$closeCurrentView('/strain-library/production-cell-library');
                this.$message.success('操作成功');
            } catch (error) {
                this.$message.error('操作失败');
@@ -264,12 +421,77 @@
    button {
        width: 180px;
        height: 36px;
        // background: #409EFF;
        // background: #409EFF;
    }
}
.strain-form {
    padding: 0 40px;
  // 菌种编号表单项样式
  .strain-code-form-item {
    // 关键:让表单项占满宽度
    flex: 1;
    min-width: 280px;
    :deep(.el-form-item__content) {
      display: flex;
      align-items: center;
      width: 100%;
      line-height: 36px;
    }
  }
  .strain-code-wrapper {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    flex: 1;
    .strain-code-input {
      flex: 1; // 改为 flex:1 自适应
      min-width: 0; // 防止溢出
      :deep(.el-input__inner) {
        height: 36px;
        line-height: 36px;
        border-radius: 4px;
        width: 100%;
      }
    }
    .strain-code-separator {
      color: #909399;
      font-size: 16px;
      font-weight: bold;
      user-select: none;
      flex-shrink: 0;
      line-height: 36px;
      padding: 0 2px;
    }
    .strain-seq-input {
      width: 90px; // 固定宽度
      flex-shrink: 0; // 不被压缩
      :deep(.el-input__inner) {
        height: 36px;
        line-height: 36px;
        text-align: center;
        padding: 0 5px;
        border-radius: 4px;
        width: 100%;
        &::-webkit-inner-spin-button,
        &::-webkit-outer-spin-button {
          -webkit-appearance: none;
          margin: 0;
        }
        -moz-appearance: textfield;
      }
    }
  }
    .form-row {
        display: flex;
@@ -436,4 +658,4 @@
        margin: 0;
    }
}
</style>
</style>