selectGoodsDialog.vue 11.2 KB
<template>
    <d2-dialog :append-to-body="appendToBody"
               btn-ok-label="确定"
               class="selectGoods"
               :is-visible.sync="selectGoodsVisible"
               :title="title"
               width="70%"
               @closeDialog="handleSelectGoodsClose"
               @confirmDialog="handleSelectGoodsConfirm">
        <d2-search ref="showSearch"
                   class="show-search"
                   :form-data="showFormData"
                   label-width="100px"
                   :search-form="searchForm">
            <template #commodityCode>
                <el-input v-model.trim="searchForm.commodityCode"
                          :clearable="true"
                          :maxlength="50"
                          placeholder="请输入商品ID"
                          :style="{ width: '100%' }"
                          @input="event => searchInput(event, 'commodityCode')" />
            </template>
            <template #type>
                <!-- <el-input v-model.trim="searchForm.type"
                          :clearable="true"
                          :maxlength="50"
                          placeholder="请输入商品编码"
                          :style="{ width: '100%' }"
                          @input="event => searchInput(event, 'type')" /> -->
                <el-input v-model.trim="searchForm.type"
                          :style="{ width: '100%' }"
                          placeholder="请输入商品编码"
                          :clearable="true"
                          :maxlength="50" />
            </template>
            <template #thirdPartyCode>
                <el-input v-model.trim="searchForm.thirdPartyCode"
                          :clearable="true"
                          :maxlength="50"
                          placeholder="请输入第三方编码"
                          :style="{ width: '100%' }"
                          @input="event => searchInput(event, 'thirdPartyCode')" />
            </template>
            <template #baseGoodsCommodityCode>
                <el-input v-model.trim="searchForm.baseGoodsCommodityCode"
                          :clearable="true"
                          :maxlength="50"
                          placeholder="请输入系列ID"
                          :style="{ width: '100%' }"
                          @input="event => searchInput(event, 'baseGoodsCommodityCode')" />
            </template>
            <template #goodsCascader>
                <el-cascader v-model="tempGoodsType"
                             clearable
                             filterable
                             :options="baseData.productTypeList"
                             placeholder="请选择"
                             :props="{ checkStrictly: true, label: 'categoryName', value: 'categoryType' }"
                             style="width: 100%;"
                             @change="selectAddAttr($event)">
                </el-cascader>
            </template>
        </d2-search>
        <div class="up-btns">
            <el-button :id="tableParams.btnId"
                       size="small"
                       type="primary">
                查询
            </el-button>
        </div>
        <d2-table ref="groundingIndexTable"
                  :before-search-fn="beforeSearchFn"
                  :get-list-api="getListApi"
                  :has-num="false"
                  :search-form="searchForm"
                  :table-params="tableParams"
                  @setTableFinishFun="setTableFinishFun">
            <template #select_btn="scope">
                <el-radio v-model="selectedRowId"
                          :disabled="packageSelectIds && packageSelectIds.includes(scope.scope.id)"
                          :label="scope.scope.id">
                    {{ '' }}
                </el-radio>
            </template>
            <template #carName="scope">
                <el-tooltip v-if="scope.scope.carTypeConfigList && scope.scope.carTypeConfigList.length" placement="top">
                    <template #content>
                        <div class="car-type-list">
                            <div v-for="(item, index) in scope.scope.carTypeConfigList" :key="index">
                                {{ item.seriesName }}
                            </div>
                        </div>
                    </template>
                    <div>{{ scope.scope.carTypeConfigList | formatCar }}</div>
                </el-tooltip>
                <div v-else>
                    -
                </div>
            </template>
        </d2-table>
    </d2-dialog>
</template>

<script>
import { getAllCategory } from "@api/product";
import { getUpgradeProductList } from "@api/productManage";
import { filterSpecsStr } from "../filterOption";

export default {
	name: "SelectGoodsDialog",
	filters: {
		formatCar(list) {
			const str = list.map((item) => {
				return item.seriesName;
			}).join(",");
			return str.length > 15 ? str.substring(0, 15) + "..." : str;
		},
	},
	props: {
		title: {
			type: String,
			default: "选择商品",
		},
		appendToBody: {
			type: Boolean,
			default: false,
		},
		selectGoodsVisible: {
			type: Boolean,
			default: false,
		},
		upgradeGoodsId: {},
		packageSelectIds: {},
		// 嘀加上架所需 -  业务场景
		// 0 为之前的 逻辑默认值
		sceneType: {
			type: Number,
			default: 0,
		},
		// 用于嘀加上架获取商品时过滤只筛选出权益和延保类
		filterParams: {
			type: Array,
			default: () => [],
		},
	},
	data() {
		return {
			getListApi: getUpgradeProductList,
			showFormData: [{
				label: "商品名称",
				name: "upgradeGoodsName",
				type: "input",
				maxlength: 50,
				isClear: true,
				keyFn: this.keyDowmGetList,
			}, {
				label: "商品ID",
				name: "commodityCode",
				addHtml: "commodityCode",
			}, {
				label: "规格",
				name: "specificationData",
				type: "input",
				maxlength: 50,
				isClear: true,
				keyFn: this.keyDowmGetList,
			}, {
				label: "商品编码",
				name: "type",
				addHtml: "type",
			}, {
				label: "第三方编码",
				name: "thirdPartyCode",
				addHtml: "thirdPartyCode",
			}, {
				label: "适用车型",
				name: "carTypeSearch",
				type: "input",
				maxlength: 50,
				isClear: true,
				keyFn: this.keyDowmGetList,
			}, {
				label: "所属系列",
				name: "goodsName",
				type: "input",
				maxlength: 50,
				isClear: true,
				keyFn: this.keyDowmGetList,
			}, {
				label: "系列ID",
				name: "baseGoodsCommodityCode",
				addHtml: "baseGoodsCommodityCode",
			}, {
				label: "所属分类",
				name: "goodsType",
				addHtml: "goodsCascader",
			}],
			searchForm: {
				upgradeGoodsName: "",
				commodityCode: "",
				specificationData: "",
				type: "",
				thirdPartyCode: "",
				carTypeSearch: "",
				goodsName: "",
				baseGoodsCommodityCode: "",
			},
			tempGoodsType: [],
			baseData: {
				productTypeList: [],
				productList: [],
			},
			tableParams: {
				btnId: new Date().getTime() + Number.parseInt(Math.random() * 1000),
				listName: "list",
				tableColumns: [{
					label: "",
					width: "60",
					addHtml: "select_btn",
				}, {
					label: "序号",
					prop: "ownSerialNum",
					type: "ownSerialNum",
					width: "60",
				}, {
					label: "商品名称",
					prop: "upgradeGoodsName",
					width: 150,
					formatter: (cellValue) => {
						return cellValue.upgradeGoodsName || "-";
					},
				}, {
					label: "商品ID",
					prop: "commodityCode",
					width: 180,
					formatter: (cellValue) => {
						return cellValue.commodityCode || "-";
					},
				}, {
					label: "规格",
					prop: "specificationData",
					formatter: (cellValue) => {
						return this.filterSpecsStr(cellValue);
					},
				}, {
					label: "商品编码",
					prop: "type",
					formatter: (cellValue) => {
						return cellValue.type || "-";
					},
				}, {
					label: "第三方编码",
					prop: "thirdPartyCode",
					width: 100,
					formatter: (cellValue) => {
						return cellValue?.thirdPartyCode || "-";
					},
				}, {
					label: "适用车型",
					prop: "",
					addHtml: "carName",
					width: 200,
					// formatter: (cellValue) => {
					//   return cellValue?.carTypeConfigList?.length ? cellValue.carTypeConfigList.map(item => item.seriesName).join('/') : '-'
					// }
				}, {
					label: "所属系列",
					prop: "goodsName",
					formatter: (cellValue) => {
						return cellValue?.goodsInfo?.goodsName || "-";
					},
					width: 140,
				}, {
					label: "系列ID",
					prop: "baseGoodsCommodityCode",
					width: 170,
				}, {
					label: "所属分类",
					prop: "goodsTypeName",
					formatter: (cellValue) => {
						const { goodsTypeName, secondaryGoodsTypeName, thirdLevelGoodsTypeName } = cellValue.goodsInfo || {};
						const types = [goodsTypeName, secondaryGoodsTypeName, thirdLevelGoodsTypeName];
						const result = types.filter(type => type).join("-");
						return result;
					},
					width: 170,
				}],
			},
			selectedRowId: "",
			filterSpecsStr, // 处理规格单位
		};
	},
	mounted() {
		this.selectedRowId = this.upgradeGoodsId;
		// 获取全部类目 屏蔽服务商自建类目
		getAllCategory({ categoryTypeList: this.selfBuildGoodsType, pageSize: 9999, currentPage: 1 }).then((res) => {
			this.baseData.productTypeList = res?.list || [];
		});
	},
	methods: {
		searchInput(e, key) {
			if (!/^[a-z0-9]*$/i.test(e)) {
				this.searchForm[key] = e.replace(/[^a-z0-9]/gi, "");
			}
		},
		getOptionsList() {
			this.showFormData.forEach((item, index) => {
				if (item.type === "select") {
					if (this.baseData[item.optionsName].length && this.baseData[item.optionsName][0][this.showFormData[index].optionLabel] !== "全部") {
						const temp = {};
						temp[this.showFormData[index].optionId] = "";
						temp[this.showFormData[index].optionLabel] = "全部";
						this.baseData[item.optionsName].unshift(temp);
					}
					this.$set(this.showFormData[index], "optionList", this.baseData[item.optionsName]);
				}
			});
		},
		setTableFinishFun(data) {
			this.tableData = data;
		},
		selectAddAttr(value) {
			this.searchForm.goodsType = value?.[0];
			this.searchForm.secondaryGoodsType = value?.[1];
			this.searchForm.thirdLevelGoodsType = value?.[2];
		},
		beforeSearchFn() {
			return new Promise((resolve) => {
				// 嘀加上架获取商品时 过滤 只 筛选出权益和延保类
				if (this.filterParams.length > 0) {
					this.searchForm[this.sceneType === 1 ? "excludeGoodsTypeList" : "goodsTypeList"] = this.filterParams;
				}
				resolve();
			});
		},
		handleSelectGoodsConfirm() {
			const selectData = this.tableData?.find(v => v.id == this.selectedRowId);
			if (!selectData)
				return this.$message.warning("请选择商品");
			this.$emit("handleSelectGoodsConfirm", selectData);
		},
		handleSelectGoodsClose() {
			this.$emit("handleSelectGoodsClose");
		},
		handleReset() {
			this.searchForm = {
				upgradeGoodsName: "",
				commodityCode: "",
				specificationData: "",
				type: "",
				thirdPartyCode: "",
				carTypeSearch: "",
				goodsName: "",
				baseGoodsCommodityCode: "",
			};
			this.tempGoodsType = [];
			// 重新查询
			this.$refs.groundingIndexTable.refreshTable(true, true);
		},
	},
};
</script>

<style lang="scss" scoped>
.car-type-list {
	max-height: 300px;
	overflow-y: scroll;
}

.show-search {
	::v-deep .d2-search-block {
		padding: 0;
	}
}
</style>