join.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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('currentRole', data.role)
  248. uni.setStorageSync('isSwitchedChild', false)
  249. uni.setStorageSync('isSwitchedTeacher', false)
  250. if (data.openid) {
  251. uni.setStorageSync('openid', data.openid)
  252. }
  253. that.$store && that.$store.commit('setToken', data.token)
  254. this.isLoggedIn = true
  255. uni.showToast({ title: '登录成功', icon: 'success' })
  256. // 重新检查家庭状态
  257. await this.checkFamily()
  258. }
  259. } catch (e) {
  260. uni.showToast({ title: '登录失败', icon: 'none' })
  261. console.log('微信登录失败', e)
  262. }
  263. this.loginLoading = false
  264. },
  265. onFamilyRoleChange(e) {
  266. this.familyRoleIndex = e.detail.value
  267. },
  268. toggleMerge() {
  269. this.mergeFamily = !this.mergeFamily
  270. },
  271. async handleAccept() {
  272. var familyRole = this.familyRoleOptions[this.familyRoleIndex] || ''
  273. if (!familyRole) {
  274. uni.showToast({ title: '请选择您在家庭中的身份', icon: 'none' })
  275. return
  276. }
  277. this.acceptLoading = true
  278. try {
  279. var res = await acceptInvitation(this.token, familyRole, this.mergeFamily)
  280. if (res && res.code === 200) {
  281. this.accepted = true
  282. uni.showToast({ title: '加入家庭成功', icon: 'success' })
  283. } else {
  284. uni.showToast({ title: res.message || '加入失败', icon: 'none' })
  285. }
  286. } catch (e) {
  287. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  288. }
  289. this.acceptLoading = false
  290. },
  291. goHome() {
  292. uni.reLaunch({ url: '/pages/index-home/index' })
  293. },
  294. async bindRef() {
  295. if (!this.refCode) {
  296. uni.showToast({ title: '推广码为空', icon: 'none' })
  297. return
  298. }
  299. this.bindLoading = true
  300. try {
  301. var res = await bindReferral(this.refCode)
  302. if (res && res.code === 200) {
  303. this.bindDone = true
  304. uni.showToast({ title: '绑定成功', icon: 'success' })
  305. } else {
  306. uni.showToast({ title: (res && res.message) || '绑定失败', icon: 'none' })
  307. }
  308. } catch (e) {
  309. uni.showToast({ title: e.message || '绑定失败', icon: 'none' })
  310. }
  311. this.bindLoading = false
  312. }
  313. }
  314. }
  315. </script>
  316. <style scoped>
  317. .page {
  318. padding: 32rpx;
  319. min-height: 100vh;
  320. background: linear-gradient(135deg, #FFF7ED 0%, #FEF3C7 100%);
  321. display: flex;
  322. flex-direction: column;
  323. align-items: center;
  324. }
  325. .loading {
  326. display: flex;
  327. flex-direction: column;
  328. align-items: center;
  329. justify-content: center;
  330. min-height: 60vh;
  331. }
  332. .loading-icon {
  333. font-size: 80rpx;
  334. margin-bottom: 20rpx;
  335. }
  336. .loading-text {
  337. color: #666;
  338. font-size: 28rpx;
  339. }
  340. .invite-card {
  341. width: 100%;
  342. max-width: 600rpx;
  343. background: #FFFFFF;
  344. border-radius: 24rpx;
  345. padding: 40rpx;
  346. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
  347. margin-top: 10vh;
  348. }
  349. .invite-header {
  350. display: flex;
  351. flex-direction: column;
  352. align-items: center;
  353. margin-bottom: 32rpx;
  354. }
  355. .invite-icon {
  356. font-size: 80rpx;
  357. margin-bottom: 16rpx;
  358. }
  359. .invite-title {
  360. font-size: 32rpx;
  361. font-weight: 700;
  362. color: #1E293B;
  363. }
  364. .invite-body {
  365. display: flex;
  366. flex-direction: column;
  367. align-items: center;
  368. margin-bottom: 40rpx;
  369. padding: 24rpx;
  370. background: #FFF7ED;
  371. border-radius: 16rpx;
  372. }
  373. .family-name {
  374. font-size: 40rpx;
  375. font-weight: 700;
  376. color: #F97316;
  377. margin-bottom: 12rpx;
  378. }
  379. .inviter {
  380. font-size: 28rpx;
  381. color: #64748B;
  382. margin-bottom: 8rpx;
  383. }
  384. .member-count {
  385. font-size: 24rpx;
  386. color: #94A3B8;
  387. }
  388. .action-area {
  389. display: flex;
  390. flex-direction: column;
  391. gap: 16rpx;
  392. }
  393. .family-warning {
  394. display: flex;
  395. align-items: flex-start;
  396. padding: 20rpx;
  397. background: #FEF3C7;
  398. border-radius: 12rpx;
  399. gap: 12rpx;
  400. }
  401. .warning-icon {
  402. font-size: 32rpx;
  403. flex-shrink: 0;
  404. }
  405. .warning-text {
  406. font-size: 24rpx;
  407. color: #92400E;
  408. line-height: 1.5;
  409. }
  410. .form-group {
  411. margin-bottom: 20rpx;
  412. }
  413. .form-label {
  414. display: block;
  415. font-size: 28rpx;
  416. color: #374151;
  417. margin-bottom: 12rpx;
  418. font-weight: 500;
  419. }
  420. .picker-select {
  421. display: flex;
  422. justify-content: space-between;
  423. align-items: center;
  424. padding: 24rpx 28rpx;
  425. background: #F9FAFB;
  426. border: 2rpx solid #E5E7EB;
  427. border-radius: 16rpx;
  428. font-size: 28rpx;
  429. color: #1E293B;
  430. }
  431. .picker-arrow {
  432. font-size: 36rpx;
  433. color: #9CA3AF;
  434. }
  435. .checkbox-label {
  436. display: flex;
  437. align-items: center;
  438. gap: 12rpx;
  439. font-size: 26rpx;
  440. color: #4B5563;
  441. }
  442. .checkbox {
  443. width: 36rpx;
  444. height: 36rpx;
  445. border: 2rpx solid #D1D5DB;
  446. border-radius: 8rpx;
  447. display: flex;
  448. align-items: center;
  449. justify-content: center;
  450. flex-shrink: 0;
  451. }
  452. .checkbox.checked {
  453. background: #F97316;
  454. border-color: #F97316;
  455. color: #FFFFFF;
  456. font-size: 24rpx;
  457. font-weight: 700;
  458. }
  459. .btn {
  460. width: 100%;
  461. padding: 28rpx;
  462. border-radius: 16rpx;
  463. font-size: 30rpx;
  464. font-weight: 600;
  465. text-align: center;
  466. border: none;
  467. }
  468. .btn-primary {
  469. background: linear-gradient(135deg, #F97316, #EA580C);
  470. color: #FFFFFF;
  471. }
  472. .btn-primary:disabled {
  473. opacity: 0.6;
  474. }
  475. .btn-outline {
  476. background: #FFFFFF;
  477. color: #F97316;
  478. border: 2rpx solid #F97316;
  479. }
  480. .tip-text {
  481. font-size: 24rpx;
  482. color: #94A3B8;
  483. text-align: center;
  484. }
  485. .success-area {
  486. display: flex;
  487. flex-direction: column;
  488. align-items: center;
  489. gap: 16rpx;
  490. }
  491. .success-icon {
  492. font-size: 80rpx;
  493. }
  494. .success-text {
  495. font-size: 36rpx;
  496. font-weight: 700;
  497. color: #16A34A;
  498. }
  499. .success-sub {
  500. font-size: 28rpx;
  501. color: #6B7280;
  502. margin-bottom: 24rpx;
  503. }
  504. .error-card {
  505. width: 100%;
  506. max-width: 600rpx;
  507. background: #FFFFFF;
  508. border-radius: 24rpx;
  509. padding: 60rpx 40rpx;
  510. display: flex;
  511. flex-direction: column;
  512. align-items: center;
  513. gap: 20rpx;
  514. margin-top: 10vh;
  515. }
  516. .error-icon {
  517. font-size: 80rpx;
  518. }
  519. .error-text {
  520. font-size: 30rpx;
  521. color: #6B7280;
  522. text-align: center;
  523. }
  524. </style>