index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <view class="container">
  3. <!-- 品牌头部 -->
  4. <PageBanner theme="action"
  5. tagline="知行合一 · 快乐成长"
  6. quote="千里之行,始于足下" />
  7. <!-- 我的任务(登录后可见,放在最上面) -->
  8. <view class="section" v-if="sectionVisible('today_tasks') && isLoggedIn">
  9. <view class="section-header">
  10. <text class="section-title">📋 我的任务</text>
  11. <text class="section-more" @click="goTasks">全部 ›</text>
  12. </view>
  13. <view class="task-wrap" v-if="todayTasks.length > 0">
  14. <view class="task-item" v-for="task in todayTasks" :key="task.id">
  15. <text class="task-name">{{ task.title }}</text>
  16. <text class="task-points">+{{ task.points || 0 }}分</text>
  17. </view>
  18. </view>
  19. <view class="task-empty" v-else>
  20. <text class="task-empty-text">今日暂无任务</text>
  21. </view>
  22. </view>
  23. <!-- 功能入口(4个:活动、商品、课程) -->
  24. <view class="func-section" v-if="sectionVisible('func_entries')">
  25. <view class="func-grid">
  26. <view class="func-item" v-for="item in funcList" :key="item.label" @click="onFuncClick(item)">
  27. <view class="func-icon-wrap">
  28. <text class="func-icon">{{ item.icon }}</text>
  29. </view>
  30. <text class="func-label">{{ item.label }}</text>
  31. </view>
  32. </view>
  33. </view>
  34. <!-- 热门活动 -->
  35. <view class="section" v-if="sectionVisible('hot_activities')">
  36. <view class="section-header">
  37. <text class="section-title">🔥 热门活动</text>
  38. <text class="section-more" @click="goMoreActivities">更多 ›</text>
  39. </view>
  40. <scroll-view scroll-x class="activity-scroll" show-scrollbar="false">
  41. <view class="activity-card" v-for="act in hotActivities" :key="act.id" @click="goActivityDetail(act.id)">
  42. <image class="activity-img" :src="act.image || '/static/default-activity.png'" mode="aspectFill" />
  43. <view class="activity-info">
  44. <text class="activity-name">{{ act.name }}</text>
  45. <text class="activity-meta">{{ act.date }} · {{ act.location }}</text>
  46. </view>
  47. </view>
  48. </scroll-view>
  49. </view>
  50. <!-- 推荐商品 -->
  51. <view class="section" v-if="sectionVisible('recommended_products')">
  52. <view class="section-header">
  53. <text class="section-title">🛍️ 推荐商品</text>
  54. <text class="section-more" @click="goMoreProducts">更多 ›</text>
  55. </view>
  56. <view class="product-list">
  57. <view class="product-card" v-for="prod in recommendedProducts" :key="prod.id" @click="goProductDetail(prod.id)">
  58. <image class="product-img" :src="prod.image || '/static/default-product.png'" mode="aspectFill" />
  59. <text class="product-name line-clamp-2">{{ prod.name }}</text>
  60. <text class="product-price">¥{{ prod.price }}</text>
  61. </view>
  62. </view>
  63. </view>
  64. <!-- 底部占位 -->
  65. <view class="bottom-spacer"></view>
  66. </view>
  67. </template>
  68. <script>
  69. import PageBanner from '../../components/PageBanner.vue'
  70. import { getVisibleSections, getEnergyOverview, getTodayTasks } from '../../utils/api.js'
  71. export default {
  72. components: { PageBanner },
  73. data() {
  74. return {
  75. isLoggedIn: false,
  76. role: null,
  77. currentChildId: null,
  78. totalEnergy: null,
  79. visibleSections: [],
  80. funcList: [
  81. { icon: '\u{1F3CB}', label: '任务', needLogin: true, page: '/pages/tasks/tasks' },
  82. { icon: '\u{1F3C0}', label: '活动', needLogin: false, page: '/pages/discover/index?type=activity' },
  83. { icon: '\u{1F6CD}', label: '商城', needLogin: false, page: '/pages/shop/index' },
  84. { icon: '\u{1F3EB}', label: '课程', needLogin: false, page: '/pages/discover/index?type=course' }
  85. ],
  86. hotActivities: [
  87. { id: 1, name: '亲子户外徒步', date: '周六', location: '奥林匹克公园', image: '' },
  88. { id: 2, name: '家庭健康跑', date: '周日', location: '朝阳公园', image: '' },
  89. { id: 3, name: '儿童瑜伽体验', date: '下周六', location: '浠艾福中心', image: '' }
  90. ],
  91. recommendedProducts: [
  92. { id: 1, name: '儿童智能手环', price: 299, image: '' },
  93. { id: 2, name: '家庭健康管理套装', price: 599, image: '' },
  94. { id: 3, name: '亲子运动礼盒', price: 399, image: '' },
  95. { id: 4, name: '儿童营养补充品', price: 199, image: '' }
  96. ],
  97. todayTasks: []
  98. }
  99. },
  100. onShow() {
  101. var token = uni.getStorageSync('token')
  102. this.isLoggedIn = !!token
  103. if (this.isLoggedIn) {
  104. this.role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || null
  105. this.currentChildId = uni.getStorageSync('currentChildId') || null
  106. }
  107. this.loadSectionConfig()
  108. if (this.isLoggedIn) {
  109. this.loadEnergyData()
  110. this.loadTodayTasks()
  111. }
  112. },
  113. methods: {
  114. async loadSectionConfig() {
  115. if (!uni.getStorageSync('token')) {
  116. this.visibleSections = ['func_entries', 'hot_activities', 'recommended_products']
  117. return
  118. }
  119. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || null
  120. try {
  121. var res = await getVisibleSections({ pageKey: 'action', role: role })
  122. if (res.code === 200 && res.data) {
  123. this.visibleSections = res.data.map(function(s) { return s.sectionKey })
  124. }
  125. } catch (e) {
  126. this.visibleSections = ['func_entries', 'hot_activities', 'recommended_products']
  127. }
  128. },
  129. async loadEnergyData() {
  130. var childId = this.currentChildId
  131. if (!childId) return
  132. try {
  133. var res = await getEnergyOverview(childId)
  134. if (res && res.data) {
  135. this.totalEnergy = res.data.totalEnergy || 0
  136. }
  137. } catch (e) { console.log('获取能量概览失败', e) }
  138. },
  139. async loadTodayTasks() {
  140. var childId = this.currentChildId
  141. if (!childId) return
  142. try {
  143. var res = await getTodayTasks(childId)
  144. if (res && res.data) {
  145. this.todayTasks = res.data.slice(0, 5)
  146. }
  147. } catch (e) { console.log('获取今日任务失败', e) }
  148. },
  149. sectionVisible(key) {
  150. return this.visibleSections.length === 0 || this.visibleSections.indexOf(key) !== -1
  151. },
  152. onFuncClick(item) {
  153. if (item.needLogin && !this.isLoggedIn) {
  154. uni.showModal({
  155. title: '提示',
  156. content: '请先登录后使用此功能',
  157. success: function(res) { if (res.confirm) uni.navigateTo({ url: '/pages/login/login' }) }
  158. })
  159. return
  160. }
  161. if (item.page) {
  162. // TabBar page uses switchTab, others use navigateTo
  163. var nativeTabs = ['/pages/tasks/tasks', '/pages/index/index', '/pages/body/index', '/pages/mind/index', '/pages/action/index', '/pages/profile/profile']
  164. if (nativeTabs.indexOf(item.page) !== -1) {
  165. uni.switchTab({ url: item.page })
  166. } else {
  167. uni.navigateTo({ url: item.page })
  168. }
  169. }
  170. },
  171. goMoreActivities() {},
  172. goActivityDetail(id) {},
  173. goMoreProducts() {},
  174. goProductDetail(id) {
  175. var token = uni.getStorageSync('token')
  176. if (token) {
  177. uni.navigateTo({ url: '/pages/discover/product-detail/product-detail?id=' + id })
  178. } else {
  179. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/action/index') })
  180. }
  181. },
  182. goTasks() { uni.switchTab({ url: '/pages/tasks/tasks' }) },
  183. goLogin() { uni.navigateTo({ url: '/pages/login/login' }) }
  184. }
  185. }
  186. </script>
  187. <style scoped>
  188. .container {
  189. min-height: 100vh;
  190. background: #f5f7fa;
  191. padding-bottom: 120rpx;
  192. }
  193. /* ===== 功能入口 ===== */
  194. .func-section {
  195. margin: -40rpx 20rpx 0;
  196. position: relative;
  197. z-index: 2;
  198. }
  199. .func-grid {
  200. background: #fff;
  201. border-radius: 24rpx;
  202. padding: 30rpx 16rpx;
  203. display: flex;
  204. box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.06);
  205. }
  206. .func-item {
  207. flex: 1;
  208. display: flex;
  209. flex-direction: column;
  210. align-items: center;
  211. }
  212. .func-item:active {
  213. opacity: 0.7;
  214. }
  215. .func-icon-wrap {
  216. width: 88rpx;
  217. height: 88rpx;
  218. border-radius: 50%;
  219. background: #FFF7ED;
  220. display: flex;
  221. align-items: center;
  222. justify-content: center;
  223. margin-bottom: 10rpx;
  224. }
  225. .func-icon {
  226. font-size: 40rpx;
  227. }
  228. .func-label {
  229. font-size: 24rpx;
  230. color: #333;
  231. font-weight: 500;
  232. }
  233. /* ===== 通用区块 ===== */
  234. .section {
  235. margin: 20rpx 20rpx;
  236. }
  237. .section-header {
  238. display: flex;
  239. justify-content: space-between;
  240. align-items: center;
  241. margin-bottom: 20rpx;
  242. }
  243. .section-title {
  244. font-size: 30rpx;
  245. font-weight: bold;
  246. color: #333;
  247. }
  248. .section-more {
  249. font-size: 24rpx;
  250. color: #999;
  251. }
  252. /* ===== 热门活动 ===== */
  253. .activity-scroll {
  254. white-space: nowrap;
  255. }
  256. .activity-card {
  257. display: inline-block;
  258. width: 280rpx;
  259. margin-right: 20rpx;
  260. background: #fff;
  261. border-radius: 16rpx;
  262. overflow: hidden;
  263. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  264. }
  265. .activity-img {
  266. width: 280rpx;
  267. height: 180rpx;
  268. background: #f0f0f0;
  269. }
  270. .activity-info {
  271. padding: 16rpx;
  272. }
  273. .activity-name {
  274. font-size: 26rpx;
  275. font-weight: bold;
  276. color: #333;
  277. display: block;
  278. }
  279. .activity-meta {
  280. font-size: 22rpx;
  281. color: #999;
  282. margin-top: 8rpx;
  283. display: block;
  284. }
  285. /* ===== 推荐商品 ===== */
  286. .product-list {
  287. display: flex;
  288. flex-wrap: wrap;
  289. gap: 20rpx;
  290. }
  291. .product-card {
  292. width: calc(50% - 10rpx);
  293. background: #fff;
  294. border-radius: 16rpx;
  295. overflow: hidden;
  296. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  297. }
  298. .product-img {
  299. width: 100%;
  300. height: 240rpx;
  301. background: #f0f0f0;
  302. }
  303. .product-name {
  304. font-size: 26rpx;
  305. color: #333;
  306. padding: 12rpx 16rpx 0;
  307. }
  308. .product-price {
  309. font-size: 28rpx;
  310. color: #F97316;
  311. font-weight: bold;
  312. padding: 8rpx 16rpx 16rpx;
  313. display: block;
  314. }
  315. .line-clamp-2 {
  316. overflow: hidden;
  317. text-overflow: ellipsis;
  318. display: -webkit-box;
  319. -webkit-line-clamp: 2;
  320. -webkit-box-orient: vertical;
  321. }
  322. /* ===== 今日任务 ===== */
  323. .task-wrap {
  324. background: #fff;
  325. border-radius: 16rpx;
  326. overflow: hidden;
  327. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  328. }
  329. .task-item {
  330. display: flex;
  331. justify-content: space-between;
  332. padding: 28rpx 24rpx;
  333. border-bottom: 1rpx solid #f0f0f0;
  334. }
  335. .task-item:last-child {
  336. border-bottom: none;
  337. }
  338. .task-name {
  339. font-size: 26rpx;
  340. color: #333;
  341. }
  342. .task-points {
  343. font-size: 24rpx;
  344. color: #F97316;
  345. }
  346. .task-empty {
  347. background: #fff;
  348. border-radius: 16rpx;
  349. padding: 60rpx;
  350. text-align: center;
  351. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  352. }
  353. .task-empty-text {
  354. font-size: 26rpx;
  355. color: #999;
  356. }
  357. /* ===== 底部 ===== */
  358. .bottom-spacer {
  359. height: 20rpx;
  360. }
  361. </style>