join.vue 19 KB

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