join.vue 15 KB

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