diff --git a/.gitignore b/.gitignore index bb3b438..c5f1ac5 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,11 @@ coverage/ .DS_Store apps/miniapp/miniprogram_npm/ apps/miniapp/project.private.config.json + +.worktrees/ +apps/taro/src/assets/ + +apps/taro/test-results/ +apps/taro/playwright-report/ + +**/.swc/ diff --git a/.prettierignore b/.prettierignore index fcc23c5..a3e7e18 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,3 +4,5 @@ dist .next prototype pnpm-lock.yaml + +.codex-tasks/**/raw/ diff --git a/apps/taro/.env.example b/apps/taro/.env.example new file mode 100644 index 0000000..06217d3 --- /dev/null +++ b/apps/taro/.env.example @@ -0,0 +1,5 @@ +# 构建时确定模板,品牌可在 Web 中通过 ?brand=linjing-doors 选择。 +TEMPLATE_KEY=acheng-campus-food +BRAND_SLUG=default-brand +API_BASE_URL=http://localhost:3001 +PREVIEW_PORT=3002 diff --git a/apps/taro/README.md b/apps/taro/README.md new file mode 100644 index 0000000..e1c2ae1 --- /dev/null +++ b/apps/taro/README.md @@ -0,0 +1,59 @@ +# Taro 模板双端工程 + +本目录在独立迁移分支提供 Web 和微信小程序共用的 React 页面。后台、Prisma 和发布 API 不变;当前原生模板构建仍保留。 + +## 启动 Web + +在仓库根目录执行: + +```sh +pnpm install +pnpm --filter @personal-brand/types build +pnpm --filter @personal-brand/taro dev:h5 +``` + +默认餐饮模板、default-brand、API http://localhost:3001,Web http://localhost:3002。 + +林境模板: + +```sh +TEMPLATE_KEY=linjing-editorial PREVIEW_PORT=3003 pnpm --filter @personal-brand/taro dev:h5 +``` + +API 地址通过 `API_BASE_URL` 设置,品牌通过 `BRAND_SLUG` 设置。H5 也可在地址查询参数使用 `?brand=linjing-doors`,但必须与构建模板匹配。环境变量通过命令环境传入;`.env.example` 是参数说明,不自动加载真实密钥。 + +## 构建 + +```sh +pnpm --filter @personal-brand/taro build:h5 +pnpm --filter @personal-brand/taro build:weapp +TEMPLATE_KEY=linjing-editorial pnpm --filter @personal-brand/taro build:h5 +TEMPLATE_KEY=linjing-editorial pnpm --filter @personal-brand/taro build:weapp +``` + +输出 `apps/taro/dist//h5` 和 `weapp`。微信开发者工具导入对应 weapp 目录;正式登录需在项目配置里设置与 API 相匹配的 AppID,配置请求合法域名。默认 touristappid 仅用于浏览,不含密钥。 + +页面结构和样式以 React 原型为参考,当前原生小程序用于校对字段、结构化来源、认证与提交行为。模板实现位于各 package 的 `taro/`;公共请求、表单、记录和状态位于此目录。新模板须提供 manifest、Taro 实现和验收用例,不新增全局 seed。 + +## 使用边界 + +Web 支持真实已发布内容、导航、餐品搜索/分类、咨询表单填写和校验。未接入 Web 身份登录,因此没有微信会话时不会提交,不生成假咨询或伪造成功。微信小程序采用 Taro.login 和原 customer/session/wechat 接口。令牌按品牌隔离;请求失败保留表单输入,401 清理会话。 + +Web 与小程序来自同一套源码,H5 排版检查不等同微信原生验收。原生微信登录、拨号、剪贴板及真机排版须在微信环境最终核验。 + +## 验证 + +```sh +pnpm --filter @personal-brand/taro typecheck +pnpm --filter @personal-brand/taro lint +pnpm --filter @personal-brand/taro test +pnpm --filter @personal-brand/taro test:browser +``` + +浏览器测试需要先启动 API 和餐饮 H5 预览。`PREVIEW_URL` 指定预览地址,`CHROMIUM_PATH` 指定本机 Chromium;测试只读取开发库,模拟错误响应用于失败路径,不写入真实数据。 + +## 回滚与切换 + +所有工作在 feat/taro-template-preview 独立 worktree。用户确认前不合并、不切换主工作区、不修改开发数据库或原 dist/miniapp。确认后另行切换默认构建;旧 template:dev 始终可用。 + +Taro 4.3 官方依赖树含部分旧依赖 deprecation 和 punycode 提示,跟进负责人为框架维护者;本项目固定版本并检查双端产物。Admin React 19 与 Taro React 18 在各 workspace 依赖边界独立,避免跨端插件 peer mismatch。 diff --git a/apps/taro/config/index.ts b/apps/taro/config/index.ts new file mode 100644 index 0000000..36224f6 --- /dev/null +++ b/apps/taro/config/index.ts @@ -0,0 +1,67 @@ +import { defineConfig } from "@tarojs/cli"; +import path from "node:path"; +import fs from "node:fs"; + +const root = path.resolve(__dirname, "../../.."); +const key = process.env.TEMPLATE_KEY || "acheng-campus-food"; +const templates = fs + .readdirSync(path.join(root, "packages")) + .filter( + (directory) => + directory.startsWith("template-") && + fs.existsSync(path.join(root, "packages", directory, "taro/index.tsx")) && + fs.existsSync(path.join(root, "packages", directory, "template.json")), + ) + .map((directory) => directory.slice("template-".length)); +if (!templates.includes(key)) throw new Error(`Unsupported Taro template: ${key}`); +const template = path.join(root, "packages", `template-${key}`); +const manifest = JSON.parse(fs.readFileSync(path.join(template, "template.json"), "utf8")) as { + packageKey: string; + defaultBrandSlug?: string; +}; +if (manifest.packageKey !== key) throw new Error(`Template key mismatch: ${key}`); +const brand = process.env.BRAND_SLUG || manifest.defaultBrandSlug || "default-brand"; +fs.cpSync(path.join(template, "miniapp/assets"), path.resolve(__dirname, "../src/assets"), { + recursive: true, +}); +export default defineConfig({ + projectName: "personal-brand-taro", + date: "2026-10-08", + designWidth: 750, + deviceRatio: { 750: 1 }, + sourceRoot: "src", + outputRoot: `dist/${key}/${process.env.TARO_ENV || "weapp"}`, + framework: "react", + compiler: { type: "vite" }, + plugins: [], + alias: { "@template": path.join(template, "taro") }, + defineConstants: { + TEMPLATE_KEY: JSON.stringify(key), + BRAND_SLUG: JSON.stringify(brand), + API_BASE_URL: JSON.stringify(process.env.API_BASE_URL || "http://localhost:3001"), + TEMPLATE_MANIFEST: JSON.stringify(manifest), + }, + copy: { + options: {}, + patterns: [ + ...(process.env.TARO_ENV === "weapp" + ? [ + { + from: path.resolve(__dirname, "../project.config.json"), + to: `dist/${key}/weapp/project.config.json`, + }, + ] + : []), + { + from: path.join(template, "miniapp/assets"), + to: `dist/${key}/${process.env.TARO_ENV || "weapp"}/assets`, + }, + ], + }, + mini: {}, + h5: { + publicPath: "/", + router: { mode: "hash" }, + devServer: { host: "0.0.0.0", port: Number(process.env.PREVIEW_PORT || 3002) }, + }, +}); diff --git a/apps/taro/package.json b/apps/taro/package.json new file mode 100644 index 0000000..cbfd1d6 --- /dev/null +++ b/apps/taro/package.json @@ -0,0 +1,39 @@ +{ + "name": "@personal-brand/taro", + "private": true, + "scripts": { + "dev:h5": "taro build --type h5 --watch", + "build:h5": "taro build --type h5", + "build:weapp": "taro build --type weapp", + "typecheck": "tsc --noEmit", + "lint": "pnpm --workspace-root exec eslint apps/taro/src apps/taro/config packages/template-acheng-campus-food/taro packages/template-linjing-editorial/taro", + "test": "node --experimental-strip-types --test test/*.test.mjs", + "test:browser": "playwright test" + }, + "dependencies": { + "@personal-brand/types": "workspace:*", + "@tarojs/components": "4.3.0", + "@tarojs/helper": "4.3.0", + "@tarojs/plugin-framework-react": "4.3.0", + "@tarojs/plugin-platform-h5": "4.3.0", + "@tarojs/plugin-platform-weapp": "4.3.0", + "@tarojs/react": "4.3.0", + "@tarojs/runtime": "4.3.0", + "@tarojs/shared": "4.3.0", + "@tarojs/taro": "4.3.0", + "react": "18.3.1", + "react-dom": "18.3.1" + }, + "devDependencies": { + "@tarojs/cli": "4.3.0", + "@tarojs/vite-runner": "4.3.0", + "@types/react": "18.3.18", + "@types/react-dom": "18.3.5", + "@vitejs/plugin-react": "4.7.0", + "vite": "4.5.14", + "@types/node": "^22.10.0", + "@babel/plugin-proposal-decorators": "^7.25.9", + "@babel/plugin-transform-class-properties": "^7.25.9", + "@playwright/test": "1.58.2" + } +} diff --git a/apps/taro/playwright.config.ts b/apps/taro/playwright.config.ts new file mode 100644 index 0000000..34fd5b2 --- /dev/null +++ b/apps/taro/playwright.config.ts @@ -0,0 +1,18 @@ +import { defineConfig } from "@playwright/test"; +export default defineConfig({ + testDir: "./test/browser", + timeout: 30000, + use: { + baseURL: process.env.PREVIEW_URL || "http://localhost:3002", + viewport: { width: 375, height: 812 }, + screenshot: "only-on-failure", + launchOptions: { + executablePath: + process.env.CHROMIUM_PATH || + "/home/que01/.cache/ms-playwright/chromium-1228/chrome-linux64/chrome", + args: ["--no-sandbox"], + }, + }, + reporter: [["list"]], + outputDir: "../../.codex-tasks/taro-migration/raw/browser", +}); diff --git a/apps/taro/project.config.json b/apps/taro/project.config.json new file mode 100644 index 0000000..8c83593 --- /dev/null +++ b/apps/taro/project.config.json @@ -0,0 +1,7 @@ +{ + "appid": "touristappid", + "projectname": "personal-brand-taro-preview", + "compileType": "miniprogram", + "setting": { "es6": false, "minified": true, "urlCheck": true }, + "description": "独立迁移分支的 Taro 小程序产物;正式登录需配置实际 AppID。" +} diff --git a/apps/taro/src/app.config.ts b/apps/taro/src/app.config.ts new file mode 100644 index 0000000..7376813 --- /dev/null +++ b/apps/taro/src/app.config.ts @@ -0,0 +1,35 @@ +const routes: Record = { + home: "pages/home/index", + offerings: "pages/offerings/index", + service: "pages/service/index", + profile: "pages/profile/index", + inquiry: "pages/inquiry/index", +}; +export default defineAppConfig({ + pages: [ + "pages/home/index", + "pages/offerings/index", + "pages/service/index", + "pages/profile/index", + "pages/inquiry/index", + "pages/detail/index", + "pages/success/index", + "pages/records/index", + ], + window: { + navigationBarTitleText: TEMPLATE_MANIFEST.displayName, + navigationBarBackgroundColor: "#ffffff", + navigationBarTextStyle: "black", + }, + tabBar: { + color: "#8b8178", + selectedColor: "#3b2118", + backgroundColor: "#ffffff", + list: TEMPLATE_MANIFEST.navigation.map((tab) => ({ + pagePath: routes[tab.page], + text: tab.label, + iconPath: tab.iconPath, + selectedIconPath: tab.selectedIconPath, + })), + }, +}); diff --git a/apps/taro/src/app.css b/apps/taro/src/app.css new file mode 100644 index 0000000..3a3c4d6 --- /dev/null +++ b/apps/taro/src/app.css @@ -0,0 +1,55 @@ +page { + background: #fafaf9; +} +body { + margin: 0; +} +view, +text, +image, +input, +textarea, +button { + box-sizing: border-box; +} +button::after { + border: 0; +} +image { + display: block; +} +.runtime-state { + min-height: 70vh; + padding: 80px 32px; + color: #78716c; + font-size: 28px; + text-align: center; +} +.runtime-state button { + margin-top: 24px; +} +.preview-context { + padding: 12px 32px; + color: #78716c; + background: #fff; + border-bottom: 1px solid #eee; + font-size: 22px; +} +@media (min-width: 600px) { + body { + max-width: 430px; + margin: 0 auto; + background: #e7e5e4; + } + #app { + min-height: 100vh; + background: #fafaf9; + } +} +/* postcss-pxtransform disable */ +@media (min-width: 600px) { + html { + font-size: 22.933333px !important; + } +} +/* postcss-pxtransform enable */ diff --git a/apps/taro/src/app.tsx b/apps/taro/src/app.tsx new file mode 100644 index 0000000..23230e8 --- /dev/null +++ b/apps/taro/src/app.tsx @@ -0,0 +1,6 @@ +import type { PropsWithChildren } from "react"; +import { WorkspaceProvider } from "./runtime/workspace"; +import "./app.css"; +export default function App({ children }: PropsWithChildren) { + return {children}; +} diff --git a/apps/taro/src/components/Inquiry.tsx b/apps/taro/src/components/Inquiry.tsx new file mode 100644 index 0000000..f0a9f90 --- /dev/null +++ b/apps/taro/src/components/Inquiry.tsx @@ -0,0 +1,206 @@ +import { useEffect, useRef, useState } from "react"; +import { + View, + Text, + Button, + Input, + Textarea, + Picker, + Checkbox, + CheckboxGroup, + Label, +} from "@tarojs/components"; +import Taro, { useDidShow, useRouter } from "@tarojs/taro"; +import type { RuntimeWorkspace, RuntimeInquiryForm } from "@personal-brand/types"; +import { missingField, visibleFields, presentation } from "../runtime/content"; +import type { Values } from "../runtime/content"; +import { ensureSession, request, brandSlug } from "../runtime/api"; +import { takeQuery, navigate } from "../runtime/navigation"; +import "./forms.css"; +export function Inquiry({ workspace }: { workspace: RuntimeWorkspace }) { + const router = useRouter(); + const [offeringId, setOfferingId] = useState(router.params.offeringId || ""); + const form: RuntimeInquiryForm | undefined = + workspace.inquiryForms.find((form) => offeringId && form.targetId === offeringId) || + workspace.inquiryForms.find((form) => form.target === "brand"); + const [values, setValues] = useState({}); + const [error, setError] = useState(""); + const [sending, setSending] = useState(false); + const busy = useRef(false); + const p = presentation(workspace, "inquiry"); + const food = TEMPLATE_KEY === "acheng-campus-food"; + useDidShow(() => { + const query = takeQuery("inquiry"); + if (query.offeringId) setOfferingId(query.offeringId); + }); + useEffect(() => { + setValues({}); + setError(""); + }, [form?.key]); + const setValue = (key: string, value: string | string[]) => + setValues((current) => ({ ...current, [key]: value })); + async function submit() { + if (busy.current) return; + if (!form) { + setError("咨询表单暂不可用,请稍后重试"); + return; + } + const missing = missingField(form.fields, values); + if (missing) { + setError("请填写" + missing.label); + return; + } + busy.current = true; + setSending(true); + setError(""); + try { + await ensureSession(); + const valid = Object.fromEntries( + visibleFields(form.fields, values) + .filter((field) => values[field.key] !== undefined) + .map((field) => [field.key, values[field.key]]), + ); + const result = await request<{ id: string }>("/customer/inquiry-submissions", { + method: "POST", + customer: true, + data: { formId: form.key, values: valid }, + }); + Taro.setStorageSync(`latestInquiry:${brandSlug()}`, result); + navigate("success"); + } catch (error) { + setError(error instanceof Error ? error.message : "提交失败,请重试"); + } finally { + setSending(false); + busy.current = false; + } + } + return ( + + + {p.eyebrow} + {p.title || "提交咨询"} + {p.subtitle} + + {!form ? ( + 尚未配置可用的咨询表单 + ) : ( + + + {visibleFields(form.fields, values).map((field) => { + const options = field.options || []; + const value = values[field.key]; + const stringValue = typeof value === "string" ? value : ""; + const chips = + food && field.key === "type" && ["select", "radio"].includes(field.type); + return ( + + + {field.label} + {field.required && *} + + {chips ? ( + + {options.map((option) => ( + + ))} + + ) : field.type === "checkbox" ? ( + setValue(field.key, event.detail.value)}> + + {options.map((option) => ( + + ))} + + + ) : ["select", "radio"].includes(field.type) ? ( + option.label)} + value={Math.max( + 0, + options.findIndex((option) => option.value === stringValue), + )} + onChange={(event) => { + const option = options[Number(event.detail.value)]; + if (option) setValue(field.key, option.value); + }} + > + + {options.find((option) => option.value === stringValue)?.label || "请选择"} + ⌄ + + + ) : field.type === "date" ? ( + setValue(field.key, event.detail.value)} + > + + {stringValue || "请选择日期"} + ⌄ + + + ) : field.type === "textarea" ? ( +