join.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  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. <!-- 未登录:显示登录按钮 -->
  29. <view class="action-area" v-if="!isLoggedIn">
  30. <button class="btn btn-primary" @tap="handleWechatLogin" :disabled="loginLoading">
  31. <text v-if="!loginLoading">微信一键登录并加入家庭</text>
  32. <text v-else>登录中...</text>
  33. </button>
  34. <text class="tip-text">登录后将自动加入此家庭</text>
  35. </view>
  36. <!-- 已登录:显示关系选择 -->
  37. <view class="action-area" v-if="isLoggedIn && !accepted && !requestSubmitted">
  38. <!-- 已有家庭时显示合并选项 -->
  39. <view class="family-warning" v-if="familyStatus && familyStatus.inFamily">
  40. <text class="warning-icon">⚠️</text>
  41. <text class="warning-text">
  42. 您当前已加入「{{ familyStatus.familyName }}」家庭,加入新家庭可选择合并(成员将一起迁移)或拒绝
  43. </text>
  44. </view>
  45. <!-- 关系选择 -->
  46. <view class="form-group">
  47. <text class="form-label">您在家庭中的身份</text>
  48. <picker @change="onFamilyRoleChange" :value="familyRoleIndex" :range="familyRoleOptions">
  49. <view class="picker-select">
  50. <text>{{ familyRoleOptions[familyRoleIndex] || '请选择您的身份' }}</text>
  51. <text class="picker-arrow">›</text>
  52. </view>
  53. </picker>
  54. </view>
  55. <!-- 已有家庭时显示合并勾选框 -->
  56. <view class="form-group" v-if="familyStatus && familyStatus.inFamily">
  57. <label class="checkbox-label" @tap="toggleMerge">
  58. <view class="checkbox" :class="{ checked: mergeFamily }">
  59. <text v-if="mergeFamily">✓</text>
  60. </view>
  61. <text>合并家庭(我的家庭成员将一起迁入新家庭)</text>
  62. </label>
  63. </view>
  64. <button class="btn btn-primary" @tap="handleSubmitRequest" :disabled="acceptLoading">
  65. <text v-if="!acceptLoading">提交申请</text>
  66. <text v-else>提交中...</text>
  67. </button>
  68. </view>
  69. <!-- 已提交申请,等待审批 -->
  70. <view class="pending-card" v-if="requestSubmitted && !accepted">
  71. <view class="pending-icon">🕐</view>
  72. <text class="pending-title">申请已提交</text>
  73. <text class="pending-text">等待家庭管理员审批</text>
  74. <text class="pending-hint">通常几分钟内会有回复</text>
  75. </view>
  76. <!-- 申请被拒绝 -->
  77. <view class="error-area" v-if="requestSubmitted && requestStatus === 'rejected'">
  78. <view class="error-icon">❌</view>
  79. <text class="error-text-main">申请已被拒绝</text>
  80. <text class="error-text-sub" v-if="rejectMessage">{{ rejectMessage }}</text>
  81. <button class="btn btn-outline" @tap="goHome">返回首页</button>
  82. </view>
  83. <!-- 加入成功 -->
  84. <view class="success-area" v-if="accepted">
  85. <view class="success-icon">✅</view>
  86. <text class="success-text">加入成功!</text>
  87. <text class="success-sub">欢迎加入「{{ inviteInfo.familyName }}」</text>
  88. <button class="btn btn-primary" @tap="goHome">进入首页</button>
  89. </view>
  90. </view>
  91. <!-- 推广码绑定卡片 -->
  92. <view class="invite-card" v-if="!loading && refCode && !bindDone">
  93. <view class="invite-header">
  94. <view class="invite-icon">🤝</view>
  95. <text class="invite-title">推广绑定</text>
  96. </view>
  97. <view class="invite-body">
  98. <text class="member-count">推荐人推广码:{{ refCode }}</text>
  99. </view>
  100. <view class="action-area" v-if="isLoggedIn">
  101. <button class="btn btn-primary" @tap="bindRef" :disabled="bindLoading">
  102. <text v-if="!bindLoading">绑定推荐人</text>
  103. <text v-else>绑定中...</text>
  104. </button>
  105. <text class="tip-text">绑定后该推荐人后续消费时记入其家庭公共账户</text>
  106. </view>
  107. <view class="action-area" v-else>
  108. <button class="btn btn-primary" @tap="handleWechatLogin" :disabled="loginLoading">
  109. <text v-if="!loginLoading">微信一键登录</text>
  110. <text v-else>登录中...</text>
  111. </button>
  112. <text class="tip-text">登录后可绑定推荐人</text>
  113. </view>
  114. </view>
  115. <!-- 推广码绑定成功 -->
  116. <view class="invite-card" v-if="!loading && refCode && bindDone">
  117. <view class="success-area">
  118. <view class="success-icon">✅</view>
  119. <text class="success-text">绑定成功</text>
  120. <text class="success-sub">已绑定推荐人,可进入首页探索</text>
  121. <button class="btn btn-primary" @tap="goHome">进入首页</button>
  122. </view>
  123. </view>
  124. <!-- 邀请无效 -->
  125. <view class="error-card" v-if="!loading && error">
  126. <view class="error-icon">❌</view>
  127. <text class="error-text">{{ error }}</text>
  128. <button class="btn btn-outline" @tap="goHome">返回首页</button>
  129. </view>
  130. </view>
  131. </template>
  132. <script>
  133. import { validateInvitation, checkUserFamily, bindReferral, requestJoinByCode, getMyPendingRequest } from '@/utils/api.js'
  134. export default {
  135. data() {
  136. return {
  137. token: '',
  138. refCode: '',
  139. bindLoading: false,
  140. bindDone: false,
  141. loading: true,
  142. loginLoading: false,
  143. acceptLoading: false,
  144. isLoggedIn: false,
  145. inviteInfo: null,
  146. familyStatus: null,
  147. familyRoleIndex: 0,
  148. mergeFamily: false,
  149. accepted: false,
  150. requestSubmitted: false,
  151. requestStatus: '',
  152. requestTimer: null,
  153. rejectMessage: '',
  154. error: '',
  155. familyRoleOptions: ['爸爸', '妈妈', '爷爷', '奶奶', '姥爷', '姥姥', '其他']
  156. }
  157. },
  158. onLoad(query) {
  159. // token:优先从 query 参数,其次从场景值(invite=xxx)
  160. var token = query.token || query.inviteToken || ''
  161. if (!token) {
  162. var scene = query.scene
  163. if (scene) {
  164. var sceneDecoded = decodeURIComponent(scene)
  165. if (sceneDecoded.indexOf('invite=') === 0) {
  166. token = sceneDecoded.substring(7)
  167. }
  168. }
  169. }
  170. // refCode:query.refCode 或场景值(ref=xxx)— 推广码注册绑定
  171. var refCode = query.refCode || ''
  172. if (!refCode && query.scene) {
  173. var refScene = decodeURIComponent(query.scene)
  174. if (refScene.indexOf('ref=') === 0) {
  175. refCode = refScene.substring(4)
  176. }
  177. }
  178. if (!token && !refCode) {
  179. this.error = '邀请链接无效'
  180. this.loading = false
  181. return
  182. }
  183. if (refCode) {
  184. // 推广码模式:仅存储 refCode,无需校验邀请
  185. this.refCode = refCode
  186. this.loading = false
  187. this.checkLoginStatus()
  188. return
  189. }
  190. this.token = token
  191. this.checkLoginStatus()
  192. this.validateToken()
  193. },
  194. onShow() {
  195. // 从登录页返回时重新检查登录状态
  196. if (this.token) {
  197. this.checkLoginStatus()
  198. if (!this.inviteInfo) {
  199. this.validateToken()
  200. }
  201. }
  202. },
  203. onUnload() {
  204. if (this.requestTimer) {
  205. clearInterval(this.requestTimer)
  206. this.requestTimer = null
  207. }
  208. },
  209. methods: {
  210. checkLoginStatus() {
  211. var token = uni.getStorageSync('token')
  212. this.isLoggedIn = !!token
  213. if (this.isLoggedIn) {
  214. this.checkFamily()
  215. }
  216. },
  217. async validateToken() {
  218. this.loading = true
  219. try {
  220. var res = await validateInvitation(this.token)
  221. if (res && res.data) {
  222. this.inviteInfo = res.data
  223. } else {
  224. this.error = '邀请已失效'
  225. }
  226. } catch (e) {
  227. this.error = e.message || '邀请验证失败'
  228. }
  229. this.loading = false
  230. },
  231. async checkFamily() {
  232. try {
  233. var res = await checkUserFamily()
  234. if (res && res.data) {
  235. this.familyStatus = res.data
  236. // 已在家庭内:同步 familyId 到 store(hasFamily getter 依赖)
  237. if (res.data.inFamily && res.data.familyId) {
  238. this.$store && this.$store.commit('setFamilyId', res.data.familyId)
  239. uni.setStorageSync('familyId', res.data.familyId)
  240. }
  241. // 如果已经在目标家庭,直接显示成功
  242. if (this.inviteInfo && this.familyStatus.inFamily && this.familyStatus.familyId === this.inviteInfo.familyId) {
  243. this.accepted = true
  244. }
  245. }
  246. } catch (e) {
  247. console.log('检查家庭状态失败', e)
  248. }
  249. },
  250. async handleWechatLogin() {
  251. this.loginLoading = true
  252. try {
  253. var that = this
  254. // 微信登录
  255. var loginRes = await new Promise(function(resolve, reject) {
  256. uni.login({
  257. provider: 'weixin',
  258. success: function(res) {
  259. resolve(res)
  260. },
  261. fail: function(err) {
  262. reject(err)
  263. }
  264. })
  265. })
  266. var code = loginRes.code
  267. if (!code) {
  268. uni.showToast({ title: '登录失败', icon: 'none' })
  269. this.loginLoading = false
  270. return
  271. }
  272. // 调起微信登录
  273. var { wechatLogin } = require('@/utils/api.js')
  274. var res = await wechatLogin(code, {})
  275. if (res && res.data) {
  276. // 保存登录信息
  277. var data = res.data
  278. uni.setStorageSync('token', data.token)
  279. uni.setStorageSync('userId', data.userId)
  280. uni.setStorageSync('role', data.role)
  281. uni.setStorageSync('currentRole', data.role)
  282. uni.setStorageSync('isSwitchedChild', false)
  283. uni.setStorageSync('isSwitchedTeacher', false)
  284. if (data.openid) {
  285. uni.setStorageSync('openid', data.openid)
  286. }
  287. that.$store && that.$store.commit('setToken', data.token)
  288. that.$store && that.$store.commit('setFamilyId', data.familyId)
  289. this.isLoggedIn = true
  290. uni.showToast({ title: '登录成功', icon: 'success' })
  291. // 重新检查家庭状态
  292. await this.checkFamily()
  293. }
  294. } catch (e) {
  295. uni.showToast({ title: '登录失败', icon: 'none' })
  296. console.log('微信登录失败', e)
  297. }
  298. this.loginLoading = false
  299. },
  300. onFamilyRoleChange(e) {
  301. this.familyRoleIndex = e.detail.value
  302. },
  303. toggleMerge() {
  304. this.mergeFamily = !this.mergeFamily
  305. },
  306. async handleSubmitRequest() {
  307. var familyRole = this.familyRoleOptions[this.familyRoleIndex] || ''
  308. if (!familyRole) {
  309. uni.showToast({ title: '请选择您在家庭中的身份', icon: 'none' })
  310. return
  311. }
  312. this.acceptLoading = true
  313. try {
  314. var res = await requestJoinByCode(this.token)
  315. if (res && res.code === 200) {
  316. this.requestSubmitted = true
  317. this.requestStatus = 'pending'
  318. this.acceptLoading = false
  319. uni.showToast({ title: '申请已提交,等待审批', icon: 'success' })
  320. this.startPolling()
  321. } else {
  322. this.acceptLoading = false
  323. uni.showToast({ title: res.message || '提交失败', icon: 'none' })
  324. }
  325. } catch (e) {
  326. this.acceptLoading = false
  327. uni.showToast({ title: e.message || '提交失败', icon: 'none' })
  328. }
  329. },
  330. startPolling() {
  331. var self = this
  332. if (this.requestTimer) {
  333. clearInterval(this.requestTimer)
  334. }
  335. this.requestTimer = setInterval(function() {
  336. if (self.accepted || self.requestStatus === 'rejected') {
  337. clearInterval(self.requestTimer)
  338. self.requestTimer = null
  339. return
  340. }
  341. self.checkRequestStatus()
  342. }, 5000)
  343. },
  344. async checkRequestStatus() {
  345. try {
  346. var res = await getMyPendingRequest()
  347. if (res && res.code === 200 && res.data) {
  348. var requestData = res.data
  349. this.requestStatus = requestData.status || 'pending'
  350. if (this.requestStatus === 'approved') {
  351. clearInterval(this.requestTimer)
  352. this.requestTimer = null
  353. this.accepted = true
  354. uni.setStorageSync('familyId', this.inviteInfo.familyId)
  355. uni.setStorageSync('currentFamilyId', this.inviteInfo.familyId)
  356. // 审批通过后同步 familyId 到 store(hasFamily getter 依赖)
  357. this.$store && this.$store.commit('setFamilyId', this.inviteInfo.familyId)
  358. uni.showToast({ title: '加入家庭成功', icon: 'success' })
  359. } else if (this.requestStatus === 'rejected') {
  360. clearInterval(this.requestTimer)
  361. this.requestTimer = null
  362. this.rejectMessage = requestData.adminComment || ''
  363. uni.showToast({ title: '申请已被拒绝', icon: 'none' })
  364. }
  365. }
  366. } catch (e) {
  367. console.log('轮询请求状态失败', e)
  368. }
  369. },
  370. goHome() {
  371. uni.reLaunch({ url: '/pages/index-home/index' })
  372. },
  373. async bindRef() {
  374. if (!this.refCode) {
  375. uni.showToast({ title: '推广码为空', icon: 'none' })
  376. return
  377. }
  378. this.bindLoading = true
  379. try {
  380. var res = await bindReferral(this.refCode)
  381. if (res && res.code === 200) {
  382. this.bindDone = true
  383. uni.showToast({ title: '绑定成功', icon: 'success' })
  384. } else {
  385. uni.showToast({ title: (res && res.message) || '绑定失败', icon: 'none' })
  386. }
  387. } catch (e) {
  388. uni.showToast({ title: e.message || '绑定失败', icon: 'none' })
  389. }
  390. this.bindLoading = false
  391. }
  392. }
  393. }
  394. </script>
  395. <style scoped>
  396. .page {
  397. padding: 32rpx;
  398. min-height: 100vh;
  399. background: linear-gradient(135deg, #FFF7ED 0%, #FEF3C7 100%);
  400. display: flex;
  401. flex-direction: column;
  402. align-items: center;
  403. }
  404. .loading {
  405. display: flex;
  406. flex-direction: column;
  407. align-items: center;
  408. justify-content: center;
  409. min-height: 60vh;
  410. }
  411. .loading-icon {
  412. font-size: 80rpx;
  413. margin-bottom: 20rpx;
  414. }
  415. .loading-text {
  416. color: #666;
  417. font-size: 28rpx;
  418. }
  419. .invite-card {
  420. width: 100%;
  421. max-width: 600rpx;
  422. background: #FFFFFF;
  423. border-radius: 24rpx;
  424. padding: 40rpx;
  425. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
  426. margin-top: 10vh;
  427. }
  428. .invite-header {
  429. display: flex;
  430. flex-direction: column;
  431. align-items: center;
  432. margin-bottom: 32rpx;
  433. }
  434. .invite-icon {
  435. font-size: 80rpx;
  436. margin-bottom: 16rpx;
  437. }
  438. .invite-title {
  439. font-size: 32rpx;
  440. font-weight: 700;
  441. color: #1E293B;
  442. }
  443. .invite-body {
  444. display: flex;
  445. flex-direction: column;
  446. align-items: center;
  447. margin-bottom: 40rpx;
  448. padding: 24rpx;
  449. background: #FFF7ED;
  450. border-radius: 16rpx;
  451. }
  452. .family-name {
  453. font-size: 40rpx;
  454. font-weight: 700;
  455. color: #F97316;
  456. margin-bottom: 12rpx;
  457. }
  458. .inviter {
  459. font-size: 28rpx;
  460. color: #64748B;
  461. margin-bottom: 8rpx;
  462. }
  463. .inviter-avatar {
  464. width: 80rpx;
  465. height: 80rpx;
  466. border-radius: 50%;
  467. margin-bottom: 12rpx;
  468. background: #F1F5F9;
  469. }
  470. .member-count {
  471. font-size: 24rpx;
  472. color: #94A3B8;
  473. }
  474. .action-area {
  475. display: flex;
  476. flex-direction: column;
  477. gap: 16rpx;
  478. }
  479. .family-warning {
  480. display: flex;
  481. align-items: flex-start;
  482. padding: 20rpx;
  483. background: #FEF3C7;
  484. border-radius: 12rpx;
  485. gap: 12rpx;
  486. }
  487. .warning-icon {
  488. font-size: 32rpx;
  489. flex-shrink: 0;
  490. }
  491. .warning-text {
  492. font-size: 24rpx;
  493. color: #92400E;
  494. line-height: 1.5;
  495. }
  496. .form-group {
  497. margin-bottom: 20rpx;
  498. }
  499. .form-label {
  500. display: block;
  501. font-size: 28rpx;
  502. color: #374151;
  503. margin-bottom: 12rpx;
  504. font-weight: 500;
  505. }
  506. .picker-select {
  507. display: flex;
  508. justify-content: space-between;
  509. align-items: center;
  510. padding: 24rpx 28rpx;
  511. background: #F9FAFB;
  512. border: 2rpx solid #E5E7EB;
  513. border-radius: 16rpx;
  514. font-size: 28rpx;
  515. color: #1E293B;
  516. }
  517. .picker-arrow {
  518. font-size: 36rpx;
  519. color: #9CA3AF;
  520. }
  521. .checkbox-label {
  522. display: flex;
  523. align-items: center;
  524. gap: 12rpx;
  525. font-size: 26rpx;
  526. color: #4B5563;
  527. }
  528. .checkbox {
  529. width: 36rpx;
  530. height: 36rpx;
  531. border: 2rpx solid #D1D5DB;
  532. border-radius: 8rpx;
  533. display: flex;
  534. align-items: center;
  535. justify-content: center;
  536. flex-shrink: 0;
  537. }
  538. .checkbox.checked {
  539. background: #F97316;
  540. border-color: #F97316;
  541. color: #FFFFFF;
  542. font-size: 24rpx;
  543. font-weight: 700;
  544. }
  545. .btn {
  546. width: 100%;
  547. padding: 28rpx;
  548. border-radius: 16rpx;
  549. font-size: 30rpx;
  550. font-weight: 600;
  551. text-align: center;
  552. border: none;
  553. }
  554. .btn-primary {
  555. background: linear-gradient(135deg, #F97316, #EA580C);
  556. color: #FFFFFF;
  557. }
  558. .btn-primary:disabled {
  559. opacity: 0.6;
  560. }
  561. .btn-outline {
  562. background: #FFFFFF;
  563. color: #F97316;
  564. border: 2rpx solid #F97316;
  565. }
  566. .tip-text {
  567. font-size: 24rpx;
  568. color: #94A3B8;
  569. text-align: center;
  570. }
  571. .success-area {
  572. display: flex;
  573. flex-direction: column;
  574. align-items: center;
  575. gap: 16rpx;
  576. }
  577. .success-icon {
  578. font-size: 80rpx;
  579. }
  580. .success-text {
  581. font-size: 36rpx;
  582. font-weight: 700;
  583. color: #16A34A;
  584. }
  585. .success-sub {
  586. font-size: 28rpx;
  587. color: #6B7280;
  588. margin-bottom: 24rpx;
  589. }
  590. .error-card {
  591. width: 100%;
  592. max-width: 600rpx;
  593. background: #FFFFFF;
  594. border-radius: 24rpx;
  595. padding: 60rpx 40rpx;
  596. display: flex;
  597. flex-direction: column;
  598. align-items: center;
  599. gap: 20rpx;
  600. margin-top: 10vh;
  601. }
  602. .error-icon {
  603. font-size: 80rpx;
  604. }
  605. .error-text {
  606. font-size: 30rpx;
  607. color: #6B7280;
  608. text-align: center;
  609. }
  610. .pending-card {
  611. display: flex;
  612. flex-direction: column;
  613. align-items: center;
  614. gap: 12rpx;
  615. padding: 40rpx;
  616. background: #FEF3C7;
  617. border-radius: 20rpx;
  618. margin-top: 20rpx;
  619. }
  620. .pending-icon {
  621. font-size: 72rpx;
  622. }
  623. .pending-title {
  624. font-size: 32rpx;
  625. font-weight: 700;
  626. color: #92400E;
  627. }
  628. .pending-text {
  629. font-size: 28rpx;
  630. color: #78350F;
  631. }
  632. .pending-hint {
  633. font-size: 24rpx;
  634. color: #A16207;
  635. }
  636. .error-area {
  637. display: flex;
  638. flex-direction: column;
  639. align-items: center;
  640. gap: 12rpx;
  641. padding: 40rpx;
  642. background: #FEF2F2;
  643. border-radius: 20rpx;
  644. margin-top: 20rpx;
  645. }
  646. .error-text-main {
  647. font-size: 32rpx;
  648. font-weight: 700;
  649. color: #DC2626;
  650. }
  651. .error-text-sub {
  652. font-size: 26rpx;
  653. color: #991B1B;
  654. margin-bottom: 20rpx;
  655. }
  656. </style>