join.vue 15 KB

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