index.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. <template>
  2. <view>
  3. <!-- Tabbar 切换过渡页 -->
  4. <tab-transition v-if="showTabTransition" dimCode="action" />
  5. <!-- 未登录态 — 发现页/引流内容,直接内嵌,不跳转 -->
  6. <view v-if="!currentRole" class="discover-container">
  7. <!-- 品牌头部 -->
  8. <PageBanner theme="home"
  9. tagline="家庭健康成长俱乐部"
  10. quote="给全家的一站式幸福提案" />
  11. <!-- 五行沙盘预览 -->
  12. <wuxing-sandbox mode="preview" badgeText="登录查看完整报告" @point-click="handleLogin" />
  13. <!-- 活动/商品推荐 -->
  14. <view class="recommend-section">
  15. <view class="section-header">
  16. <text class="section-title">🔥 热门推荐</text>
  17. <text class="section-more" @click="goShop">查看更多 ›</text>
  18. </view>
  19. <scroll-view scroll-x enable-flex show-scrollbar="false" class="type-filter-scroll">
  20. <view
  21. v-for="item in typeList"
  22. :key="item.value"
  23. :class="['filter-chip', currentType === item.value ? 'active' : '']"
  24. @click="onTypeChange(item.value)"
  25. >
  26. {{ item.label }}
  27. </view>
  28. </scroll-view>
  29. <view v-if="loading" class="loading-state">
  30. <text class="loading-text">加载中...</text>
  31. </view>
  32. <view v-else-if="products.length === 0" class="empty-state">
  33. <text class="empty-text">暂无商品,去商城看看吧</text>
  34. </view>
  35. <view v-else class="product-grid">
  36. <view
  37. v-for="item in products"
  38. :key="item.id"
  39. class="product-card"
  40. @click="handleLogin()"
  41. >
  42. <image class="product-cover" :src="item.coverImage || '/static/default-product.png'" mode="aspectFill" />
  43. <view class="product-info">
  44. <text class="product-name">{{ item.name }}</text>
  45. <view class="product-price-row">
  46. <!-- 游客不显示价格 -->
  47. <text class="product-price-login">登录查看</text>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </view>
  53. <!-- 五维全景幸福矩阵 -->
  54. <view class="why-section">
  55. <view class="section-header">
  56. <text class="section-title">🌏 五维全景幸福矩阵 — 给全家的一站式幸福提案</text>
  57. </view>
  58. <view class="dim-matrix">
  59. <view
  60. v-for="dim in dimList" :key="dim.code"
  61. class="dim-card"
  62. :style="'border-left:6rpx solid ' + dim.color"
  63. @click="handleLogin"
  64. >
  65. <view class="dim-icon-circle" :style="'background:' + dim.bgColor">
  66. <text class="dim-icon">{{ dim.icon }}</text>
  67. </view>
  68. <view class="dim-tag" :style="'color:' + dim.color + ';background:' + dim.bgColor">{{ dim.name }} · {{ dim.title }}</view>
  69. <text class="dim-desc">{{ dim.desc }}</text>
  70. </view>
  71. </view>
  72. <!-- 五维能量环装饰 -->
  73. <view class="dim-energy-row">
  74. <view
  75. v-for="c in energyDotColors" :key="c"
  76. class="energy-dot"
  77. :style="'background:' + c"
  78. ></view>
  79. </view>
  80. </view>
  81. <!-- 底部登录按钮 -->
  82. <view class="login-footer">
  83. <button class="login-btn" @click="handleLogin">登录 / 注册</button>
  84. <text class="login-footer-text">登录即表示同意《用户协议》和《隐私政策》</text>
  85. </view>
  86. <!-- 底部占位 -->
  87. <view style="height: 120rpx;"></view>
  88. </view>
  89. <!-- 登录态 — 按角色渲染(带淡入淡出过渡)-->
  90. <view v-else class="role-switch-wrapper">
  91. <transition name="fade" mode="out-in">
  92. <parent-index v-if="currentRole === 'parent'" key="parent" />
  93. <child-index v-else-if="currentRole === 'child'" key="child" />
  94. <!-- teacher-index 已废弃,教师功能折入 parent-index -->
  95. <view v-else class="loading-container" key="loading">
  96. <text>加载中...</text>
  97. </view>
  98. </transition>
  99. </view>
  100. </view>
  101. </template>
  102. <script>
  103. import ParentIndex from './parent-index.vue'
  104. import ChildIndex from './child-index.vue'
  105. // TeacherIndex 已废弃,教师功能折入 parent-index
  106. import PageBanner from '../../components/PageBanner.vue'
  107. import WuxingSandbox from '../../components/wuxing-sandbox.vue'
  108. import TabTransition from '../../components/tab-transition.vue'
  109. import { acceptParentInvite, productList } from '../../utils/api.js'
  110. var DIMENSIONS = [
  111. { code: 'body', name: '身', icon: '🌏', color: '#8D6E63', bgColor: 'rgba(141,110,99,0.15)', title: '主动健康', desc: '用最适合的方法,从根源调理家人体质,畅享高质量长寿生活' },
  112. { code: 'mind', name: '心', icon: '🔥', color: '#FF6B35', bgColor: 'rgba(255,107,53,0.15)', title: '大爱传承', desc: '将长辈陪护升华为家族纽带,让家风温暖代代相传' },
  113. { code: 'wisdom', name: '智', icon: '⚡', color: '#FFD700', bgColor: 'rgba(255,215,0,0.15)', title: '赋能未来', desc: '专业测评与素质培养,开启孩子面向未来的大智慧' },
  114. { code: 'action', name: '行', icon: '🌿', color: '#4CAF50', bgColor: 'rgba(76,175,80,0.15)', title: '知行合一', desc: '在亲子户外与沙龙中,让健康生活方式真正知行合一' },
  115. { code: 'wealth', name: '富', icon: '💧', color: '#42A5F5', bgColor: 'rgba(66,165,245,0.15)', title: '利他创富', desc: '依托创客生态,在利他分享中实现家庭第二收入曲线' }
  116. ]
  117. export default {
  118. components: {
  119. ParentIndex,
  120. ChildIndex,
  121. PageBanner,
  122. WuxingSandbox,
  123. TabTransition
  124. },
  125. data() {
  126. // 从 storage 预取登录态/角色,确保首次渲染即正确(WeChat 渲染层/逻辑层分离架构下 onLoad 执行前已渲染)
  127. var _token = uni.getStorageSync('token')
  128. var _currentRole = ''
  129. if (_token) {
  130. _currentRole = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || ''
  131. }
  132. return {
  133. currentRole: _currentRole,
  134. showTabTransition: false,
  135. typeList: [
  136. { label: '全部', value: '' },
  137. { label: '活动', value: 'activity' },
  138. { label: '课程', value: 'course' },
  139. { label: '实物', value: 'physical' },
  140. { label: '数字', value: 'digital' },
  141. { label: '服务', value: 'service' }
  142. ],
  143. currentType: '',
  144. products: [],
  145. loading: false
  146. }
  147. },
  148. onLoad(options) {
  149. this._doInit(options)
  150. },
  151. onShow() {
  152. // Tabbar 切换过渡动画:从 splash 首次进入时跳过(splash 本身就是过渡)
  153. var fromSplash = uni.getStorageSync('_fromSplash')
  154. if (fromSplash === '1') {
  155. uni.removeStorageSync('_fromSplash')
  156. } else {
  157. this.showTabTransition = true
  158. var self = this
  159. setTimeout(function() {
  160. self.showTabTransition = false
  161. }, 1100)
  162. }
  163. // 始终执行初始化,确保登录态变更后及时切换视图
  164. this._doInit()
  165. },
  166. methods: {
  167. _doInit(options) {
  168. options = options || {}
  169. if (!this.isLoggedIn()) {
  170. this.currentRole = ''
  171. this.loadProducts()
  172. return
  173. }
  174. this.syncRoleAndTabBar()
  175. if (options.inviteCode && options.type === 'parent_invite') {
  176. this.handleParentInvite(options.inviteCode)
  177. } else {
  178. var storedCode = uni.getStorageSync('inviteCode')
  179. var storedType = uni.getStorageSync('inviteType')
  180. if (storedCode && storedType === 'parent_invite') {
  181. this.handleParentInvite(storedCode)
  182. uni.removeStorageSync('inviteCode')
  183. uni.removeStorageSync('inviteType')
  184. }
  185. }
  186. },
  187. isLoggedIn() {
  188. return !!uni.getStorageSync('token')
  189. },
  190. normalizeCurrentRole() {
  191. const role = uni.getStorageSync('role') || 'parent'
  192. const currentRole = uni.getStorageSync('currentRole')
  193. if (!currentRole) {
  194. return role
  195. }
  196. // 非规划师账号不允许落到规划师首页
  197. if (currentRole === 'teacher' && role !== 'teacher') {
  198. return role
  199. }
  200. // 只有家长账号允许切换到孩子视角
  201. if (currentRole === 'child' && role !== 'parent') {
  202. return role
  203. }
  204. return currentRole
  205. },
  206. syncRoleAndTabBar() {
  207. this.currentRole = this.normalizeCurrentRole()
  208. uni.setStorageSync('currentRole', this.currentRole)
  209. },
  210. // ===== 发现页 / 未登录态方法 =====
  211. loadProducts() {
  212. this.loading = true
  213. var params = { page: 1, size: 10 }
  214. if (this.currentType) {
  215. params.productType = this.currentType
  216. }
  217. productList(params).then(function(res) {
  218. this.loading = false
  219. if (res.code === 200 && res.data) {
  220. this.products = res.data.records || []
  221. }
  222. }.bind(this)).catch(function() {
  223. this.loading = false
  224. }.bind(this))
  225. },
  226. onTypeChange(value) {
  227. this.currentType = value
  228. this.loadProducts()
  229. },
  230. goShop() {
  231. uni.navigateTo({ url: '/pages/action/index' })
  232. },
  233. goDetail(id) {
  234. uni.navigateTo({ url: '/pages/discover/product-detail/product-detail?id=' + id })
  235. },
  236. handleLogin() {
  237. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/index/index') })
  238. },
  239. async handleParentInvite(code) {
  240. // 延迟显示,等待登录完成
  241. setTimeout(async () => {
  242. const hasToken = uni.getStorageSync('token')
  243. if (!hasToken) {
  244. // 未登录,先去登录
  245. uni.reLaunch({
  246. url: '/pages/login/login?inviteCode=' + code
  247. })
  248. return
  249. }
  250. // 已登录,显示确认框
  251. uni.showModal({
  252. title: '加入家庭',
  253. content: '您已被邀请加入家庭,是否接受?',
  254. success: async (res) => {
  255. if (res.confirm) {
  256. try {
  257. uni.showLoading({ title: '加入中...' })
  258. const result = await acceptParentInvite(code)
  259. uni.hideLoading()
  260. if (result.code === 200) {
  261. const { familyId, role, token } = result.data
  262. uni.setStorageSync('familyId', familyId)
  263. uni.setStorageSync('role', role)
  264. uni.setStorageSync('token', token)
  265. uni.showToast({ title: '加入成功', icon: 'success' })
  266. // 刷新页面
  267. setTimeout(() => {
  268. uni.reLaunch({ url: '/pages/index/index' })
  269. }, 1500)
  270. }
  271. } catch (e) {
  272. uni.hideLoading()
  273. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  274. }
  275. }
  276. }
  277. })
  278. }, 500)
  279. },
  280. },
  281. computed: {
  282. dimList: function() {
  283. return DIMENSIONS
  284. },
  285. energyDotColors: function() {
  286. return ['#8D6E63', '#FF6B35', '#FFD700', '#4CAF50', '#42A5F5']
  287. }
  288. }
  289. }
  290. </script>
  291. <style>
  292. .discover-container {
  293. min-height: 100vh;
  294. background: #f5f7fa;
  295. padding-bottom: 100rpx;
  296. }
  297. /* ===== 推荐 ===== */
  298. .recommend-section {
  299. margin: 20rpx 30rpx;
  300. }
  301. .section-header {
  302. display: flex;
  303. justify-content: space-between;
  304. align-items: center;
  305. margin-bottom: 20rpx;
  306. }
  307. .section-title {
  308. font-size: 30rpx;
  309. font-weight: bold;
  310. color: #333;
  311. }
  312. .section-more {
  313. font-size: 24rpx;
  314. color: #5B9BD5;
  315. }
  316. .type-filter-scroll {
  317. white-space: nowrap;
  318. margin-bottom: 20rpx;
  319. }
  320. .filter-chip {
  321. display: inline-block;
  322. padding: 8rpx 24rpx;
  323. font-size: 24rpx;
  324. color: #666;
  325. background: #f0f0f0;
  326. border-radius: 30rpx;
  327. margin-right: 16rpx;
  328. }
  329. .filter-chip.active {
  330. background: #5B9BD5;
  331. color: #fff;
  332. }
  333. .loading-state,
  334. .empty-state {
  335. display: flex;
  336. justify-content: center;
  337. padding: 60rpx 0;
  338. }
  339. .loading-text,
  340. .empty-text {
  341. font-size: 26rpx;
  342. color: #999;
  343. }
  344. .product-grid {
  345. display: flex;
  346. flex-wrap: wrap;
  347. justify-content: space-between;
  348. }
  349. .product-card {
  350. width: 48%;
  351. background: #fff;
  352. border-radius: 16rpx;
  353. overflow: hidden;
  354. margin-bottom: 20rpx;
  355. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  356. }
  357. .product-cover {
  358. width: 100%;
  359. height: 200rpx;
  360. background: #f0f0f0;
  361. }
  362. .product-info {
  363. padding: 14rpx;
  364. }
  365. .product-name {
  366. display: block;
  367. font-size: 26rpx;
  368. color: #333;
  369. margin-bottom: 10rpx;
  370. overflow: hidden;
  371. text-overflow: ellipsis;
  372. white-space: nowrap;
  373. }
  374. .product-price-row {
  375. display: flex;
  376. align-items: baseline;
  377. }
  378. .product-price {
  379. font-size: 28rpx;
  380. color: #5B9BD5;
  381. font-weight: bold;
  382. margin-right: 10rpx;
  383. }
  384. .product-price-login {
  385. font-size: 24rpx;
  386. color: #999;
  387. }
  388. .product-member {
  389. font-size: 20rpx;
  390. color: #999;
  391. }
  392. /* ===== 五维全景幸福矩阵 ===== */
  393. .why-section {
  394. margin: 30rpx;
  395. }
  396. .dim-matrix {
  397. display: flex;
  398. flex-wrap: wrap;
  399. gap: 20rpx;
  400. justify-content: center;
  401. }
  402. .dim-card {
  403. width: calc(33.33% - 14rpx);
  404. box-sizing: border-box;
  405. background: #fff;
  406. border-radius: 20rpx;
  407. padding: 28rpx 16rpx 24rpx;
  408. display: flex;
  409. flex-direction: column;
  410. align-items: center;
  411. text-align: center;
  412. box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.06);
  413. transition: transform 0.2s ease, box-shadow 0.2s ease;
  414. position: relative;
  415. overflow: hidden;
  416. }
  417. .dim-card:active {
  418. transform: scale(0.96);
  419. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
  420. }
  421. .dim-icon-circle {
  422. width: 72rpx;
  423. height: 72rpx;
  424. border-radius: 50%;
  425. display: flex;
  426. align-items: center;
  427. justify-content: center;
  428. margin-bottom: 14rpx;
  429. flex-shrink: 0;
  430. }
  431. .dim-icon {
  432. font-size: 36rpx;
  433. line-height: 1;
  434. }
  435. .dim-tag {
  436. font-size: 22rpx;
  437. font-weight: 600;
  438. padding: 4rpx 14rpx;
  439. border-radius: 20rpx;
  440. margin-bottom: 10rpx;
  441. max-width: 100%;
  442. overflow: hidden;
  443. text-overflow: ellipsis;
  444. white-space: nowrap;
  445. }
  446. .dim-desc {
  447. font-size: 20rpx;
  448. color: #888;
  449. line-height: 1.5;
  450. display: -webkit-box;
  451. -webkit-line-clamp: 2;
  452. -webkit-box-orient: vertical;
  453. overflow: hidden;
  454. }
  455. /* 五维能量环 */
  456. .dim-energy-row {
  457. display: flex;
  458. justify-content: center;
  459. gap: 12rpx;
  460. margin-top: 24rpx;
  461. opacity: 0.5;
  462. }
  463. .energy-dot {
  464. width: 10rpx;
  465. height: 10rpx;
  466. border-radius: 50%;
  467. }
  468. /* ===== 底部登录 ===== */
  469. .login-footer {
  470. margin: 40rpx 30rpx;
  471. display: flex;
  472. flex-direction: column;
  473. align-items: center;
  474. }
  475. .login-btn {
  476. width: 100%;
  477. height: 88rpx;
  478. line-height: 88rpx;
  479. background: linear-gradient(135deg, #5B9BD5, #3A7CC4);
  480. color: #fff;
  481. font-size: 32rpx;
  482. font-weight: bold;
  483. border-radius: 44rpx;
  484. text-align: center;
  485. border: none;
  486. }
  487. .login-btn::after {
  488. border: none;
  489. }
  490. .login-footer-text {
  491. font-size: 20rpx;
  492. color: #bbb;
  493. margin-top: 16rpx;
  494. }
  495. /* ===== 加载占位 ===== */
  496. .loading-container {
  497. display: flex;
  498. justify-content: center;
  499. align-items: center;
  500. height: 100vh;
  501. }
  502. /* ===== 角色切换淡入淡出 ===== */
  503. .role-switch-wrapper {
  504. width: 100%;
  505. min-height: 100vh;
  506. }
  507. .fade-enter-active,
  508. .fade-leave-active {
  509. transition: opacity 0.3s ease;
  510. }
  511. .fade-enter,
  512. .fade-leave-to {
  513. opacity: 0;
  514. }
  515. </style>