feat(shop-admin): manage shop coupon templates
Add a coupons page where a shop user lists, creates, edits, enables/disables, and deletes only its own templates, with localized titles, minor-unit discount and threshold, currency, claim stock, and an active window. Expose it as a coupons navigation entry beside the existing shop operations.
This commit is contained in:
@@ -25,6 +25,7 @@ watchEffect(() => {
|
||||
<NuxtLink to="/orders">{{ $t("nav.orders") }}</NuxtLink>
|
||||
<NuxtLink to="/shipments">{{ $t("nav.shipments") }}</NuxtLink>
|
||||
<NuxtLink to="/invoices">{{ $t("nav.invoices") }}</NuxtLink>
|
||||
<NuxtLink to="/coupons">{{ $t("nav.coupons") }}</NuxtLink>
|
||||
</aside>
|
||||
<main>
|
||||
<div class="main-head">
|
||||
|
||||
@@ -4,6 +4,9 @@ export const enExtra = {
|
||||
auth: {
|
||||
shopRoleError: "This account is not authorized for the merchant console.",
|
||||
},
|
||||
nav: {
|
||||
coupons: "Coupons",
|
||||
},
|
||||
shop: {
|
||||
dashboardTitle: "Merchant dashboard",
|
||||
profile: "Shop profile",
|
||||
@@ -59,6 +62,29 @@ export const enExtra = {
|
||||
issueInvoice: "Issue invoice",
|
||||
taxNo: "Tax number",
|
||||
kind: "Type",
|
||||
couponList: "Coupons",
|
||||
newCoupon: "New coupon",
|
||||
editCoupon: "Edit coupon",
|
||||
couponTitleEn: "Title (English)",
|
||||
couponTitleZh: "Title (中文)",
|
||||
couponAmount: "Discount (minor units)",
|
||||
couponThreshold: "Minimum spend (minor units)",
|
||||
couponCurrency: "Currency",
|
||||
couponStock: "Claim stock",
|
||||
couponEnabled: "Enabled",
|
||||
couponStartsAt: "Starts at",
|
||||
couponEndsAt: "Ends at",
|
||||
createCoupon: "Create coupon",
|
||||
updateCoupon: "Update coupon",
|
||||
couponSaved: "Coupon saved.",
|
||||
couponDeleted: "Coupon deleted.",
|
||||
couponTitleRequired: "Both language titles are required.",
|
||||
couponAmountInvalid: "Discount must be a positive integer.",
|
||||
couponThresholdInvalid: "Minimum spend cannot be negative.",
|
||||
couponStockInvalid: "Claim stock cannot be negative.",
|
||||
couponWindowInvalid: "End must not precede start.",
|
||||
enabledOn: "Enabled",
|
||||
enabledOff: "Disabled",
|
||||
},
|
||||
} as Record<string, unknown>;
|
||||
|
||||
@@ -66,6 +92,9 @@ export const zhExtra = {
|
||||
auth: {
|
||||
shopRoleError: "此账号无权访问商家控制台。",
|
||||
},
|
||||
nav: {
|
||||
coupons: "优惠券",
|
||||
},
|
||||
shop: {
|
||||
dashboardTitle: "商家仪表盘",
|
||||
profile: "店铺信息",
|
||||
@@ -121,5 +150,28 @@ export const zhExtra = {
|
||||
issueInvoice: "开具发票",
|
||||
taxNo: "税号",
|
||||
kind: "类型",
|
||||
couponList: "优惠券",
|
||||
newCoupon: "新建优惠券",
|
||||
editCoupon: "编辑优惠券",
|
||||
couponTitleEn: "标题(英文)",
|
||||
couponTitleZh: "标题(中文)",
|
||||
couponAmount: "优惠金额(最小单位)",
|
||||
couponThreshold: "使用门槛(最小单位)",
|
||||
couponCurrency: "币种",
|
||||
couponStock: "可领取库存",
|
||||
couponEnabled: "启用",
|
||||
couponStartsAt: "开始时间",
|
||||
couponEndsAt: "结束时间",
|
||||
createCoupon: "创建优惠券",
|
||||
updateCoupon: "更新优惠券",
|
||||
couponSaved: "优惠券已保存。",
|
||||
couponDeleted: "优惠券已删除。",
|
||||
couponTitleRequired: "中英文标题均为必填项。",
|
||||
couponAmountInvalid: "优惠金额必须为正整数。",
|
||||
couponThresholdInvalid: "使用门槛不能为负数。",
|
||||
couponStockInvalid: "可领取库存不能为负数。",
|
||||
couponWindowInvalid: "结束时间不能早于开始时间。",
|
||||
enabledOn: "已启用",
|
||||
enabledOff: "已停用",
|
||||
},
|
||||
} as Record<string, unknown>;
|
||||
|
||||
@@ -0,0 +1,316 @@
|
||||
<script setup lang="ts">
|
||||
import type { CouponTemplate, CouponTemplateInput, Currency } from "@vmall/shared";
|
||||
|
||||
definePageMeta({ middleware: "auth" });
|
||||
|
||||
const { $api } = useNuxtApp();
|
||||
const { locale, t: translate } = useI18n();
|
||||
const { load: loadMoney, fmt } = useMoney();
|
||||
|
||||
const templates = ref<CouponTemplate[]>([]);
|
||||
const currencies = ref<Currency[]>([]);
|
||||
const loading = ref(true);
|
||||
const saving = ref(false);
|
||||
const actionId = ref("");
|
||||
const error = ref("");
|
||||
const message = ref("");
|
||||
const editingId = ref("");
|
||||
|
||||
const form = reactive({
|
||||
titleEn: "",
|
||||
titleZh: "",
|
||||
amountMinor: 0,
|
||||
thresholdMinor: 0,
|
||||
currency: "USD",
|
||||
stock: 0,
|
||||
enabled: true,
|
||||
startsAt: "",
|
||||
endsAt: "",
|
||||
});
|
||||
|
||||
function toLocalInput(iso: string): string {
|
||||
const d = new Date(iso);
|
||||
const pad = (n: number) => String(n).padStart(2, "0");
|
||||
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
||||
}
|
||||
|
||||
function resetForm(): void {
|
||||
const now = new Date();
|
||||
const later = new Date(now.getTime() + 30 * 24 * 3600 * 1000);
|
||||
editingId.value = "";
|
||||
form.titleEn = "";
|
||||
form.titleZh = "";
|
||||
form.amountMinor = 0;
|
||||
form.thresholdMinor = 0;
|
||||
form.currency =
|
||||
currencies.value.find((c) => c.is_base)?.code ?? currencies.value[0]?.code ?? "USD";
|
||||
form.stock = 0;
|
||||
form.enabled = true;
|
||||
form.startsAt = toLocalInput(now.toISOString());
|
||||
form.endsAt = toLocalInput(later.toISOString());
|
||||
}
|
||||
|
||||
async function loadTemplates(): Promise<void> {
|
||||
loading.value = true;
|
||||
error.value = "";
|
||||
try {
|
||||
templates.value = await $api.shop.listCouponTemplates();
|
||||
} catch (err: unknown) {
|
||||
error.value = err instanceof Error ? err.message : translate("common.error");
|
||||
} finally {
|
||||
loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
async function loadCurrencies(): Promise<void> {
|
||||
try {
|
||||
currencies.value = (await $api.listCurrencies()).filter((c) => c.enabled);
|
||||
} catch {
|
||||
currencies.value = [];
|
||||
}
|
||||
}
|
||||
|
||||
function payload(): CouponTemplateInput {
|
||||
return {
|
||||
title: { en: form.titleEn.trim(), zh: form.titleZh.trim() },
|
||||
amount_minor: Number(form.amountMinor),
|
||||
threshold_minor: Number(form.thresholdMinor),
|
||||
currency: form.currency,
|
||||
stock: Number(form.stock),
|
||||
enabled: form.enabled,
|
||||
starts_at: new Date(form.startsAt).toISOString(),
|
||||
ends_at: new Date(form.endsAt).toISOString(),
|
||||
};
|
||||
}
|
||||
|
||||
async function submit(): Promise<void> {
|
||||
error.value = "";
|
||||
message.value = "";
|
||||
const body = payload();
|
||||
if (!body.title.en || !body.title.zh) {
|
||||
error.value = translate("shop.couponTitleRequired");
|
||||
return;
|
||||
}
|
||||
if (!(body.amount_minor > 0)) {
|
||||
error.value = translate("shop.couponAmountInvalid");
|
||||
return;
|
||||
}
|
||||
if (body.threshold_minor < 0) {
|
||||
error.value = translate("shop.couponThresholdInvalid");
|
||||
return;
|
||||
}
|
||||
if (body.stock < 0) {
|
||||
error.value = translate("shop.couponStockInvalid");
|
||||
return;
|
||||
}
|
||||
if (new Date(body.ends_at) < new Date(body.starts_at)) {
|
||||
error.value = translate("shop.couponWindowInvalid");
|
||||
return;
|
||||
}
|
||||
saving.value = true;
|
||||
try {
|
||||
if (editingId.value) await $api.shop.updateCouponTemplate(editingId.value, body);
|
||||
else await $api.shop.createCouponTemplate(body);
|
||||
message.value = translate("shop.couponSaved");
|
||||
resetForm();
|
||||
await loadTemplates();
|
||||
} catch (err: unknown) {
|
||||
error.value = err instanceof Error ? err.message : translate("common.error");
|
||||
} finally {
|
||||
saving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
function edit(template: CouponTemplate): void {
|
||||
editingId.value = template.id;
|
||||
form.titleEn = template.title.en ?? "";
|
||||
form.titleZh = template.title.zh ?? "";
|
||||
form.amountMinor = template.amount_minor;
|
||||
form.thresholdMinor = template.threshold_minor;
|
||||
form.currency = template.currency;
|
||||
form.stock = template.stock;
|
||||
form.enabled = template.enabled;
|
||||
form.startsAt = toLocalInput(template.starts_at);
|
||||
form.endsAt = toLocalInput(template.ends_at);
|
||||
}
|
||||
|
||||
async function toggleEnabled(template: CouponTemplate): Promise<void> {
|
||||
actionId.value = template.id;
|
||||
error.value = "";
|
||||
try {
|
||||
await $api.shop.updateCouponTemplate(template.id, {
|
||||
title: template.title,
|
||||
amount_minor: template.amount_minor,
|
||||
threshold_minor: template.threshold_minor,
|
||||
currency: template.currency,
|
||||
stock: template.stock,
|
||||
enabled: !template.enabled,
|
||||
starts_at: template.starts_at,
|
||||
ends_at: template.ends_at,
|
||||
});
|
||||
await loadTemplates();
|
||||
} catch (err: unknown) {
|
||||
error.value = err instanceof Error ? err.message : translate("common.error");
|
||||
} finally {
|
||||
actionId.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(template: CouponTemplate): Promise<void> {
|
||||
actionId.value = template.id;
|
||||
error.value = "";
|
||||
message.value = "";
|
||||
try {
|
||||
await $api.shop.deleteCouponTemplate(template.id);
|
||||
message.value = translate("shop.couponDeleted");
|
||||
await loadTemplates();
|
||||
} catch (err: unknown) {
|
||||
error.value = err instanceof Error ? err.message : translate("common.error");
|
||||
} finally {
|
||||
actionId.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(async () => {
|
||||
loadMoney();
|
||||
await loadCurrencies();
|
||||
resetForm();
|
||||
await loadTemplates();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="page">
|
||||
<h1 class="page-title">{{ $t("shop.couponList") }}</h1>
|
||||
<div v-if="error" class="error-text" role="alert">{{ error }}</div>
|
||||
<div v-if="message" class="muted" role="status">{{ message }}</div>
|
||||
|
||||
<section class="card">
|
||||
<h2>{{ editingId ? $t("shop.editCoupon") : $t("shop.newCoupon") }}</h2>
|
||||
<div class="form-grid">
|
||||
<label>
|
||||
<span>{{ $t("shop.couponTitleEn") }}</span>
|
||||
<input v-model="form.titleEn" class="input" type="text" />
|
||||
</label>
|
||||
<label>
|
||||
<span>{{ $t("shop.couponTitleZh") }}</span>
|
||||
<input v-model="form.titleZh" class="input" type="text" />
|
||||
</label>
|
||||
<label>
|
||||
<span>{{ $t("shop.couponAmount") }}</span>
|
||||
<input v-model.number="form.amountMinor" class="input" type="number" min="1" step="1" />
|
||||
</label>
|
||||
<label>
|
||||
<span>{{ $t("shop.couponThreshold") }}</span>
|
||||
<input v-model.number="form.thresholdMinor" class="input" type="number" min="0" step="1" />
|
||||
</label>
|
||||
<label>
|
||||
<span>{{ $t("shop.couponCurrency") }}</span>
|
||||
<select v-model="form.currency" class="input">
|
||||
<option v-for="c in currencies" :key="c.code" :value="c.code">{{ c.code }}</option>
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
<span>{{ $t("shop.couponStock") }}</span>
|
||||
<input v-model.number="form.stock" class="input" type="number" min="0" step="1" />
|
||||
</label>
|
||||
<label>
|
||||
<span>{{ $t("shop.couponStartsAt") }}</span>
|
||||
<input v-model="form.startsAt" class="input" type="datetime-local" />
|
||||
</label>
|
||||
<label>
|
||||
<span>{{ $t("shop.couponEndsAt") }}</span>
|
||||
<input v-model="form.endsAt" class="input" type="datetime-local" />
|
||||
</label>
|
||||
<label class="checkbox">
|
||||
<input v-model="form.enabled" type="checkbox" />
|
||||
<span>{{ $t("shop.couponEnabled") }}</span>
|
||||
</label>
|
||||
</div>
|
||||
<div class="form-actions">
|
||||
<button class="btn primary" :disabled="saving" @click="submit">
|
||||
{{ editingId ? $t("shop.updateCoupon") : $t("shop.createCoupon") }}
|
||||
</button>
|
||||
<button v-if="editingId" class="btn" type="button" @click="resetForm">
|
||||
{{ $t("common.cancel") }}
|
||||
</button>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<p v-if="loading" class="muted">{{ $t("common.loading") }}</p>
|
||||
<div v-else-if="!templates.length" class="card muted">{{ $t("common.empty") }}</div>
|
||||
<div v-else class="table-wrap">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ $t("shop.couponTitleEn") }}</th>
|
||||
<th>{{ $t("shop.couponAmount") }}</th>
|
||||
<th>{{ $t("shop.couponThreshold") }}</th>
|
||||
<th>{{ $t("shop.couponStock") }}</th>
|
||||
<th>{{ $t("shop.couponStartsAt") }}</th>
|
||||
<th>{{ $t("shop.couponEndsAt") }}</th>
|
||||
<th>{{ $t("common.status") }}</th>
|
||||
<th>{{ $t("common.actions") }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="template in templates" :key="template.id">
|
||||
<td>{{ template.title[locale] ?? template.title.en }}</td>
|
||||
<td>{{ fmt(template.amount_minor, template.currency) }}</td>
|
||||
<td>{{ fmt(template.threshold_minor, template.currency) }}</td>
|
||||
<td>{{ template.stock }}</td>
|
||||
<td>{{ template.starts_at.slice(0, 10) }}</td>
|
||||
<td>{{ template.ends_at.slice(0, 10) }}</td>
|
||||
<td>
|
||||
<span class="badge" :class="template.enabled ? 'green' : 'red'">
|
||||
{{ template.enabled ? $t("shop.enabledOn") : $t("shop.enabledOff") }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="actions">
|
||||
<button class="btn sm" :disabled="actionId === template.id" @click="edit(template)">
|
||||
{{ $t("shop.edit") }}
|
||||
</button>
|
||||
<button class="btn sm" :disabled="actionId === template.id" @click="toggleEnabled(template)">
|
||||
{{ template.enabled ? $t("shop.enabledOff") : $t("shop.enabledOn") }}
|
||||
</button>
|
||||
<button class="btn sm" :disabled="actionId === template.id" @click="remove(template)">
|
||||
{{ $t("common.delete") }}
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.form-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
||||
gap: 12px;
|
||||
margin: 12px 0;
|
||||
}
|
||||
.form-grid label {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
font-size: 13px;
|
||||
}
|
||||
.form-grid .checkbox {
|
||||
flex-direction: row;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.form-actions {
|
||||
display: flex;
|
||||
gap: 8px;
|
||||
}
|
||||
.table-wrap {
|
||||
overflow-x: auto;
|
||||
}
|
||||
.actions {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user