join.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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 } 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. error: '',
  95. // 动态文案
  96. tipText: '',
  97. btnText: ''
  98. }
  99. },
  100. onLoad(query) {
  101. query = query || {}
  102. // 1. 解析家庭邀请码(单一参数,兼容 inviteCode/familyInviteCode)
  103. var familyInviteCode = query.familyInviteCode || query.inviteCode || ''
  104. if (!familyInviteCode && query.scene) {
  105. var sceneDecoded = decodeURIComponent(query.scene)
  106. // 支持 scene=fam_xxx 或 ref=xxx 格式
  107. if (sceneDecoded.indexOf('fam_') === 0) {
  108. familyInviteCode = sceneDecoded
  109. } else if (sceneDecoded.indexOf('ref=') === 0) {
  110. // ref=xxx 归为推广码
  111. } else if (sceneDecoded.length >= 6) {
  112. // 兜底:长度>=6 视为家庭邀请码
  113. familyInviteCode = familyInviteCode || sceneDecoded
  114. }
  115. }
  116. // 2. 解析推广码(兼容旧链接)
  117. var refCode = query.refCode || ''
  118. if (!refCode && query.scene) {
  119. var sceneDecoded = decodeURIComponent(query.scene)
  120. if (sceneDecoded.indexOf('ref=') === 0) {
  121. refCode = sceneDecoded.substring(4)
  122. }
  123. }
  124. // 3. 无任何有效参数 -> 首页
  125. if (!familyInviteCode && !refCode) {
  126. uni.reLaunch({ url: '/pages/index-home/index' })
  127. return
  128. }
  129. // 4. 有家庭邀请码 -> 存储并校验
  130. if (familyInviteCode) {
  131. this.familyInviteCode = familyInviteCode
  132. // 存入本地,供登录页/个人信息页使用
  133. uni.setStorageSync('pendingFamilyInviteCode', familyInviteCode)
  134. this.validateFamilyInvite(familyInviteCode)
  135. return
  136. }
  137. // 5. 仅有推广码 -> 走原有推广绑定流程
  138. this.refCode = refCode
  139. this.loading = false
  140. this.checkLoginStatus()
  141. },
  142. onShow() {
  143. // 从登录页返回,重新检查
  144. if (this.familyInviteCode) {
  145. this.checkLoginStatus()
  146. if (!this.inviteInfo) {
  147. this.validateFamilyInvite(this.familyInviteCode)
  148. }
  149. } else if (this.refCode) {
  150. this.checkLoginStatus()
  151. }
  152. },
  153. methods: {
  154. checkLoginStatus() {
  155. var token = uni.getStorageSync('token')
  156. this.isLoggedIn = !!token
  157. },
  158. // 校验家庭邀请码有效性(仅获取家庭信息用于展示)
  159. async validateFamilyInvite(code) {
  160. this.loading = true
  161. try {
  162. var res = await validateFamilyCode(code)
  163. if (res && res.code === 200) {
  164. this.inviteInfo = res.data
  165. // 已登录用户:检查是否已在目标家庭
  166. if (this.isLoggedIn) {
  167. await this.checkFamilyAndRedirect()
  168. } else {
  169. // 未登录:设置文案引导去登录
  170. this.setRedirectInfo(false)
  171. this.loading = false
  172. }
  173. } else {
  174. this.error = '邀请已失效'
  175. this.loading = false
  176. }
  177. } catch (e) {
  178. this.error = e.message || '邀请验证失败'
  179. this.loading = false
  180. }
  181. },
  182. // 检查家庭状态并决定跳转
  183. async checkFamilyAndRedirect() {
  184. try {
  185. var res = await checkUserFamily()
  186. if (res && res.data) {
  187. this.familyStatus = res.data
  188. // 同步 familyId
  189. if (res.data.inFamily && res.data.familyId) {
  190. this.$store && this.$store.commit('setFamilyId', res.data.familyId)
  191. uni.setStorageSync('familyId', res.data.familyId)
  192. }
  193. // 已在目标家庭 -> 直接进首页
  194. if (this.inviteInfo && this.familyStatus.inFamily &&
  195. this.familyStatus.familyId === this.inviteInfo.familyId) {
  196. this.setRedirectInfo(true)
  197. this.loading = false
  198. // 延迟跳转,让用户看到成功提示
  199. setTimeout(() => this.goHome(), 1000)
  200. return
  201. }
  202. // 不在目标家庭 -> 去完善个人信息页提交申请
  203. this.setRedirectInfo(false)
  204. this.loading = false
  205. } else {
  206. this.setRedirectInfo(false)
  207. this.loading = false
  208. }
  209. } catch (e) {
  210. console.log('检查家庭状态失败', e)
  211. this.setRedirectInfo(false)
  212. this.loading = false
  213. }
  214. },
  215. setRedirectInfo(isInTargetFamily) {
  216. if (isInTargetFamily) {
  217. this.tipText = '您已是该家庭成员'
  218. this.btnText = '进入首页'
  219. } else {
  220. this.tipText = '完善个人信息后将自动提交加入申请'
  221. this.btnText = '去完善信息'
  222. }
  223. },
  224. // 统一跳转逻辑
  225. goNext() {
  226. if (this.redirecting) return
  227. this.redirecting = true
  228. if (this.inviteInfo && this.familyStatus && this.familyStatus.inFamily &&
  229. this.familyStatus.familyId === this.inviteInfo.familyId) {
  230. this.goHome()
  231. } else {
  232. // 跳转个人信息完善页,带上邀请码参数
  233. uni.navigateTo({
  234. url: '/pages/user-edit/user-edit?familyInviteCode=' + this.familyInviteCode
  235. })
  236. }
  237. },
  238. goHome() {
  239. uni.reLaunch({ url: '/pages/index-home/index' })
  240. },
  241. // 登录页跳转(带上邀请码)
  242. goLogin() {
  243. uni.navigateTo({
  244. url: '/pages/login/login?familyInviteCode=' + this.familyInviteCode
  245. })
  246. },
  247. // 推广码场景:去登录
  248. goLoginWithRef() {
  249. uni.navigateTo({
  250. url: '/pages/login/login?refCode=' + this.refCode
  251. })
  252. },
  253. // 推广码绑定
  254. async bindRef() {
  255. if (!this.refCode) {
  256. uni.showToast({ title: '推广码为空', icon: 'none' })
  257. return
  258. }
  259. this.bindLoading = true
  260. try {
  261. var res = await bindReferral(this.refCode)
  262. if (res && res.code === 200) {
  263. this.bindDone = true
  264. uni.showToast({ title: '绑定成功', icon: 'success' })
  265. } else {
  266. uni.showToast({ title: (res && res.message) || '绑定失败', icon: 'none' })
  267. }
  268. } catch (e) {
  269. uni.showToast({ title: e.message || '绑定失败', icon: 'none' })
  270. }
  271. this.bindLoading = false
  272. }
  273. }
  274. }
  275. </script>
  276. <style scoped>
  277. .page {
  278. padding: 32rpx;
  279. min-height: 100vh;
  280. background: linear-gradient(135deg, #FFF7ED 0%, #FEF3C7 100%);
  281. display: flex;
  282. flex-direction: column;
  283. align-items: center;
  284. }
  285. .loading {
  286. display: flex;
  287. flex-direction: column;
  288. align-items: center;
  289. justify-content: center;
  290. min-height: 60vh;
  291. }
  292. .loading-icon {
  293. font-size: 80rpx;
  294. margin-bottom: 20rpx;
  295. }
  296. .loading-text {
  297. color: #666;
  298. font-size: 28rpx;
  299. }
  300. .invite-card {
  301. width: 100%;
  302. max-width: 600rpx;
  303. background: #FFFFFF;
  304. border-radius: 24rpx;
  305. padding: 40rpx;
  306. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 008);
  307. margin-top: 10vh;
  308. }
  309. .invite-header {
  310. display: flex;
  311. flex-direction: column;
  312. align-items: center;
  313. margin-bottom: 32rpx;
  314. }
  315. .invite-icon {
  316. font-size: 80rpx;
  317. margin-bottom: 16rpx;
  318. }
  319. .invite-title {
  320. font-size: 32rpx;
  321. font-weight: 700;
  322. color: #1E293B;
  323. }
  324. .invite-body {
  325. display: flex;
  326. flex-direction: column;
  327. align-items: center;
  328. margin-bottom: 40rpx;
  329. padding: 24rpx;
  330. background: #FFF7ED;
  331. border-radius: 16rpx;
  332. }
  333. .family-name {
  334. font-size: 40rpx;
  335. font-weight: 700;
  336. color: #F97316;
  337. margin-bottom: 12rpx;
  338. }
  339. .inviter {
  340. font-size: 28rpx;
  341. color: #64748B;
  342. margin-bottom: 8rpx;
  343. }
  344. .inviter-avatar {
  345. width: 80rpx;
  346. height: 80rpx;
  347. border-radius: 50%;
  348. margin-bottom: 12rpx;
  349. background: #F1F5F9;
  350. }
  351. .member-count {
  352. font-size: 24rpx;
  353. color: #94A3B8;
  354. }
  355. .action-area {
  356. display: flex;
  357. flex-direction: column;
  358. gap: 16rpx;
  359. width: 100%;
  360. }
  361. .tip-text {
  362. font-size: 24rpx;
  363. color: #94A3B8;
  364. text-align: center;
  365. }
  366. .btn {
  367. width: 100%;
  368. padding: 28rpx;
  369. border-radius: 16rpx;
  370. font-size: 30rpx;
  371. font-weight: 600;
  372. text-align: center;
  373. border: none;
  374. }
  375. .btn-primary {
  376. background: linear-gradient(135deg, #F97316, #EA580C);
  377. color: #FFFFFF;
  378. }
  379. .btn-primary:disabled {
  380. opacity: 06;
  381. }
  382. .btn-outline {
  383. background: #FFFFFF;
  384. color: #F97316;
  385. border: 2rpx solid #F97316;
  386. }
  387. .success-area {
  388. display: flex;
  389. flex-direction: column;
  390. align-items: center;
  391. gap: 16rpx;
  392. }
  393. .success-icon {
  394. font-size: 80rpx;
  395. }
  396. .success-text {
  397. font-size: 36rpx;
  398. font-weight: 700;
  399. color: #16A34A;
  400. }
  401. .success-sub {
  402. font-size: 28rpx;
  403. color: #6B7280;
  404. margin-bottom: 24rpx;
  405. }
  406. .error-card {
  407. width: 100%;
  408. max-width: 600rpx;
  409. background: #FFFFFF;
  410. border-radius: 24rpx;
  411. padding: 60rpx 40rpx;
  412. display: flex;
  413. flex-direction: column;
  414. align-items: center;
  415. gap: 20rpx;
  416. margin-top: 10vh;
  417. }
  418. .error-icon {
  419. font-size: 80rpx;
  420. }
  421. .error-text {
  422. font-size: 30rpx;
  423. color: #6B7280;
  424. text-align: center;
  425. }
  426. </style>