重新设计车间选择逻辑,使其能够根据产品类型自动设定;优化车间展示界面
已修改1个文件
222 ■■■■■ 文件已修改
pages/wareHouse/nuclearScale/createwriteoffform.vue 222 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pages/wareHouse/nuclearScale/createwriteoffform.vue
@@ -188,34 +188,20 @@
        label="生产车间:"
        :border-bottom="false"
        prop="workshop"
        style="font-size: 16px; height: 40px"
        class="workshop-form-item"
        style="font-size: 16px"
      >
        <picker
          class="item-one item-two"
          style="width: 100%; text-align: right; font-size: 16px"
          @change="workshopChange($event, workshopList)"
          :value="workshopIndex"
          :range="workshopList"
          range-key="label"
          :disabled="!form.productType"
        >
          <view>
            <text
              :style="{ color: workshopIndex == null ? '#a5abb4' : '#0c0c0c' }"
              >{{
                workshopIndex == null
                  ? "请选择"
                  : workshopList[workshopIndex].label
              }}</text
            >
          </view>
        </picker>
        <u-icon
          v-if="workshopIndex == null"
          name="arrow-right"
          color="#687792"
          size="28"
        ></u-icon>
        <view class="workshop-field-body">
          <text v-if="!form.productType" class="workshop-field-text is-placeholder"
            >请先选择产品类型</text
          >
          <text
            v-else
            class="workshop-field-text"
            :class="{ 'is-placeholder': !workshopDisplayText }"
            >{{ workshopDisplayText || "正在匹配车间..." }}</text
          >
        </view>
      </u-form-item>
      <u-form-item
        label="司磅员:"
@@ -398,9 +384,8 @@
      selClientList: [],
      selClientIndex1: null,
      selClientIndex2: null,
      // 生产车间
      // 生产车间(由产品类型自动带出,不可改)
      workshopList: [],
      workshopIndex: null,
      // 司磅员
      weighmanList: [],
      weighmanIndex: null,
@@ -410,6 +395,33 @@
      id2: "",
      isSubmitting: false,
    };
  },
  computed: {
    workshopDisplayText() {
      const raw = this.form.workshop;
      if (!raw) return "";
      const values = String(raw)
        .split(",")
        .map((s) => s.trim())
        .filter(Boolean);
      const labels = values.map((v) => {
        const w = this.workshopList.find(
          (x) => String(x.value) === String(v)
        );
        return w ? w.label : v;
      });
      return labels.join("、");
    },
  },
  watch: {
    workshopList: {
      handler(list) {
        if (list && list.length && this.form.productType) {
          this.$nextTick(() => this.syncWorkshopFromProductType());
        }
      },
      deep: true,
    },
  },
  onReady() {
    this.$refs.uForm.setRules(this.rules);
@@ -443,7 +455,6 @@
    };
    this.selClientIndex1 = null;
    this.selClientIndex2 = null;
    this.workshopIndex = null;
    this.weighmanIndex = null;
    this.checkboxList = this.checkboxList.map((item) => ({
      ...item,
@@ -505,6 +516,7 @@
              });
              this.workshopList.push(obj);
            });
            this.$nextTick(() => this.syncWorkshopFromProductType());
          } else {
            uni.showToast({
              title: res.msg || "获取车间列表失败",
@@ -700,48 +712,69 @@
        this.id1 = selectedCustomer.id;
      }
    },
    // 生产车间
    workshopChange(e, storage) {
      if (storage.length <= 0) {
        this.workshopIndex = null;
    /**
     * 按产品类型自动设置生产车间(单选/多选均由类型决定,用户不可改)
     */
    syncWorkshopFromProductType() {
      const pt = this.form.productType;
      const list = this.workshopList;
      if (!pt) {
        this.form.workshop = "";
        return;
      }
      const productType = this.form.productType;
      const selectedWorkshop = storage[e.target.value];
      console.log("选择的车间:", selectedWorkshop);
      // 检查产品类型是否为铜杆(值为TG01)
      if (productType === "TG01") {
        // 铜杆只能选择铜杆相关车间
        if (!selectedWorkshop.label.includes("铜杆")) {
          uni.showModal({
            title: "提示",
            content: "铜杆产品只能选择铜杆相关车间",
            showCancel: false,
          });
          return;
        }
      } else {
        // 导体产品不能选择铜杆车间
        if (selectedWorkshop.label.includes("铜杆")) {
          uni.showModal({
            title: "提示",
            content: "导体产品不能选择铜杆车间",
            showCancel: false,
          });
          return;
        }
      if (!list.length) {
        this.form.workshop = "";
        return;
      }
      if (selectedWorkshop.label.includes("直发") && productType !== "DT02") {
        uni.showModal({
          title: "提示",
          content: "直发产品只能选择导体2.6车间",
          showCancel: false,
      if (pt === "TG01") {
        const w =
          list.find((x) => x.label === "铜杆") ||
          list.find((x) => x.label.includes("铜杆"));
        if (w) {
          this.form.workshop = w.value;
        } else {
          this.form.workshop = "";
          uni.showToast({ title: "字典中未找到铜杆车间", icon: "none" });
        }
        return;
      }
      if (pt === "DT01") {
        this.setWorkshopByLabels(["导体车间", "导体车间2"]);
        return;
      }
      if (pt === "DT02") {
        this.setWorkshopByLabels(["导体车间", "导体车间2", "直发"]);
      }
    },
    setWorkshopByLabels(requiredLabels) {
      const list = this.workshopList;
      const picked = [];
      const missing = [];
      requiredLabels.forEach((lbl) => {
        let w = list.find((x) => x.label === lbl);
        if (!w && lbl === "直发") {
          w = list.find((x) => x.label.includes("直发"));
        }
        if (w) picked.push(w);
        else missing.push(lbl);
      });
      if (missing.length) {
        this.form.workshop = "";
        uni.showToast({
          title: `车间字典缺少:${missing.join("、")}`,
          icon: "none",
        });
        return;
      }
      this.workshopIndex = e.target.value;
      this.form.workshop = selectedWorkshop.value;
      const uniq = [];
      const seen = new Set();
      picked.forEach((w) => {
        if (!seen.has(w.value)) {
          seen.add(w.value);
          uniq.push(w);
        }
      });
      this.form.workshop = uniq.map((x) => x.value).join(",");
    },
    // 司磅员
    weighmanChange(e, storage) {
@@ -759,11 +792,10 @@
    confirm() {},
    /**
     * 产品类型变化时清空车间选择
     * 产品类型变化时按规则自动带出生产车间
     */
    handleProductTypeChange() {
      this.workshopIndex = null;
      this.form.workshop = "";
      this.syncWorkshopFromProductType();
    },
    // 提交按钮
    async submit() {
@@ -945,4 +977,56 @@
  // margin-right: 5px;
  transform: scale(1.2);
}
/* 生产车间多行展示,避免固定高度挤压覆盖下一行 */
::v-deep .workshop-form-item.u-form-item {
  height: auto !important;
  min-height: 40px;
  line-height: 1.45 !important;
  align-items: stretch;
}
::v-deep .workshop-form-item .u-form-item__body {
  align-items: flex-start;
}
::v-deep .workshop-form-item .u-form-item--left {
  align-items: flex-start;
  padding-top: 4px;
}
::v-deep .workshop-form-item .u-form-item--left__content,
::v-deep .workshop-form-item .u-form-item--left__content__label {
  align-items: flex-start;
}
::v-deep .workshop-form-item .u-form-item--right__content {
  align-items: flex-start;
}
::v-deep .workshop-form-item .u-form-item--right__content__slot {
  align-items: flex-start !important;
}
.workshop-field-body {
  width: 100%;
  box-sizing: border-box;
  padding: 4px 0 6px;
  text-align: right;
}
.workshop-field-text {
  display: block;
  width: 100%;
  font-size: 16px;
  line-height: 1.45;
  color: #0c0c0c;
  text-align: right;
  word-break: break-word;
  white-space: normal;
}
.workshop-field-text.is-placeholder {
  color: #a5abb4;
}
</style>