index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <view class="container">
  3. <PageBanner theme="wisdom"
  4. tagline="学以启智 · 慧及未来"
  5. quote="知者不惑,仁者不忧" />
  6. <view class="section" v-if="sectionVisible('today_tasks') && isLoggedIn">
  7. <view class="section-header">
  8. <text class="section-title">📋 我的任务</text>
  9. <text class="section-more" @click="goTasks">全部 ›</text>
  10. </view>
  11. <view class="task-wrap" v-if="todayTasks.length > 0">
  12. <view class="task-item" v-for="task in todayTasks" :key="task.id">
  13. <text class="task-name">{{ task.title }}</text>
  14. <text class="task-points">+{{ task.points || 0 }}分</text>
  15. </view>
  16. </view>
  17. <view class="task-empty" v-else>
  18. <text class="task-empty-text">今日暂无任务</text>
  19. </view>
  20. </view>
  21. <view class="func-section" v-if="sectionVisible('func_entries')">
  22. <view class="func-grid">
  23. <view class="func-item" v-for="item in funcList" :key="item.label" @click="onFuncClick(item)">
  24. <view class="func-icon-wrap">
  25. <text class="func-icon">{{ item.icon }}</text>
  26. </view>
  27. <text class="func-label">{{ item.label }}</text>
  28. </view>
  29. </view>
  30. </view>
  31. <view class="section" v-if="sectionVisible('hot_activities')">
  32. <view class="section-header">
  33. <text class="section-title">🔥 热门活动</text>
  34. <text class="section-more" @click="goMoreActivities">更多 ›</text>
  35. </view>
  36. <scroll-view scroll-x class="activity-scroll" show-scrollbar="false">
  37. <view class="activity-card" v-for="act in hotActivities" :key="act.id" @click="goActivityDetail(act.id)">
  38. <image class="activity-img" :src="act.image || '/static/default-activity.png'" mode="aspectFill" />
  39. <view class="activity-info">
  40. <text class="activity-name">{{ act.name }}</text>
  41. <text class="activity-meta">{{ act.date }} · {{ act.location }}</text>
  42. </view>
  43. </view>
  44. </scroll-view>
  45. </view>
  46. <view class="section" v-if="sectionVisible('recommended_products')">
  47. <view class="section-header">
  48. <text class="section-title">🛍️ 推荐商品</text>
  49. <text class="section-more" @click="goMoreProducts">更多 ›</text>
  50. </view>
  51. <view class="product-list">
  52. <view class="product-card" v-for="prod in recommendedProducts" :key="prod.id" @click="goProductDetail(prod.id)">
  53. <image class="product-img" :src="prod.image || '/static/default-product.png'" mode="aspectFill" />
  54. <text class="product-name line-clamp-2">{{ prod.name }}</text>
  55. <text class="product-price">{{ formatPriceWithSymbol(prod.price) }}</text>
  56. </view>
  57. </view>
  58. </view>
  59. <view class="bottom-spacer"></view>
  60. </view>
  61. </template>
  62. <script>
  63. import PageBanner from '../../components/PageBanner.vue'
  64. import { getVisibleSections, getEnergyOverview, getTodayTasks } from '../../utils/api.js'
  65. export default {
  66. components: { PageBanner },
  67. data() {
  68. return {
  69. isLoggedIn: false,
  70. role: null,
  71. currentChildId: null,
  72. totalEnergy: null,
  73. visibleSections: [],
  74. funcList: [
  75. { icon: '\u{1F4DA}', label: '学习任务', needLogin: true, page: '/pages/tasks/tasks' },
  76. { icon: '\u{1F3AE}', label: '小游戏', needLogin: false, page: '/pages/games/list' },
  77. { icon: '\u{1F9E9}', label: '测评中心', needLogin: false, page: '/pages/assessment/apply' },
  78. { icon: '\u{1F4D6}', label: '阅读天地', needLogin: false, page: '/pages/mind/articles' },
  79. { icon: '\u{1F4A1}', label: '知识挑战', needLogin: false, page: '' }
  80. ],
  81. hotActivities: [
  82. { id: 1, name: '少儿编程启蒙营', date: '周六', location: '线上', image: '' },
  83. { id: 2, name: '经典阅读分享会', date: '周日', location: '浠艾福中心', image: '' },
  84. { id: 3, name: '科学实验探索课', date: '下周六', location: '浠艾福中心', image: '' }
  85. ],
  86. recommendedProducts: [
  87. { id: 1, name: '儿童百科全书套装', price: 199, image: '' },
  88. { id: 2, name: '益智棋类礼盒', price: 159, image: '' },
  89. { id: 3, name: '科学实验套装', price: 299, image: '' },
  90. { id: 4, name: '中外名著精选', price: 129, image: '' }
  91. ],
  92. todayTasks: []
  93. }
  94. },
  95. onShow() {
  96. var token = uni.getStorageSync('token')
  97. this.isLoggedIn = !!token
  98. if (this.isLoggedIn) {
  99. this.role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || null
  100. this.currentChildId = uni.getStorageSync('currentChildId') || null
  101. }
  102. this.loadSectionConfig()
  103. if (this.isLoggedIn) {
  104. this.loadEnergyData()
  105. this.loadTodayTasks()
  106. }
  107. },
  108. methods: {
  109. async loadSectionConfig() {
  110. var baseSections = ['func_entries', 'hot_activities', 'recommended_products']
  111. if (!uni.getStorageSync('token')) {
  112. this.visibleSections = baseSections
  113. return
  114. }
  115. var role = uni.getStorageSync('currentRole') || uni.getStorageSync('role') || null
  116. try {
  117. var res = await getVisibleSections({ pageKey: 'wisdom', role: role })
  118. if (res.code === 200 && res.data) {
  119. var keys = res.data.map(function(s) { return s.sectionKey })
  120. var merged = baseSections.slice()
  121. keys.forEach(function(k) { if (merged.indexOf(k) === -1) merged.push(k) })
  122. this.visibleSections = merged
  123. }
  124. } catch (e) {
  125. this.visibleSections = ['func_entries', 'hot_activities', 'recommended_products']
  126. }
  127. },
  128. async loadEnergyData() {
  129. var childId = this.currentChildId
  130. if (!childId) return
  131. try {
  132. var res = await getEnergyOverview(childId)
  133. if (res && res.data) {
  134. this.totalEnergy = res.data.totalEnergy || 0
  135. }
  136. } catch (e) { console.log('获取能量概览失败', e) }
  137. },
  138. async loadTodayTasks() {
  139. var childId = this.currentChildId
  140. if (!childId) return
  141. try {
  142. var res = await getTodayTasks(childId)
  143. if (res && res.data) {
  144. this.todayTasks = res.data.slice(0, 5)
  145. }
  146. } catch (e) { console.log('获取今日任务失败', e) }
  147. },
  148. sectionVisible(key) {
  149. return this.visibleSections.length === 0 || this.visibleSections.indexOf(key) !== -1
  150. },
  151. onFuncClick(item) {
  152. if (item.needLogin && !this.isLoggedIn) {
  153. uni.showModal({
  154. title: '提示',
  155. content: '请先登录后使用此功能',
  156. success: function(res) { if (res.confirm) uni.navigateTo({ url: '/pages/login/login' }) }
  157. })
  158. return
  159. }
  160. if (item.page) {
  161. var nativeTabs = ['/pages/tasks/tasks', '/pages/index/index', '/pages/body/index', '/pages/mind/index', '/pages/action/index', '/pages/wisdom/index', '/pages/profile/profile']
  162. if (nativeTabs.indexOf(item.page) !== -1) {
  163. uni.switchTab({ url: item.page })
  164. } else {
  165. uni.navigateTo({ url: item.page })
  166. }
  167. }
  168. },
  169. goMoreActivities() {
  170. uni.navigateTo({ url: '/pages/activity/index' })
  171. },
  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/wisdom/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. .func-section {
  194. margin: -40rpx 20rpx 0;
  195. position: relative;
  196. z-index: 2;
  197. }
  198. .func-grid {
  199. background: #fff;
  200. border-radius: 24rpx;
  201. padding: 30rpx 16rpx;
  202. display: flex;
  203. box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.06);
  204. }
  205. .func-item {
  206. flex: 1;
  207. display: flex;
  208. flex-direction: column;
  209. align-items: center;
  210. }
  211. .func-item:active {
  212. opacity: 0.7;
  213. }
  214. .func-icon-wrap {
  215. width: 88rpx;
  216. height: 88rpx;
  217. border-radius: 50%;
  218. background: #FFF8E1;
  219. display: flex;
  220. align-items: center;
  221. justify-content: center;
  222. margin-bottom: 10rpx;
  223. }
  224. .func-icon {
  225. font-size: 40rpx;
  226. }
  227. .func-label {
  228. font-size: 24rpx;
  229. color: #333;
  230. font-weight: 500;
  231. }
  232. .section {
  233. margin: 20rpx 20rpx;
  234. }
  235. .section-header {
  236. display: flex;
  237. justify-content: space-between;
  238. align-items: center;
  239. margin-bottom: 20rpx;
  240. }
  241. .section-title {
  242. font-size: 30rpx;
  243. font-weight: bold;
  244. color: #333;
  245. }
  246. .section-more {
  247. font-size: 24rpx;
  248. color: #999;
  249. }
  250. .activity-scroll {
  251. white-space: nowrap;
  252. }
  253. .activity-card {
  254. display: inline-block;
  255. width: 280rpx;
  256. margin-right: 20rpx;
  257. background: #fff;
  258. border-radius: 16rpx;
  259. overflow: hidden;
  260. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  261. }
  262. .activity-img {
  263. width: 280rpx;
  264. height: 180rpx;
  265. background: #f0f0f0;
  266. }
  267. .activity-info {
  268. padding: 16rpx;
  269. }
  270. .activity-name {
  271. font-size: 26rpx;
  272. font-weight: bold;
  273. color: #333;
  274. display: block;
  275. }
  276. .activity-meta {
  277. font-size: 22rpx;
  278. color: #999;
  279. margin-top: 8rpx;
  280. display: block;
  281. }
  282. .product-list {
  283. display: flex;
  284. flex-wrap: wrap;
  285. gap: 20rpx;
  286. }
  287. .product-card {
  288. width: calc(50% - 10rpx);
  289. background: #fff;
  290. border-radius: 16rpx;
  291. overflow: hidden;
  292. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  293. }
  294. .product-img {
  295. width: 100%;
  296. height: 240rpx;
  297. background: #f0f0f0;
  298. }
  299. .product-name {
  300. font-size: 26rpx;
  301. color: #333;
  302. padding: 12rpx 16rpx 0;
  303. }
  304. .product-price {
  305. font-size: 28rpx;
  306. color: #F97316;
  307. font-weight: bold;
  308. padding: 8rpx 16rpx 16rpx;
  309. display: block;
  310. }
  311. .line-clamp-2 {
  312. overflow: hidden;
  313. text-overflow: ellipsis;
  314. display: -webkit-box;
  315. -webkit-line-clamp: 2;
  316. -webkit-box-orient: vertical;
  317. }
  318. .task-wrap {
  319. background: #fff;
  320. border-radius: 16rpx;
  321. overflow: hidden;
  322. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  323. }
  324. .task-item {
  325. display: flex;
  326. justify-content: space-between;
  327. padding: 28rpx 24rpx;
  328. border-bottom: 1rpx solid #f0f0f0;
  329. }
  330. .task-item:last-child {
  331. border-bottom: none;
  332. }
  333. .task-name {
  334. font-size: 26rpx;
  335. color: #333;
  336. }
  337. .task-points {
  338. font-size: 24rpx;
  339. color: #F97316;
  340. }
  341. .task-empty {
  342. background: #fff;
  343. border-radius: 16rpx;
  344. padding: 60rpx;
  345. text-align: center;
  346. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  347. }
  348. .task-empty-text {
  349. font-size: 26rpx;
  350. color: #999;
  351. }
  352. .bottom-spacer {
  353. height: 20rpx;
  354. }
  355. </style>