join.vue 15 KB

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