index.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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 切换过渡动画(最少停留 1 秒)
  153. this.showTabTransition = true
  154. var self = this
  155. setTimeout(function() {
  156. self.showTabTransition = false
  157. }, 1100)
  158. // 始终执行初始化,确保登录态变更后及时切换视图
  159. this._doInit()
  160. },
  161. methods: {
  162. _doInit(options) {
  163. options = options || {}
  164. if (!this.isLoggedIn()) {
  165. this.currentRole = ''
  166. this.loadProducts()
  167. return
  168. }
  169. this.syncRoleAndTabBar()
  170. if (options.inviteCode && options.type === 'parent_invite') {
  171. this.handleParentInvite(options.inviteCode)
  172. } else {
  173. var storedCode = uni.getStorageSync('inviteCode')
  174. var storedType = uni.getStorageSync('inviteType')
  175. if (storedCode && storedType === 'parent_invite') {
  176. this.handleParentInvite(storedCode)
  177. uni.removeStorageSync('inviteCode')
  178. uni.removeStorageSync('inviteType')
  179. }
  180. }
  181. },
  182. isLoggedIn() {
  183. return !!uni.getStorageSync('token')
  184. },
  185. normalizeCurrentRole() {
  186. const role = uni.getStorageSync('role') || 'parent'
  187. const currentRole = uni.getStorageSync('currentRole')
  188. if (!currentRole) {
  189. return role
  190. }
  191. // 非规划师账号不允许落到规划师首页
  192. if (currentRole === 'teacher' && role !== 'teacher') {
  193. return role
  194. }
  195. // 只有家长账号允许切换到孩子视角
  196. if (currentRole === 'child' && role !== 'parent') {
  197. return role
  198. }
  199. return currentRole
  200. },
  201. syncRoleAndTabBar() {
  202. this.currentRole = this.normalizeCurrentRole()
  203. uni.setStorageSync('currentRole', this.currentRole)
  204. },
  205. // ===== 发现页 / 未登录态方法 =====
  206. loadProducts() {
  207. this.loading = true
  208. var params = { page: 1, size: 10 }
  209. if (this.currentType) {
  210. params.productType = this.currentType
  211. }
  212. productList(params).then(function(res) {
  213. this.loading = false
  214. if (res.code === 200 && res.data) {
  215. this.products = res.data.records || []
  216. }
  217. }.bind(this)).catch(function() {
  218. this.loading = false
  219. }.bind(this))
  220. },
  221. onTypeChange(value) {
  222. this.currentType = value
  223. this.loadProducts()
  224. },
  225. goShop() {
  226. uni.navigateTo({ url: '/pages/action/index' })
  227. },
  228. goDetail(id) {
  229. uni.navigateTo({ url: '/pages/discover/product-detail/product-detail?id=' + id })
  230. },
  231. handleLogin() {
  232. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/index/index') })
  233. },
  234. async handleParentInvite(code) {
  235. // 延迟显示,等待登录完成
  236. setTimeout(async () => {
  237. const hasToken = uni.getStorageSync('token')
  238. if (!hasToken) {
  239. // 未登录,先去登录
  240. uni.reLaunch({
  241. url: '/pages/login/login?inviteCode=' + code
  242. })
  243. return
  244. }
  245. // 已登录,显示确认框
  246. uni.showModal({
  247. title: '加入家庭',
  248. content: '您已被邀请加入家庭,是否接受?',
  249. success: async (res) => {
  250. if (res.confirm) {
  251. try {
  252. uni.showLoading({ title: '加入中...' })
  253. const result = await acceptParentInvite(code)
  254. uni.hideLoading()
  255. if (result.code === 200) {
  256. const { familyId, role, token } = result.data
  257. uni.setStorageSync('familyId', familyId)
  258. uni.setStorageSync('role', role)
  259. uni.setStorageSync('token', token)
  260. uni.showToast({ title: '加入成功', icon: 'success' })
  261. // 刷新页面
  262. setTimeout(() => {
  263. uni.reLaunch({ url: '/pages/index/index' })
  264. }, 1500)
  265. }
  266. } catch (e) {
  267. uni.hideLoading()
  268. uni.showToast({ title: e.message || '加入失败', icon: 'none' })
  269. }
  270. }
  271. }
  272. })
  273. }, 500)
  274. },
  275. },
  276. computed: {
  277. dimList: function() {
  278. return DIMENSIONS
  279. },
  280. energyDotColors: function() {
  281. return ['#8D6E63', '#FF6B35', '#FFD700', '#4CAF50', '#42A5F5']
  282. }
  283. }
  284. }
  285. </script>
  286. <style>
  287. .discover-container {
  288. min-height: 100vh;
  289. background: #f5f7fa;
  290. padding-bottom: 100rpx;
  291. }
  292. /* ===== 推荐 ===== */
  293. .recommend-section {
  294. margin: 20rpx 30rpx;
  295. }
  296. .section-header {
  297. display: flex;
  298. justify-content: space-between;
  299. align-items: center;
  300. margin-bottom: 20rpx;
  301. }
  302. .section-title {
  303. font-size: 30rpx;
  304. font-weight: bold;
  305. color: #333;
  306. }
  307. .section-more {
  308. font-size: 24rpx;
  309. color: #5B9BD5;
  310. }
  311. .type-filter-scroll {
  312. white-space: nowrap;
  313. margin-bottom: 20rpx;
  314. }
  315. .filter-chip {
  316. display: inline-block;
  317. padding: 8rpx 24rpx;
  318. font-size: 24rpx;
  319. color: #666;
  320. background: #f0f0f0;
  321. border-radius: 30rpx;
  322. margin-right: 16rpx;
  323. }
  324. .filter-chip.active {
  325. background: #5B9BD5;
  326. color: #fff;
  327. }
  328. .loading-state,
  329. .empty-state {
  330. display: flex;
  331. justify-content: center;
  332. padding: 60rpx 0;
  333. }
  334. .loading-text,
  335. .empty-text {
  336. font-size: 26rpx;
  337. color: #999;
  338. }
  339. .product-grid {
  340. display: flex;
  341. flex-wrap: wrap;
  342. justify-content: space-between;
  343. }
  344. .product-card {
  345. width: 48%;
  346. background: #fff;
  347. border-radius: 16rpx;
  348. overflow: hidden;
  349. margin-bottom: 20rpx;
  350. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.06);
  351. }
  352. .product-cover {
  353. width: 100%;
  354. height: 200rpx;
  355. background: #f0f0f0;
  356. }
  357. .product-info {
  358. padding: 14rpx;
  359. }
  360. .product-name {
  361. display: block;
  362. font-size: 26rpx;
  363. color: #333;
  364. margin-bottom: 10rpx;
  365. overflow: hidden;
  366. text-overflow: ellipsis;
  367. white-space: nowrap;
  368. }
  369. .product-price-row {
  370. display: flex;
  371. align-items: baseline;
  372. }
  373. .product-price {
  374. font-size: 28rpx;
  375. color: #5B9BD5;
  376. font-weight: bold;
  377. margin-right: 10rpx;
  378. }
  379. .product-price-login {
  380. font-size: 24rpx;
  381. color: #999;
  382. }
  383. .product-member {
  384. font-size: 20rpx;
  385. color: #999;
  386. }
  387. /* ===== 五维全景幸福矩阵 ===== */
  388. .why-section {
  389. margin: 30rpx;
  390. }
  391. .dim-matrix {
  392. display: flex;
  393. flex-wrap: wrap;
  394. gap: 20rpx;
  395. justify-content: center;
  396. }
  397. .dim-card {
  398. width: calc(33.33% - 14rpx);
  399. box-sizing: border-box;
  400. background: #fff;
  401. border-radius: 20rpx;
  402. padding: 28rpx 16rpx 24rpx;
  403. display: flex;
  404. flex-direction: column;
  405. align-items: center;
  406. text-align: center;
  407. box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.06);
  408. transition: transform 0.2s ease, box-shadow 0.2s ease;
  409. position: relative;
  410. overflow: hidden;
  411. }
  412. .dim-card:active {
  413. transform: scale(0.96);
  414. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.1);
  415. }
  416. .dim-icon-circle {
  417. width: 72rpx;
  418. height: 72rpx;
  419. border-radius: 50%;
  420. display: flex;
  421. align-items: center;
  422. justify-content: center;
  423. margin-bottom: 14rpx;
  424. flex-shrink: 0;
  425. }
  426. .dim-icon {
  427. font-size: 36rpx;
  428. line-height: 1;
  429. }
  430. .dim-tag {
  431. font-size: 22rpx;
  432. font-weight: 600;
  433. padding: 4rpx 14rpx;
  434. border-radius: 20rpx;
  435. margin-bottom: 10rpx;
  436. max-width: 100%;
  437. overflow: hidden;
  438. text-overflow: ellipsis;
  439. white-space: nowrap;
  440. }
  441. .dim-desc {
  442. font-size: 20rpx;
  443. color: #888;
  444. line-height: 1.5;
  445. display: -webkit-box;
  446. -webkit-line-clamp: 2;
  447. -webkit-box-orient: vertical;
  448. overflow: hidden;
  449. }
  450. /* 五维能量环 */
  451. .dim-energy-row {
  452. display: flex;
  453. justify-content: center;
  454. gap: 12rpx;
  455. margin-top: 24rpx;
  456. opacity: 0.5;
  457. }
  458. .energy-dot {
  459. width: 10rpx;
  460. height: 10rpx;
  461. border-radius: 50%;
  462. }
  463. /* ===== 底部登录 ===== */
  464. .login-footer {
  465. margin: 40rpx 30rpx;
  466. display: flex;
  467. flex-direction: column;
  468. align-items: center;
  469. }
  470. .login-btn {
  471. width: 100%;
  472. height: 88rpx;
  473. line-height: 88rpx;
  474. background: linear-gradient(135deg, #5B9BD5, #3A7CC4);
  475. color: #fff;
  476. font-size: 32rpx;
  477. font-weight: bold;
  478. border-radius: 44rpx;
  479. text-align: center;
  480. border: none;
  481. }
  482. .login-btn::after {
  483. border: none;
  484. }
  485. .login-footer-text {
  486. font-size: 20rpx;
  487. color: #bbb;
  488. margin-top: 16rpx;
  489. }
  490. /* ===== 加载占位 ===== */
  491. .loading-container {
  492. display: flex;
  493. justify-content: center;
  494. align-items: center;
  495. height: 100vh;
  496. }
  497. /* ===== 角色切换淡入淡出 ===== */
  498. .role-switch-wrapper {
  499. width: 100%;
  500. min-height: 100vh;
  501. }
  502. .fade-enter-active,
  503. .fade-leave-active {
  504. transition: opacity 0.3s ease;
  505. }
  506. .fade-enter,
  507. .fade-leave-to {
  508. opacity: 0;
  509. }
  510. </style>