index.vue 7.51 KB
<template>
  <d2-container v-loading="pageLoading" class="customer-portrait-page">
    <div class="page-content">
      <CustomerBasicInfoCard :basic-info="portraitData.basicInfo"
                             :customer-row="customerRow" />

      <section class="detail-card">
        <h3 class="detail-card__title">
          消费力标签({{ consumptionScore }}分)
        </h3>
        <template v-if="consumptionDimensions.length">
          <div v-for="(dimension, index) in consumptionDimensions"
               :key="`consumption-${index}`"
               class="field-row">
            <span class="field-label">{{ dimension.dimensionName }}:</span>
            <div class="tag-list">
              <template v-if="dimension.tags.length">
                <el-tag v-for="(tag, tagIndex) in dimension.tags"
                        :key="`consumption-tag-${index}-${tagIndex}`"
                        class="tag-item"
                        size="small">
                  {{ tag }}
                </el-tag>
              </template>
              <span v-else class="empty-text">-</span>
            </div>
          </div>
        </template>
        <div v-else class="field-row">
          <span class="empty-text">-</span>
        </div>
      </section>

      <section class="detail-card">
        <h3 class="detail-card__title">
          产品标签({{ productScore }}分)
        </h3>
        <template v-if="productDimensions.length">
          <div v-for="(dimension, index) in productDimensions"
               :key="`product-${index}`"
               class="field-row">
            <span class="field-label">{{ dimension.dimensionName }}:</span>
            <div class="tag-list">
              <template v-if="dimension.tags.length">
                <el-tag v-for="(tag, tagIndex) in dimension.tags"
                        :key="`product-tag-${index}-${tagIndex}`"
                        class="tag-item"
                        size="small">
                  {{ tag }}
                </el-tag>
              </template>
              <span v-else class="empty-text">-</span>
            </div>
          </div>
        </template>
        <div v-else class="field-row">
          <span class="empty-text">-</span>
        </div>
      </section>

      <section class="detail-card">
        <h3 class="detail-card__title">
          行为标签({{ behaviorScore }}分)
        </h3>
        <el-table class="behavior-table"
                  size="small"
                  border
                  :data="behaviorTagList">
          <el-table-column align="center"
                           label="序号"
                           type="index"
                           width="60" />
          <el-table-column align="center"
                           label="标签维度"
                           min-width="200"
                           prop="dimensionName" />
          <el-table-column align="center"
                           label="枚举值"
                           min-width="120">
            <template #default="scope">
              <el-tag v-if="scope.row.enumName"
                      class="tag-item"
                      size="small">
                {{ scope.row.enumName }}
              </el-tag>
              <span v-else>-</span>
            </template>
          </el-table-column>
          <el-table-column align="left"
                           label="依据"
                           min-width="320"
                           prop="basis">
            <template #default="scope">
              <span class="basis-text">{{ scope.row.basis || "-" }}</span>
            </template>
          </el-table-column>
        </el-table>
      </section>

      <div class="footer-actions">
        <el-button size="medium"
                   type="primary"
                   @click="handleBack">
          返回
        </el-button>
      </div>
    </div>
  </d2-container>
</template>

<script>
import { queryCustomerPortrait } from "@api/customerDetail";
import CustomerBasicInfoCard from "../components/CustomerBasicInfoCard.vue";
import {
	loadCustomerSnapshot,
	mapProfileResponse,
	resolveCustomerRowFromRoute,
} from "../utils/customerSnapshot";

export default {
	name: "CustomerPortrait",
	components: { CustomerBasicInfoCard },
	data() {
		return {
			pageLoading: false,
			customerId: "",
			customerRow: {},
			portraitData: {
				basicInfo: {},
				consumptionScore: 0,
				consumptionDimensions: [],
				productScore: 0,
				productDimensions: [],
				behaviorScore: 0,
				behaviorTagList: [],
			},
		};
	},
	computed: {
		consumptionScore() {
			return this.portraitData.consumptionScore || 0;
		},
		productScore() {
			return this.portraitData.productScore || 0;
		},
		behaviorScore() {
			return this.portraitData.behaviorScore || 0;
		},
		consumptionDimensions() {
			return this.portraitData.consumptionDimensions || [];
		},
		productDimensions() {
			return this.portraitData.productDimensions || [];
		},
		behaviorTagList() {
			return this.portraitData.behaviorTagList || [];
		},
	},
	created() {
		this.customerId = this.$route.query.customerId;
		if (!this.customerId) {
			this.$message.warning("缺少客户参数");
			this.$router.back();
			return;
		}
		this.customerRow = resolveCustomerRowFromRoute(
			this.$route,
			loadCustomerSnapshot(),
		);
		this.loadPortrait();
	},
	methods: {
		async loadPortrait() {
			this.pageLoading = true;
			try {
				const res = await queryCustomerPortrait({
					id: this.customerId,
				});
				this.applyPortraitData(
					mapProfileResponse(res || {}, this.customerRow),
				);
			} catch (err) {
				this.$message.error(err.message || "加载客户画像失败");
				this.applyPortraitData(
					mapProfileResponse({}, this.customerRow),
				);
			} finally {
				this.pageLoading = false;
			}
		},
		applyPortraitData(res) {
			this.portraitData = {
				basicInfo: res.basicInfo || {},
				consumptionScore: res.consumptionScore || 0,
				consumptionDimensions: res.consumptionDimensions || [],
				productScore: res.productScore || 0,
				productDimensions: res.productDimensions || [],
				behaviorScore: res.behaviorScore || 0,
				behaviorTagList: res.behaviorTagList || [],
			};
		},
		handleBack() {
			this.$router.push({ name: "customerList" });
		},
	},
};
</script>

<style lang="scss" scoped>
.customer-portrait-page {
	::v-deep .d2-container-full__body {
		padding: 0 !important;
		background: #f0f2f5;
	}

	.page-content {
		width: 100%;
		box-sizing: border-box;
		padding: 12px 20px 32px;
	}

	.detail-card {
		background: #fff;
		border: 1px solid #ebeef5;
		border-radius: 4px;
		padding: 16px 20px;
		margin-bottom: 12px;

		&__title {
			margin: 0 0 16px;
			padding-left: 10px;
			border-left: 3px solid #409eff;
			font-size: 14px;
			font-weight: 600;
			color: #303133;
			line-height: 20px;
		}
	}

	.field-row {
		display: flex;
		align-items: flex-start;
		margin-bottom: 12px;
		font-size: 14px;
		line-height: 22px;

		&:last-child {
			margin-bottom: 0;
		}
	}

	.field-label {
		flex-shrink: 0;
		min-width: 90px;
		color: #606266;
	}

	.tag-list {
		display: flex;
		flex-wrap: wrap;
		gap: 8px;
		flex: 1;
	}

	.tag-item {
		background: #ecf5ff;
		border-color: #d9ecff;
		color: #409eff;

		&--plain {
			background: #f4f4f5;
			border-color: #e9e9eb;
			color: #909399;
		}
	}

	.empty-text {
		color: #909399;
	}

	.behavior-table {
		width: 100%;
	}

	.basis-text {
		display: inline-block;
		word-break: break-all;
		white-space: pre-wrap;
		line-height: 20px;
		color: #606266;
	}

	.footer-actions {
		margin-top: 8px;
		padding: 16px 0 8px;
		text-align: center;
	}
}
</style>