DEV_TASK_STAGE1.md 9.9 KB

开发任务书 · 多小程序会员体系区分 · 阶段 1(数据层与会员闭环)

派发物料,非设计正本。设计正本在资料库云端;本文件是给开发会话的自包含任务说明。 主理人裁决(2026-11-10 11:33):P1 不通用、P2 按小程序分、分两阶段开工。本任务只做阶段 1。

0. 工作目录与安全约束(最高优先级,违反即验收不通过)

  • 工作目录 = E:/tmp/wm-appscope-stage1,已建好:分支 feature/wm-appscope-stage1,基线 origin/cfclub @ 4de82cb81,工作区干净。
  • 只在该 worktree 内工作。禁止在 E:/cfc 主工作树执行任何 git 写操作;主工作树有大量他人未提交改动,一律不碰。
  • 严禁:git reset --hard、git checkout -- .、git clean、git stash、git add -A、git add .。
  • 提交前用 git status --porcelain 与 git diff --cached --name-only 精确核验变更文件数,只 git add -- <明确路径>。
  • 完成后 git push -u origin feature/wm-appscope-stage1。不要合并 cfclub,不要 push 到 cfclub。
  • 只读参考(不得修改):E:/cfc/weight-miniapp-design/APP_SCOPE_SPEC.md(§4/§5/§6/§9)、MEMBER_SPEC.md、PAGE_SPEC.md。

1. 背景与已裁决口径

  • 不同小程序使用不同会员体系:减重 WEIGHT 与爱伴 AIBAN 共用 wm_* 会员表;浠艾福主小程序 CFC_MAIN 沿用存量 6 套会员表。
  • 新维度统一命名 app_code VARCHAR(32)。不要复用 app_key(它是首页快捷方式模块入口 key,且微信支付签名密钥也叫 appKey,同名三义)。
  • P1 不通用:现有家庭会员 / 健康启航计划仅 CFC_MAIN 放行;WEIGHT / AIBAN 未购对应会员一律 4001。
  • P2 按小程序分:user_data_permissions 改 (user_id, app_code) 分端授权。
  • 价格未拍板:只建表,不向 wm_member_plan 插入任何档位数据。
  • 阶段 2 不在本任务范围:登录身份切换、JWT claim、JwtInterceptor、WechatService 多 appid 路由。三个小程序凭证已就绪,留到阶段 2 联调。

2. 交付范围

2.1 新建 7 张表(全部 app_code 相关)

表 关键字段 唯一键 / 索引
app_registry app_code(PK), name, wx_appid, secret_ref(配置项引用,不落明文密钥), member_system(LEGACY_CFC/WM), pay_config_ref, cf_earn_enabled, status, created_at PK(app_code)
user_app_identity id, user_id, app_code, openid, unionid, session_key, created_at, updated_at uk_app_openid(app_code,openid) + uk_user_app(user_id,app_code)
wm_member_plan app_code, tier_code, name, price_cent, period_days, quota_json, status uk_app_tier(app_code,tier_code),不能只按 tier_code 唯一
wm_subscription app_code, id, user_id, family_id, tier_code, status, active_flag, start_at, end_at, source_order_id, cycle_stage idx(app_code,user_id,status,end_at) + uk_active(app_code,user_id,tier_code,active_flag)(仅 active 行置 1,其余为 NULL,MySQL NULL 不参与唯一)
wm_order app_code, id, order_no, user_id, tier_code, amount_cent, discount_cent, deduct_from_order_id, status, paid_at, channel uk_order_no 全局唯一 + idx(app_code,user_id,status)
wm_addon app_code(冗余), id, subscription_id, member_id, amount_cent, status idx(app_code,subscription_id,member_id)
wm_entitlement_usage app_code(冗余), id, subscription_id, ent_type, quota, used, period uk(app_code,subscription_id,ent_type,period)
  • app_registry 只初始化 CFC_MAIN 一条(member_system=LEGACY_CFC),WEIGHT / AIBAN 记录待阶段 2 凭证接入时再插。
  • user_app_identity 本阶段只建表结构,不写任何登录逻辑。

2.2 13 张存量表加 app_code

一律 ADD COLUMN app_code VARCHAR(32) NOT NULL DEFAULT 'CFC_MAIN' COMMENT '小程序/业务线',并为高频查询加联合索引(把 app_code 作为前导列):

health_reports、health_report_drafts、health_plans、report_summary、tasks、task_plan_instances、diet_records、exercise_record、fat_weight_record、fat_diet_record、fat_exercise_record、user_consent_records、user_data_permissions。

约束:

  • 历史行自动归属 CFC_MAIN,不得出现 NULL。
  • report_summary.uk_source(source_table,source_id)、indicator_values.uk_source_definition(...) 等既有唯一键保持不变,不要把 app_code 塞进唯一键。
  • 外键继承表(health_indicators、report_blocks、health_report_edit_logs、health_report_permissions、report_member_permissions、attribution_reports、task_plan_items、task_executions、diet_record_items、indicator_values、subscription_benefit_log、cf_balance_log)不加字段。
  • 主数据 / 字典 / CF 账本 / 存量 6 套会员表(users、families、family_members、membership_levels、membership_benefits、family_memberships、member_subscription*、payment_orders、purchase_orders、member_upgrade_records、cf_balance*、coupon*、user_shortcut_config、user_app_usage)不加字段。

2.3 P2:授权分端

  • user_data_permissions 新增 UNIQUE KEY uk_user_app (user_id, app_code);加唯一键前先检查存量是否有同 user_id 多行,有则保留最小 id 行并归属 CFC_MAIN,其余合并后删除(在迁移里幂等处理)。
  • 提供按 (user_id, app_code) 读写的 Service 方法;新端首次使用时按端初始化一行(复制 CFC_MAIN 的开关值或取默认关闭,取值在代码里注明)。
  • 在爱伴关闭 AI 分析不得影响 CFC_MAIN 的同名开关。

2.4 P1:付费墙带 appCode 的判定

新增 WmEntitlementService.canGeneratePlan(userId, appCode):

① appCode == CFC_MAIN && !"FREE".equals(membershipService.getMemberLevel(userId))
② appCode == CFC_MAIN && Boolean.TRUE.equals(purchaseService.checkPurchase(userId).get("isActive"))
③ wmMemberService.hasActiveEntitlement(userId, appCode, "PLAN_GENERATE")
  • HealthPlanController 的 POST /api/health/plan/generate 与 POST /api/health/plan/attribution 两处判定改为调用它;不得只加第三路 if 散落在 Controller。
  • 返回值附 tierCode / draftOnly / appCode;M0 仍只出 draft=true 初稿、不进 /api/health/plan/pending-review/* 发布队列。
  • 阶段 1 的 appCode 来源:JWT claim 属阶段 2,本阶段 Controller 侧先传服务端常量 CFC_MAIN(或从服务端上下文兜底),但 Service 签名必须带 appCode 参数,且严禁从请求 body / query / header 取 appCode 做权益判定(前端伪造即可跨小程序冒用权益)。
  • 兜底:无应用上下文时按 CFC_MAIN,保证存量家庭会员与启航计划用户 /generate 行为不回归。

2.5 最小代码结构

  • entity/:AppRegistry、UserAppIdentity、WmMemberPlan、WmSubscription、WmOrder、WmAddon、WmEntitlementUsage(MyBatis-Plus @TableName + @TableId(type=IdType.AUTO))。
  • mapper/:对应 BaseMapper。
  • service/wm/:WmMemberService(hasActiveEntitlement)、WmEntitlementService(canGeneratePlan)。
  • controller/:WmMemberController,路由 /api/wm/member/*,本阶段只做最小只读接口 /tiers、/current;下单/支付不实现(价格未拍板)。
  • 类名/Bean 名不得与现有类冲突;新增接口前查 docs/superpowers/api/API_REFERENCE.md 并做路由重复检查。

2.6 单测(必须实际执行)

  1. canGeneratePlan 三路放行 + P1:CFC_MAIN 家庭会员 / 启航计划用户在 WEIGHT 与 AIBAN 调 /generate 返回 4001。
  2. wm_member_plan 中 WEIGHT:M1 与 AIBAN:M1 可共存,唯一键生效。
  3. 13 张存量表历史行 app_code 全为 CFC_MAIN,无 NULL。
  4. P2:同一 userId 的 WEIGHT 与 CFC_MAIN 两行授权独立读写。
  5. 存量回归:现有家庭会员、启航计划用户的 /generate 行为不变。
  6. wm_member_plan 本轮零档位数据。

3. 明令禁止(越界即验收不通过)

  • 改 UserService.wechatLogin / JwtConfig / JwtInterceptor / WechatService.code2Session 的登录与鉴权行为(阶段 2)。
  • 写 users.member_level / family_memberships / purchase_orders;触发 F18 健康启航计划 CF 值发放(CfGrowthPathwayEarnService)。
  • 向 wm_member_plan 插入任何价格 / 档位数据。
  • 调用 /api/health/report/create(老同步接口,只认启航计划);建档只走 /upload-only → /collect/status → /bind。
  • 修改 E:/cfc/weight-miniapp-design/ 下任何设计文档。
  • 前端页面 / 小程序代码改动(阶段 1 只做后端数据层与会员闭环)。

4. 迁移与项目纪律(AGENTS.md)

  • DDL 唯一入口 DatabaseInitializer.runMigrations(),用 ensureColumn / 幂等 DDL;先 grep // 迁移 确认实际最新编号(核查时为 327),从下一个编号递增,不要硬编码 328。
  • 每次迁移必须同步 cfc-backend/src/main/resources/schema.sql 的 CREATE TABLE。
  • 新增 Controller/Service 前:mvn clean compile、查 API_REFERENCE.md、路由重复检查、Bean 名冲突检查。
  • 接口统一 @PostMapping + Result<T>;DI 用 @Resource;JWT Bearer(本阶段不新增白名单)。
  • 用户模型统一「能登录用户 / 不能登录用户」,不引入「孩子」概念。

5. 完成判据与汇报

  1. mvn clean compile 通过,单测实际执行通过(贴执行结果,不要只写「已写测试」)。
  2. git diff --cached --name-only 复核变更文件清单,确认无无关文件、无「全仓删除」假 diff。
  3. git push -u origin feature/wm-appscope-stage1 成功,回报 commit sha。
  4. 回报:新增表数、加字段表数、单测通过数、canGeneratePlan 判定结果、是否触碰禁止项(应为否)。