index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. console.log('请求的 URL:', url); // 添加调试信息
  108. uni.request({
  109. url: url,
  110. method: 'POST',
  111. success: (res) => {
  112. console.log('请求成功,响应数据:', res); // 添加调试信息
  113. if (res.statusCode === 200) {
  114. if (res.data && Array.isArray(res.data.data)) {
  115. // 检查数据是否为 null 或 undefined
  116. const validCourses = res.data.data.map(course => course !== null && course !== undefined ? course : {});
  117. this.courses = validCourses;
  118. } else {
  119. console.error('响应数据格式不符合预期:', res.data);
  120. uni.showToast({
  121. title: '响应数据格式错误',
  122. icon: 'none'
  123. });
  124. }
  125. } else {
  126. console.error('获取课程列表数据失败:', `HTTP error! status: ${res.statusCode}`);
  127. uni.showToast({
  128. title: `获取课程列表失败,状态码: ${res.statusCode}`,
  129. icon: 'none'
  130. });
  131. }
  132. },
  133. fail: (error) => {
  134. console.error('获取课程列表数据失败:', error.errMsg);
  135. uni.showToast({
  136. title: `获取课程列表失败: ${error.errMsg}`,
  137. icon: 'none'
  138. });
  139. },
  140. complete: () => {
  141. // 请求完成后的操作,可根据需要添加
  142. }
  143. });
  144. },
  145. changeTab(index) {
  146. this.currentTab = index;
  147. this.fetchCourses();
  148. },
  149. onSearch() {
  150. this.fetchCourses();
  151. },
  152. // 切换显示全部分类状态
  153. toggleShowAll() {
  154. this.showAll = !this.showAll;
  155. }
  156. }
  157. };
  158. </script>
  159. <style lang="scss" scoped>
  160. .container {
  161. padding: 10px;
  162. }
  163. .search-bar {
  164. background-color: #f5f5f5;
  165. padding: 8px;
  166. border-radius: 5px;
  167. margin-bottom: 10px;
  168. }
  169. .search-bar input {
  170. width: 100%;
  171. border: none;
  172. }
  173. .category-scroll {
  174. white-space: nowrap;
  175. }
  176. .category-nav {
  177. display: inline-flex;
  178. justify-content: space-around;
  179. margin-bottom: 10px;
  180. }
  181. .category-item {
  182. padding: 5px 10px;
  183. border-radius: 3px;
  184. cursor: pointer;
  185. display: inline-block;
  186. }
  187. .category-item.active {
  188. background-color: #ff9900;
  189. color: white;
  190. }
  191. // 全部分类展开时的样式,修改为一行展示四个分类
  192. .category-nav.show-all {
  193. display: flex;
  194. flex-wrap: wrap;
  195. white-space: normal;
  196. }
  197. .category-nav.show-all .category-item {
  198. width: calc(25% - 10px); // 修改宽度为 25%,减去边距
  199. margin: 5px;
  200. box-sizing: border-box;
  201. text-align: center;
  202. }
  203. .course-list {
  204. display: flex;
  205. flex-wrap: wrap;
  206. justify-content: space-between;
  207. }
  208. .course-item {
  209. width: 48%;
  210. background-color: #fff;
  211. border-radius: 5px;
  212. box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  213. margin-bottom: 15px;
  214. overflow: hidden;
  215. }
  216. .course-img {
  217. width: 100%;
  218. height: 180px;
  219. position: relative;
  220. }
  221. .course-img image {
  222. width: 100%;
  223. height: 100%;
  224. }
  225. .course-info {
  226. padding: 10px;
  227. }
  228. .course-title {
  229. font-size: 16px;
  230. line-height: 1.4;
  231. margin-bottom: 5px;
  232. overflow: hidden;
  233. text-overflow: ellipsis;
  234. display: -webkit-box;
  235. -webkit-line-clamp: 2;
  236. -webkit-box-orient: vertical;
  237. }
  238. .course-price {
  239. color: #ff4500;
  240. font-size: 14px;
  241. }
  242. </style>