index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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-extra/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. uni.navigateTo({ url: '/pages/shop/index' })
  175. },
  176. goProductDetail(id) {
  177. var token = uni.getStorageSync('token')
  178. if (token) {
  179. uni.navigateTo({ url: '/pages/discover/product-detail/product-detail?id=' + id })
  180. } else {
  181. uni.navigateTo({ url: '/pages/login/login?redirect=' + encodeURIComponent('/pages/wisdom/index') })
  182. }
  183. },
  184. goTasks() { uni.switchTab({ url: '/pages/tasks/tasks' }) },
  185. goLogin() { uni.navigateTo({ url: '/pages/login/login' }) }
  186. }
  187. }
  188. </script>
  189. <style scoped>
  190. .container {
  191. min-height: 100vh;
  192. background: #f5f7fa;
  193. padding-bottom: 120rpx;
  194. }
  195. .func-section {
  196. margin: -40rpx 20rpx 0;
  197. position: relative;
  198. z-index: 2;
  199. }
  200. .func-grid {
  201. background: #fff;
  202. border-radius: 24rpx;
  203. padding: 30rpx 16rpx;
  204. display: flex;
  205. box-shadow: 0 4rpx 20rpx rgba(0,0,0,0.06);
  206. }
  207. .func-item {
  208. flex: 1;
  209. display: flex;
  210. flex-direction: column;
  211. align-items: center;
  212. }
  213. .func-item:active {
  214. opacity: 0.7;
  215. }
  216. .func-icon-wrap {
  217. width: 88rpx;
  218. height: 88rpx;
  219. border-radius: 50%;
  220. background: #FFF8E1;
  221. display: flex;
  222. align-items: center;
  223. justify-content: center;
  224. margin-bottom: 10rpx;
  225. }
  226. .func-icon {
  227. font-size: 40rpx;
  228. }
  229. .func-label {
  230. font-size: 24rpx;
  231. color: #333;
  232. font-weight: 500;
  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. .activity-scroll {
  253. white-space: nowrap;
  254. }
  255. .activity-card {
  256. display: inline-block;
  257. width: 280rpx;
  258. margin-right: 20rpx;
  259. background: #fff;
  260. border-radius: 16rpx;
  261. overflow: hidden;
  262. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  263. }
  264. .activity-img {
  265. width: 280rpx;
  266. height: 180rpx;
  267. background: #f0f0f0;
  268. }
  269. .activity-info {
  270. padding: 16rpx;
  271. }
  272. .activity-name {
  273. font-size: 26rpx;
  274. font-weight: bold;
  275. color: #333;
  276. display: block;
  277. }
  278. .activity-meta {
  279. font-size: 22rpx;
  280. color: #999;
  281. margin-top: 8rpx;
  282. display: block;
  283. }
  284. .product-list {
  285. display: flex;
  286. flex-wrap: wrap;
  287. gap: 20rpx;
  288. }
  289. .product-card {
  290. width: calc(50% - 10rpx);
  291. background: #fff;
  292. border-radius: 16rpx;
  293. overflow: hidden;
  294. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  295. }
  296. .product-img {
  297. width: 100%;
  298. height: 240rpx;
  299. background: #f0f0f0;
  300. }
  301. .product-name {
  302. font-size: 26rpx;
  303. color: #333;
  304. padding: 12rpx 16rpx 0;
  305. }
  306. .product-price {
  307. font-size: 28rpx;
  308. color: #F97316;
  309. font-weight: bold;
  310. padding: 8rpx 16rpx 16rpx;
  311. display: block;
  312. }
  313. .line-clamp-2 {
  314. overflow: hidden;
  315. text-overflow: ellipsis;
  316. display: -webkit-box;
  317. -webkit-line-clamp: 2;
  318. -webkit-box-orient: vertical;
  319. }
  320. .task-wrap {
  321. background: #fff;
  322. border-radius: 16rpx;
  323. overflow: hidden;
  324. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  325. }
  326. .task-item {
  327. display: flex;
  328. justify-content: space-between;
  329. padding: 28rpx 24rpx;
  330. border-bottom: 1rpx solid #f0f0f0;
  331. }
  332. .task-item:last-child {
  333. border-bottom: none;
  334. }
  335. .task-name {
  336. font-size: 26rpx;
  337. color: #333;
  338. }
  339. .task-points {
  340. font-size: 24rpx;
  341. color: #F97316;
  342. }
  343. .task-empty {
  344. background: #fff;
  345. border-radius: 16rpx;
  346. padding: 60rpx;
  347. text-align: center;
  348. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  349. }
  350. .task-empty-text {
  351. font-size: 26rpx;
  352. color: #999;
  353. }
  354. .bottom-spacer {
  355. height: 20rpx;
  356. }
  357. </style>