join.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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 参数,其次从场景值
  160. var token = query.token || query.inviteToken || ''
  161. if (!token) {
  162. var scene = query.scene
  163. if (scene) {
  164. var sceneDecoded = decodeURIComponent(scene)
  165. // 新格式:JSON 场景值 {"t":"TOKEN"}
  166. if (sceneDecoded.indexOf('{') === 0) {
  167. try {
  168. var sceneObj = JSON.parse(sceneDecoded)
  169. token = sceneObj.t || sceneObj.token || ''
  170. } catch (e) {
  171. token = ''
  172. }
  173. } else if (sceneDecoded.indexOf('invite=') === 0) {
  174. // 旧格式兼容
  175. token = sceneDecoded.substring(7)
  176. } else {
  177. token = sceneDecoded
  178. }
  179. }
  180. }
  181. // refCode:query.refCode 或场景值(ref=xxx)— 推广码注册绑定
  182. var refCode = query.refCode || ''
  183. if (!refCode && query.scene) {
  184. var refScene = decodeURIComponent(query.scene)
  185. if (refScene.indexOf('ref=') === 0) {
  186. refCode = refScene.substring(4)
  187. }
  188. }
  189. if (!token && !refCode) {
  190. this.error = '邀请链接无效'
  191. this.loading = false
  192. return
  193. }
  194. if (refCode) {
  195. // 推广码模式:仅存储 refCode,无需校验邀请
  196. this.refCode = refCode
  197. this.loading = false
  198. this.checkLoginStatus()
  199. return
  200. }
  201. this.token = token
  202. this.checkLoginStatus()
  203. this.validateToken()
  204. },
  205. onShow() {
  206. // 从登录页返回时重新检查登录状态
  207. if (this.token) {
  208. this.checkLoginStatus()
  209. if (!this.inviteInfo) {
  210. this.validateToken()
  211. }
  212. }
  213. },
  214. onUnload() {
  215. if (this.requestTimer) {
  216. clearInterval(this.requestTimer)
  217. this.requestTimer = null
  218. }
  219. },
  220. methods: {
  221. checkLoginStatus() {
  222. var token = uni.getStorageSync('token')
  223. this.isLoggedIn = !!token
  224. if (this.isLoggedIn) {
  225. this.checkFamily()
  226. }
  227. },
  228. async validateToken() {
  229. this.loading = true
  230. try {
  231. var res = await validateInvitation(this.token)
  232. if (res && res.data) {
  233. this.inviteInfo = res.data
  234. } else {
  235. this.error = '邀请已失效'
  236. }
  237. } catch (e) {
  238. this.error = e.message || '邀请验证失败'
  239. }
  240. this.loading = false
  241. },
  242. async checkFamily() {
  243. try {
  244. var res = await checkUserFamily()
  245. if (res && res.data) {
  246. this.familyStatus = res.data
  247. // 已在家庭内:同步 familyId 到 store(hasFamily getter 依赖)
  248. if (res.data.inFamily && res.data.familyId) {
  249. this.$store && this.$store.commit('setFamilyId', res.data.familyId)
  250. uni.setStorageSync('familyId', res.data.familyId)
  251. }
  252. // 如果已经在目标家庭,直接显示成功
  253. if (this.inviteInfo && this.familyStatus.inFamily && this.familyStatus.familyId === this.inviteInfo.familyId) {
  254. this.accepted = true
  255. }
  256. }
  257. } catch (e) {
  258. console.log('检查家庭状态失败', e)
  259. }
  260. },
  261. async handleWechatLogin() {
  262. this.loginLoading = true
  263. try {
  264. var that = this
  265. // 微信登录
  266. var loginRes = await new Promise(function(resolve, reject) {
  267. uni.login({
  268. provider: 'weixin',
  269. success: function(res) {
  270. resolve(res)
  271. },
  272. fail: function(err) {
  273. reject(err)
  274. }
  275. })
  276. })
  277. var code = loginRes.code
  278. if (!code) {
  279. uni.showToast({ title: '登录失败', icon: 'none' })
  280. this.loginLoading = false
  281. return
  282. }
  283. // 调起微信登录
  284. var { wechatLogin } = require('@/utils/api.js')
  285. var res = await wechatLogin(code, {})
  286. if (res && res.data) {
  287. // 保存登录信息
  288. var data = res.data
  289. uni.setStorageSync('token', data.token)
  290. uni.setStorageSync('userId', data.userId)
  291. uni.setStorageSync('role', data.role)
  292. uni.setStorageSync('currentRole', data.role)
  293. uni.setStorageSync('isSwitchedChild', false)
  294. uni.setStorageSync('isSwitchedTeacher', false)
  295. if (data.openid) {
  296. uni.setStorageSync('openid', data.openid)
  297. }
  298. that.$store && that.$store.commit('setToken', data.token)
  299. that.$store && that.$store.commit('setFamilyId', data.familyId)
  300. this.isLoggedIn = true
  301. uni.showToast({ title: '登录成功', icon: 'success' })
  302. // 重新检查家庭状态
  303. await this.checkFamily()
  304. }
  305. } catch (e) {
  306. uni.showToast({ title: '登录失败', icon: 'none' })
  307. console.log('微信登录失败', e)
  308. }
  309. this.loginLoading = false
  310. },
  311. onFamilyRoleChange(e) {
  312. this.familyRoleIndex = e.detail.value
  313. },
  314. toggleMerge() {
  315. this.mergeFamily = !this.mergeFamily
  316. },
  317. async handleSubmitRequest() {
  318. var familyRole = this.familyRoleOptions[this.familyRoleIndex] || ''
  319. if (!familyRole) {
  320. uni.showToast({ title: '请选择您在家庭中的身份', icon: 'none' })
  321. return
  322. }
  323. this.acceptLoading = true
  324. try {
  325. var res = await requestJoinByCode(this.token, familyRole)
  326. if (res && res.code === 200) {
  327. this.requestSubmitted = true
  328. this.requestStatus = 'pending'
  329. this.acceptLoading = false
  330. uni.showToast({ title: '申请已提交,等待审批', icon: 'success' })
  331. this.startPolling()
  332. } else {
  333. this.acceptLoading = false
  334. uni.showToast({ title: res.message || '提交失败', icon: 'none' })
  335. }
  336. } catch (e) {
  337. this.acceptLoading = false
  338. uni.showToast({ title: e.message || '提交失败', icon: 'none' })
  339. }
  340. },
  341. startPolling() {
  342. var self = this
  343. if (this.requestTimer) {
  344. clearInterval(this.requestTimer)
  345. }
  346. this.requestTimer = setInterval(function() {
  347. if (self.accepted || self.requestStatus === 'rejected') {
  348. clearInterval(self.requestTimer)
  349. self.requestTimer = null
  350. return
  351. }
  352. self.checkRequestStatus()
  353. }, 5000)
  354. },
  355. async checkRequestStatus() {
  356. try {
  357. var res = await getMyPendingRequest()
  358. if (res && res.code === 200 && res.data) {
  359. var requestData = res.data
  360. this.requestStatus = requestData.status || 'pending'
  361. if (this.requestStatus === 'approved') {
  362. clearInterval(this.requestTimer)
  363. this.requestTimer = null
  364. this.accepted = true
  365. uni.setStorageSync('familyId', this.inviteInfo.familyId)
  366. uni.setStorageSync('currentFamilyId', this.inviteInfo.familyId)
  367. // 审批通过后同步 familyId 到 store(hasFamily getter 依赖)
  368. this.$store && this.$store.commit('setFamilyId', this.inviteInfo.familyId)
  369. uni.showToast({ title: '加入家庭成功', icon: 'success' })
  370. } else if (this.requestStatus === 'rejected') {
  371. clearInterval(this.requestTimer)
  372. this.requestTimer = null
  373. this.rejectMessage = requestData.adminComment || ''
  374. uni.showToast({ title: '申请已被拒绝', icon: 'none' })
  375. }
  376. }
  377. } catch (e) {
  378. console.log('轮询请求状态失败', e)
  379. }
  380. },
  381. goHome() {
  382. uni.reLaunch({ url: '/pages/index-home/index' })
  383. },
  384. async bindRef() {
  385. if (!this.refCode) {
  386. uni.showToast({ title: '推广码为空', icon: 'none' })
  387. return
  388. }
  389. this.bindLoading = true
  390. try {
  391. var res = await bindReferral(this.refCode)
  392. if (res && res.code === 200) {
  393. this.bindDone = true
  394. uni.showToast({ title: '绑定成功', icon: 'success' })
  395. } else {
  396. uni.showToast({ title: (res && res.message) || '绑定失败', icon: 'none' })
  397. }
  398. } catch (e) {
  399. uni.showToast({ title: e.message || '绑定失败', icon: 'none' })
  400. }
  401. this.bindLoading = false
  402. }
  403. }
  404. }
  405. </script>
  406. <style scoped>
  407. .page {
  408. padding: 32rpx;
  409. min-height: 100vh;
  410. background: linear-gradient(135deg, #FFF7ED 0%, #FEF3C7 100%);
  411. display: flex;
  412. flex-direction: column;
  413. align-items: center;
  414. }
  415. .loading {
  416. display: flex;
  417. flex-direction: column;
  418. align-items: center;
  419. justify-content: center;
  420. min-height: 60vh;
  421. }
  422. .loading-icon {
  423. font-size: 80rpx;
  424. margin-bottom: 20rpx;
  425. }
  426. .loading-text {
  427. color: #666;
  428. font-size: 28rpx;
  429. }
  430. .invite-card {
  431. width: 100%;
  432. max-width: 600rpx;
  433. background: #FFFFFF;
  434. border-radius: 24rpx;
  435. padding: 40rpx;
  436. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
  437. margin-top: 10vh;
  438. }
  439. .invite-header {
  440. display: flex;
  441. flex-direction: column;
  442. align-items: center;
  443. margin-bottom: 32rpx;
  444. }
  445. .invite-icon {
  446. font-size: 80rpx;
  447. margin-bottom: 16rpx;
  448. }
  449. .invite-title {
  450. font-size: 32rpx;
  451. font-weight: 700;
  452. color: #1E293B;
  453. }
  454. .invite-body {
  455. display: flex;
  456. flex-direction: column;
  457. align-items: center;
  458. margin-bottom: 40rpx;
  459. padding: 24rpx;
  460. background: #FFF7ED;
  461. border-radius: 16rpx;
  462. }
  463. .family-name {
  464. font-size: 40rpx;
  465. font-weight: 700;
  466. color: #F97316;
  467. margin-bottom: 12rpx;
  468. }
  469. .inviter {
  470. font-size: 28rpx;
  471. color: #64748B;
  472. margin-bottom: 8rpx;
  473. }
  474. .inviter-avatar {
  475. width: 80rpx;
  476. height: 80rpx;
  477. border-radius: 50%;
  478. margin-bottom: 12rpx;
  479. background: #F1F5F9;
  480. }
  481. .member-count {
  482. font-size: 24rpx;
  483. color: #94A3B8;
  484. }
  485. .action-area {
  486. display: flex;
  487. flex-direction: column;
  488. gap: 16rpx;
  489. }
  490. .family-warning {
  491. display: flex;
  492. align-items: flex-start;
  493. padding: 20rpx;
  494. background: #FEF3C7;
  495. border-radius: 12rpx;
  496. gap: 12rpx;
  497. }
  498. .warning-icon {
  499. font-size: 32rpx;
  500. flex-shrink: 0;
  501. }
  502. .warning-text {
  503. font-size: 24rpx;
  504. color: #92400E;
  505. line-height: 1.5;
  506. }
  507. .form-group {
  508. margin-bottom: 20rpx;
  509. }
  510. .form-label {
  511. display: block;
  512. font-size: 28rpx;
  513. color: #374151;
  514. margin-bottom: 12rpx;
  515. font-weight: 500;
  516. }
  517. .picker-select {
  518. display: flex;
  519. justify-content: space-between;
  520. align-items: center;
  521. padding: 24rpx 28rpx;
  522. background: #F9FAFB;
  523. border: 2rpx solid #E5E7EB;
  524. border-radius: 16rpx;
  525. font-size: 28rpx;
  526. color: #1E293B;
  527. }
  528. .picker-arrow {
  529. font-size: 36rpx;
  530. color: #9CA3AF;
  531. }
  532. .checkbox-label {
  533. display: flex;
  534. align-items: center;
  535. gap: 12rpx;
  536. font-size: 26rpx;
  537. color: #4B5563;
  538. }
  539. .checkbox {
  540. width: 36rpx;
  541. height: 36rpx;
  542. border: 2rpx solid #D1D5DB;
  543. border-radius: 8rpx;
  544. display: flex;
  545. align-items: center;
  546. justify-content: center;
  547. flex-shrink: 0;
  548. }
  549. .checkbox.checked {
  550. background: #F97316;
  551. border-color: #F97316;
  552. color: #FFFFFF;
  553. font-size: 24rpx;
  554. font-weight: 700;
  555. }
  556. .btn {
  557. width: 100%;
  558. padding: 28rpx;
  559. border-radius: 16rpx;
  560. font-size: 30rpx;
  561. font-weight: 600;
  562. text-align: center;
  563. border: none;
  564. }
  565. .btn-primary {
  566. background: linear-gradient(135deg, #F97316, #EA580C);
  567. color: #FFFFFF;
  568. }
  569. .btn-primary:disabled {
  570. opacity: 0.6;
  571. }
  572. .btn-outline {
  573. background: #FFFFFF;
  574. color: #F97316;
  575. border: 2rpx solid #F97316;
  576. }
  577. .tip-text {
  578. font-size: 24rpx;
  579. color: #94A3B8;
  580. text-align: center;
  581. }
  582. .success-area {
  583. display: flex;
  584. flex-direction: column;
  585. align-items: center;
  586. gap: 16rpx;
  587. }
  588. .success-icon {
  589. font-size: 80rpx;
  590. }
  591. .success-text {
  592. font-size: 36rpx;
  593. font-weight: 700;
  594. color: #16A34A;
  595. }
  596. .success-sub {
  597. font-size: 28rpx;
  598. color: #6B7280;
  599. margin-bottom: 24rpx;
  600. }
  601. .error-card {
  602. width: 100%;
  603. max-width: 600rpx;
  604. background: #FFFFFF;
  605. border-radius: 24rpx;
  606. padding: 60rpx 40rpx;
  607. display: flex;
  608. flex-direction: column;
  609. align-items: center;
  610. gap: 20rpx;
  611. margin-top: 10vh;
  612. }
  613. .error-icon {
  614. font-size: 80rpx;
  615. }
  616. .error-text {
  617. font-size: 30rpx;
  618. color: #6B7280;
  619. text-align: center;
  620. }
  621. .pending-card {
  622. display: flex;
  623. flex-direction: column;
  624. align-items: center;
  625. gap: 12rpx;
  626. padding: 40rpx;
  627. background: #FEF3C7;
  628. border-radius: 20rpx;
  629. margin-top: 20rpx;
  630. }
  631. .pending-icon {
  632. font-size: 72rpx;
  633. }
  634. .pending-title {
  635. font-size: 32rpx;
  636. font-weight: 700;
  637. color: #92400E;
  638. }
  639. .pending-text {
  640. font-size: 28rpx;
  641. color: #78350F;
  642. }
  643. .pending-hint {
  644. font-size: 24rpx;
  645. color: #A16207;
  646. }
  647. .error-area {
  648. display: flex;
  649. flex-direction: column;
  650. align-items: center;
  651. gap: 12rpx;
  652. padding: 40rpx;
  653. background: #FEF2F2;
  654. border-radius: 20rpx;
  655. margin-top: 20rpx;
  656. }
  657. .error-text-main {
  658. font-size: 32rpx;
  659. font-weight: 700;
  660. color: #DC2626;
  661. }
  662. .error-text-sub {
  663. font-size: 26rpx;
  664. color: #991B1B;
  665. margin-bottom: 20rpx;
  666. }
  667. </style>