join.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <template>
  2. <view class="page">
  3. <!-- 通用登录浮层 -->
  4. <login-sheet ref="loginSheet" @success="onSheetLoginSuccess" />
  5. <!-- 加载中 -->
  6. <view class="loading" v-if="loading">
  7. <view class="loading-icon">⏳</view>
  8. <text class="loading-text">加载中...</text>
  9. </view>
  10. <!-- 邀请信息卡片(仅用于展示,不做交互) -->
  11. <view class="invite-card" v-if="!loading && inviteInfo">
  12. <view class="invite-header">
  13. <view class="invite-icon">🏠</view>
  14. <text class="invite-title">家庭邀请</text>
  15. </view>
  16. <view class="invite-body">
  17. <image
  18. class="inviter-avatar"
  19. :src="(inviteInfo.inviterAvatar || '/static/default-avatar.png')"
  20. mode="aspectFill"
  21. ></image>
  22. <text class="family-name">{{ inviteInfo.familyName }}</text>
  23. <text class="inviter" v-if="inviteInfo.inviterName">
  24. 邀请人:{{ inviteInfo.inviterName }}
  25. </text>
  26. <text class="member-count">
  27. 已有 {{ inviteInfo.memberCount }} 位家庭成员
  28. </text>
  29. </view>
  30. <view class="action-area">
  31. <text class="tip-text">{{ tipText }}</text>
  32. <button class="btn btn-primary" @tap="goNext" :disabled="redirecting">
  33. <text v-if="!redirecting">{{ btnText }}</text>
  34. <text v-else>跳转中...</text>
  35. </button>
  36. </view>
  37. </view>
  38. <!-- 推广码绑定卡片(兼容旧链接) -->
  39. <view class="invite-card" v-if="!loading && refCode && !bindDone">
  40. <view class="invite-header">
  41. <view class="invite-icon">🤝</view>
  42. <text class="invite-title">推广绑定</text>
  43. </view>
  44. <view class="invite-body">
  45. <text class="member-count">推荐人推广码:{{ refCode }}</text>
  46. </view>
  47. <view class="action-area" v-if="isLoggedIn">
  48. <button class="btn btn-primary" @tap="bindRef" :disabled="bindLoading">
  49. <text v-if="!bindLoading">绑定推荐人</text>
  50. <text v-else>绑定中...</text>
  51. </button>
  52. <text class="tip-text">绑定后该推荐人后续消费时记入其家庭公共账户</text>
  53. </view>
  54. <view class="action-area" v-else>
  55. <button class="btn btn-primary" @tap="goLoginWithRef" :disabled="loginLoading">
  56. <text v-if="!loginLoading">微信一键登录</text>
  57. <text v-else>登录中...</text>
  58. </button>
  59. <text class="tip-text">登录后可绑定推荐人</text>
  60. </view>
  61. </view>
  62. <!-- 推广码绑定成功 -->
  63. <view class="invite-card" v-if="!loading && refCode && bindDone">
  64. <view class="success-area">
  65. <view class="success-icon">✅</view>
  66. <text class="success-text">绑定成功</text>
  67. <text class="success-sub">已绑定推荐人,可进入首页探索</text>
  68. <button class="btn btn-primary" @tap="goHome">进入首页</button>
  69. </view>
  70. </view>
  71. <!-- 邀请无效 -->
  72. <view class="error-card" v-if="!loading && error">
  73. <view class="error-icon">❌</view>
  74. <text class="error-text">{{ error }}</text>
  75. <button class="btn btn-outline" @tap="goHome">返回首页</button>
  76. </view>
  77. </view>
  78. </template>
  79. <script>
  80. import { validateInvitation, validateFamilyCode, checkUserFamily, bindReferral } from '@/utils/api.js'
  81. export default {
  82. data() {
  83. return {
  84. // 家庭邀请码(单一参数)
  85. familyInviteCode: '',
  86. // 推广码(兼容)
  87. refCode: '',
  88. bindLoading: false,
  89. bindDone: false,
  90. loading: true,
  91. loginLoading: false,
  92. redirecting: false,
  93. isLoggedIn: false,
  94. inviteInfo: null,
  95. familyStatus: null,
  96. error: '',
  97. // 动态文案
  98. tipText: '',
  99. btnText: ''
  100. }
  101. },
  102. onLoad(query) {
  103. this._onLoadFired = true
  104. query = query || {}
  105. // 0. 防御:若 URL 直接带 familyInviteCode(未经过本页分享链路的直达场景),
  106. // 确保本地 pendingFamilyInviteCode 已写入,供 user-edit 识别家庭邀请流程
  107. if (query.familyInviteCode) {
  108. uni.setStorageSync('pendingFamilyInviteCode', query.familyInviteCode)
  109. }
  110. // 1. 解析家庭邀请码(单一参数,兼容 inviteCode/familyInviteCode/token)
  111. // token 为家庭成员管理页(family-members.vue)分享卡片/小程序码传入的邀请令牌,值与邀请码一致
  112. var familyInviteCode = query.familyInviteCode || query.inviteCode || query.token || ''
  113. if (!familyInviteCode && query.scene) {
  114. var sceneDecoded = decodeURIComponent(query.scene)
  115. // 支持 scene=invite=xxx(小程序码)、fam_xxx 或 ref=xxx 格式
  116. if (sceneDecoded.indexOf('invite=') === 0) {
  117. familyInviteCode = sceneDecoded.substring(7)
  118. } else if (sceneDecoded.indexOf('fam_') === 0) {
  119. familyInviteCode = sceneDecoded
  120. } else if (sceneDecoded.indexOf('ref=') === 0) {
  121. // ref=xxx 归为推广码
  122. } else if (sceneDecoded.length >= 6) {
  123. // 兜底:长度>=6 视为家庭邀请码
  124. familyInviteCode = familyInviteCode || sceneDecoded
  125. }
  126. }
  127. // 2. 解析推广码(兼容旧链接)
  128. var refCode = query.refCode || ''
  129. if (!refCode && query.scene) {
  130. var sceneDecoded = decodeURIComponent(query.scene)
  131. if (sceneDecoded.indexOf('ref=') === 0) {
  132. refCode = sceneDecoded.substring(4)
  133. }
  134. }
  135. // 3. 无任何有效参数 -> 首页
  136. if (!familyInviteCode && !refCode) {
  137. uni.reLaunch({ url: '/pages/index-home/index' })
  138. return
  139. }
  140. // 4. 有家庭邀请码 -> 存储并校验
  141. if (familyInviteCode) {
  142. this.familyInviteCode = familyInviteCode
  143. // 存入本地,供登录页/个人信息页使用
  144. uni.setStorageSync('pendingFamilyInviteCode', familyInviteCode)
  145. // 必须先初始化登录状态:已登录用户走"去完善信息"直达个人信息页,未登录才引导去登录
  146. this.checkLoginStatus()
  147. this.validateFamilyInvite(familyInviteCode)
  148. return
  149. }
  150. // 5. 仅有推广码 -> 走原有推广绑定流程
  151. this.refCode = refCode
  152. this.loading = false
  153. this.checkLoginStatus()
  154. },
  155. onShow() {
  156. // 首次进入:onLoad 已处理,跳过避免双发;从登录页返回时重新检查
  157. if (this._onLoadFired) {
  158. this._onLoadFired = false
  159. return
  160. }
  161. // 从登录页返回,重新检查
  162. if (this.familyInviteCode) {
  163. this.checkLoginStatus()
  164. if (!this.inviteInfo) {
  165. this.validateFamilyInvite(this.familyInviteCode)
  166. }
  167. } else if (this.refCode) {
  168. this.checkLoginStatus()
  169. }
  170. },
  171. methods: {
  172. checkLoginStatus() {
  173. var token = uni.getStorageSync('token')
  174. this.isLoggedIn = !!token
  175. },
  176. // 校验家庭邀请码有效性(仅获取家庭信息用于展示)
  177. // 兼容两种格式:32位hex为邀请令牌token(走 validateInvitation),否则为家庭短邀请码(走 validateFamilyCode)
  178. async validateFamilyInvite(code) {
  179. this.loading = true
  180. try {
  181. var isToken = /^[0-9a-fA-F]{32}$/.test(code)
  182. var res = isToken ? await validateInvitation(code) : await validateFamilyCode(code)
  183. if (res && res.code === 200) {
  184. this.inviteInfo = res.data
  185. // 已登录用户:检查是否已在目标家庭
  186. if (this.isLoggedIn) {
  187. await this.checkFamilyAndRedirect()
  188. } else {
  189. // 未登录:设置文案引导去登录
  190. this.setRedirectInfo(false)
  191. this.loading = false
  192. }
  193. } else {
  194. this.error = '邀请已失效'
  195. this.loading = false
  196. }
  197. } catch (e) {
  198. this.error = e.message || '邀请验证失败'
  199. this.loading = false
  200. }
  201. },
  202. // 检查家庭状态并决定跳转
  203. async checkFamilyAndRedirect() {
  204. try {
  205. var res = await checkUserFamily()
  206. if (res && res.data) {
  207. this.familyStatus = res.data
  208. // 同步 familyId
  209. if (res.data.inFamily && res.data.familyId) {
  210. this.$store && this.$store.commit('setFamilyId', res.data.familyId)
  211. uni.setStorageSync('familyId', res.data.familyId)
  212. }
  213. // 已在目标家庭 -> 直接进首页
  214. if (this.inviteInfo && this.familyStatus.inFamily &&
  215. this.familyStatus.familyId === this.inviteInfo.familyId) {
  216. this.setRedirectInfo(true)
  217. this.loading = false
  218. // 延迟跳转,让用户看到成功提示
  219. setTimeout(() => this.goHome(), 1000)
  220. return
  221. }
  222. // 不在目标家庭 -> 自动跳转到完善个人信息页
  223. this.loading = false
  224. this.redirecting = true
  225. this.goJoinForm()
  226. return
  227. } else {
  228. this.setRedirectInfo(false)
  229. this.loading = false
  230. }
  231. } catch (e) {
  232. console.log('检查家庭状态失败', e)
  233. this.setRedirectInfo(false)
  234. this.loading = false
  235. }
  236. },
  237. setRedirectInfo(isInTargetFamily) {
  238. if (isInTargetFamily) {
  239. this.tipText = '您已是该家庭成员'
  240. this.btnText = '进入首页'
  241. } else {
  242. this.tipText = '完善个人信息后将自动提交加入申请'
  243. this.btnText = '去完善信息'
  244. }
  245. },
  246. // 统一跳转逻辑
  247. goNext() {
  248. if (this.redirecting) return
  249. this.redirecting = true
  250. // 已在目标家庭 -> 直接进首页
  251. if (this.inviteInfo && this.familyStatus && this.familyStatus.inFamily &&
  252. this.familyStatus.familyId === this.inviteInfo.familyId) {
  253. this.redirecting = false
  254. this.goHome()
  255. return
  256. }
  257. // 未登录:先去登录(pendingFamilyInviteCode 已存本地,登录后新用户流程会提交加入申请)
  258. if (!this.isLoggedIn) {
  259. this.redirecting = false
  260. this.goLogin()
  261. return
  262. }
  263. // 已登录但不在目标家庭:跳加入家庭表单页。
  264. // join-form 填真实姓名 + 家庭身份后提交加入申请(requestJoinByCode)
  265. this.goJoinForm()
  266. },
  267. // 已登录用户跳转加入家庭表单页(接受家庭邀请流程)
  268. goJoinForm() {
  269. var inviteCode = this.familyInviteCode || uni.getStorageSync('pendingFamilyInviteCode') || ''
  270. uni.navigateTo({
  271. url: '/pages/invite/join-form?familyInviteCode=' + encodeURIComponent(inviteCode)
  272. })
  273. },
  274. goHome() {
  275. uni.reLaunch({ url: '/pages/index-home/index' })
  276. },
  277. // 打开登录浮层(家庭邀请码已存 pendingFamilyInviteCode,登录成功后走家庭邀请流程)
  278. goLogin() {
  279. this.openLoginSheet()
  280. },
  281. // 推广码场景:打开登录浮层(refCode 交给登录浮层静默绑定)
  282. goLoginWithRef() {
  283. this.openLoginSheet({ refCode: this.refCode })
  284. },
  285. // 登录成功:刷新登录态并重新校验邀请
  286. onLoginSuccess() {
  287. this.checkLoginStatus()
  288. if (this.familyInviteCode) {
  289. this.validateFamilyInvite(this.familyInviteCode)
  290. }
  291. },
  292. // 推广码绑定
  293. async bindRef() {
  294. if (!this.refCode) {
  295. uni.showToast({ title: '推广码为空', icon: 'none' })
  296. return
  297. }
  298. this.bindLoading = true
  299. try {
  300. var res = await bindReferral(this.refCode)
  301. if (res && res.code === 200) {
  302. this.bindDone = true
  303. uni.showToast({ title: '绑定成功', icon: 'success' })
  304. } else {
  305. uni.showToast({ title: (res && res.message) || '绑定失败', icon: 'none' })
  306. }
  307. } catch (e) {
  308. uni.showToast({ title: e.message || '绑定失败', icon: 'none' })
  309. }
  310. this.bindLoading = false
  311. }
  312. }
  313. }
  314. </script>
  315. <style scoped>
  316. .page {
  317. padding: 32rpx;
  318. min-height: 100vh;
  319. background: linear-gradient(135deg, #FFF7ED 0%, #FEF3C7 100%);
  320. display: flex;
  321. flex-direction: column;
  322. align-items: center;
  323. }
  324. .loading {
  325. display: flex;
  326. flex-direction: column;
  327. align-items: center;
  328. justify-content: center;
  329. min-height: 60vh;
  330. }
  331. .loading-icon {
  332. font-size: 80rpx;
  333. margin-bottom: 20rpx;
  334. }
  335. .loading-text {
  336. color: #666;
  337. font-size: 28rpx;
  338. }
  339. .invite-card {
  340. width: 100%;
  341. max-width: 600rpx;
  342. background: #FFFFFF;
  343. border-radius: 24rpx;
  344. padding: 40rpx;
  345. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 008);
  346. margin-top: 10vh;
  347. }
  348. .invite-header {
  349. display: flex;
  350. flex-direction: column;
  351. align-items: center;
  352. margin-bottom: 32rpx;
  353. }
  354. .invite-icon {
  355. font-size: 80rpx;
  356. margin-bottom: 16rpx;
  357. }
  358. .invite-title {
  359. font-size: 32rpx;
  360. font-weight: 700;
  361. color: #1E293B;
  362. }
  363. .invite-body {
  364. display: flex;
  365. flex-direction: column;
  366. align-items: center;
  367. margin-bottom: 40rpx;
  368. padding: 24rpx;
  369. background: #FFF7ED;
  370. border-radius: 16rpx;
  371. }
  372. .family-name {
  373. font-size: 40rpx;
  374. font-weight: 700;
  375. color: #F97316;
  376. margin-bottom: 12rpx;
  377. }
  378. .inviter {
  379. font-size: 28rpx;
  380. color: #64748B;
  381. margin-bottom: 8rpx;
  382. }
  383. .inviter-avatar {
  384. width: 80rpx;
  385. height: 80rpx;
  386. border-radius: 50%;
  387. margin-bottom: 12rpx;
  388. background: #F1F5F9;
  389. }
  390. .member-count {
  391. font-size: 24rpx;
  392. color: #94A3B8;
  393. }
  394. .action-area {
  395. display: flex;
  396. flex-direction: column;
  397. gap: 16rpx;
  398. width: 100%;
  399. }
  400. .tip-text {
  401. font-size: 24rpx;
  402. color: #94A3B8;
  403. text-align: center;
  404. }
  405. .btn {
  406. width: 100%;
  407. padding: 28rpx;
  408. border-radius: 16rpx;
  409. font-size: 30rpx;
  410. font-weight: 600;
  411. text-align: center;
  412. border: none;
  413. }
  414. .btn-primary {
  415. background: linear-gradient(135deg, #F97316, #EA580C);
  416. color: #FFFFFF;
  417. }
  418. .btn-primary:disabled {
  419. opacity: 06;
  420. }
  421. .btn-outline {
  422. background: #FFFFFF;
  423. color: #F97316;
  424. border: 2rpx solid #F97316;
  425. }
  426. .success-area {
  427. display: flex;
  428. flex-direction: column;
  429. align-items: center;
  430. gap: 16rpx;
  431. }
  432. .success-icon {
  433. font-size: 80rpx;
  434. }
  435. .success-text {
  436. font-size: 36rpx;
  437. font-weight: 700;
  438. color: #16A34A;
  439. }
  440. .success-sub {
  441. font-size: 28rpx;
  442. color: #6B7280;
  443. margin-bottom: 24rpx;
  444. }
  445. .error-card {
  446. width: 100%;
  447. max-width: 600rpx;
  448. background: #FFFFFF;
  449. border-radius: 24rpx;
  450. padding: 60rpx 40rpx;
  451. display: flex;
  452. flex-direction: column;
  453. align-items: center;
  454. gap: 20rpx;
  455. margin-top: 10vh;
  456. }
  457. .error-icon {
  458. font-size: 80rpx;
  459. }
  460. .error-text {
  461. font-size: 30rpx;
  462. color: #6B7280;
  463. text-align: center;
  464. }
  465. </style>