index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <template>
  2. <view class="container">
  3. <!-- 搜索栏 -->
  4. <view class="search-bar">
  5. <input
  6. type="text"
  7. placeholder="搜索课程"
  8. v-model="searchQuery"
  9. @input="onSearch"
  10. />
  11. </view>
  12. <!-- 分类导航 -->
  13. <scroll-view class="category-scroll" scroll-x>
  14. <view class="category-nav">
  15. <view
  16. v-for="(category, index) in showCategories"
  17. :key="index"
  18. class="category-item"
  19. :class="{active: currentTab === index}"
  20. @click="changeTab(index)"
  21. >
  22. {{ category.categoryName }}
  23. </view>
  24. <!-- 全部分类按钮 -->
  25. <view
  26. v-if="categories.length > 3"
  27. class="category-item"
  28. @click="toggleShowAll"
  29. >
  30. {{ showAll ? '收起分类' : '全部分类' }}
  31. </view>
  32. </view>
  33. </scroll-view>
  34. <!-- 课程列表 -->
  35. <view class="course-list">
  36. <view
  37. v-for="(course, index) in courses"
  38. :key="index"
  39. class="course-item"
  40. @click="goToCourseDetail(course)"
  41. >
  42. <view class="course-img">
  43. <image :src="course.imgUrl" mode="aspectFill"></image>
  44. </view>
  45. <view class="course-info">
  46. <view class="course-title">{{course.courseName}}</view>
  47. <view class="course-price">¥{{course.coursePrice}}</view>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. </template>
  53. <script>
  54. export default {
  55. data() {
  56. return {
  57. currentTab: 0,
  58. categories: [],
  59. allCourses: [], // 存储所有课程数据
  60. courses: [], // 存储当前显示的课程数据
  61. searchQuery: '',
  62. showAll: false, // 控制是否显示全部分类
  63. // 定义分类与课程的映射
  64. categoryCourseMap: {
  65. 0: [
  66. {
  67. imgUrl: 'https://picx.zhimg.com/v2-d6f44389971daab7e688e5b37046e4e4_720w.jpg?source=172ae18b',
  68. courseName: '课程1',
  69. coursePrice: 99
  70. },
  71. {
  72. imgUrl: 'https://img.ixintu.com/download/jpg/201911/e25b904bc42a74d7d77aed81e66d772c.jpg!con',
  73. courseName: '课程2',
  74. coursePrice: 199
  75. }
  76. ],
  77. 1: [
  78. {
  79. imgUrl: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.Nj6o69waOC3JTbebyCu-hgHaEc?rs=1&pid=ImgDetMain',
  80. courseName: '课程3',
  81. coursePrice: 20
  82. },
  83. {
  84. imgUrl: 'https://pic1.zhimg.com/v2-02760a1bf058904006740d3f66b2c9ac_r.jpg?source=1940ef5c',
  85. courseName: '课程4',
  86. coursePrice: 150
  87. }
  88. ],
  89. 2: [
  90. {
  91. imgUrl: 'https://ts1.tc.mm.bing.net/th/id/R-C.987f582c510be58755c4933cda68d525?rik=C0D21hJDYvXosw&riu=http%3a%2f%2fimg.pconline.com.cn%2fimages%2fupload%2fupc%2ftx%2fwallpaper%2f1305%2f16%2fc4%2f20990657_1368686545122.jpg&ehk=netN2qzcCVS4ALUQfDOwxAwFcy41oxC%2b0xTFvOYy5ds%3d&risl=&pid=ImgRaw&r=0',
  92. courseName: '课程5',
  93. coursePrice: 50
  94. },
  95. {
  96. imgUrl: 'https://ts1.tc.mm.bing.net/th/id/OIP-C.MQoT8Pi43oSD6cQGpvs-lAHaEK?rs=1&pid=ImgDetMain',
  97. courseName: '课程6',
  98. coursePrice: 80
  99. }
  100. ],
  101. 3: [
  102. {
  103. imgUrl: 'https://ts1.tc.mm.bing.net/th/id/R-C.cf381aa2a44f28ade81cb72e0e8decae?rik=xctKMrYyWQKvDg&riu=http%3a%2f%2fwww.1mpi.com%2fpic%2f9af8385ddd409baf50e081d6%2f2-810-jpg_6-1080-0-0-1080.jpg&ehk=Fw4Rzzq%2bSED%2bgMqemTtEk7fUdSL%2fkwW0KcsnygzTqp4%3d&risl=&pid=ImgRaw&r=0',
  104. courseName: '课程7',
  105. coursePrice: 120
  106. },
  107. {
  108. imgUrl: 'https://img.shetu66.com/2023/04/25/1682391088567884.png',
  109. courseName: '课程8',
  110. coursePrice: 180
  111. }
  112. ],
  113. 4: [
  114. {
  115. imgUrl: 'https://ts1.tc.mm.bing.net/th/id/R-C.82ece114307f62dc0b8aa2c366b9ef37?rik=GwmaljjvGJ75zg&riu=http%3a%2f%2fimg.pconline.com.cn%2fimages%2fupload%2fupc%2ftx%2fwallpaper%2f1308%2f15%2fc2%2f24494094_1376530595238.jpg&ehk=L%2fNVdWJDsmRvIsmKuuUcvCGnK4DJwpxad4JswscWIN0%3d&risl=&pid=ImgRaw&r=0',
  116. courseName: '课程9',
  117. coursePrice: 30
  118. },
  119. {
  120. imgUrl: 'https://bpic.588ku.com/back_origin_min_pic/21/07/09/8c9d5f41848ca6a292d52b3457ed418a.jpg',
  121. courseName: '课程10',
  122. coursePrice: 60
  123. }
  124. ],
  125. 5: [
  126. {
  127. imgUrl: 'https://pic.616pic.com/ys_bnew_img/00/13/11/J1kFGL8p6F.jpg',
  128. courseName: '课程11',
  129. coursePrice: 90
  130. },
  131. {
  132. imgUrl: 'https://pic.616pic.com/ys_bnew_img/00/47/45/WwJs1U7BJW.jpg',
  133. courseName: '课程12',
  134. coursePrice: 110
  135. }
  136. ],
  137. 6: [
  138. {
  139. imgUrl: 'https://pic.616pic.com/ys_bnew_img/00/06/07/KT7I0AbOoI.jpg',
  140. courseName: '课程13',
  141. coursePrice: 40
  142. },
  143. {
  144. imgUrl: 'https://ts1.tc.mm.bing.net/th/id/R-C.72eb213371fd74859ad6c574cf223070?rik=AjZbpwPKouep7A&riu=http%3a%2f%2fattach.66rpg.com%2fbbs%2fattachment%2fforum%2f201501%2f09%2f211837s6hkh6g5mg35336s.jpg&ehk=gJTX%2bHzLsoWwDctstn13%2bJY3QmA86kqpzexWicVHgbw%3d&risl=&pid=ImgRaw&r=0',
  145. courseName: '课程14',
  146. coursePrice: 70
  147. }
  148. ],
  149. 7: [
  150. {
  151. imgUrl: 'https://ts1.tc.mm.bing.net/th/id/R-C.478f595a4d79e0aacc812494d3f8ab9f?rik=%2bqgBvxUlXAuqqQ&riu=http%3a%2f%2ffile.51pptmoban.com%2fd%2ffile%2f2023%2f08%2f25%2f43b8e6ed4fa04f190edd0c8bd83d52b0.jpg&ehk=qiyuhH8mm%2bU9cHz1Px%2fLOZqDKA6PaSlPbn8yD5mnQGk%3d&risl=&pid=ImgRaw&r=0',
  152. courseName: '课程15',
  153. coursePrice: 130
  154. },
  155. {
  156. imgUrl: 'https://img.keaitupian.cn/newupload/07/1688632400492441.jpg',
  157. courseName: '课程16',
  158. coursePrice: 160
  159. }
  160. ],
  161. 8: [
  162. {
  163. imgUrl: 'https://img.tukuppt.com/photo-big/18/10/08/07/27/181008072732.jpg',
  164. courseName: '课程17',
  165. coursePrice: 200
  166. },
  167. {
  168. imgUrl: 'https://file.51pptmoban.com/d/file/2022/09/13/b2d85362febcf895e78916e0696f1a59.jpg',
  169. courseName: '课程18',
  170. coursePrice: 220
  171. }
  172. ]
  173. }
  174. };
  175. },
  176. computed: {
  177. // 根据 showAll 状态计算需要显示的分类
  178. showCategories() {
  179. if (this.showAll || this.categories.length <= 4) {
  180. return this.categories;
  181. } else {
  182. return this.categories.slice(0, 4);
  183. }
  184. }
  185. },
  186. mounted() {
  187. this.fetchCategories();
  188. // 初始化所有课程数据
  189. this.allCourses = Object.values(this.categoryCourseMap).flat();
  190. this.fetchCourses();
  191. },
  192. methods: {
  193. // 获取分类导航数据
  194. fetchCategories() {
  195. uni.request({
  196. url: 'http://localhost:8080/system/category/findAll',
  197. method: 'GET',
  198. success: (res) => {
  199. console.log('分类导航响应数据:', res);
  200. if (res.statusCode === 200) {
  201. // 假设数据被嵌套在 data 字段里
  202. if (res.data && Array.isArray(res.data.data)) {
  203. this.categories = res.data.data;
  204. } else {
  205. console.error('分类导航数据格式不正确:', res.data);
  206. }
  207. } else {
  208. console.error('获取分类导航数据失败:', `HTTP error! status: ${res.statusCode}`);
  209. }
  210. },
  211. fail: (error) => {
  212. console.error('获取分类导航数据失败:', error.errMsg);
  213. }
  214. });
  215. },
  216. // 获取课程列表数据
  217. // 修改 fetchCourses 方法,接收 index 参数
  218. fetchCourses(index) {
  219. const categoryIndex = index !== undefined ? index : this.currentTab;
  220. let categoryCourses = this.categoryCourseMap[categoryIndex] || [];
  221. if (this.searchQuery) {
  222. categoryCourses = categoryCourses.filter(course =>
  223. course.courseName.includes(this.searchQuery)
  224. );
  225. }
  226. this.courses = categoryCourses;
  227. },
  228. changeTab(index) {
  229. this.currentTab = index;
  230. // 调用 fetchCourses 时传递 index 参数
  231. this.fetchCourses(index);
  232. },
  233. onSearch() {
  234. // 搜索时调用 fetchCourses 不传递 index 参数,使用默认逻辑
  235. this.fetchCourses();
  236. },
  237. // 切换显示全部分类状态
  238. toggleShowAll() {
  239. this.showAll = !this.showAll;
  240. },
  241. goToCourseDetail(course) {
  242. uni.navigateTo({
  243. url: `/pages/courseDetail/courseDetail?imgUrl=${encodeURIComponent(course.imgUrl)}&courseName=${encodeURIComponent(course.courseName)}&coursePrice=${course.coursePrice}`
  244. });
  245. }
  246. }
  247. };
  248. </script>
  249. <style lang="scss" scoped>
  250. .container {
  251. padding: 10px;
  252. }
  253. .search-bar {
  254. background-color: #f5f5f5;
  255. padding: 8px;
  256. border-radius: 5px;
  257. margin-bottom: 10px;
  258. }
  259. .search-bar input {
  260. width: 100%;
  261. border: none;
  262. }
  263. .category-scroll {
  264. white-space: nowrap;
  265. }
  266. .category-nav {
  267. display: inline-flex;
  268. justify-content: space-around;
  269. margin-bottom: 10px;
  270. }
  271. .category-item {
  272. padding: 5px 10px;
  273. border-radius: 3px;
  274. cursor: pointer;
  275. display: inline-block;
  276. }
  277. .category-item.active {
  278. background-color: #ff9900;
  279. color: white;
  280. }
  281. // 全部分类展开时的样式,修改为一行展示四个分类
  282. .category-nav.show-all {
  283. display: flex;
  284. flex-wrap: wrap;
  285. white-space: normal;
  286. }
  287. .category-nav.show-all .category-item {
  288. width: calc(25% - 10px); // 修改宽度为 25%,减去边距
  289. margin: 5px;
  290. box-sizing: border-box;
  291. text-align: center;
  292. }
  293. .course-list {
  294. display: flex;
  295. flex-wrap: wrap;
  296. justify-content: space-between;
  297. }
  298. .course-item {
  299. width: 48%;
  300. background-color: #fff;
  301. border-radius: 5px;
  302. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  303. margin-bottom: 15px;
  304. overflow: hidden;
  305. }
  306. .course-img {
  307. width: 100%;
  308. height: 180px;
  309. position: relative;
  310. }
  311. .course-img image {
  312. width: 100%;
  313. height: 100%;
  314. }
  315. .course-info {
  316. padding: 10px;
  317. }
  318. .course-title {
  319. font-size: 16px;
  320. line-height: 1.4;
  321. margin-bottom: 5px;
  322. overflow: hidden;
  323. text-overflow: ellipsis;
  324. display: -webkit-box;
  325. -webkit-line-clamp: 2;
  326. -webkit-box-orient: vertical;
  327. }
  328. .course-price {
  329. color: #ff4500;
  330. font-size: 14px;
  331. }
  332. </style>