puzhibing
6 天以前 0db8c9b94a9ac655549d5eba5c2a6c92ac386c06
culture/src/views/strain-library/main-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="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">
                    <el-input v-model="form.strainCode" placeholder="请输入"></el-input>
<!--                    <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>
@@ -14,8 +49,8 @@
            </div>
            <div class="form-row">
                <el-form-item label="鉴定方法" prop="appraisalMethod">
                    <el-input v-model="form.appraisalMethod" placeholder="请输入"></el-input>
                <el-form-item label="鉴定方法" prop="appraisalMethod" class="full-width">
                    <el-input type="textarea" :rows="4" v-model="form.appraisalMethod" placeholder="请输入"></el-input>
                </el-form-item>
            </div>
@@ -78,149 +113,270 @@
<script>
import SignatureCanvas from '@/components/SignatureCanvas.vue'
import { add, edit, getDetail, addBatch } from './service'
export default {
    name: 'StrainLibraryManageAdd',
    components: {
        SignatureCanvas
    },
    data() {
        return {
            batchAddDialogVisible: false,
            signatureVisible: false,
            currentAction: '', // 'submit' or 'batchAdd'
            batchForm: {
                count: ''
            },
            form: {
                strainCode: '',
                strainName: '',
                source: '',
                appraisalMethod: '',
                characteristics: '',
                storageLocation: '',
                preservationMethod: '',
                remark: ''
            },
            rules: {
                strainCode: [{
                    validator: (rule, value, callback) => {
                        if (this.currentAction === 'submit' && !value) {
                            callback(new Error('请输入菌种编号'));
                        } else {
                            callback();
                        }
                    },
                    trigger: 'change'
                }],
                strainName: [{ required: true, message: '请输入菌种名称', trigger: 'blur' }],
                strainSource: [{ required: true, message: '请输入菌种来源', trigger: 'blur' }],
                appraisalMethod: [{ required: true, message: '请输入鉴定方法', trigger: 'blur' }],
                features: [{ required: true, message: '请输入特征描述', trigger: 'blur' }],
                saveLocation: [{ required: true, message: '请输入保存位置', trigger: 'blur' }],
                saveMethod: [{ required: true, message: '请输入菌种保存方法', trigger: 'blur' }]
            }
        }
    },
    mounted() {
        this.form = {
            strainCode: '',
            strainName: '',
            source: '',
            appraisalMethod: '',
            characteristics: '',
            storageLocation: '',
            preservationMethod: '',
            remark: ''
        }
        if (this.$route.query.id) {
            getDetail({ id: this.$route.query.id }).then(res => {
                this.form = res
            })
        }
    },
    watch: {
        '$route.query.id'() {
            this.form = {
                strainCode: '',
                strainName: '',
                source: '',
                appraisalMethod: '',
                characteristics: '',
                storageLocation: '',
                preservationMethod: '',
                remark: ''
            }
        }
    },
    methods: {
        handleSubmit(isDraft) {
            this.currentAction = 'submit'
            this.$refs.strainForm.validate((valid) => {
                if (valid) {
                    this.form.isDraft = isDraft
                    if (isDraft == 1) {
                        //存草稿
                        this.handleSignatureConfirm('')
                    } else {
                        this.signatureVisible = true
                    }
                }
            })
        },
        handleBatchAdd() {
            this.currentAction = 'batchAdd'
            this.$refs.strainForm.validate((valid) => {
                if (valid) {
                    this.batchAddDialogVisible = true
                }
            })
        },
        handleConfirmBatchAdd() {
            this.$refs.batchFormRef.validate((valid) => {
                if (valid) {
                    this.batchAddDialogVisible = false
                    this.signatureVisible = true
                }
            })
        },
        async handleSignatureConfirm(signatureImage) {
            let requestData = {
                strainCode: this.form.strainCode,
                strainName: this.form.strainName,
                strainSource: this.form.strainSource,
                appraisalMethod: this.form.appraisalMethod,
                features: this.form.features,
                saveLocation: this.form.saveLocation,
                saveMethod: this.form.saveMethod,
                remark: this.form.remark,
                signature: signatureImage,
                type: 2,
            };
            if (this.currentAction === 'batchAdd') {
                requestData.batchCount = this.batchForm.count;
  name: 'MainCellLibraryAdd',
  components: {
    SignatureCanvas
  },
  data() {
    return {
      teamList: [], // 项目组列表
      teamLoading: false, // 加载状态
      cacheForm: null,
      batchAddDialogVisible: false,
      signatureVisible: false,
      currentAction: '', // 'submit' or 'batchAdd'
      batchForm: {
        count: ''
      },
      displayStrainSeq: '', // 显示用的序号(可能包含前导零)
      form: {
        strainCode: '',
        strainName: '',
        source: '',
        appraisalMethod: '',
        projectId: '',
        characteristics: '',
        storageLocation: '',
        preservationMethod: '',
        remark: ''
      },
      rules: {
        projectId: [{ required: true, message: '请选择所属项目组', trigger: 'change' }],
        strainCode: [{required: true,
          validator: (rule, value, callback) => {
            if (this.currentAction === 'submit' && !value) {
              callback(new Error('请输入菌种编号'));
            } else {
                requestData.isDraft = this.form.isDraft
              callback();
            }
            try {
                if (this.$route.query.id) {
                    requestData.id = this.$route.query.id;
                    await edit(requestData);
                } else if (this.currentAction === 'batchAdd') {
                    await addBatch(requestData);
                } else {
                    await add(requestData);
                }
                this.signatureVisible = false;
                this.$router.push({ path: '/strain-library/main-cell-library' });
                this.$message.success('操作成功');
            } catch (error) {
                this.$message.error('操作失败');
            }
        }
          },
          trigger: 'change'
        }],
        strainName: [{ required: true, message: '请输入菌种名称', trigger: 'blur' }],
        strainSource: [{ required: true, message: '请输入菌种来源', trigger: 'blur' }],
        appraisalMethod: [{ required: true, message: '请输入鉴定方法', trigger: 'blur' }],
        features: [{ required: true, message: '请输入特征描述', trigger: 'blur' }],
        saveLocation: [{ required: true, message: '请输入保存位置', trigger: 'blur' }],
        saveMethod: [{ required: true, message: '请输入菌种保存方法', trigger: 'blur' }]
      }
    }
  },
  async activated() {
    await this.fetchTeamList()
    this.resetForm()
    if (this.cacheForm) {
      this.form = this.cacheForm;
      if (this.cacheForm.displayStrainSeq) {
        this.displayStrainSeq = this.cacheForm.displayStrainSeq;
      }
    }
    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;
          }
        }
      })
    }
  },
  deactivated() {
    this.cacheForm = JSON.parse(JSON.stringify(this.form));
    this.cacheForm.displayStrainSeq = this.displayStrainSeq
  },
  watch: {
    '$route.query.id'() {
      this.form = {
        strainCode: '',
        strainName: '',
        source: '',
        appraisalMethod: '',
        characteristics: '',
        storageLocation: '',
        preservationMethod: '',
        remark: ''
      }
    }
  },
  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
      }
    },
    resetForm() {
      this.form = {
        strainCode: '',
        strainName: '',
        source: '',
        appraisalMethod: '',
        characteristics: '',
        projectId: '',
        storageLocation: '',
        preservationMethod: '',
        remark: ''
      };
    },
    handleSubmit(isDraft) {
      this.currentAction = 'submit'
      this.$refs.strainForm.validate((valid) => {
        if (valid) {
          this.form.isDraft = isDraft
          if (isDraft == 1) {
            //存草稿
            this.handleSignatureConfirm('')
          } else {
            this.signatureVisible = true
          }
        }
      })
    },
    handleBatchAdd() {
      this.currentAction = 'batchAdd'
      this.$refs.strainForm.validate((valid) => {
        if (valid) {
          this.batchAddDialogVisible = true
        }
      })
    },
    handleConfirmBatchAdd() {
      this.$refs.batchFormRef.validate((valid) => {
        if (valid) {
          this.batchAddDialogVisible = false
          this.signatureVisible = true
        }
      })
    },
    async handleSignatureConfirm(signatureImage) {
      const fullStrainCode = this.getFullStrainCode();
      let requestData = {
        projectId: this.form.projectId,
        strainCode: fullStrainCode,
        strainName: this.form.strainName,
        strainSource: this.form.strainSource,
        appraisalMethod: this.form.appraisalMethod,
        features: this.form.features,
        saveLocation: this.form.saveLocation,
        saveMethod: this.form.saveMethod,
        remark: this.form.remark,
        signature: signatureImage,
        type: 2,
      };
      if (this.currentAction === 'batchAdd') {
        requestData.batchCount = this.batchForm.count;
      } else {
        requestData.isDraft = this.form.isDraft
      }
      try {
        if (this.$route.query.id) {
          requestData.id = this.$route.query.id;
          await edit(requestData);
        } else if (this.currentAction === 'batchAdd') {
          await addBatch(requestData);
        } else {
          await add(requestData);
        }
        this.cacheForm = null
        this.displayStrainSeq = null
        this.signatureVisible = false;
        this.$closeCurrentView('/strain-library/main-cell-library');
        this.$message.success('操作成功');
      } catch (error) {
        this.$message.error('操作失败');
      }
    }
  }
}
import { add, edit, getDetail, addBatch } from './service'
import {myTeamPageList} from "@/components/chooseProject/servce";
</script>
<style scoped lang="less">
@@ -264,12 +420,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 +657,4 @@
        margin: 0;
    }
}
</style>
</style>