join.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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('currentFamilyId', data.familyId)
  199. uni.setStorageSync('currentRole', data.role)
  200. uni.setStorageSync('isSwitchedChild', false)
  201. uni.setStorageSync('isSwitchedTeacher', false)
  202. if (data.openid) {
  203. uni.setStorageSync('openid', data.openid)
  204. }
  205. that.$store && that.$store.commit('setToken', data.token)
  206. this.isLoggedIn = true
  207. uni.showToast({ title: '登录成功', icon: 'success' })
  208. // 重新检查家庭状态
  209. await this.checkFamily()
  210. }
  211. } catch (e) {
  212. uni.showToast({ title: '登录失败', icon: 'none' })
  213. console.log('微信登录失败', e)
  214. }
  215. this.loginLoading = false
  216. },
  217. onFamilyRoleChange(e) {
  218. this.familyRoleIndex = e.detail.value
  219. },
  220. toggleMerge() {
  221. this.mergeFamily = !this.mergeFamily
  222. },
  223. async handleAccept() {
  224. var familyRole = this.familyRoleOptions[this.familyRoleIndex] || ''
  225. if (!familyRole) {
  226. uni.showToast({ title: '请选择您在家庭中的身份', icon: 'none' })
  227. return
  228. }
  229. this.acceptLoading = true
  230. try {
  231. var res = await acceptInvitation(this.token, familyRole, this.mergeFamily)
  232. if (res && res.code === 200) {
  233. this.accepted = true
  234. // 更新缓存的 familyId
  235. uni.setStorageSync('familyId', this.inviteInfo.familyId)
  236. uni.setStorageSync('currentFamilyId', this.inviteInfo.familyId)
  237. uni.showToast({ title: '加入家庭成功', icon: 'success' })
  238. } else {
  239. uni.showToast({ title: res.message || '加入失败', icon: 'none' })
  240. }
  241. } catch (e) {
  242. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  243. }
  244. this.acceptLoading = false
  245. },
  246. goHome() {
  247. uni.reLaunch({ url: '/pages/index/index' })
  248. }
  249. }
  250. }
  251. </script>
  252. <style scoped>
  253. .page {
  254. padding: 32rpx;
  255. min-height: 100vh;
  256. background: linear-gradient(135deg, #FFF7ED 0%, #FEF3C7 100%);
  257. display: flex;
  258. flex-direction: column;
  259. align-items: center;
  260. }
  261. .loading {
  262. display: flex;
  263. flex-direction: column;
  264. align-items: center;
  265. justify-content: center;
  266. min-height: 60vh;
  267. }
  268. .loading-icon {
  269. font-size: 80rpx;
  270. margin-bottom: 20rpx;
  271. }
  272. .loading-text {
  273. color: #666;
  274. font-size: 28rpx;
  275. }
  276. .invite-card {
  277. width: 100%;
  278. max-width: 600rpx;
  279. background: #FFFFFF;
  280. border-radius: 24rpx;
  281. padding: 40rpx;
  282. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.08);
  283. margin-top: 10vh;
  284. }
  285. .invite-header {
  286. display: flex;
  287. flex-direction: column;
  288. align-items: center;
  289. margin-bottom: 32rpx;
  290. }
  291. .invite-icon {
  292. font-size: 80rpx;
  293. margin-bottom: 16rpx;
  294. }
  295. .invite-title {
  296. font-size: 32rpx;
  297. font-weight: 700;
  298. color: #1E293B;
  299. }
  300. .invite-body {
  301. display: flex;
  302. flex-direction: column;
  303. align-items: center;
  304. margin-bottom: 40rpx;
  305. padding: 24rpx;
  306. background: #FFF7ED;
  307. border-radius: 16rpx;
  308. }
  309. .family-name {
  310. font-size: 40rpx;
  311. font-weight: 700;
  312. color: #F97316;
  313. margin-bottom: 12rpx;
  314. }
  315. .inviter {
  316. font-size: 28rpx;
  317. color: #64748B;
  318. margin-bottom: 8rpx;
  319. }
  320. .member-count {
  321. font-size: 24rpx;
  322. color: #94A3B8;
  323. }
  324. .action-area {
  325. display: flex;
  326. flex-direction: column;
  327. gap: 16rpx;
  328. }
  329. .family-warning {
  330. display: flex;
  331. align-items: flex-start;
  332. padding: 20rpx;
  333. background: #FEF3C7;
  334. border-radius: 12rpx;
  335. gap: 12rpx;
  336. }
  337. .warning-icon {
  338. font-size: 32rpx;
  339. flex-shrink: 0;
  340. }
  341. .warning-text {
  342. font-size: 24rpx;
  343. color: #92400E;
  344. line-height: 1.5;
  345. }
  346. .form-group {
  347. margin-bottom: 20rpx;
  348. }
  349. .form-label {
  350. display: block;
  351. font-size: 28rpx;
  352. color: #374151;
  353. margin-bottom: 12rpx;
  354. font-weight: 500;
  355. }
  356. .picker-select {
  357. display: flex;
  358. justify-content: space-between;
  359. align-items: center;
  360. padding: 24rpx 28rpx;
  361. background: #F9FAFB;
  362. border: 2rpx solid #E5E7EB;
  363. border-radius: 16rpx;
  364. font-size: 28rpx;
  365. color: #1E293B;
  366. }
  367. .picker-arrow {
  368. font-size: 36rpx;
  369. color: #9CA3AF;
  370. }
  371. .checkbox-label {
  372. display: flex;
  373. align-items: center;
  374. gap: 12rpx;
  375. font-size: 26rpx;
  376. color: #4B5563;
  377. }
  378. .checkbox {
  379. width: 36rpx;
  380. height: 36rpx;
  381. border: 2rpx solid #D1D5DB;
  382. border-radius: 8rpx;
  383. display: flex;
  384. align-items: center;
  385. justify-content: center;
  386. flex-shrink: 0;
  387. }
  388. .checkbox.checked {
  389. background: #F97316;
  390. border-color: #F97316;
  391. color: #FFFFFF;
  392. font-size: 24rpx;
  393. font-weight: 700;
  394. }
  395. .btn {
  396. width: 100%;
  397. padding: 28rpx;
  398. border-radius: 16rpx;
  399. font-size: 30rpx;
  400. font-weight: 600;
  401. text-align: center;
  402. border: none;
  403. }
  404. .btn-primary {
  405. background: linear-gradient(135deg, #F97316, #EA580C);
  406. color: #FFFFFF;
  407. }
  408. .btn-primary:disabled {
  409. opacity: 0.6;
  410. }
  411. .btn-outline {
  412. background: #FFFFFF;
  413. color: #F97316;
  414. border: 2rpx solid #F97316;
  415. }
  416. .tip-text {
  417. font-size: 24rpx;
  418. color: #94A3B8;
  419. text-align: center;
  420. }
  421. .success-area {
  422. display: flex;
  423. flex-direction: column;
  424. align-items: center;
  425. gap: 16rpx;
  426. }
  427. .success-icon {
  428. font-size: 80rpx;
  429. }
  430. .success-text {
  431. font-size: 36rpx;
  432. font-weight: 700;
  433. color: #16A34A;
  434. }
  435. .success-sub {
  436. font-size: 28rpx;
  437. color: #6B7280;
  438. margin-bottom: 24rpx;
  439. }
  440. .error-card {
  441. width: 100%;
  442. max-width: 600rpx;
  443. background: #FFFFFF;
  444. border-radius: 24rpx;
  445. padding: 60rpx 40rpx;
  446. display: flex;
  447. flex-direction: column;
  448. align-items: center;
  449. gap: 20rpx;
  450. margin-top: 10vh;
  451. }
  452. .error-icon {
  453. font-size: 80rpx;
  454. }
  455. .error-text {
  456. font-size: 30rpx;
  457. color: #6B7280;
  458. text-align: center;
  459. }
  460. </style>