diff --git a/.env.example b/.env.example index 6d3eaa3..2c0f776 100644 --- a/.env.example +++ b/.env.example @@ -4,7 +4,7 @@ DATABASE_URL="mysql://crm_dev:PASSWORD@124.223.187.95:3306/crm_dev" # Auth.js 会话加密密钥:openssl rand -base64 32 AUTH_SECRET="please-run-openssl-rand-base64-32" -# 开发环境不需要 AUTH_URL(trustHost:true 自动识别 host/端口)。 +# 本地开发不要设置 AUTH_URL,否则打开 localhost 会被跳到该地址。 # 生产必须设为浏览器可访问的地址(真实域名或公网 IP), # 切勿写成监听地址 0.0.0.0,否则登录/退出会跳到 http://0.0.0.0:端口 # AUTH_URL="https://crm.yourdomain.com" diff --git a/prisma/schema.prisma b/prisma/schema.prisma index ea150c8..446bcad 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -17,6 +17,7 @@ datasource db { model Customer { id Int @id @default(autoincrement()) customerName String? @default("") @map("customer_name") + customerNo String? @map("customer_no") @db.Text // 商户编号,数字与字母,区分大小写,不限位数 contacts String? // 联系人 contactsPhone String? @default("") @map("contacts_phone") // 联系人手机号 balanceUser String? @map("balance_user") // 结算人 diff --git a/sql/add-customer-no.sql b/sql/add-customer-no.sql new file mode 100644 index 0000000..ac78661 --- /dev/null +++ b/sql/add-customer-no.sql @@ -0,0 +1,6 @@ +-- 商户表增加商户编号(数字+字母,区分大小写,不限位数) +-- 执行:本地与生产库均需执行;执行后运行 npm run prisma:generate +-- 历史行保持 NULL,新录入/导入必填 + +ALTER TABLE customers + ADD COLUMN customer_no TEXT NULL COMMENT '商户编号' AFTER customer_name; diff --git a/src/app/(dashboard)/customers/actions.ts b/src/app/(dashboard)/customers/actions.ts index 0dd8b77..59497eb 100644 --- a/src/app/(dashboard)/customers/actions.ts +++ b/src/app/(dashboard)/customers/actions.ts @@ -11,8 +11,15 @@ import { isSuperAdmin } from "@/lib/acl"; import { fmtDate, parseDate } from "@/lib/utils"; // 校验规则对齐老系统必填项(customer_name/contacts/balance_user/detail/salesman/report_date) +const customerNoSchema = z + .string() + .trim() + .min(1, "商户编号必填") + .regex(/^[A-Za-z0-9]+$/, "商户编号只能包含数字和字母,区分大小写"); + const schema = z.object({ customerName: z.string().trim().min(1, "商户名称必填").max(500), + customerNo: customerNoSchema, contacts: z.string().trim().min(1, "联系人必填").max(500), balanceUser: z.string().trim().min(1, "结算人必填").max(500), detail: z.string().trim().min(1, "产品明细必填"), @@ -26,8 +33,8 @@ const schema = z.object({ balanceUserPhone: z.string().trim().optional().default(""), deposit: z.string().trim().optional().default(""), buy: z.string().trim().optional().default(""), - birthdayType: z.coerce.number().int().min(1).max(2).default(1), - birthdayOwner: z.coerce.number().int().min(1).max(2).default(2), + birthdayType: z.coerce.number().refine((n) => n === 1, "生日类型只能是阳历"), + birthdayOwner: z.coerce.number().refine((n) => n === 1, "生日归属只能是结算人"), birthday: z.string().trim().optional().default(""), remark: z.string().trim().optional().default(""), }); @@ -57,6 +64,7 @@ export type ActionState = { error?: string }; function pickForm(formData: FormData) { return { customerName: formData.get("customerName"), + customerNo: formData.get("customerNo"), contacts: formData.get("contacts"), balanceUser: formData.get("balanceUser"), detail: formData.get("detail"), @@ -66,8 +74,8 @@ function pickForm(formData: FormData) { balanceUserPhone: formData.get("balanceUserPhone") ?? "", deposit: formData.get("deposit") ?? "", buy: formData.get("buy") ?? "", - birthdayType: formData.get("birthdayType") ?? 1, - birthdayOwner: formData.get("birthdayOwner") ?? 2, + birthdayType: 1, + birthdayOwner: 1, birthday: formData.get("birthday") ?? "", remark: formData.get("remark") ?? "", }; @@ -105,7 +113,15 @@ export async function updateCustomer( const r = schema.safeParse(pickForm(formData)); if (!r.success) return { error: r.error.issues[0].message }; - await prisma.customer.update({ where: { id }, data: toData(r.data) }); + const existing = await prisma.customer.findUnique({ + where: { id }, + select: { birthdayType: true }, + }); + const data = toData(r.data); + // 改版前的阴历生日不能在保存其它字段时改成阳历,否则明年提醒日会错 + if (existing?.birthdayType === 2) data.birthdayType = 2; + + await prisma.customer.update({ where: { id }, data }); revalidatePath("/customers"); redirect("/customers"); } @@ -125,6 +141,7 @@ export async function deleteCustomer(id: number): Promise { const HEADER_MAP: Record = { 商户名称: "customerName", + 商户编号: "customerNo", 联系人: "contacts", 联系人电话: "contactsPhone", 结算人: "balanceUser", @@ -134,8 +151,6 @@ const HEADER_MAP: Record = { 产品明细: "detail", 押金: "deposit", 买断: "buy", - 生日类型: "birthdayType", - 生日归属: "birthdayOwner", 生日: "birthday", 业务员: "salesman", 报单日期: "reportDate", @@ -170,6 +185,8 @@ export async function importCustomers(formData: FormData): Promise const errors: string[] = []; if (!rec.customerName) errors.push("商户名称必填"); + if (!rec.customerNo) errors.push("商户编号必填"); + else if (!/^[A-Za-z0-9]+$/.test(rec.customerNo)) errors.push("商户编号只能包含数字和字母"); const reportDate = parseDate(rec.reportDate); if (!rec.reportDate || !reportDate) errors.push("报单日期必填且格式为 YYYY-MM-DD"); if (rec.birthday && !parseDate(rec.birthday)) errors.push("生日格式应为 YYYY-MM-DD"); @@ -179,6 +196,7 @@ export async function importCustomers(formData: FormData): Promise } else { validData.push({ customerName: rec.customerName, + customerNo: rec.customerNo, contacts: rec.contacts || null, contactsPhone: rec.contactsPhone || null, balanceUser: rec.balanceUser || null, @@ -189,8 +207,8 @@ export async function importCustomers(formData: FormData): Promise buy: rec.buy || null, salesman: rec.salesman || null, remark: rec.remark || null, - birthdayType: rec.birthdayType === "阴历" || rec.birthdayType === "2" ? 2 : 1, - birthdayOwner: rec.birthdayOwner === "结算人" || rec.birthdayOwner === "1" ? 1 : 2, + birthdayType: 1, + birthdayOwner: 1, birthday: parseDate(rec.birthday), reportDate, }); diff --git a/src/app/(dashboard)/customers/birthday/page.tsx b/src/app/(dashboard)/customers/birthday/page.tsx index 1ac25af..2c757bb 100644 --- a/src/app/(dashboard)/customers/birthday/page.tsx +++ b/src/app/(dashboard)/customers/birthday/page.tsx @@ -1,27 +1,29 @@ +import type { ReactNode } from "react"; import { Prisma } from "@prisma/client"; import { prisma } from "@/lib/prisma"; import { auth } from "@/lib/auth"; -import { isSuperAdmin } from "@/lib/acl"; -import { birthdayMatchLists } from "@/lib/lunar"; +import { isStaff, isSuperAdmin } from "@/lib/acl"; +import { birthdayMatchLists, lunarBirthdaySolarThisYear } from "@/lib/lunar"; import { fmtDate, maskPhone } from "@/lib/utils"; import { Card } from "@/components/ui/card"; -import { Table, THead, TR, TH, TD } from "@/components/ui/table"; -import { Badge } from "@/components/ui/badge"; import { BirthdayControl } from "./birthday-control"; type Row = { id: number; customerName: string | null; + customerNo: string | null; contacts: string | null; contactsPhone: string | null; balanceUser: string | null; balanceUserPhone: string | null; - phone: string | null; // 旧公共电话,仅作新字段为空时的回退 + phone: string | null; // 旧公共电话,仅作结算人手机号为空时的回退 + detail: string | null; + deposit: string | null; + buy: string | null; salesman: string | null; reportDate: Date | null; birthday: Date | null; birthdayType: number | null; - birthdayOwner: number | null; remark: string | null; }; @@ -33,13 +35,19 @@ function baseDate(s?: string): Date { return new Date(); } +function showPhone(raw: string | null | undefined, full: boolean) { + const value = raw?.trim() || ""; + if (!value) return "—"; + return full ? value : maskPhone(value); +} + export default async function BirthdayPage({ searchParams, }: { searchParams: Promise<{ mode?: string; date?: string }>; }) { const session = await auth(); - const superAdmin = isSuperAdmin(session?.user?.roles); + const fullPhone = isSuperAdmin(session?.user?.roles) || isStaff(session?.user?.roles); const sp = await searchParams; const mode = sp.mode === "week" ? "week" : "today"; const dateStr = sp.date ?? ""; @@ -54,16 +62,19 @@ export default async function BirthdayPage({ SELECT id, customer_name AS customerName, + customer_no AS customerNo, contacts, contacts_phone AS contactsPhone, balance_user AS balanceUser, balance_user_phone AS balanceUserPhone, phone, + detail, + deposit, + buy, salesman, report_date AS reportDate, birthday, birthday_type AS birthdayType, - birthday_owner AS birthdayOwner, remark FROM customers WHERE deleted_at IS NULL AND ( @@ -79,98 +90,73 @@ export default async function BirthdayPage({
- 命中 {rows.length} 位商户 + 共 {rows.length} 位商户过生日
- - - - - - - - - - - - - - {rows.length === 0 && ( - - - - )} - {rows.map((c) => { - // 生日归属人的姓名和手机号;新手机号为空时回退旧公共 phone(历史数据) - const ownerName = c.birthdayOwner === 1 ? c.balanceUser : c.contacts; - const ownerPhone = - (c.birthdayOwner === 1 ? c.balanceUserPhone : c.contactsPhone) || c.phone; - return ( - - - - - - - - - ); - })} - -
商户名称归属人业务员生日历法归属
- 该时段暂无生日商户 -
{c.customerName} -
{ownerName}
-
- {superAdmin ? ownerPhone : maskPhone(ownerPhone)} -
-
{c.salesman}{fmtDate(c.birthday)} - - {c.birthdayType === 2 ? "农历" : "阳历"} - - {c.birthdayOwner === 1 ? "结算人" : "联系人"}
-
+ {rows.length === 0 && ( + + 没有过生日的商户 + + )} -
- {rows.length === 0 && ( - - 该时段暂无生日商户 - - )} +
{rows.map((c) => { - const ownerName = c.birthdayOwner === 1 ? c.balanceUser : c.contacts; - const ownerPhone = - (c.birthdayOwner === 1 ? c.balanceUserPhone : c.contactsPhone) || c.phone; + const balancePhone = c.balanceUserPhone || c.phone; + const lunar = Number(c.birthdayType) === 2; + const solarThisYear = lunar ? lunarBirthdaySolarThisYear(fmtDate(c.birthday)) : null; return ( - +
-
{c.customerName}
-
- {c.salesman || "—"} +
{c.customerName || "—"}
+
+ 编号 {c.customerNo || "—"} +
+
+
+
+ {lunar ? "今年是" : "生日"} +
+
+ {lunar ? solarThisYear || fmtDate(c.birthday) : fmtDate(c.birthday)}
- - {c.birthdayType === 2 ? "农历" : "阳历"} -
-
-
-
归属人
-
{ownerName || "—"}
-
- {superAdmin ? ownerPhone : maskPhone(ownerPhone)} -
-
-
-
生日
-
{fmtDate(c.birthday)}
-
- {c.birthdayOwner === 1 ? "结算人" : "联系人"} -
-
-
+ +
+ + + + + + + + + + + + + + {lunar ? ( + + 农历 {fmtDate(c.birthday).slice(5) || "—"} + + 按农历过生日{solarThisYear ? `,今年是 ${solarThisYear}` : ""} + + + ) : ( + fmtDate(c.birthday) || "—" + )} + + + +
+ +
+ + +
); })} @@ -178,3 +164,33 @@ export default async function BirthdayPage({
); } + +function Field({ + label, + value, + pre, + children, +}: { + label: string; + value?: string | null; + pre?: boolean; + children?: ReactNode; +}) { + return ( +
+
{label}
+
+ {children ?? (value?.trim() ? value : "—")} +
+
+ ); +} + +function Phone({ value, callable }: { value: string; callable: boolean }) { + if (!callable || value === "—") return {value}; + return ( + + {value} + + ); +} diff --git a/src/app/(dashboard)/customers/customer-form.tsx b/src/app/(dashboard)/customers/customer-form.tsx index dd11918..422641f 100644 --- a/src/app/(dashboard)/customers/customer-form.tsx +++ b/src/app/(dashboard)/customers/customer-form.tsx @@ -3,13 +3,14 @@ import { useActionState } from "react"; import Link from "next/link"; import { Button } from "@/components/ui/button"; -import { Input, Textarea, Select } from "@/components/ui/input"; +import { Input, Textarea } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { fmtDate } from "@/lib/utils"; import type { ActionState } from "./actions"; type CustomerValues = { customerName?: string | null; + customerNo?: string | null; contacts?: string | null; contactsPhone?: string | null; balanceUser?: string | null; @@ -17,9 +18,8 @@ type CustomerValues = { detail?: string | null; deposit?: string | null; buy?: string | null; - birthdayType?: number | null; - birthdayOwner?: number | null; birthday?: Date | null; + birthdayType?: number | null; salesman?: string | null; reportDate?: Date | null; remark?: string | null; @@ -108,6 +108,14 @@ export function CustomerForm({ pattern={requireAll ? "\\S+" : undefined} title={requireAll ? "商户名称不能包含空格" : undefined} /> +
-
- - -
-
- - -
+ {v.birthdayType === 2 && ( +

+ 这个生日是按农历记的。 +

+ )} diff --git a/src/app/(dashboard)/customers/customer-toolbar.tsx b/src/app/(dashboard)/customers/customer-toolbar.tsx index 71f9c1d..b981d9b 100644 --- a/src/app/(dashboard)/customers/customer-toolbar.tsx +++ b/src/app/(dashboard)/customers/customer-toolbar.tsx @@ -84,7 +84,7 @@ export function CustomerToolbar({ diff --git a/src/app/(dashboard)/customers/customer-view.tsx b/src/app/(dashboard)/customers/customer-view.tsx index 8276ea5..a46a755 100644 --- a/src/app/(dashboard)/customers/customer-view.tsx +++ b/src/app/(dashboard)/customers/customer-view.tsx @@ -9,6 +9,7 @@ import { fmtDate, cn } from "@/lib/utils"; type CustomerViewProps = { customer: { customerName?: string | null; + customerNo?: string | null; contacts?: string | null; contactsPhone?: string | null; balanceUser?: string | null; @@ -16,8 +17,6 @@ type CustomerViewProps = { detail?: string | null; deposit?: string | null; buy?: string | null; - birthdayType?: number | null; - birthdayOwner?: number | null; birthday?: Date | null; salesman?: string | null; reportDate?: Date | null; @@ -43,11 +42,8 @@ export function CustomerView({ customer }: CustomerViewProps) { }; }, [open]); - const birthdayLabel = customer.birthdayType === 2 ? "阴历" : "阳历"; - const birthdayOwnerLabel = customer.birthdayOwner === 1 ? "结算人" : "联系人"; - // 各分区是否至少有一个字段,用于隐藏整块为空的小标题 - const hasBasic = !!(customer.customerName || customer.detail); + const hasBasic = !!(customer.customerName || customer.customerNo || customer.detail); const hasContacts = !!(customer.contacts || customer.contactsPhone); const hasBalanceUser = !!(customer.balanceUser || customer.balanceUserPhone); const hasBusiness = !!(customer.deposit || customer.buy || customer.salesman || customer.reportDate); @@ -85,6 +81,7 @@ export function CustomerView({ customer }: CustomerViewProps) { {hasBasic && (
+
)} @@ -102,8 +99,6 @@ export function CustomerView({ customer }: CustomerViewProps) { )} {customer.birthday && (
- -
)} diff --git a/src/app/(dashboard)/customers/export/route.ts b/src/app/(dashboard)/customers/export/route.ts index 89e9257..b9cba41 100644 --- a/src/app/(dashboard)/customers/export/route.ts +++ b/src/app/(dashboard)/customers/export/route.ts @@ -32,6 +32,7 @@ export async function GET(req: NextRequest) { for (const c of rows) { aoa.push([ c.customerName ?? "", + c.customerNo ?? "", c.contacts ?? "", c.contactsPhone ?? "", c.balanceUser ?? "", @@ -39,8 +40,6 @@ export async function GET(req: NextRequest) { c.detail ?? "", c.deposit ?? "", c.buy ?? "", - c.birthdayType === 2 ? "阴历" : "阳历", - c.birthdayOwner === 1 ? "结算人" : "联系人", fmtDate(c.birthday), c.salesman ?? "", fmtDate(c.reportDate), diff --git a/src/app/(dashboard)/customers/page.tsx b/src/app/(dashboard)/customers/page.tsx index 1342ca6..e3fd15d 100644 --- a/src/app/(dashboard)/customers/page.tsx +++ b/src/app/(dashboard)/customers/page.tsx @@ -5,7 +5,6 @@ import { isStaff, isSuperAdmin } from "@/lib/acl"; import { fmtDate, maskPhone } from "@/lib/utils"; import { Card } from "@/components/ui/card"; import { Table, THead, TR, TH, TD } from "@/components/ui/table"; -import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { CustomerToolbar } from "./customer-toolbar"; import { DeleteButton } from "./delete-button"; @@ -66,6 +65,7 @@ export default async function CustomersPage({ 商户名称 + 商户编号 联系人 结算人 产品明细 @@ -81,7 +81,7 @@ export default async function CustomersPage({ {rows.length === 0 && ( - + 暂无数据 @@ -92,6 +92,7 @@ export default async function CustomersPage({ return ( {c.customerName} + {c.customerNo || "—"}
{c.contacts}
@@ -107,19 +108,7 @@ export default async function CustomersPage({ {c.detail} {c.deposit} {c.buy} - - {fmtDate(c.birthday)} - {c.birthday && ( - - {c.birthdayType === 2 ? "农历" : "阳历"} - - )} - {c.birthday && ( - - {c.birthdayOwner === 1 ? "结算人" : "联系人"} - - )} - + {fmtDate(c.birthday)} {c.salesman} {fmtDate(c.reportDate)} {c.remark} @@ -159,16 +148,19 @@ export default async function CustomersPage({
{c.customerName}
+
+ 编号 {c.customerNo || "—"} +
{c.salesman} {c.reportDate ? ` · ${fmtDate(c.reportDate)}` : ""}
- {c.birthday && ( - - {c.birthdayType === 2 ? "农历" : "阳历"} {fmtDate(c.birthday)} - - )} + {c.birthday ? ( + + {fmtDate(c.birthday)} + + ) : null}
diff --git a/src/app/(dashboard)/customers/query.ts b/src/app/(dashboard)/customers/query.ts index 0fd106b..ff2ac4b 100644 --- a/src/app/(dashboard)/customers/query.ts +++ b/src/app/(dashboard)/customers/query.ts @@ -14,6 +14,7 @@ export function buildCustomerWhere(sp: CustomerSearch): Prisma.CustomerWhereInpu if (keyword) { where.OR = [ { customerName: { contains: keyword } }, + { customerNo: { contains: keyword } }, { contacts: { contains: keyword } }, { contactsPhone: { contains: keyword } }, { balanceUser: { contains: keyword } }, @@ -32,6 +33,7 @@ export function buildCustomerWhere(sp: CustomerSearch): Prisma.CustomerWhereInpu /** 导出/导入的列定义(表头 ↔ 字段) */ export const EXCEL_COLUMNS: { header: string; key: string }[] = [ { header: "商户名称", key: "customerName" }, + { header: "商户编号", key: "customerNo" }, { header: "联系人", key: "contacts" }, { header: "联系人电话", key: "contactsPhone" }, { header: "结算人", key: "balanceUser" }, @@ -39,8 +41,6 @@ export const EXCEL_COLUMNS: { header: string; key: string }[] = [ { header: "产品明细", key: "detail" }, { header: "押金", key: "deposit" }, { header: "买断", key: "buy" }, - { header: "生日类型", key: "birthdayType" }, - { header: "生日归属", key: "birthdayOwner" }, { header: "生日", key: "birthday" }, { header: "业务员", key: "salesman" }, { header: "报单日期", key: "reportDate" }, diff --git a/src/app/(dashboard)/customers/template/route.ts b/src/app/(dashboard)/customers/template/route.ts index 94a74ee..11d0feb 100644 --- a/src/app/(dashboard)/customers/template/route.ts +++ b/src/app/(dashboard)/customers/template/route.ts @@ -10,6 +10,7 @@ export async function GET() { const headers = [ "商户名称", + "商户编号", "联系人", "联系人电话", "结算人", @@ -17,8 +18,6 @@ export async function GET() { "产品明细", "押金", "买断", - "生日类型", - "生日归属", "生日", "业务员", "报单日期", @@ -28,6 +27,7 @@ export async function GET() { // 示例行(用户导入前需删除此行) const example = [ "模板名称_此行删除", + "Ab12Cd", "侯磊", "13813855555", "张三", @@ -35,8 +35,6 @@ export async function GET() { "POS+CRM", "100", "100", - "阴历", - "联系人", "生日格式要求:2022-06-21", "A", "报单日期格式要求:2022-06-21", diff --git a/src/app/(dashboard)/page.tsx b/src/app/(dashboard)/page.tsx index 102d619..f4db66c 100644 --- a/src/app/(dashboard)/page.tsx +++ b/src/app/(dashboard)/page.tsx @@ -6,7 +6,6 @@ import { birthdayMatchLists } from "@/lib/lunar"; import { fmtDate } from "@/lib/utils"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Table, THead, TR, TH, TD } from "@/components/ui/table"; -import { Badge } from "@/components/ui/badge"; import { MonthlyReportChart, type MonthPoint } from "./report-chart"; export default async function DashboardPage() { @@ -131,6 +130,7 @@ export default async function DashboardPage() { 商户名称 + 商户编号 联系人 业务员 报单时间 @@ -141,17 +141,11 @@ export default async function DashboardPage() { {recent.map((c) => ( {c.customerName} + {c.customerNo || "—"} {c.contacts} {c.salesman} {fmtDate(c.reportDate)} - - {fmtDate(c.birthday)} - {c.birthday && ( - - {c.birthdayType === 2 ? "农历" : "阳历"} - - )} - + {fmtDate(c.birthday)} ))} @@ -167,6 +161,11 @@ export default async function DashboardPage() {
{c.customerName}
+ {c.customerNo ? ( +
+ 编号 {c.customerNo} +
+ ) : null}
{c.contacts || "—"} {c.salesman ? ` · ${c.salesman}` : ""} @@ -176,11 +175,11 @@ export default async function DashboardPage() {
{fmtDate(c.reportDate)}
- {c.birthday && ( - - {c.birthdayType === 2 ? "农历" : "阳历"} {fmtDate(c.birthday)} - - )} + {c.birthday ? ( +
+ {fmtDate(c.birthday)} +
+ ) : null}
))} diff --git a/src/lib/auth.config.ts b/src/lib/auth.config.ts index 9526709..5c038cb 100644 --- a/src/lib/auth.config.ts +++ b/src/lib/auth.config.ts @@ -1,5 +1,10 @@ import type { NextAuthConfig, DefaultSession } from "next-auth"; +// 本地 `next dev` 忽略 .env 里的生产 AUTH_URL,避免 localhost 被跳到公网机。 +if (process.env.NODE_ENV === "development") { + delete process.env.AUTH_URL; +} + // Edge 安全的基础配置:不引入 Prisma / bcrypt,供 middleware 使用。 // 真正带数据库校验的 Credentials provider 在 auth.ts 里注入。 diff --git a/src/lib/lunar.ts b/src/lib/lunar.ts index b28de51..8919d2f 100644 --- a/src/lib/lunar.ts +++ b/src/lib/lunar.ts @@ -1,4 +1,4 @@ -import { Solar } from "lunar-typescript"; +import { Lunar, Solar } from "lunar-typescript"; function pad2(n: number): string { return String(Math.abs(n)).padStart(2, "0"); @@ -39,3 +39,21 @@ export function birthdayMatchLists( return { solar: [...solar], lunar: [...lunar] }; } + +/** + * 历史阴历生日存的是农历月日(年份占位)。换成今年对应的公历日期,供提醒页对照。 + * 闰月等无法换算时返回 null,不猜日期。 + */ +export function lunarBirthdaySolarThisYear( + isoDate: string, + year = new Date().getFullYear() +): string | null { + const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(isoDate); + if (!m) return null; + try { + const solar = Lunar.fromYmd(year, Number(m[2]), Number(m[3])).getSolar(); + return `${solar.getYear()}-${pad2(solar.getMonth())}-${pad2(solar.getDay())}`; + } catch { + return null; + } +}