discover.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. <template>
  2. <view class="discover-container">
  3. <!-- 自定义状态栏 -->
  4. <view class="status-bar"></view>
  5. <!-- 搜索框 -->
  6. <view class="search-section">
  7. <view class="search-box">
  8. <text class="search-icon">🔍</text>
  9. <input class="search-input" placeholder="python · html · java..." />
  10. </view>
  11. </view>
  12. <!-- 轮播图 -->
  13. <view class="banner-section" v-if="swipeGroup.length > 0">
  14. <swiper class="banner-swiper" :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
  15. <swiper-item v-for="(item, index) in swipeGroup" :key="index">
  16. <image
  17. class="swipe-image"
  18. :src="item.cover"
  19. mode="aspectFill"
  20. @click="navigateToSwipeLink(index)"
  21. />
  22. </swiper-item>
  23. </swiper>
  24. </view>
  25. <!-- 功能模块 -->
  26. <view class="function-section">
  27. <view class="function-row">
  28. <view class="function-item" v-for="(item, index) in functionList.slice(0, 5)" :key="index" @click="handleFunctionClick(item)">
  29. <view class="function-icon">
  30. <image :src="item.icon" mode="aspectFit"></image>
  31. </view>
  32. <text class="function-text">{{ item.name }}</text>
  33. <view v-if="item.badge" class="function-badge">{{ item.badge }}</view>
  34. </view>
  35. </view>
  36. <view class="function-row">
  37. <view class="function-item" v-for="(item, index) in functionList.slice(5, 10)" :key="index" @click="handleFunctionClick(item)">
  38. <view class="function-icon" :style="{ backgroundColor: item.bgColor }">
  39. <image :src="item.icon" mode="aspectFit"></image>
  40. </view>
  41. <text class="function-text">{{ item.name }}</text>
  42. </view>
  43. </view>
  44. </view>
  45. <!-- 课程推荐卡片 -->
  46. <view class="course-cards">
  47. <view class="course-card" v-for="(card, index) in courseCards" :key="index" :style="{ backgroundColor: card.bgColor }">
  48. <view class="card-content">
  49. <text class="card-title">{{ card.title }}</text>
  50. <text class="card-subtitle">{{ card.subtitle }}</text>
  51. </view>
  52. <image class="card-image" :src="card.image" mode="aspectFit"></image>
  53. </view>
  54. </view>
  55. <!-- 课程列表 -->
  56. <view class="course-section">
  57. <view class="section-header">
  58. <text class="section-title">大家都在学</text>
  59. <view class="more-btn">
  60. <text>更多课程</text>
  61. <text class="arrow-icon">›</text>
  62. </view>
  63. </view>
  64. <view class="course-list">
  65. <view class="course-item" v-for="(course, index) in courseList" :key="index" @click="handleCourseClick(course)">
  66. <image class="course-cover" :src="course.cover" mode="aspectFill"></image>
  67. <view class="course-info">
  68. <text class="course-title">{{ course.title }}</text>
  69. <view class="course-stats">
  70. <text class="course-students">{{ course.students }}人学习</text>
  71. <text class="course-type">{{ course.type }}</text>
  72. </view>
  73. <text class="course-desc">{{ course.description }}</text>
  74. </view>
  75. </view>
  76. </view>
  77. </view>
  78. </view>
  79. </template>
  80. <script setup>
  81. import { ref, onMounted } from 'vue'
  82. import requestModule from '@/utils/request'
  83. import config from '@/config/index.js'
  84. // 轮播图数据
  85. const swipeGroup = ref([])
  86. // 功能模块数据
  87. const functionList = ref([
  88. { name: '学练课', icon: '/static/icons/study.png', bgColor: '#FF6B6B', badge: '好课' },
  89. { name: '视频课', icon: '/static/icons/video.png', bgColor: '#9B59B6' },
  90. { name: '题库', icon: '/static/icons/question.png', bgColor: '#F39C12' },
  91. { name: '教程', icon: '/static/icons/tutorial.png', bgColor: '#2ECC71' },
  92. { name: 'VIP会员', icon: '/static/icons/vip.png', bgColor: '#E74C3C', badge: '16周年' },
  93. { name: '基础入门', icon: '/static/icons/basic.png', bgColor: '#3498DB' },
  94. { name: '数据分析', icon: '/static/icons/data.png', bgColor: '#1ABC9C' },
  95. { name: '人工智能', icon: '/static/icons/ai.png', bgColor: '#95A5A6' },
  96. { name: '爬虫', icon: '/static/icons/spider.png', bgColor: '#34495E' },
  97. { name: '办公自动化', icon: '/static/icons/office.png', bgColor: '#16A085' }
  98. ])
  99. // 课程推荐卡片
  100. const courseCards = ref([
  101. {
  102. title: 'Python高薪就业课',
  103. subtitle: '面向大学、职场就业提效',
  104. bgColor: 'linear-gradient(135deg, #667eea 0%, #764ba2 100%)',
  105. image: '/static/cards/job-card.png'
  106. },
  107. {
  108. title: 'Python少儿编程',
  109. subtitle: '面向小学、初高中学习',
  110. bgColor: 'linear-gradient(135deg, #f093fb 0%, #f5576c 100%)',
  111. image: '/static/cards/kids-card.png'
  112. }
  113. ])
  114. // 课程列表数据
  115. const courseList = ref([
  116. {
  117. cover: '/static/courses/python-basic.png',
  118. title: 'Python3 入门课程',
  119. students: '5037',
  120. type: '学练课',
  121. description: '爬虫+机器学习? Python无所不能...'
  122. },
  123. {
  124. cover: '/static/courses/python-zero.png',
  125. title: '零基础入门学Python',
  126. students: '619',
  127. type: '视频课',
  128. description: '通过Python入门到实战全套教程...'
  129. },
  130. {
  131. cover: '/static/courses/python-guide.png',
  132. title: 'Python3零基础入门先导课',
  133. students: '5847',
  134. type: '学练课',
  135. description: '0基础的python入门先导课程...'
  136. }
  137. ])
  138. // 获取轮播图
  139. const getSwipe = () => {
  140. try {
  141. // 先尝试从本地存储获取缓存数据
  142. const cachedSwipe = uni.getStorageSync('swipeGroup')
  143. if (cachedSwipe) {
  144. swipeGroup.value = JSON.parse(cachedSwipe)
  145. }
  146. } catch (e) {
  147. console.error('获取缓存轮播图失败:', e)
  148. }
  149. // 检查必要的依赖
  150. if (!requestModule || !config) {
  151. console.error('request或config模块未正确导入')
  152. return
  153. }
  154. try {
  155. // 安全获取系统信息
  156. const systemInfo = uni.getSystemInfoSync()
  157. // 从服务器获取最新数据 - 使用正确的API调用方式
  158. requestModule.request('/api/appapi/getSlide', {}, {
  159. apptype: config.APP_TYPE || 'production',
  160. systemType: systemInfo.platform || 'unknown'
  161. }, (ret, err) => {
  162. try {
  163. if (ret && ret.statusCode < 300 && ret.data) {
  164. swipeGroup.value = []
  165. // console.log('轮播图原始数据:', JSON.stringify(ret.data))
  166. // console.log('ret.data类型:', typeof ret.data, 'isArray:', Array.isArray(ret.data))
  167. // 确保ret.data是数组
  168. let dataArray = ret.data
  169. if (typeof ret.data === 'string') {
  170. try {
  171. dataArray = JSON.parse(ret.data)
  172. } catch (e) {
  173. console.error('解析轮播图数据失败:', e)
  174. dataArray = []
  175. }
  176. }
  177. // 如果不是数组,尝试从其他可能的字段获取
  178. if (!Array.isArray(dataArray)) {
  179. if (dataArray && dataArray.list && Array.isArray(dataArray.list)) {
  180. dataArray = dataArray.list
  181. } else if (dataArray && dataArray.data && Array.isArray(dataArray.data)) {
  182. dataArray = dataArray.data
  183. } else {
  184. console.error('轮播图数据格式不正确:', dataArray)
  185. dataArray = []
  186. }
  187. }
  188. // 处理数组数据
  189. if (Array.isArray(dataArray) && dataArray.length > 0) {
  190. dataArray.forEach((item) => {
  191. if (item && item.cover) {
  192. const swipeItem = {
  193. cover: config.BASE_URL + '/' + item.cover,
  194. link: item.link || '',
  195. title: item.title || ''
  196. }
  197. swipeGroup.value.push(swipeItem)
  198. }
  199. })
  200. }
  201. // 缓存到本地存储
  202. try {
  203. uni.setStorageSync('swipeGroup', JSON.stringify(swipeGroup.value))
  204. } catch (e) {
  205. console.error('缓存轮播图失败:', e)
  206. }
  207. // console.log('处理后的轮播图数据:', JSON.stringify(swipeGroup.value))
  208. } else if (swipeGroup.value.length === 0) {
  209. // 如果没有数据且缓存也为空,使用默认图片
  210. swipeGroup.value = [{
  211. cover: '/static/banner/default-slide.png',
  212. link: '',
  213. title: '默认轮播图'
  214. }]
  215. }
  216. if (err) {
  217. console.error('获取轮播图失败:', err)
  218. }
  219. } catch (error) {
  220. console.error('处理轮播图数据时出错:', error)
  221. }
  222. })
  223. } catch (error) {
  224. console.error('获取系统信息或发起请求时出错:', error)
  225. }
  226. }
  227. // 轮播图点击跳转
  228. const navigateToSwipeLink = (index) => {
  229. const item = swipeGroup.value[index]
  230. if (!item || !item.link) return
  231. console.log('点击轮播图:', item.title, item.link)
  232. // 处理不同类型的链接
  233. if (item.link.startsWith('http')) {
  234. // 外部链接,使用webview打开
  235. uni.navigateTo({
  236. url: `/pages/webview/webview?url=${encodeURIComponent(item.link)}&title=${encodeURIComponent(item.title || '详情')}`
  237. })
  238. } else if (item.link.includes('||')) {
  239. // 内部页面跳转格式: winname||path||params
  240. const linkParts = item.link.split('||')
  241. if (linkParts.length >= 2) {
  242. const pagePath = linkParts[1]
  243. const params = linkParts[2] || ''
  244. // 构建跳转路径
  245. let navigateUrl = `/pages/${pagePath}`
  246. if (params) {
  247. navigateUrl += `?params=${encodeURIComponent(params)}`
  248. }
  249. uni.navigateTo({
  250. url: navigateUrl
  251. })
  252. }
  253. }
  254. }
  255. // 方法
  256. const handleFunctionClick = (item) => {
  257. console.log('点击功能:', item.name)
  258. // 这里可以添加跳转逻辑
  259. }
  260. const handleCourseClick = (course) => {
  261. console.log('点击课程:', course.title)
  262. // 这里可以添加跳转到课程详情的逻辑
  263. }
  264. onMounted(() => {
  265. try {
  266. console.log('发现页面加载完成')
  267. // 获取轮播图数据
  268. getSwipe()
  269. } catch (error) {
  270. console.error('页面初始化时出错:', error)
  271. }
  272. })
  273. </script>
  274. <style lang="scss" scoped>
  275. .discover-container {
  276. background-color: #f5f5f5;
  277. min-height: 100vh;
  278. }
  279. // 自定义状态栏
  280. .status-bar {
  281. background-color: #fff;
  282. /* 使用CSS环境变量适配安全区域 */
  283. height: env(safe-area-inset-top);
  284. height: constant(safe-area-inset-top); /* 兼容iOS < 11.2 */
  285. /* 设置最小高度,确保在没有刘海屏的设备上也有合适的高度 */
  286. min-height: 20px;
  287. }
  288. // 搜索框样式
  289. .search-section {
  290. background-color: #fff;
  291. padding: 20rpx 30rpx;
  292. .search-box {
  293. display: flex;
  294. align-items: center;
  295. background-color: #f8f8f8;
  296. border-radius: 50rpx;
  297. padding: 20rpx 30rpx;
  298. .search-icon {
  299. font-size: 32rpx;
  300. color: #999;
  301. }
  302. .search-input {
  303. flex: 1;
  304. margin-left: 20rpx;
  305. font-size: 28rpx;
  306. color: #333;
  307. }
  308. }
  309. }
  310. // 轮播图样式
  311. .banner-section {
  312. margin: 20rpx 30rpx;
  313. .banner-swiper {
  314. height: 300rpx;
  315. border-radius: 20rpx;
  316. overflow: hidden;
  317. }
  318. .swipe-image {
  319. width: 100%;
  320. height: 100%;
  321. border-radius: 20rpx;
  322. }
  323. }
  324. // 功能模块样式
  325. .function-section {
  326. background-color: #fff;
  327. margin: 20rpx 30rpx;
  328. border-radius: 20rpx;
  329. padding: 40rpx 20rpx;
  330. .function-row {
  331. display: flex;
  332. justify-content: space-around;
  333. margin-bottom: 40rpx;
  334. &:last-child {
  335. margin-bottom: 0;
  336. }
  337. }
  338. .function-item {
  339. display: flex;
  340. flex-direction: column;
  341. align-items: center;
  342. position: relative;
  343. .function-icon {
  344. width: 80rpx;
  345. height: 80rpx;
  346. border-radius: 20rpx;
  347. display: flex;
  348. align-items: center;
  349. justify-content: center;
  350. margin-bottom: 15rpx;
  351. }
  352. .function-text {
  353. font-size: 24rpx;
  354. color: #333;
  355. }
  356. .function-badge {
  357. position: absolute;
  358. top: -10rpx;
  359. right: -10rpx;
  360. background-color: #FF4757;
  361. color: #fff;
  362. font-size: 18rpx;
  363. padding: 4rpx 8rpx;
  364. border-radius: 20rpx;
  365. transform: scale(0.8);
  366. }
  367. }
  368. }
  369. // 课程推荐卡片样式
  370. .course-cards {
  371. margin: 20rpx 30rpx;
  372. display: flex;
  373. gap: 20rpx;
  374. .course-card {
  375. flex: 1;
  376. height: 200rpx;
  377. border-radius: 20rpx;
  378. padding: 30rpx;
  379. display: flex;
  380. align-items: center;
  381. justify-content: space-between;
  382. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  383. .card-content {
  384. flex: 1;
  385. .card-title {
  386. display: block;
  387. color: #fff;
  388. font-size: 28rpx;
  389. font-weight: bold;
  390. margin-bottom: 10rpx;
  391. }
  392. .card-subtitle {
  393. display: block;
  394. color: #fff;
  395. font-size: 22rpx;
  396. opacity: 0.9;
  397. }
  398. }
  399. .card-image {
  400. width: 80rpx;
  401. height: 80rpx;
  402. }
  403. }
  404. }
  405. // 课程列表样式
  406. .course-section {
  407. background-color: #fff;
  408. margin: 20rpx 30rpx;
  409. border-radius: 20rpx;
  410. padding: 30rpx;
  411. .section-header {
  412. display: flex;
  413. align-items: center;
  414. justify-content: space-between;
  415. margin-bottom: 30rpx;
  416. .section-title {
  417. font-size: 32rpx;
  418. font-weight: bold;
  419. color: #333;
  420. }
  421. .more-btn {
  422. display: flex;
  423. align-items: center;
  424. color: #999;
  425. font-size: 24rpx;
  426. .arrow-icon {
  427. font-size: 28rpx;
  428. margin-left: 8rpx;
  429. color: #999;
  430. }
  431. }
  432. }
  433. .course-list {
  434. .course-item {
  435. display: flex;
  436. margin-bottom: 30rpx;
  437. &:last-child {
  438. margin-bottom: 0;
  439. }
  440. .course-cover {
  441. width: 160rpx;
  442. height: 120rpx;
  443. border-radius: 12rpx;
  444. margin-right: 20rpx;
  445. }
  446. .course-info {
  447. flex: 1;
  448. .course-title {
  449. display: block;
  450. font-size: 28rpx;
  451. font-weight: bold;
  452. color: #333;
  453. margin-bottom: 10rpx;
  454. }
  455. .course-stats {
  456. display: flex;
  457. align-items: center;
  458. margin-bottom: 10rpx;
  459. .course-students {
  460. font-size: 22rpx;
  461. color: #999;
  462. margin-right: 20rpx;
  463. }
  464. .course-type {
  465. font-size: 22rpx;
  466. color: #999;
  467. }
  468. }
  469. .course-desc {
  470. font-size: 24rpx;
  471. color: #666;
  472. line-height: 1.4;
  473. }
  474. }
  475. }
  476. }
  477. }
  478. </style>