fix(contacts): ช่องทางติดต่อกดใช้ได้จริง (tel:/mailto:/โซเชียล) + ซ่อมข้อมูลเดิม
ปัญหา: href ในข้อมูลดิบไม่สม่ำเสมอ (ว่าง / www. ไม่มี scheme / เบอร์ไม่มี tel: / facebook ติด fbclid) ทำให้กดแล้วไม่เกิดอะไร และปุ่มโทรใช้ไม่ได้ - app/lib/contact-link.ts: resolveContact() จับคู่ลิงก์ตามชนิด · เบอร์ → tel:+66… (แปลง 0 นำหน้า, ตัดขีด/ช่องว่าง/วงเล็บ) · อีเมล → mailto: · LINE @id → line.me · เว็บ → https + ตัด fbclid/_rdc/utm_* - ContactPanel: ไอคอนตามชนิด (phone/mail/fb/ig/line/ticket/globe), tel/mailto เปิดแท็บเดิม, เว็บเปิดแท็บใหม่; แถวที่ไม่มีลิงก์แสดงเป็นข้อความ (ไม่หลอกให้กด) - sanitizeCinemas: คำนวณ href ใหม่ทุกครั้งที่บันทึก → ข้อมูลเก่าไม่กลับมาเพี้ยน - ContactsEditor: auto-format ตอนออกจากช่อง + พรีวิว "✓ กดโทรได้ → tel:+66…" - scripts/audit-contacts.mjs [--fix]: ตรวจ/ซ่อมทั้งไฟล์ (แก้ไป 13 จุด, สำรองข้อมูลแล้ว) - contact-link.spec.ts 13 tests · docs: admin-guide/api/DESIGN
This commit is contained in:
1 parent
2c5918a614
commit
42572020d8
9 files changed
+656
-26
No files matched your search
@@ -253,6 +253,7 @@ Single source of truth per concern — do not duplicate these:
|
||||
- ขนาดรูป: กว้างไม่เกิน **1600px** และบีบอัด (JPEG q82 / WebP q82) — โฟลเดอร์ `public/images/cinemas` ควรอยู่ราว **≤ 3 MB**
|
||||
- **ทุก `<img>` ของโรงหนังต้องใช้ `<PhotoImage>`** (`app/components/site/PhotoImage.tsx`) — มี `onError` สลับไปโลโก้แทนรูปแตก และจำ `src` ที่พังเพื่อให้เปลี่ยนรูปใหม่แล้วโหลดได้อีก (ห้ามเขียน `<img src={photo}>` ตรง ๆ ใน component ใหม่)
|
||||
- **อัปโหลดจากหลังบ้าน**: `PhotosEditor` → `POST /api/admin/uploads` (multipart, ≤ 8 MB, ตรวจ magic bytes) → เก็บ `data/uploads/cinemas/` → คืน `/uploads/cinemas/<slug>-<random>-<ts>.<ext>` (gitignored) เสิร์ฟผ่าน Nitro route `server/routes/uploads/[...path].get.ts` — **ห้ามย้ายไปเขียนใน `public/`** เพราะตอน production โฟลเดอร์นั้นถูก copy ตอน build (เขียน runtime ไม่ติด)
|
||||
- **ช่องทางติดต่อ (`contacts`)** — `href` **ต้องใช้ได้จริง** และถูกคำนวณใหม่ทุกครั้งที่บันทึกผ่าน `contactHref()` (`app/lib/contact-link.ts`): เบอร์ → `tel:+66…`, อีเมล → `mailto:`, เว็บ/โซเชียล → `https://` (ตัด `fbclid`/`_rdc`/`utm_*`, เปลี่ยน `web.facebook.com` → `www.facebook.com`), LINE ID `@x` → `line.me/R/ti/p/~x` · หน้าเว็บใช้ `resolveContact()` ตัวเดียวกัน — **ห้าม hardcode href ใน component** และห้ามแสดง `<a>` ที่ไม่มี href (ให้แสดงเป็นข้อความแทน) · ตรวจ/ซ่อมทั้งไฟล์: `node scripts/audit-contacts.mjs [--fix]`
|
||||
- `app/lib/upload.ts` คือกติกากลาง (เพดานขนาด · ชนิดที่รับ · sanitize ชื่อ · กัน path traversal) — ใช้ทั้ง client และ server ห้ามประกาศซ้ำ
|
||||
|
||||
## ลำดับการ์ดหน้าแรก (สลับทุกวัน)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { IconPlus, IconTrash } from "@tabler/icons-react";
|
||||
|
||||
import type { ContactChannel } from "@/lib/cinemas";
|
||||
import { resolveContact } from "@/lib/contact-link";
|
||||
|
||||
/**
|
||||
* แก้ช่องทางติดต่อของการ์ด "สอบถามข้อมูลและจองตั๋ว" ต่อโรง
|
||||
@@ -27,7 +28,12 @@ type Props = {
|
||||
labelClass: string;
|
||||
};
|
||||
|
||||
export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: Props) {
|
||||
export function ContactsEditor({
|
||||
contacts,
|
||||
onChange,
|
||||
inputClass,
|
||||
labelClass,
|
||||
}: Props) {
|
||||
function updateAt(index: number, patch: Partial<ContactChannel>) {
|
||||
onChange(
|
||||
contacts.map((item, i) => (i === index ? { ...item, ...patch } : item)),
|
||||
@@ -56,7 +62,9 @@ export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: P
|
||||
value={contact.label}
|
||||
placeholder="Website"
|
||||
list="contact-label-options"
|
||||
onChange={(event) => updateAt(index, { label: event.target.value })}
|
||||
onChange={(event) =>
|
||||
updateAt(index, { label: event.target.value })
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label className={labelClass}>
|
||||
@@ -65,20 +73,32 @@ export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: P
|
||||
className={inputClass}
|
||||
value={contact.value}
|
||||
placeholder="www.housesamyan.com"
|
||||
onChange={(event) => updateAt(index, { value: event.target.value })}
|
||||
onChange={(event) =>
|
||||
updateAt(index, { value: event.target.value })
|
||||
}
|
||||
/>
|
||||
</label>
|
||||
<label className={labelClass}>
|
||||
ลิงก์ (ไม่บังคับ)
|
||||
ลิงก์ (ไม่บังคับ — ระบบเติมให้อัตโนมัติ)
|
||||
<input
|
||||
className={inputClass}
|
||||
value={contact.href ?? ""}
|
||||
placeholder="https://… หรือ tel:+66…"
|
||||
onChange={(event) =>
|
||||
updateAt(index, {
|
||||
href: event.target.value.trim() === "" ? null : event.target.value,
|
||||
href:
|
||||
event.target.value.trim() === ""
|
||||
? null
|
||||
: event.target.value,
|
||||
})
|
||||
}
|
||||
onBlur={() => {
|
||||
// จัดรูปแบบลิงก์ให้กดใช้ได้จริง (tel:/mailto:/https:) ทันทีที่ออกจากช่อง
|
||||
const resolved = resolveContact(contact);
|
||||
if (resolved.href !== (contact.href ?? null)) {
|
||||
updateAt(index, { href: resolved.href });
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</label>
|
||||
<div className="flex items-end">
|
||||
@@ -91,6 +111,23 @@ export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: P
|
||||
<IconTrash size={14} />
|
||||
</button>
|
||||
</div>
|
||||
<span className="text-[11px] text-[#777] sm:col-span-4">
|
||||
{(() => {
|
||||
const resolved = resolveContact(contact);
|
||||
if (!resolved.href) {
|
||||
return contact.value.trim() === ""
|
||||
? "ใส่ข้อความหรือเบอร์ก่อน — ระบบจะสร้างลิงก์ให้"
|
||||
: "⚠ ค่านี้ยังใช้เป็นลิงก์ไม่ได้ (ต้องเป็นเบอร์ / อีเมล / โดเมน / URL)";
|
||||
}
|
||||
const label =
|
||||
resolved.kind === "tel"
|
||||
? "กดโทรได้"
|
||||
: resolved.kind === "mailto"
|
||||
? "เปิดอีเมล"
|
||||
: "เปิดเว็บ (แท็บใหม่)";
|
||||
return `✓ ${label} → ${resolved.href}`;
|
||||
})()}
|
||||
</span>
|
||||
</div>
|
||||
))}
|
||||
|
||||
@@ -103,7 +140,9 @@ export function ContactsEditor({ contacts, onChange, inputClass, labelClass }: P
|
||||
<button
|
||||
type="button"
|
||||
className={`${smallButtonClass} w-fit`}
|
||||
onClick={() => onChange([...contacts, { label: "", value: "", href: null }])}
|
||||
onClick={() =>
|
||||
onChange([...contacts, { label: "", value: "", href: null }])
|
||||
}
|
||||
>
|
||||
<IconPlus size={14} />
|
||||
เพิ่มช่องทางติดต่อ
|
||||
|
||||
@@ -1,27 +1,102 @@
|
||||
import { IconCircleCheck, IconExternalLink } from "@tabler/icons-react";
|
||||
import {
|
||||
IconBrandFacebook,
|
||||
IconBrandInstagram,
|
||||
IconBrandLine,
|
||||
IconCircleCheck,
|
||||
IconExternalLink,
|
||||
IconMail,
|
||||
IconPhone,
|
||||
IconTicket,
|
||||
IconWorld,
|
||||
} from "@tabler/icons-react";
|
||||
|
||||
import type { Cinema, ContactChannel } from "@/lib/cinemas";
|
||||
import { resolveContact, type ContactKind } from "@/lib/contact-link";
|
||||
import { COLORS } from "@/lib/design-tokens";
|
||||
import { useI18n } from "@/lib/i18n";
|
||||
import { translate } from "@/lib/i18n/messages";
|
||||
import { COLORS } from "@/lib/design-tokens";
|
||||
|
||||
function isExternal(href: string | null): boolean {
|
||||
return href !== null && href.startsWith("http");
|
||||
/** ไอคอนตามชนิดช่องทาง (เดาจาก label ก่อน แล้วค่อยดูจาก kind/URL) */
|
||||
function ChannelIcon({
|
||||
label,
|
||||
kind,
|
||||
href,
|
||||
}: {
|
||||
label: string;
|
||||
kind: ContactKind | null;
|
||||
href: string | null;
|
||||
}) {
|
||||
const text = label.toLowerCase();
|
||||
const url = (href ?? "").toLowerCase();
|
||||
|
||||
if (kind === "tel") return <IconPhone size={16} aria-hidden="true" />;
|
||||
if (kind === "mailto") return <IconMail size={16} aria-hidden="true" />;
|
||||
if (url.includes("facebook") || text.includes("facebook")) {
|
||||
return <IconBrandFacebook size={16} aria-hidden="true" />;
|
||||
}
|
||||
if (url.includes("instagram") || text.includes("instagram")) {
|
||||
return <IconBrandInstagram size={16} aria-hidden="true" />;
|
||||
}
|
||||
if (url.includes("line.me") || text.includes("line")) {
|
||||
return <IconBrandLine size={16} aria-hidden="true" />;
|
||||
}
|
||||
if (
|
||||
text.includes("จอง") ||
|
||||
text.includes("book") ||
|
||||
text.includes("ticket")
|
||||
) {
|
||||
return <IconTicket size={16} aria-hidden="true" />;
|
||||
}
|
||||
return <IconWorld size={16} aria-hidden="true" />;
|
||||
}
|
||||
|
||||
function ContactRow({ channel }: { channel: ContactChannel }) {
|
||||
const resolved = resolveContact(channel);
|
||||
const value =
|
||||
channel.value.trim() === "" ? (channel.href ?? "") : channel.value;
|
||||
|
||||
const body = (
|
||||
<>
|
||||
<span className="flex min-w-0 items-center gap-2">
|
||||
<span className="text-[#999]">
|
||||
<ChannelIcon
|
||||
label={channel.label}
|
||||
kind={resolved.kind}
|
||||
href={resolved.href}
|
||||
/>
|
||||
</span>
|
||||
<span className="truncate">{value}</span>
|
||||
</span>
|
||||
{resolved.external ? (
|
||||
<IconExternalLink
|
||||
size={16}
|
||||
aria-hidden="true"
|
||||
className="shrink-0 text-[#999]"
|
||||
/>
|
||||
) : null}
|
||||
</>
|
||||
);
|
||||
|
||||
const rowClass =
|
||||
"flex min-h-11 items-center justify-between gap-3 rounded-lg text-sm transition-colors";
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-2">
|
||||
<span className="text-sm text-[#999]">{channel.label}</span>
|
||||
<a
|
||||
href={channel.href ?? undefined}
|
||||
target={isExternal(channel.href) ? "_blank" : undefined}
|
||||
rel="noreferrer"
|
||||
className="flex items-center justify-between"
|
||||
>
|
||||
<span>{channel.value}</span>
|
||||
<IconExternalLink size={16} />
|
||||
</a>
|
||||
{resolved.href ? (
|
||||
<a
|
||||
href={resolved.href}
|
||||
{...(resolved.external
|
||||
? { target: "_blank", rel: "noopener noreferrer" }
|
||||
: { rel: "nofollow" })}
|
||||
className={`${rowClass} hover:text-[#D47272] focus-visible:outline focus-visible:outline-2 focus-visible:outline-[#A92626]`}
|
||||
>
|
||||
{body}
|
||||
</a>
|
||||
) : (
|
||||
// ไม่มีข้อมูลที่ใช้เป็นลิงก์ได้ — แสดงเป็นข้อความ ไม่หลอกให้กด
|
||||
<span className={rowClass}>{body}</span>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -37,11 +112,13 @@ export function ContactPanel({ cinema }: { cinema: Cinema }) {
|
||||
>
|
||||
<div className="flex flex-col gap-4">
|
||||
<h2 className="text-2xl font-semibold leading-[1.3]">
|
||||
{(cinema.contact_heading?.trim() || messages.detail.contactTitle)}
|
||||
{cinema.contact_heading?.trim() || messages.detail.contactTitle}
|
||||
</h2>
|
||||
|
||||
{cinema.contacts.length === 0 ? (
|
||||
<p className="text-sm text-[#999]">{messages.detail.contactsFallback}</p>
|
||||
<p className="text-sm text-[#999]">
|
||||
{messages.detail.contactsFallback}
|
||||
</p>
|
||||
) : (
|
||||
cinema.contacts.map((channel) => (
|
||||
<ContactRow key={channel.label} channel={channel} />
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
/**
|
||||
* ทดสอบการจับคู่ลิงก์ของช่องทางติดต่อ — เคสที่พังจริงบนหน้าเว็บ
|
||||
* (เบอร์กดไม่ได้, facebook ติด fbclid, `www.…` ไม่มี scheme, LINE ID)
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
contactHref,
|
||||
looksLikePhone,
|
||||
normalizePhone,
|
||||
resolveContact,
|
||||
} from "@/lib/contact-link";
|
||||
|
||||
describe("normalizePhone", () => {
|
||||
it("แปลงเบอร์ไทย 0 นำหน้าเป็น +66 และตัดขีด/ช่องว่าง", () => {
|
||||
expect(normalizePhone("098-869-0997")).toBe("+66988690997");
|
||||
expect(normalizePhone("098 869 0997")).toBe("+66988690997");
|
||||
expect(normalizePhone("(02) 123-4567")).toBe("+6621234567");
|
||||
});
|
||||
|
||||
it("รองรับที่ขึ้นต้น 66 หรือ +66 อยู่แล้ว", () => {
|
||||
expect(normalizePhone("66812345678")).toBe("+66812345678");
|
||||
expect(normalizePhone("+66 81 234 5678")).toBe("+66812345678");
|
||||
});
|
||||
|
||||
it("ไม่ใช่เบอร์ → null", () => {
|
||||
expect(normalizePhone("12")).toBeNull();
|
||||
expect(normalizePhone("housesamyan")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("looksLikePhone", () => {
|
||||
it("แยกเบอร์ออกจากข้อความ", () => {
|
||||
expect(looksLikePhone("098-869-0997")).toBe(true);
|
||||
expect(looksLikePhone("www.housesamyan.com")).toBe(false);
|
||||
expect(looksLikePhone("House Samyan")).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveContact", () => {
|
||||
it("Call + เบอร์ → tel: (กดโทรได้จริง)", () => {
|
||||
const resolved = resolveContact({
|
||||
label: "Call",
|
||||
value: "098-869-0997",
|
||||
href: null,
|
||||
});
|
||||
expect(resolved.href).toBe("tel:+66988690997");
|
||||
expect(resolved.kind).toBe("tel");
|
||||
expect(resolved.external).toBe(false);
|
||||
});
|
||||
|
||||
it("เบอร์ที่ href ผิดรูปแบบ → แก้ให้ถูก", () => {
|
||||
expect(
|
||||
contactHref({ label: "Call", value: "098-869-0997", href: "0988690997" }),
|
||||
).toBe("tel:+66988690997");
|
||||
expect(
|
||||
contactHref({
|
||||
label: "Call",
|
||||
value: "098-869-0997",
|
||||
href: "tel:0988690997",
|
||||
}),
|
||||
).toBe("tel:+66988690997");
|
||||
});
|
||||
|
||||
it("Facebook → https + ตัด fbclid/_rdc + เปลี่ยน web. เป็น www.", () => {
|
||||
const resolved = resolveContact({
|
||||
label: "Facebook",
|
||||
value: "Century Sukhumvit",
|
||||
href: "https://web.facebook.com/CenturySukhumvit/?_rdc=1&_rdr",
|
||||
});
|
||||
expect(resolved.kind).toBe("web");
|
||||
expect(resolved.external).toBe(true);
|
||||
expect(resolved.href).toBe("https://www.facebook.com/CenturySukhumvit/");
|
||||
});
|
||||
|
||||
it("เว็บที่ไม่มี scheme → เติม https", () => {
|
||||
expect(
|
||||
contactHref({
|
||||
label: "Website",
|
||||
value: "www.housesamyan.com",
|
||||
href: null,
|
||||
}),
|
||||
).toBe("https://www.housesamyan.com/");
|
||||
});
|
||||
|
||||
it("Instagram ที่ติด fbclid → ตัดออก", () => {
|
||||
const href = contactHref({
|
||||
label: "Instagram",
|
||||
value: "centurysukhumvit",
|
||||
href: "https://www.instagram.com/centurysukhumvit?fbclid=abc123",
|
||||
});
|
||||
expect(href).toBe("https://www.instagram.com/centurysukhumvit");
|
||||
});
|
||||
|
||||
it("อีเมล → mailto:", () => {
|
||||
expect(
|
||||
resolveContact({ label: "อีเมล", value: "[email protected]" }).href,
|
||||
).toBe("mailto:[email protected]");
|
||||
});
|
||||
|
||||
it("LINE ID (@id) → line.me", () => {
|
||||
expect(resolveContact({ label: "Line", value: "@alterflix" }).href).toBe(
|
||||
"https://line.me/R/ti/p/~alterflix",
|
||||
);
|
||||
});
|
||||
|
||||
it("ค่าไม่มีข้อมูล/ว่าง → null (ไม่สร้างลิงก์หลอก)", () => {
|
||||
expect(resolveContact({ label: "Call", value: "-" }).href).toBeNull();
|
||||
expect(resolveContact({ label: "Call", value: "" }).href).toBeNull();
|
||||
expect(
|
||||
resolveContact({ label: "Website", value: "ไม่มีข้อมูล" }).href,
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it("ค่าแปลก ๆ ที่ไม่ใช่ทั้งเบอร์และโดเมน → null", () => {
|
||||
expect(
|
||||
resolveContact({ label: "Call", value: "โทรสอบถาม" }).href,
|
||||
).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,194 @@
|
||||
/**
|
||||
* แปลง "ช่องทางติดต่อ" ของโรงหนังให้เป็นลิงก์ที่กดใช้ได้จริง
|
||||
*
|
||||
* ปัญหาเดิม: ข้อมูลเก็บ `href` ดิบจากหลังบ้าน (บางทีว่าง, บางทีเป็น `www.…` ไม่มี scheme,
|
||||
* เบอร์โทรไม่ขึ้นต้นด้วย `tel:`) → หน้าเว็บได้ `<a>` ที่กดแล้วไม่เกิดอะไร
|
||||
*
|
||||
* กติกาที่นี่ (ใช้ทั้งตอนบันทึกในหลังบ้านและตอนแสดงผล):
|
||||
* - เบอร์โทร → `tel:+66…` (แปลง 0 นำหน้าเป็น +66, ตัดขีด/วงเล็บ/ช่องว่าง)
|
||||
* - อีเมล → `mailto:`
|
||||
* - LINE ID (`@abc`) → `https://line.me/R/ti/p/~abc`
|
||||
* - เว็บ/โซเชียล → บังคับ `https://`, แปลง `web.facebook.com` → `www.facebook.com`
|
||||
* และตัดพารามิเตอร์ติดตาม (fbclid, _rdc, utm_*, igshid)
|
||||
*/
|
||||
|
||||
export type ContactKind = "tel" | "mailto" | "web";
|
||||
|
||||
export type ResolvedContact = {
|
||||
/** href พร้อมใช้ — null = ไม่มีข้อมูลที่ใช้เป็นลิงก์ได้ */
|
||||
href: string | null;
|
||||
kind: ContactKind | null;
|
||||
/** true = ลิงก์ออกเว็บนอก (เปิดแท็บใหม่) */
|
||||
external: boolean;
|
||||
};
|
||||
|
||||
const EMPTY_VALUES = new Set([
|
||||
"-",
|
||||
"–",
|
||||
"—",
|
||||
"n/a",
|
||||
"na",
|
||||
"null",
|
||||
"undefined",
|
||||
"ไม่มี",
|
||||
"ไม่มีข้อมูล",
|
||||
]);
|
||||
|
||||
const TRACKING_PARAMS = [
|
||||
"fbclid",
|
||||
"gclid",
|
||||
"_rdc",
|
||||
"_rdr",
|
||||
"igshid",
|
||||
"ref",
|
||||
"ref_src",
|
||||
"mibextid",
|
||||
];
|
||||
|
||||
function isTrackingParam(name: string): boolean {
|
||||
const key = name.toLowerCase();
|
||||
return TRACKING_PARAMS.includes(key) || key.startsWith("utm_");
|
||||
}
|
||||
|
||||
/** แปลงเบอร์เป็นรูปแบบที่กดโทรได้: `+66812345678` (คืน null ถ้าไม่ใช่เบอร์ที่ใช้ได้) */
|
||||
export function normalizePhone(raw: string): string | null {
|
||||
const trimmed = raw.trim();
|
||||
if (trimmed === "") return null;
|
||||
|
||||
const hasPlus = trimmed.startsWith("+");
|
||||
const digits = trimmed.replace(/[^\d]/g, "");
|
||||
if (digits.length < 8 || digits.length > 15) return null;
|
||||
|
||||
if (hasPlus) return `+${digits}`;
|
||||
if (digits.startsWith("66")) return `+${digits}`;
|
||||
if (digits.startsWith("0")) return `+66${digits.slice(1)}`;
|
||||
return `+${digits}`;
|
||||
}
|
||||
|
||||
/** ดูว่าข้อความนี้ "หน้าตาเป็นเบอร์โทร" ไหม (ใช้ตัดสินก่อนถือว่าเป็นเว็บ) */
|
||||
export function looksLikePhone(raw: string): boolean {
|
||||
const trimmed = raw.trim();
|
||||
if (trimmed === "") return false;
|
||||
if (/[a-z@]/i.test(trimmed)) return false;
|
||||
const digits = trimmed.replace(/\D/g, "");
|
||||
return digits.length >= 8 && digits.length <= 15;
|
||||
}
|
||||
|
||||
function stripTracking(url: URL): URL {
|
||||
const keep = new URLSearchParams();
|
||||
url.searchParams.forEach((value, key) => {
|
||||
if (!isTrackingParam(key)) keep.append(key, value);
|
||||
});
|
||||
const query = keep.toString();
|
||||
url.search = query === "" ? "" : `?${query}`;
|
||||
return url;
|
||||
}
|
||||
|
||||
function normalizeWebUrl(raw: string): string | null {
|
||||
let candidate = raw.trim();
|
||||
if (candidate === "") return null;
|
||||
|
||||
// LINE ID แบบ @id
|
||||
if (/^@[\w.\-_]+$/.test(candidate)) {
|
||||
return `https://line.me/R/ti/p/~${candidate.slice(1)}`;
|
||||
}
|
||||
if (/^line:/i.test(candidate)) {
|
||||
candidate = candidate.replace(/^line:\/\//i, "https://line.me/R/ti/p/~");
|
||||
}
|
||||
|
||||
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(candidate)) {
|
||||
// `www.x.com` / `x.com/path` / `x.com` → https
|
||||
if (!/^[\w-]+(\.[\w-]+)+/.test(candidate)) return null;
|
||||
candidate = `https://${candidate}`;
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(candidate);
|
||||
if (url.protocol === "http:") url.protocol = "https:";
|
||||
if (url.hostname === "web.facebook.com") url.hostname = "www.facebook.com";
|
||||
url.hostname = url.hostname.toLowerCase();
|
||||
return stripTracking(url).toString();
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function looksLikeEmail(raw: string): boolean {
|
||||
return /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(raw.trim());
|
||||
}
|
||||
|
||||
/**
|
||||
* แปลงช่องทางติดต่อหนึ่งรายการ → ลิงก์ที่ใช้ได้
|
||||
* ลำดับความเชื่อ: `href` ที่ผู้ใช้กรอก → ลักษณะของ `value` → label
|
||||
*/
|
||||
export function resolveContact(channel: {
|
||||
label?: string;
|
||||
value?: string;
|
||||
href?: string | null;
|
||||
}): ResolvedContact {
|
||||
const value = (channel.value ?? "").trim();
|
||||
const href = (channel.href ?? "").trim();
|
||||
const label = (channel.label ?? "").trim().toLowerCase();
|
||||
|
||||
const candidates = [href, value].filter(
|
||||
(item) => item !== "" && !EMPTY_VALUES.has(item.toLowerCase()),
|
||||
);
|
||||
if (candidates.length === 0)
|
||||
return { href: null, kind: null, external: false };
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const lower = candidate.toLowerCase();
|
||||
|
||||
// 1) เบอร์โทรที่ระบุชัด
|
||||
if (lower.startsWith("tel:")) {
|
||||
const phone = normalizePhone(candidate.slice(4));
|
||||
if (phone) return { href: `tel:${phone}`, kind: "tel", external: false };
|
||||
}
|
||||
|
||||
// 2) อีเมล
|
||||
if (lower.startsWith("mailto:")) {
|
||||
const address = candidate.slice(7).trim();
|
||||
if (looksLikeEmail(address)) {
|
||||
return { href: `mailto:${address}`, kind: "mailto", external: false };
|
||||
}
|
||||
}
|
||||
if (looksLikeEmail(candidate)) {
|
||||
return { href: `mailto:${candidate}`, kind: "mailto", external: false };
|
||||
}
|
||||
|
||||
// 3) ลิงก์เว็บ/โซเชียล (ตัดสินจาก scheme หรือรูปทรงโดเมน)
|
||||
if (
|
||||
/^https?:\/\//i.test(candidate) ||
|
||||
/^line:/i.test(candidate) ||
|
||||
/^@[\w.\-_]+$/.test(candidate) ||
|
||||
/^[\w-]+(\.[\w-]+)+(\/|$)/.test(candidate)
|
||||
) {
|
||||
const url = normalizeWebUrl(candidate);
|
||||
if (url) return { href: url, kind: "web", external: true };
|
||||
}
|
||||
|
||||
// 4) เบอร์โทรที่ไม่ได้ใส่ tel:
|
||||
if (
|
||||
looksLikePhone(candidate) &&
|
||||
/call|โทร|phone|tel|เบอร์|มือถือ/.test(label + " " + value)
|
||||
) {
|
||||
const phone = normalizePhone(candidate);
|
||||
if (phone) return { href: `tel:${phone}`, kind: "tel", external: false };
|
||||
}
|
||||
if (looksLikePhone(candidate)) {
|
||||
const phone = normalizePhone(candidate);
|
||||
if (phone) return { href: `tel:${phone}`, kind: "tel", external: false };
|
||||
}
|
||||
}
|
||||
|
||||
return { href: null, kind: null, external: false };
|
||||
}
|
||||
|
||||
/** ค่าที่ควรบันทึกกลับลง `href` (ใช้ตอน sanitize/หลังบ้าน) */
|
||||
export function contactHref(channel: {
|
||||
label?: string;
|
||||
value?: string;
|
||||
href?: string | null;
|
||||
}): string | null {
|
||||
return resolveContact(channel).href;
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import path from "node:path";
|
||||
|
||||
import { sanitizeArticleBlocks } from "@/lib/article";
|
||||
import type { Cinema } from "@/lib/cinemas";
|
||||
import { contactHref } from "@/lib/contact-link";
|
||||
import {
|
||||
seedAbout,
|
||||
seedContent,
|
||||
@@ -126,10 +127,14 @@ export function sanitizeCinemas(input: unknown): Cinema[] {
|
||||
contacts: Array.isArray(raw.contacts)
|
||||
? raw.contacts.map((contact) => {
|
||||
const entry = (contact ?? {}) as Record<string, unknown>;
|
||||
const label = asString(entry.label);
|
||||
const value = asString(entry.value);
|
||||
const href = typeof entry.href === "string" ? entry.href : null;
|
||||
// บังคับ href ให้เป็นลิงก์ที่กดใช้ได้จริง (tel:/mailto:/https:) ทุกครั้งที่บันทึก
|
||||
return {
|
||||
label: asString(entry.label),
|
||||
value: asString(entry.value),
|
||||
href: typeof entry.href === "string" ? entry.href : null,
|
||||
label,
|
||||
value,
|
||||
href: contactHref({ label, value, href }),
|
||||
};
|
||||
})
|
||||
: [],
|
||||
|
||||
+15
-1
@@ -97,7 +97,21 @@
|
||||
| หัวข้อการ์ด | "หัวข้อ 'สอบถามข้อมูลและจองตั๋ว' (เฉพาะโรงนี้)" — ว่าง = ใช้ค่ากลาง |
|
||||
| Website / Facebook / Call | แถว **ช่องทางติดต่อ** → ป้ายชื่อ / ข้อความที่แสดง / ลิงก์ |
|
||||
| เพิ่มช่องทางใหม่ | **+ เพิ่มช่องทางติดต่อ** (ป้ายชื่อมีตัวเลือกแนะนำ) |
|
||||
| เบอร์โทรให้กดโทรได้ | ลิงก์ใส่ `tel:+668…` |
|
||||
| เบอร์โทรให้กดโทรได้ | ไม่ต้องพิมพ์ `tel:` เอง — ใส่เบอร์ในช่อง **ข้อความที่แสดง** ระบบเติมลิงก์ให้ |
|
||||
|
||||
**ระบบสร้างลิงก์ให้อัตโนมัติ** (เห็นข้อความ `✓ กดโทรได้ → tel:+66…` ใต้แถวนั้น)
|
||||
|
||||
| กรอก | ได้ลิงก์ |
|
||||
|---|---|
|
||||
| `098-869-0997` หรือ `0 9886 90997` | `tel:+66988690997` (แปลง 0 → +66 ให้) |
|
||||
| `[email protected]` | `mailto:[email protected]` |
|
||||
| `www.housesamyan.com` / `lidoconnect.com` | `https://…` (เปิดแท็บใหม่) |
|
||||
| `https://web.facebook.com/x/?_rdc=1&_rdr` | `https://www.facebook.com/x/` (ตัด `fbclid`, `_rdc`, `utm_*` ให้) |
|
||||
| `@lineid` | `https://line.me/R/ti/p/~lineid` |
|
||||
|
||||
- ถ้าขึ้น **⚠ ค่านี้ยังใช้เป็นลิงก์ไม่ได้** = ข้อความนั้นไม่ใช่เบอร์/อีเมล/โดเมน → แก้ค่าให้เป็นอย่างใดอย่างหนึ่ง
|
||||
- แถวที่ไม่มีลิงก์จะแสดงเป็น **ข้อความธรรมดา** (ไม่หลอกให้กด)
|
||||
- ตรวจ/ซ่อมทั้งไฟล์พร้อมกัน: `node scripts/audit-contacts.mjs` (ดู) · `node scripts/audit-contacts.mjs --fix` (เขียนกลับ + สำรองไฟล์)
|
||||
|
||||
ค่ากลางของหัวข้อ (มีผลทุกโรงที่ไม่ได้กรอก) → แท็บ **คำศัพท์หลายภาษา** → `detail.contactTitle`
|
||||
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@
|
||||
| GET | `/api/admin/session` | — | ตรวจว่ายังล็อกอินอยู่ |
|
||||
| GET | `/api/admin/content` | — | อ่าน `ContentStore` ทั้งก้อน (`cinemas`, `site`, `about`, `forCinemas`, `forFilmLovers`) |
|
||||
| POST | `/api/admin/uploads` | `multipart/form-data`: `file` (บังคับ, ≤ 8 MB), `slug` (ไม่บังคับ) | อัปโหลดรูปโรงหนัง — ตรวจชนิดจริงจาก magic bytes (JPEG/PNG/WebP/AVIF) เก็บที่ `data/uploads/cinemas/` คืน `{ url, bytes, type }` โดย `url` = `/uploads/cinemas/<ไฟล์>` |
|
||||
| PUT | `/api/admin/content` | `{ cinemas?, site?, about?, forCinemas?, forFilmLovers? }` | บันทึกเฉพาะส่วนที่ส่งมา — ผ่าน `sanitizeCinemas` / `sanitizeSite` / `sanitizeAbout` / `sanitizeForCinemas` / `sanitizeForFilmLovers` แล้วเขียนไฟล์แบบ atomic |
|
||||
| PUT | `/api/admin/content` | `{ cinemas?, site?, about?, forCinemas?, forFilmLovers? }` | บันทึกเฉพาะส่วนที่ส่งมา — ผ่าน `sanitizeCinemas` / `sanitizeSite` / `sanitizeAbout` / `sanitizeForCinemas` / `sanitizeForFilmLovers` แล้วเขียนไฟล์แบบ atomic · **`contacts[].href` ถูกคำนวณใหม่ให้กดใช้ได้จริง (tel:/mailto:/https:) ทุกครั้ง** |
|
||||
| POST | `/api/admin/cinemas/import` | `{ cinemas: Cinema[] }` หรือรายการเดียว | นำเข้าโรงหนังโดยใช้ slug เป็นกุญแจ (มีอยู่ = ทับ) |
|
||||
| GET | `/api/admin/analytics` | `?days=1\|7\|30\|90` | สรุปสถิติ (`summarize()`) — คำค้นยอดนิยม, คำค้นไม่พบผล, CTR, โรงที่ถูกเปิด/คลิก |
|
||||
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
/**
|
||||
* ตรวจ/ซ่อม `href` ของช่องทางติดต่อใน content.json ให้กดใช้ได้จริง
|
||||
*
|
||||
* ปัญหาที่เจอ: href ว่าง, เป็น `www.…` ไม่มี scheme, เบอร์ไม่มี `tel:`,
|
||||
* facebook ติด `fbclid`, หรือ label "Call" แต่ค่าไม่ใช่เบอร์
|
||||
*
|
||||
* ใช้: node scripts/audit-contacts.mjs # ดูอย่างเดียว
|
||||
* node scripts/audit-contacts.mjs --fix # เขียน href ที่แก้ได้กลับลงไฟล์ (สำรองก่อน)
|
||||
*/
|
||||
import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
const CONTENT_PATH = path.join("data", "content", "content.json");
|
||||
const FIX = process.argv.includes("--fix");
|
||||
|
||||
const PHONE_LABEL = /call|โทร|phone|tel|เบอร์|มือถือ/i;
|
||||
|
||||
function emptyish(value) {
|
||||
const text = String(value ?? "")
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
return (
|
||||
text === "" || ["-", "–", "n/a", "null", "ไม่มี", "ไม่มีข้อมูล"].includes(text)
|
||||
);
|
||||
}
|
||||
|
||||
function normalizePhone(raw) {
|
||||
const text = String(raw ?? "").trim();
|
||||
if (text === "") return null;
|
||||
const hasPlus = text.startsWith("+");
|
||||
const digits = text.replace(/[^\d]/g, "");
|
||||
if (digits.length < 8 || digits.length > 15) return null;
|
||||
if (hasPlus) return `+${digits}`;
|
||||
if (digits.startsWith("66")) return `+${digits}`;
|
||||
if (digits.startsWith("0")) return `+66${digits.slice(1)}`;
|
||||
return `+${digits}`;
|
||||
}
|
||||
|
||||
/** เหมือน app/lib/contact-link.ts (สคริปต์เป็น .mjs จึงต้องพอร์ตกฎมาใช้เอง) */
|
||||
function resolve(channel) {
|
||||
const value = String(channel.value ?? "").trim();
|
||||
const href = String(channel.href ?? "").trim();
|
||||
const label = String(channel.label ?? "").toLowerCase();
|
||||
const candidates = [href, value].filter((item) => !emptyish(item));
|
||||
if (candidates.length === 0) return { href: null, kind: null };
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const lower = candidate.toLowerCase();
|
||||
if (lower.startsWith("tel:")) {
|
||||
const phone = normalizePhone(candidate.slice(4));
|
||||
if (phone) return { href: `tel:${phone}`, kind: "tel" };
|
||||
}
|
||||
if (lower.startsWith("mailto:")) {
|
||||
return { href: `mailto:${candidate.slice(7).trim()}`, kind: "mailto" };
|
||||
}
|
||||
if (/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/.test(candidate)) {
|
||||
return { href: `mailto:${candidate}`, kind: "mailto" };
|
||||
}
|
||||
if (
|
||||
/^https?:\/\//i.test(candidate) ||
|
||||
/^line:/i.test(candidate) ||
|
||||
/^@[\w.\-_]+$/.test(candidate) ||
|
||||
/^[\w-]+(\.[\w-]+)+(\/|$)/.test(candidate)
|
||||
) {
|
||||
let url = candidate;
|
||||
if (/^@[\w.\-_]+$/.test(url))
|
||||
url = `https://line.me/R/ti/p/~${url.slice(1)}`;
|
||||
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(url)) url = `https://${url}`;
|
||||
try {
|
||||
const parsed = new URL(url);
|
||||
if (parsed.protocol === "http:") parsed.protocol = "https:";
|
||||
if (parsed.hostname === "web.facebook.com")
|
||||
parsed.hostname = "www.facebook.com";
|
||||
for (const key of [...parsed.searchParams.keys()]) {
|
||||
if (
|
||||
[
|
||||
"fbclid",
|
||||
"gclid",
|
||||
"_rdc",
|
||||
"_rdr",
|
||||
"igshid",
|
||||
"mibextid",
|
||||
"ref",
|
||||
].includes(key.toLowerCase()) ||
|
||||
key.toLowerCase().startsWith("utm_")
|
||||
) {
|
||||
parsed.searchParams.delete(key);
|
||||
}
|
||||
}
|
||||
return { href: parsed.toString(), kind: "web" };
|
||||
} catch {
|
||||
return { href: null, kind: null };
|
||||
}
|
||||
}
|
||||
const digits = candidate.replace(/\D/g, "");
|
||||
if (
|
||||
digits.length >= 8 &&
|
||||
digits.length <= 15 &&
|
||||
!/[a-z@]/i.test(candidate)
|
||||
) {
|
||||
const phone = normalizePhone(candidate);
|
||||
if (phone) return { href: `tel:${phone}`, kind: "tel" };
|
||||
}
|
||||
}
|
||||
return { href: null, kind: null };
|
||||
}
|
||||
|
||||
const raw = fs.readFileSync(CONTENT_PATH, "utf8");
|
||||
const content = JSON.parse(raw);
|
||||
|
||||
const rows = [];
|
||||
const phoneOwners = new Map();
|
||||
let changed = 0;
|
||||
|
||||
for (const cinema of content.cinemas ?? []) {
|
||||
const contacts = cinema.contacts ?? [];
|
||||
if (contacts.length === 0) {
|
||||
rows.push({
|
||||
slug: cinema.slug,
|
||||
label: "—",
|
||||
value: "—",
|
||||
href: null,
|
||||
kind: null,
|
||||
note: "ไม่มีช่องทางติดต่อ",
|
||||
});
|
||||
continue;
|
||||
}
|
||||
for (const channel of contacts) {
|
||||
const resolved = resolve(channel);
|
||||
const notes = [];
|
||||
if (!resolved.href) notes.push("ลิงก์ใช้ไม่ได้");
|
||||
if (PHONE_LABEL.test(channel.label ?? "") && resolved.kind !== "tel") {
|
||||
notes.push("label โทร แต่ค่าไม่ใช่เบอร์");
|
||||
}
|
||||
if (resolved.kind === "tel") {
|
||||
const owner = phoneOwners.get(resolved.href);
|
||||
if (owner && owner !== cinema.slug) notes.push(`เบอร์ซ้ำกับ ${owner}`);
|
||||
else phoneOwners.set(resolved.href, cinema.slug);
|
||||
}
|
||||
if (channel.href && channel.href !== resolved.href)
|
||||
notes.push("href จะถูกปรับ");
|
||||
rows.push({
|
||||
slug: cinema.slug,
|
||||
label: channel.label,
|
||||
value: channel.value,
|
||||
href: resolved.href,
|
||||
kind: resolved.kind,
|
||||
note: notes.join(", "),
|
||||
});
|
||||
|
||||
if (FIX && resolved.href !== channel.href) {
|
||||
channel.href = resolved.href;
|
||||
changed++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const row of rows) {
|
||||
const state = row.href ? row.kind.padEnd(6) : "ไม่มี ";
|
||||
console.log(
|
||||
`${state} ${row.slug} · ${row.label} · ${String(row.value).slice(0, 42)}${row.note ? ` ⚠ ${row.note}` : ""}`,
|
||||
);
|
||||
if (row.href && row.href !== row.value) console.log(` → ${row.href}`);
|
||||
}
|
||||
|
||||
const withoutContacts = rows.filter((row) => row.label === "—").length;
|
||||
console.log(
|
||||
`\nสรุป: ${rows.length - withoutContacts} ช่องทางจาก ${(content.cinemas ?? []).length - withoutContacts} โรง · ไม่มีช่องทางติดต่อ ${withoutContacts} โรง`,
|
||||
);
|
||||
|
||||
if (FIX) {
|
||||
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
|
||||
fs.writeFileSync(`${CONTENT_PATH}.bak-contacts-${stamp}`, raw);
|
||||
fs.writeFileSync(CONTENT_PATH, `${JSON.stringify(content, null, 2)}\n`);
|
||||
console.log(
|
||||
`ปรับ href ${changed} จุด · สำรองไว้ที่ ${CONTENT_PATH}.bak-contacts-${stamp}`,
|
||||
);
|
||||
} else {
|
||||
console.log("ยังไม่เขียนไฟล์ — ใส่ --fix เพื่อบันทึก href ที่ปรับแล้ว");
|
||||
}
|
||||
Reference in new issue
Block a user