소스 검색

重构咨询流程:无需登录填写,触发咨询时登录,登录后绑定关系

核心变更:
- US-6.2: 咨询页无需登录→点击开始咨询→检查token→未登录跳转登录页
- 推广关系仅在用户完成微信登录后建立,不登录不建立
- 首次注册用户在登录页弹出资料完善浮层(预填姓名/生日),支持保存/跳过
- 浮层保存后继续进入命盘页
- pending_referrer仅在登录后提交并清除,登录中断保留
- 版本 v20260601.1
liaoxg 5 달 전
부모
커밋
f3541854da
4개의 변경된 파일과 291개의 추가작업 그리고 41개의 파일을 삭제
  1. 13 0
      client/pages/index/index.vue
  2. 215 19
      client/pages/login/index.vue
  3. 33 21
      docs/phase1-user-stories.md
  4. 30 1
      docs/requirements-changelog.md

+ 13 - 0
client/pages/index/index.vue

@@ -124,9 +124,22 @@ async function onAnalyze() {
     return
     return
   }
   }
 
 
+  const token = uni.getStorageSync('token')
   const birthdayStr = birthday.value
   const birthdayStr = birthday.value
   confirming.value = true
   confirming.value = true
 
 
+  // US-6.2 §9: Login gate — not logged in → redirect to login with params
+  if (!token) {
+    confirming.value = false
+    const params = new URLSearchParams({
+      birthday: birthdayStr,
+      name: name.value,
+      questions: questions.value
+    })
+    uni.navigateTo({ url: `/pages/login/index?${params.toString()}` })
+    return
+  }
+
   try {
   try {
     // Check if this birthday has existing records — US-1.1 §7-9
     // Check if this birthday has existing records — US-1.1 §7-9
     const isNew = await checkNewBirthday(birthdayStr)
     const isNew = await checkNewBirthday(birthdayStr)

+ 215 - 19
client/pages/login/index.vue

@@ -28,14 +28,70 @@
 
 
       <text class="agreement">登录即表示同意《用户协议》和《隐私政策》</text>
       <text class="agreement">登录即表示同意《用户协议》和《隐私政策》</text>
     </view>
     </view>
+
+    <!-- US-6.2 §10: Profile completion overlay for first-time users -->
+    <view v-if="showProfileForm" class="profile-overlay" @touchmove.prevent>
+      <view class="profile-card">
+        <text class="profile-card__title">完善个人资料</text>
+        <text class="profile-card__subtitle">以下信息帮助提供更精准的分析</text>
+
+        <view class="profile-card__field">
+          <text class="profile-card__label">昵称</text>
+          <input class="profile-card__input" v-model="profileForm.nickname" placeholder="输入昵称" maxlength="20" />
+        </view>
+        <view class="profile-card__field">
+          <text class="profile-card__label">性别</text>
+          <view class="profile-card__radio-group">
+            <text class="profile-card__radio" :class="{ active: profileForm.gender === 1 }" @click="profileForm.gender = 1">男</text>
+            <text class="profile-card__radio" :class="{ active: profileForm.gender === 2 }" @click="profileForm.gender = 2">女</text>
+          </view>
+        </view>
+        <view class="profile-card__field">
+          <text class="profile-card__label">出生日期</text>
+          <text class="profile-card__prefilled">已填写:{{ profileForm.birthDate || '未填写' }}</text>
+        </view>
+
+        <view class="profile-card__actions">
+          <text class="profile-card__btn profile-card__btn--skip" @click="onSkipProfile">跳过</text>
+          <text class="profile-card__btn profile-card__btn--save" @click="onSaveProfile">保存</text>
+        </view>
+      </view>
+    </view>
   </view>
   </view>
 </template>
 </template>
 
 
 <script setup>
 <script setup>
+import { ref, onLoad } from '@dcloudio/uni-app'
 import { useUserStore } from '@/stores/user'
 import { useUserStore } from '@/stores/user'
+import { profileApi } from '@/utils/api'
 
 
 const userStore = useUserStore()
 const userStore = useUserStore()
 
 
+// Params passed from index page (US-6.2 §9)
+const queryName = ref('')
+const queryBirthday = ref('')
+const queryQuestions = ref('')
+const redirectUrl = ref('')
+
+// Profile completion overlay
+const showProfileForm = ref(false)
+const profileForm = ref({
+  nickname: '',
+  gender: 0,
+  birthDate: ''
+})
+
+onLoad((options) => {
+  queryName.value = options.name || ''
+  queryBirthday.value = options.birthday || ''
+  queryQuestions.value = options.questions || ''
+  redirectUrl.value = options.redirect_to || ''
+
+  // Pre-fill profile form from consultation page
+  profileForm.value.nickname = queryName.value
+  profileForm.value.birthDate = queryBirthday.value
+})
+
 async function onWechatLogin() {
 async function onWechatLogin() {
   uni.showLoading({ title: '登录中...' })
   uni.showLoading({ title: '登录中...' })
   try {
   try {
@@ -75,7 +131,7 @@ async function onWechatLogin() {
     // Step 3: Send code to backend
     // Step 3: Send code to backend
     await userStore.wechatLogin(loginRes.code, nickname, avatarUrl, pendingReferrer)
     await userStore.wechatLogin(loginRes.code, nickname, avatarUrl, pendingReferrer)
 
 
-    // Clear pending referrer after using it
+    // Clear pending referrer after using it (US-6.2 §6: login success → clear)
     if (pendingReferrer) {
     if (pendingReferrer) {
       uni.removeStorageSync('pending_referrer')
       uni.removeStorageSync('pending_referrer')
     }
     }
@@ -83,29 +139,72 @@ async function onWechatLogin() {
     uni.hideLoading()
     uni.hideLoading()
     uni.showToast({ title: '登录成功', icon: 'success' })
     uni.showToast({ title: '登录成功', icon: 'success' })
 
 
-    // Check redirect_to param (US-6.6: "登录后开通" flow)
-    const pages = getCurrentPages()
-    const page = pages[pages.length - 1]
-    const params = page?.options || {}
-    const redirectTo = params.redirect_to || ''
-
-    // US-5.1 §8: Redirect to registration completion if profile incomplete
+    // US-6.2 §10: First-time user → show profile completion overlay
     if (userStore.profileIncomplete) {
     if (userStore.profileIncomplete) {
-      setTimeout(() => {
-        uni.redirectTo({ url: '/pages/register/index' })
-      }, 500)
-    } else if (redirectTo) {
-      setTimeout(() => {
-        uni.redirectTo({ url: redirectTo })
-      }, 500)
-    } else {
-      setTimeout(() => {
-        uni.switchTab({ url: '/pages/index/index' })
-      }, 500)
+      showProfileForm.value = true
+      return // wait for user action (save/skip) before continuing
     }
     }
+
+    // Existing user → continue to chart
+    continueToChart()
   } catch (e) {
   } catch (e) {
     uni.hideLoading()
     uni.hideLoading()
     uni.showToast({ title: e.message || '登录失败', icon: 'none' })
     uni.showToast({ title: e.message || '登录失败', icon: 'none' })
+    // US-6.2 §7: Login interrupted → keep pending_referrer
+  }
+}
+
+async function onSaveProfile() {
+  uni.showLoading({ title: '保存中...' })
+  try {
+    await profileApi.complete({
+      nickname: profileForm.value.nickname,
+      gender: profileForm.value.gender,
+      birthDate: profileForm.value.birthDate,
+      // birthday and name are pre-filled from consultation page
+      hasExternalBirthday: !!queryBirthday.value,
+      externalName: queryName.value
+    })
+    userStore.profileIncomplete = false
+    uni.hideLoading()
+    uni.showToast({ title: '保存成功', icon: 'success' })
+  } catch (e) {
+    uni.hideLoading()
+    uni.showToast({ title: e.message || '保存失败', icon: 'none' })
+    return
+  }
+  showProfileForm.value = false
+  continueToChart()
+}
+
+function onSkipProfile() {
+  showProfileForm.value = false
+  uni.showToast({ title: '已跳过,可随时在个人中心完善', icon: 'none' })
+  continueToChart()
+}
+
+function continueToChart() {
+  // Check redirect_to param
+  const redirectTarget = redirectUrl.value || ''
+
+  if (redirectTarget) {
+    setTimeout(() => {
+      uni.redirectTo({ url: redirectTarget })
+    }, 500)
+  } else if (queryName.value || queryBirthday.value) {
+    // Navigate to chart with the consultation params from index
+    setTimeout(() => {
+      const params = new URLSearchParams({
+        name: queryName.value,
+        birthday: queryBirthday.value,
+        questions: queryQuestions.value
+      })
+      uni.redirectTo({ url: `/pages/chart/index?${params.toString()}` })
+    }, 500)
+  } else {
+    setTimeout(() => {
+      uni.switchTab({ url: '/pages/index/index' })
+    }, 500)
   }
   }
 }
 }
 </script>
 </script>
@@ -183,4 +282,101 @@ async function onWechatLogin() {
   color: #9CA3AF;
   color: #9CA3AF;
   margin-top: 16px;
   margin-top: 16px;
 }
 }
+
+/* US-6.2 §10: Profile completion overlay */
+.profile-overlay {
+  position: fixed;
+  inset: 0;
+  z-index: 100;
+  background: rgba(0, 0, 0, 0.5);
+  display: flex;
+  align-items: center;
+  justify-content: center;
+  padding: 0 24px;
+}
+.profile-card {
+  background: #fff;
+  border-radius: 20px;
+  padding: 32px 24px 24px;
+  width: 100%;
+  max-width: 360px;
+}
+.profile-card__title {
+  font-size: 20px;
+  font-weight: 700;
+  color: #1F2937;
+  display: block;
+  text-align: center;
+}
+.profile-card__subtitle {
+  font-size: 13px;
+  color: #9CA3AF;
+  display: block;
+  text-align: center;
+  margin-top: 4px;
+  margin-bottom: 24px;
+}
+.profile-card__field {
+  margin-bottom: 16px;
+}
+.profile-card__label {
+  font-size: 13px;
+  color: #6B7280;
+  display: block;
+  margin-bottom: 6px;
+}
+.profile-card__input {
+  height: 44px;
+  border: 1px solid #E5E7EB;
+  border-radius: 10px;
+  padding: 0 14px;
+  font-size: 16px;
+  width: 100%;
+  box-sizing: border-box;
+  color: #1F2937;
+}
+.profile-card__prefilled {
+  font-size: 14px;
+  color: #6B7280;
+  line-height: 44px;
+}
+.profile-card__radio-group {
+  display: flex;
+  gap: 12px;
+}
+.profile-card__radio {
+  flex: 1;
+  text-align: center;
+  padding: 10px 0;
+  border: 1px solid #E5E7EB;
+  border-radius: 10px;
+  font-size: 15px;
+  color: #6B7280;
+}
+.profile-card__radio.active {
+  border-color: #7C3AED;
+  color: #7C3AED;
+  background: #F5F3FF;
+}
+.profile-card__actions {
+  display: flex;
+  gap: 12px;
+  margin-top: 24px;
+}
+.profile-card__btn {
+  flex: 1;
+  text-align: center;
+  padding: 12px 0;
+  border-radius: 10px;
+  font-size: 15px;
+  font-weight: 500;
+}
+.profile-card__btn--skip {
+  background: #F3F4F6;
+  color: #6B7280;
+}
+.profile-card__btn--save {
+  background: linear-gradient(135deg, #4A148C, #7B2DC8);
+  color: #fff;
+}
 </style>
 </style>

+ 33 - 21
docs/phase1-user-stories.md

@@ -19,8 +19,10 @@
 3. 验证:年份必须为 1900-当前年份、月份 1-12、日期根据月份合法性验证
 3. 验证:年份必须为 1900-当前年份、月份 1-12、日期根据月份合法性验证
 4. 输入无效时显示具体错误提示(如"请输入正确的出生日期")
 4. 输入无效时显示具体错误提示(如"请输入正确的出生日期")
 5. "想了解的问题" 输入框限制 100 字以内(输入时实时显示剩余字数)
 5. "想了解的问题" 输入框限制 100 字以内(输入时实时显示剩余字数)
-6. 点击"开始咨询"后,调用后端 `POST /api/consultation/start` 发起咨询
-7. **已登录用户自动填入自己生日**(用户注册时已填 `birth_date`),但允许修改(能量师可输入客户生日)
+6. 点击"开始咨询"后**先检查登录状态**:
+   - **未登录** → 携带 `birthday`、`name`、`questions` 参数跳转到登录页(`/pages/login/index`)
+   - **已登录** → 正常调用 `POST /api/consultation/start`
+7. 已登录用户自动填入自己生日(用户注册时已填 `birth_date`),但允许修改(能量师可输入客户生日)
 
 
 **验收标准(咨询会话管理):**
 **验收标准(咨询会话管理):**
 
 
@@ -628,33 +630,43 @@
 
 
 **验收标准:**
 **验收标准:**
 
 
-**分享链路:**
+**验收标准(分享链路与前端缓存):**
+
 1. 推广链接格式:`/pages/index/index?ref=ABC123`(6位推广码)
 1. 推广链接格式:`/pages/index/index?ref=ABC123`(6位推广码)
 2. 分享卡片 / 小程序码同样携带 `ref` 参数
 2. 分享卡片 / 小程序码同样携带 `ref` 参数
-
-**前端缓存策略:**
-3. 用户通过链接打开小程序时:
-   - `onLaunch(options)` 读取 `options.query.ref`,转为大写后写入缓存 `pending_referrer`
-   - **每次点击新链接都覆盖**已有值,以用户**最后一次点击的链接**为准(新点击意图覆盖旧的)
-4. `pending_referrer` **永不过期**,直到注册成功后才清除
-5. 用户注册(微信授权登录)时,从缓存读取 `pending_referrer` 传入注册接口
+3. 用户通过推广链接打开小程序时,`onLoad(options)` 读取 `options.query.ref`,转为大写后写入缓存 `pending_referrer`
+4. **每次点击新链接都覆盖**已有值,以用户**最后一次点击的链接**为准
+5. `pending_referrer` **仅在用户完成微信登录后**才提交到后端建立关系,**不登录不建立关系**
+6. 登录成功后立即清除 `pending_referrer`(无论绑定成功与否)
+7. 登录中途中断(拒绝授权)→ 不清除 `pending_referrer`,下次触发登录仍携带
+
+**前端流程(咨询→登录→关系绑定):**
+
+8. 用户打开首页(`index/index.vue`)→ 填写姓名、生日、问题 → **无需登录**
+9. 点击"开始咨询" → **检查 token**:
+   - 未登录 → 携带参数 `birthday`, `name`, `questions` 跳转到登录页 `login/index.vue`
+   - 已登录 → 直接调用 `POST /api/consultation/start`
+10. 登录页流程:
+    - 微信授权登录成功 → 从 `pending_referrer` 读取推广码提交后端 → 清除缓存
+    - 登录成功且是**首次注册用户**(`profileIncomplete=true`)→ 弹出**资料完善浮层**,预填首页传入的 `birthday` 和 `name`
+    - 浮层允许用户 **保存** 或 **跳过**
+    - 资料完善后(保存或跳过)→ 继续进入命盘页
+11. 始终不登录的用户(关闭登录页)→ `pending_referrer` 保留但**永不使用**,无关系绑定
 
 
 **后端注册逻辑(`UserService.loginOrRegister()`):**
 **后端注册逻辑(`UserService.loginOrRegister()`):**
-6. 用户不存在(新用户)+ `referrerCode` 有效 → `invitedBy = 上级ID`
-7. 用户不存在 + `referrerCode` 无效 → `invitedBy = null`(静默降级,不报错)
-8. 用户不存在 + `referrerCode` 为空 → `invitedBy = null`(正常注册)
-9. 用户不存在 + `referrerCode` 为**自己的新openid** → `invitedBy = null`(防止自邀请)
-10. 用户**已存在** → 忽略所有 `referrerCode`,不覆盖已有 `invitedBy`(关系一次性锁定)
-11. `referrerCode` 查询不到对应上级 → 静默跳过,不抛异常
+12. 用户不存在(新用户)+ `referrerCode` 有效 → `invitedBy = 上级ID`
+13. 用户不存在 + `referrerCode` 无效 → `invitedBy = null`(静默降级,不报错)
+14. 用户不存在 + `referrerCode` 为空 → `invitedBy = null`(正常注册)
+15. 用户不存在 + `referrerCode` 为**自己的新openid** → `invitedBy = null`(防止自邀请)
+16. 用户**已存在** → 忽略所有 `referrerCode`,不覆盖已有 `invitedBy`(关系一次性锁定)
+17. `referrerCode` 查询不到对应上级 → 静默跳过,不抛异常
 
 
 **数据库更新:**
 **数据库更新:**
-12. 绑定成功后,如果上级存在,`inviter.directCount += 1`(实时更新统计字段)
+18. 绑定成功后,如果上级存在,`inviter.directCount += 1`(实时更新统计字段)
 
 
 **边界与异常:**
 **边界与异常:**
-13. 用户未登录直接生成命盘(纯前端计算)→ 允许,不强制登录
-14. 登录中途中断(拒绝授权)→ 不清除 `pending_referrer`,下次触发登录仍携带
-15. 注册接口网络失败 → 保留 `pending_referrer`,重试时继续携带
-16. 同一设备切换微信账号 → 新的 openid 按新用户处理,正常绑定关系
+19. 注册接口网络失败 → 保留 `pending_referrer`,重试时继续携带
+20. 同一设备切换微信账号 → 新的 openid 按新用户处理,正常绑定关系
 
 
 ---
 ---
 
 

+ 30 - 1
docs/requirements-changelog.md

@@ -5,7 +5,36 @@
 
 
 ---
 ---
 
 
-## v20260531.4(当前版本)
+## v20260601.1(当前版本)
+
+**发布日期:** 2026-06-01
+**变更类型:** 流程重构
+**状态:** ✅ 已确认
+
+### 版本摘要
+
+重构咨询发起流程:用户扫推广码后进入咨询填写页**无需登录**,点击"开始咨询"时触发登录,登录后才建立推广关系。首次注册用户弹出资料完善浮层(预填姓名/生日),支持保存或跳过。
+
+### 变更清单
+
+| # | 类型 | 变更 | 涉及位置 |
+|---|------|------|---------|
+| 1 | 🔄 重写 | US-6.2 流程:咨询页无需登录 → 发起时触发登录 → 登录后绑定关系 → 不登录不建立 | US-6.2 |
+| 2 | 🔧 更新 | US-1.1:onAnalyze 增加登录守卫(未登录→跳转登录页) | US-1.1 |
+| 3 | 🆕 新增 | login/index.vue:接收 index 传入的 birthday/name/questions 参数 | login/index.vue |
+| 4 | 🆕 新增 | login/index.vue:首次用户弹出资料完善浮层(昵称/性别/生日),含 保存/跳过 按钮 | login/index.vue |
+| 5 | 🔧 更新 | index/index.vue:onAnalyze 未登录时携带参数 navigateTo 到登录页 | index/index.vue |
+| 6 | 🔧 更新 | `pending_referrer` 仅在登录成功后提交并清除,不登录永不使用 | US-6.2 |
+
+### 影响范围
+
+- 前端:index/index.vue(登录守卫)、login/index.vue(接收参数+浮层)
+- 需求文档:US-6.2(完整重写流程部分)、US-1.1(第6条重写)
+- 后端不受影响(登录接口不变)
+
+---
+
+## v20260531.4(已归档)
 
 
 **发布日期:** 2026-05-31
 **发布日期:** 2026-05-31
 **变更类型:** 修正
 **变更类型:** 修正