All files / src/views BenefitCenterView.vue

78.37% Statements 29/37
75% Branches 48/64
70% Functions 14/20
81.81% Lines 27/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323                                          2x 2x 2x             3x               3x               6x 2x         4x 2x             2x 2x                           6x                     2x 2x 2x                   2x   2x     4x   1x                             1x   1x                 2x                                     3x         1x   1x                 3x     1x                                                                                                                                                                                                                                                                                                                  
<script setup lang="ts">
import { computed, watch } from "vue";
import { RouterLink } from "vue-router";
import {
  PjButton,
  PjPageContainer,
  PjStatusNotice,
} from "@plain-journal/ui";
 
import {
  formatMoney,
  type Benefit,
} from "@plain-journal/foundation";
 
import {
  BenefitAccessChangedError,
  type BenefitAccessContext,
  useBenefitsStore,
} from "../entities/benefit";
import { useSessionStore } from "../features/customer-session";
 
const store = useBenefitsStore();
const session = useSessionStore();
const accessContext = computed<BenefitAccessContext>(() => ({
  authenticated: session.authenticated,
  ownerId: session.profile?.id ?? null,
  accessToken: session.accessToken,
}));
 
function typeLabel(value: string): string {
  return {
    COUPON: "优惠券",
    RED_PACKET: "红包",
    SUBSIDY: "平台补贴",
  }[value] ?? value;
}
 
function statusLabel(value: string): string {
  return {
    AVAILABLE: "可用",
    LOCKED: "订单已锁定",
    REDEEMED: "已使用",
  }[value] ?? value;
}
 
function statusPresentation(benefit: Benefit) {
  if (benefit.status === "AVAILABLE") {
    return {
      tone: "success" as const,
      description: "满足门槛与地区条件时,可在结算中选择。",
    };
  }
  if (benefit.status === "LOCKED") {
    return {
      tone: "processing" as const,
      description: benefit.lockedOrderNo
        ? `已为订单 ${benefit.lockedOrderNo} 保留,等待订单结果。`
        : "已为一笔订单保留,等待订单结果。",
    };
  }
  Eif (benefit.status === "REDEEMED") {
    return {
      tone: "neutral" as const,
      description: benefit.redeemedOrderNo
        ? `已由订单 ${benefit.redeemedOrderNo} 使用。`
        : "该权益已经使用,不能再次用于结算。",
    };
  }
  return {
    tone: "attention" as const,
    description: "这是服务端返回的未识别状态,请以订单与结算事实为准。",
  };
}
 
function formatTimestamp(value: string): string {
  return new Date(value).toLocaleString("zh-CN");
}
 
function regionLabel(level: string): string {
  return {
    PROVINCE: "省级",
    CITY: "市级",
    DISTRICT: "区县",
  }[level] ?? level;
}
 
watch(accessContext, async (context) => {
  try {
    await store.load(context);
  } catch (cause) {
    if (!(cause instanceof BenefitAccessChangedError)) {
      // The benefit entity keeps the current owner's read error.
    }
  }
}, { immediate: true });
</script>
 
<template>
  <PjPageContainer as="section" size="wide" class="benefit-center-page">
    <nav class="benefit-path" aria-label="当前位置">
      <RouterLink to="/account">账户</RouterLink>
      <span aria-hidden="true">/</span>
      <span>优惠权益</span>
    </nav>
 
    <header class="benefit-intro">
      <div>
        <p class="benefit-context">账户权益</p>
        <h1>优惠权益</h1>
      </div>
      <p>{{ store.availableCount }} 份当前可用</p>
    </header>
 
    <PjStatusNotice
      v-if="store.error"
      class="benefit-error"
      tone="danger"
      title="权益事实未能读取"
      assertive
    >
      <p>{{ store.error }}</p>
      <template #actions>
        <PjButton variant="text" @click="store.load(accessContext)">重新查询</PjButton>
      </template>
    </PjStatusNotice>
 
    <PjStatusNotice
      v-if="store.loading && store.benefits.length === 0"
      tone="processing"
      title="正在读取当前账户的权益"
    >
      <p>只展示当前登录账户由服务端返回的权益事实。</p>
    </PjStatusNotice>
 
    <PjStatusNotice
      v-else-if="!store.error && store.benefits.length === 0"
      tone="neutral"
      title="当前账户没有可展示的优惠权益"
    >
      <p>页面不会自行生成优惠;可继续浏览商品,等待真实权益发放。</p>
      <template #actions>
        <RouterLink to="/products">继续浏览商品 →</RouterLink>
      </template>
    </PjStatusNotice>
 
    <section
      v-else-if="store.benefits.length > 0"
      class="benefit-list"
      aria-label="当前账户优惠权益"
    >
      <article
        v-for="benefit in store.benefits"
        :key="benefit.benefitNo"
        class="benefit-row"
      >
        <header class="benefit-row__summary">
          <div>
            <p class="benefit-context">{{ typeLabel(benefit.benefitType) }}</p>
            <h2>{{ formatMoney(benefit.discountAmount) }}</h2>
            <p>满 {{ formatMoney(benefit.thresholdAmount) }} 可用</p>
          </div>
          <PjStatusNotice
            class="benefit-row__status"
            :tone="statusPresentation(benefit).tone"
            :title="statusLabel(benefit.status)"
          >
            <p>{{ statusPresentation(benefit).description }}</p>
          </PjStatusNotice>
        </header>
        <dl class="benefit-row__facts">
          <div>
            <dt>权益编号</dt>
            <dd>{{ benefit.benefitNo }}</dd>
          </div>
          <div>
            <dt>规则</dt>
            <dd>{{ benefit.ruleCode }}</dd>
          </div>
          <div>
            <dt>有效期</dt>
            <dd>{{ formatTimestamp(benefit.validFrom) }} 至 {{ formatTimestamp(benefit.validUntil) }}</dd>
          </div>
          <div v-if="benefit.lockedOrderNo">
            <dt>锁定订单</dt>
            <dd>{{ benefit.lockedOrderNo }}</dd>
          </div>
          <div v-if="benefit.redeemedOrderNo">
            <dt>核销订单</dt>
            <dd>{{ benefit.redeemedOrderNo }}</dd>
          </div>
        </dl>
        <p v-if="benefit.regions.length" class="benefit-row__regions">
          地区限制:{{
            benefit.regions
              .map((region) => `${regionLabel(region.level)} ${region.regionCode}`)
              .join("、")
          }}
        </p>
      </article>
    </section>
  </PjPageContainer>
</template>
 
<style scoped>
.benefit-center-page {
  padding-block: var(--pj-space-7) var(--pj-space-8);
}
 
.benefit-path {
  display: flex;
  gap: var(--pj-space-2);
  margin-bottom: var(--pj-space-7);
  color: var(--pj-text-secondary);
  font-size: var(--pj-font-size-sm);
}
 
.benefit-intro {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: var(--pj-space-5);
  margin-bottom: var(--pj-space-6);
}
 
.benefit-intro h1 {
  margin: 0;
  font-size: var(--pj-font-size-xl);
  font-weight: 520;
  letter-spacing: -0.04em;
  line-height: var(--pj-line-height-tight);
}
 
.benefit-intro > p,
.benefit-context,
.benefit-row__summary > div > p:last-child,
.benefit-row__facts dt,
.benefit-row__regions {
  color: var(--pj-text-secondary);
}
 
.benefit-context {
  margin: 0 0 var(--pj-space-3);
  font-size: var(--pj-font-size-xs);
  font-weight: 650;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
 
.benefit-error {
  margin-bottom: var(--pj-space-5);
}
 
.benefit-list {
  display: grid;
}
 
.benefit-row {
  padding-block: var(--pj-space-6);
  border-top: 1px solid var(--pj-border-subtle);
}
 
.benefit-row:last-child {
  border-bottom: 1px solid var(--pj-border-subtle);
}
 
.benefit-row__summary {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(18rem, 0.55fr);
  gap: var(--pj-space-6);
  align-items: start;
}
 
.benefit-row__summary h2 {
  margin: 0;
  font-size: clamp(2rem, 4vw, 3.5rem);
  font-weight: 520;
  letter-spacing: -0.04em;
  line-height: var(--pj-line-height-tight);
}
 
.benefit-row__summary > div > p:last-child {
  margin: var(--pj-space-2) 0 0;
}
 
.benefit-row__facts {
  display: grid;
  margin: var(--pj-space-5) 0 0;
}
 
.benefit-row__facts > div {
  display: grid;
  grid-template-columns: minmax(8rem, 0.3fr) minmax(0, 1fr);
  gap: var(--pj-space-4);
  padding-block: var(--pj-space-3);
  border-top: 1px solid var(--pj-border-subtle);
}
 
.benefit-row__facts dd {
  margin: 0;
  overflow-wrap: anywhere;
}
 
.benefit-row__regions {
  margin: var(--pj-space-4) 0 0;
}
 
@media (max-width: 48rem) {
  .benefit-intro {
    align-items: flex-start;
    flex-direction: column;
  }
 
  .benefit-row__summary {
    grid-template-columns: 1fr;
  }
 
  .benefit-row__facts > div {
    grid-template-columns: 1fr;
    gap: var(--pj-space-1);
  }
}
</style>