join.vue 12 KB

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