index.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. >
  41. <view class="course-img">
  42. <image :src="course.imgUrl" mode="aspectFill"></image>
  43. </view>
  44. <view class="course-info">
  45. <view class="course-title">{{course.courseName}}</view>
  46. <view class="course-price">¥{{course.coursePrice}}</view>
  47. </view>
  48. </view>
  49. </view>
  50. </view>
  51. </template>
  52. <script>
  53. export default {
  54. data() {
  55. return {
  56. currentTab: 1,
  57. categories: [],
  58. courses: [],
  59. searchQuery: '',
  60. showAll: false // 控制是否显示全部分类
  61. };
  62. },
  63. computed: {
  64. // 根据 showAll 状态计算需要显示的分类
  65. showCategories() {
  66. if (this.showAll || this.categories.length <= 4) {
  67. return this.categories;
  68. } else {
  69. return this.categories.slice(0, 4);
  70. }
  71. }
  72. },
  73. mounted() {
  74. this.fetchCategories();
  75. this.fetchCourses();
  76. },
  77. methods: {
  78. // 获取分类导航数据
  79. fetchCategories() {
  80. uni.request({
  81. url: 'http://localhost:8080/system/category/findAll',
  82. method: 'GET',
  83. success: (res) => {
  84. console.log('分类导航响应数据:', res);
  85. if (res.statusCode === 200) {
  86. // 假设数据被嵌套在 data 字段里
  87. if (res.data && Array.isArray(res.data.data)) {
  88. this.categories = res.data.data;
  89. } else {
  90. console.error('分类导航数据格式不正确:', res.data);
  91. }
  92. } else {
  93. console.error('获取分类导航数据失败:', `HTTP error! status: ${res.statusCode}`);
  94. }
  95. },
  96. fail: (error) => {
  97. console.error('获取分类导航数据失败:', error.errMsg);
  98. }
  99. });
  100. },
  101. // 获取课程列表数据
  102. fetchCourses() {
  103. let url = `http://localhost:8080/system/course/findById?courseCategory=${this.currentTab}`;
  104. if (this.searchQuery) {
  105. url += `&search=${this.searchQuery}`;
  106. }
  107. uni.request({
  108. url: url,
  109. method: 'GET',
  110. success: (res) => {
  111. if (res.statusCode === 200) {
  112. this.courses = res.data;
  113. } else {
  114. console.error('获取课程列表数据失败:', `HTTP error! status: ${res.statusCode}`);
  115. uni.showToast({
  116. title: `获取课程列表失败,状态码: ${res.statusCode}`,
  117. icon: 'none'
  118. });
  119. }
  120. },
  121. fail: (error) => {
  122. console.error('获取课程列表数据失败:', error.errMsg);
  123. uni.showToast({
  124. title: `获取课程列表失败: ${error.errMsg}`,
  125. icon: 'none'
  126. });
  127. },
  128. complete: () => {
  129. // 请求完成后的操作,可根据需要添加
  130. }
  131. });
  132. },
  133. changeTab(index) {
  134. this.currentTab = index;
  135. this.fetchCourses();
  136. },
  137. onSearch() {
  138. this.fetchCourses();
  139. },
  140. // 切换显示全部分类状态
  141. toggleShowAll() {
  142. this.showAll = !this.showAll;
  143. }
  144. }
  145. };
  146. </script>
  147. <style lang="scss" scoped>
  148. .container {
  149. padding: 10px;
  150. }
  151. .search-bar {
  152. background-color: #f5f5f5;
  153. padding: 8px;
  154. border-radius: 5px;
  155. margin-bottom: 10px;
  156. }
  157. .search-bar input {
  158. width: 100%;
  159. border: none;
  160. }
  161. .category-scroll {
  162. white-space: nowrap;
  163. }
  164. .category-nav {
  165. display: inline-flex;
  166. justify-content: space-around;
  167. margin-bottom: 10px;
  168. }
  169. .category-item {
  170. padding: 5px 10px;
  171. border-radius: 3px;
  172. cursor: pointer;
  173. display: inline-block;
  174. }
  175. .category-item.active {
  176. background-color: #ff9900;
  177. color: white;
  178. }
  179. // 全部分类展开时的样式,修改为一行展示四个分类
  180. .category-nav.show-all {
  181. display: flex;
  182. flex-wrap: wrap;
  183. white-space: normal;
  184. }
  185. .category-nav.show-all .category-item {
  186. width: calc(25% - 10px); // 修改宽度为 25%,减去边距
  187. margin: 5px;
  188. box-sizing: border-box;
  189. text-align: center;
  190. }
  191. .course-list {
  192. display: flex;
  193. flex-wrap: wrap;
  194. justify-content: space-between;
  195. }
  196. .course-item {
  197. width: 48%;
  198. background-color: #fff;
  199. border-radius: 5px;
  200. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  201. margin-bottom: 15px;
  202. overflow: hidden;
  203. }
  204. .course-img {
  205. width: 100%;
  206. height: 180px;
  207. position: relative;
  208. }
  209. .course-img image {
  210. width: 100%;
  211. height: 100%;
  212. }
  213. .course-info {
  214. padding: 10px;
  215. }
  216. .course-title {
  217. font-size: 16px;
  218. line-height: 1.4;
  219. margin-bottom: 5px;
  220. overflow: hidden;
  221. text-overflow: ellipsis;
  222. display: -webkit-box;
  223. -webkit-line-clamp: 2;
  224. -webkit-box-orient: vertical;
  225. }
  226. .course-price {
  227. color: #ff4500;
  228. font-size: 14px;
  229. }
  230. </style>