join.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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="error-card" v-if="!loading && error">
  74. <view class="error-icon">❌</view>
  75. <text class="error-text">{{ error }}</text>
  76. <button class="btn btn-outline" @tap="goHome">返回首页</button>
  77. </view>
  78. </view>
  79. </template>
  80. <script>
  81. import { validateInvitation, acceptInvitation, checkUserFamily } from '@/utils/api.js'
  82. export default {
  83. data() {
  84. return {
  85. token: '',
  86. loading: true,
  87. loginLoading: false,
  88. acceptLoading: false,
  89. isLoggedIn: false,
  90. inviteInfo: null,
  91. familyStatus: null,
  92. familyRoleIndex: 0,
  93. mergeFamily: false,
  94. accepted: false,
  95. error: '',
  96. familyRoleOptions: ['爸爸', '妈妈', '爷爷', '奶奶', '姥爷', '姥姥', '其他']
  97. }
  98. },
  99. onLoad(query) {
  100. // 获取 token:优先从 query 参数,其次从场景值
  101. var token = query.token || query.inviteToken || ''
  102. if (!token) {
  103. // 检查场景值(从小程序码扫码进入)
  104. var scene = query.scene
  105. if (scene) {
  106. var sceneDecoded = decodeURIComponent(scene)
  107. if (sceneDecoded.indexOf('invite=') === 0) {
  108. token = sceneDecoded.substring(7)
  109. }
  110. }
  111. }
  112. if (!token) {
  113. this.error = '邀请链接无效'
  114. this.loading = false
  115. return
  116. }
  117. this.token = token
  118. this.checkLoginStatus()
  119. this.validateToken()
  120. },
  121. onShow() {
  122. // 从登录页返回时重新检查登录状态
  123. if (this.token) {
  124. this.checkLoginStatus()
  125. if (!this.inviteInfo) {
  126. this.validateToken()
  127. }
  128. }
  129. },
  130. methods: {
  131. checkLoginStatus() {
  132. var token = uni.getStorageSync('token')
  133. this.isLoggedIn = !!token
  134. if (this.isLoggedIn) {
  135. this.checkFamily()
  136. }
  137. },
  138. async validateToken() {
  139. this.loading = true
  140. try {
  141. var res = await validateInvitation(this.token)
  142. if (res && res.data) {
  143. this.inviteInfo = res.data
  144. } else {
  145. this.error = '邀请已失效'
  146. }
  147. } catch (e) {
  148. this.error = e.message || '邀请验证失败'
  149. }
  150. this.loading = false
  151. },
  152. async checkFamily() {
  153. try {
  154. var res = await checkUserFamily()
  155. if (res && res.data) {
  156. this.familyStatus = res.data
  157. // 如果已经在目标家庭,直接显示成功
  158. if (this.familyStatus.inFamily && this.familyStatus.familyId === this.inviteInfo.familyId) {
  159. this.accepted = true
  160. }
  161. }
  162. } catch (e) {
  163. console.log('检查家庭状态失败', e)
  164. }
  165. },
  166. async handleWechatLogin() {
  167. this.loginLoading = true
  168. try {
  169. var that = this
  170. // 微信登录
  171. var loginRes = await new Promise(function(resolve, reject) {
  172. uni.login({
  173. provider: 'weixin',
  174. success: function(res) {
  175. resolve(res)
  176. },
  177. fail: function(err) {
  178. reject(err)
  179. }
  180. })
  181. })
  182. var code = loginRes.code
  183. if (!code) {
  184. uni.showToast({ title: '登录失败', icon: 'none' })
  185. this.loginLoading = false
  186. return
  187. }
  188. // 调起微信登录
  189. var { wechatLogin } = require('@/utils/api.js')
  190. var res = await wechatLogin(code, {})
  191. if (res && res.data) {
  192. // 保存登录信息
  193. var data = res.data
  194. uni.setStorageSync('token', data.token)
  195. uni.setStorageSync('userId', data.userId)
  196. uni.setStorageSync('role', data.role)
  197. uni.setStorageSync('familyId', data.familyId)
  198. uni.setStorageSync('currentRole', data.role)
  199. uni.setStorageSync('isSwitchedChild', false)
  200. uni.setStorageSync('isSwitchedTeacher', false)
  201. if (data.openid) {
  202. uni.setStorageSync('openid', data.openid)
  203. }
  204. that.$store && that.$store.commit('setToken', data.token)
  205. this.isLoggedIn = true
  206. uni.showToast({ title: '登录成功', icon: 'success' })
  207. // 重新检查家庭状态
  208. await this.checkFamily()
  209. }
  210. } catch (e) {
  211. uni.showToast({ title: '登录失败', icon: 'none' })
  212. console.log('微信登录失败', e)
  213. }
  214. this.loginLoading = false
  215. },
  216. onFamilyRoleChange(e) {
  217. this.familyRoleIndex = e.detail.value
  218. },
  219. toggleMerge() {
  220. this.mergeFamily = !this.mergeFamily
  221. },
  222. async handleAccept() {
  223. var familyRole = this.familyRoleOptions[this.familyRoleIndex] || ''
  224. if (!familyRole) {
  225. uni.showToast({ title: '请选择您在家庭中的身份', icon: 'none' })
  226. return
  227. }
  228. this.acceptLoading = true
  229. try {
  230. var res = await acceptInvitation(this.token, familyRole, this.mergeFamily)
  231. if (res && res.code === 200) {
  232. this.accepted = true
  233. // 更新缓存的 familyId
  234. uni.setStorageSync('familyId', this.inviteInfo.familyId)
  235. uni.showToast({ title: '加入家庭成功', icon: 'success' })
  236. } else {
  237. uni.showToast({ title: res.message || '加入失败', icon: 'none' })
  238. }
  239. } catch (e) {
  240. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  241. }
  242. this.acceptLoading = false
  243. },
  244. goHome() {
  245. uni.reLaunch({ url: '/pages/index/index' })
  246. }
  247. }
  248. }
  249. </script>
  250. <style scoped>
  251. .page {
  252. padding: 32rpx;
  253. min-height: 100vh;
  254. background: linear-gradient(135deg, #FFF7ED 0%, #FEF3C7 100%);
  255. display: flex;
  256. flex-direction: column;
  257. align-items: center;
  258. }
  259. .loading {
  260. display: flex;
  261. flex-direction: column;
  262. align-items: center;
  263. justify-content: center;
  264. min-height: 60vh;
  265. }
  266. .loading-icon {
  267. font-size: 80rpx;
  268. margin-bottom: 20rpx;
  269. }
  270. .loading-text {
  271. color: #666;
  272. font-size: 28rpx;
  273. }
  274. .invite-card {
  275. width: 100%;
  276. max-width: 600rpx;
  277. background: #FFFFFF;
  278. border-radius: 24rpx;
  279. padding: 40rpx;
  280. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
  281. margin-top: 10vh;
  282. }
  283. .invite-header {
  284. display: flex;
  285. flex-direction: column;
  286. align-items: center;
  287. margin-bottom: 32rpx;
  288. }
  289. .invite-icon {
  290. font-size: 80rpx;
  291. margin-bottom: 16rpx;
  292. }
  293. .invite-title {
  294. font-size: 32rpx;
  295. font-weight: 700;
  296. color: #1E293B;
  297. }
  298. .invite-body {
  299. display: flex;
  300. flex-direction: column;
  301. align-items: center;
  302. margin-bottom: 40rpx;
  303. padding: 24rpx;
  304. background: #FFF7ED;
  305. border-radius: 16rpx;
  306. }
  307. .family-name {
  308. font-size: 40rpx;
  309. font-weight: 700;
  310. color: #F97316;
  311. margin-bottom: 12rpx;
  312. }
  313. .inviter {
  314. font-size: 28rpx;
  315. color: #64748B;
  316. margin-bottom: 8rpx;
  317. }
  318. .member-count {
  319. font-size: 24rpx;
  320. color: #94A3B8;
  321. }
  322. .action-area {
  323. display: flex;
  324. flex-direction: column;
  325. gap: 16rpx;
  326. }
  327. .family-warning {
  328. display: flex;
  329. align-items: flex-start;
  330. padding: 20rpx;
  331. background: #FEF3C7;
  332. border-radius: 12rpx;
  333. gap: 12rpx;
  334. }
  335. .warning-icon {
  336. font-size: 32rpx;
  337. flex-shrink: 0;
  338. }
  339. .warning-text {
  340. font-size: 24rpx;
  341. color: #92400E;
  342. line-height: 1.5;
  343. }
  344. .form-group {
  345. margin-bottom: 20rpx;
  346. }
  347. .form-label {
  348. display: block;
  349. font-size: 28rpx;
  350. color: #374151;
  351. margin-bottom: 12rpx;
  352. font-weight: 500;
  353. }
  354. .picker-select {
  355. display: flex;
  356. justify-content: space-between;
  357. align-items: center;
  358. padding: 24rpx 28rpx;
  359. background: #F9FAFB;
  360. border: 2rpx solid #E5E7EB;
  361. border-radius: 16rpx;
  362. font-size: 28rpx;
  363. color: #1E293B;
  364. }
  365. .picker-arrow {
  366. font-size: 36rpx;
  367. color: #9CA3AF;
  368. }
  369. .checkbox-label {
  370. display: flex;
  371. align-items: center;
  372. gap: 12rpx;
  373. font-size: 26rpx;
  374. color: #4B5563;
  375. }
  376. .checkbox {
  377. width: 36rpx;
  378. height: 36rpx;
  379. border: 2rpx solid #D1D5DB;
  380. border-radius: 8rpx;
  381. display: flex;
  382. align-items: center;
  383. justify-content: center;
  384. flex-shrink: 0;
  385. }
  386. .checkbox.checked {
  387. background: #F97316;
  388. border-color: #F97316;
  389. color: #FFFFFF;
  390. font-size: 24rpx;
  391. font-weight: 700;
  392. }
  393. .btn {
  394. width: 100%;
  395. padding: 28rpx;
  396. border-radius: 16rpx;
  397. font-size: 30rpx;
  398. font-weight: 600;
  399. text-align: center;
  400. border: none;
  401. }
  402. .btn-primary {
  403. background: linear-gradient(135deg, #F97316, #EA580C);
  404. color: #FFFFFF;
  405. }
  406. .btn-primary:disabled {
  407. opacity: 0.6;
  408. }
  409. .btn-outline {
  410. background: #FFFFFF;
  411. color: #F97316;
  412. border: 2rpx solid #F97316;
  413. }
  414. .tip-text {
  415. font-size: 24rpx;
  416. color: #94A3B8;
  417. text-align: center;
  418. }
  419. .success-area {
  420. display: flex;
  421. flex-direction: column;
  422. align-items: center;
  423. gap: 16rpx;
  424. }
  425. .success-icon {
  426. font-size: 80rpx;
  427. }
  428. .success-text {
  429. font-size: 36rpx;
  430. font-weight: 700;
  431. color: #16A34A;
  432. }
  433. .success-sub {
  434. font-size: 28rpx;
  435. color: #6B7280;
  436. margin-bottom: 24rpx;
  437. }
  438. .error-card {
  439. width: 100%;
  440. max-width: 600rpx;
  441. background: #FFFFFF;
  442. border-radius: 24rpx;
  443. padding: 60rpx 40rpx;
  444. display: flex;
  445. flex-direction: column;
  446. align-items: center;
  447. gap: 20rpx;
  448. margin-top: 10vh;
  449. }
  450. .error-icon {
  451. font-size: 80rpx;
  452. }
  453. .error-text {
  454. font-size: 30rpx;
  455. color: #6B7280;
  456. text-align: center;
  457. }
  458. </style>