index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <template>
  2. <view class="container">
  3. <view class="spacer"></view>
  4. <view class="search-bar">
  5. <input v-model="searchText" @confirm="onSearch" placeholder="搜索课程" />
  6. <text class="iconfont icon-search"></text>
  7. </view>
  8. <view class="category-tabs">
  9. <view
  10. v-for="(tab, idx) in categories"
  11. :key="tab.id"
  12. :class="['tab', idx === activeTab ? 'active' : '']"
  13. @click="activeTab = idx"
  14. >{{ tab.name }}</view>
  15. </view>
  16. <!-- Loading state -->
  17. <view v-if="loading" class="loading-container">
  18. <uni-load-more status="loading" />
  19. </view>
  20. <!-- Error state -->
  21. <!-- <view v-else-if="error" class="error-container">
  22. <text class="error-text">{{ error }}</text>
  23. <button class="retry-btn" @click="findAll">重试</button>
  24. </view> -->
  25. <!-- Content -->
  26. <view v-else class="course-grid">
  27. <view v-for="cour in courses" :key="cour.id" class="card" @click="navigateToDetail(cour.id)">
  28. <image :src="cour.mainImage" class="cover" mode="aspectFill" style="width: 200px;height: 200px;"/>
  29. <view class="content">
  30. <view class="title">{{ cour.name }}</view>
  31. <view class="desc">{{ cour.description }}</view>
  32. <view class="price-row">
  33. <text class="price">¥{{ cour.price?.toFixed(2) || '0.00' }}</text>
  34. <text class="sales">已售 {{ cour.sales || 0 }}</text>
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. export default {
  43. data() {
  44. return {
  45. searchText: '',
  46. activeTab: 0,
  47. categories: [],
  48. courses: [],
  49. loading: false,
  50. error: null,
  51. defaultCover: '/static/default-course.png'
  52. }
  53. },
  54. computed: {
  55. filteredCourses() {
  56. let list = this.courses
  57. if (this.activeTab !== 0) {
  58. list = list.filter(c => c.category === this.categories[this.activeTab])
  59. }
  60. if (this.searchText) {
  61. list = list.filter(c =>
  62. c.title.toLowerCase().includes(this.searchText.toLowerCase()) ||
  63. c.description.toLowerCase().includes(this.searchText.toLowerCase())
  64. )
  65. }
  66. console.log(list)
  67. return list
  68. }
  69. },
  70. onLoad() {
  71. this.findAll();
  72. this.findCategory();
  73. },
  74. methods: {
  75. onSearch() {
  76. // Search is handled by computed property
  77. },
  78. async findCategory() {
  79. this.loading = true
  80. this.error = null
  81. try {
  82. const res = await uni.request({
  83. url: 'http://localhost:9527/product/findCategory',
  84. method: 'GET'
  85. })
  86. console.log(res.data)
  87. if (res.statusCode === 200 ) {
  88. this.categories = res.data.data
  89. } else {
  90. throw new Error(res.data.message || '获取数据失败')
  91. }
  92. } catch (err) {
  93. this.error = err.message || '网络错误,请稍后重试'
  94. console.error('获取课程列表失败:', err)
  95. } finally {
  96. this.loading = false
  97. }
  98. },
  99. async findAll() {
  100. this.loading = true
  101. this.error = null
  102. try {
  103. const res = await uni.request({
  104. url: 'http://localhost:9527/product/findAll',
  105. method: 'GET'
  106. })
  107. if (res.statusCode === 200 ) {
  108. console.log(res.data.data)
  109. this.courses = res.data.data
  110. } else {
  111. throw new Error(res.data.message || '获取数据失败')
  112. }
  113. } catch (err) {
  114. this.error = err.message || '网络错误,请稍后重试'
  115. console.error('获取课程列表失败:', err)
  116. } finally {
  117. this.loading = false
  118. }
  119. },
  120. navigateToDetail(id) {
  121. console.log(id)
  122. uni.navigateTo({
  123. url: `/pages/course/detail?id=${id}`
  124. })
  125. }
  126. }
  127. }
  128. </script>
  129. <style lang="scss">
  130. .container {
  131. min-height: 100vh;
  132. background: linear-gradient(180deg,#eaf6ff 0%,#f7f8fa 100%);
  133. padding-bottom: 120rpx;
  134. }
  135. .spacer {
  136. height: 60rpx;
  137. padding-top: 40px;
  138. }
  139. .search-bar {
  140. display: flex;
  141. align-items: center;
  142. background: #fff;
  143. border-radius: 40rpx;
  144. box-shadow: 0 2rpx 12rpx rgba(0,0,0,0.06);
  145. padding: 0 32rpx;
  146. margin: 0 auto 24rpx auto;
  147. width: 80vw;
  148. max-width: 600rpx;
  149. height: 80rpx;
  150. }
  151. input {
  152. flex: 1;
  153. border: none;
  154. background: transparent;
  155. font-size: 30rpx;
  156. height: 80rpx;
  157. }
  158. .iconfont {
  159. color: #bbb;
  160. font-size: 36rpx;
  161. margin-left: 10rpx;
  162. }
  163. .category-tabs {
  164. display: flex;
  165. margin: 20rpx 0 0 0;
  166. background: #fff;
  167. border-radius: 20rpx;
  168. overflow-x: auto;
  169. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.03);
  170. }
  171. .tab {
  172. flex: 1;
  173. text-align: center;
  174. padding: 20rpx 0;
  175. font-size: 30rpx;
  176. color: #666;
  177. position: relative;
  178. white-space: nowrap;
  179. }
  180. .tab.active {
  181. color: #3498db;
  182. font-weight: bold;
  183. }
  184. .tab.active::after {
  185. content: '';
  186. position: absolute;
  187. left: 50%;
  188. bottom: 0;
  189. transform: translateX(-50%);
  190. width: 40rpx;
  191. height: 6rpx;
  192. background: #3498db;
  193. border-radius: 3rpx;
  194. }
  195. .course-grid {
  196. display: grid;
  197. grid-template-columns: repeat(2, 1fr);
  198. gap: 32rpx;
  199. padding: 32rpx 24rpx 0 24rpx;
  200. }
  201. .card {
  202. background: #fff;
  203. border-radius: 20rpx;
  204. box-shadow: 0 4rpx 16rpx rgba(0,0,0,0.08);
  205. overflow: hidden;
  206. transition: transform 0.2s;
  207. display: flex;
  208. flex-direction: column;
  209. min-height: 380rpx;
  210. &:active {
  211. transform: scale(0.98);
  212. }
  213. }
  214. .cover {
  215. width: 100%;
  216. height: 140rpx;
  217. background-color: #f5f5f5;
  218. border-radius: 20rpx 20rpx 0 0;
  219. }
  220. .content {
  221. flex: 1;
  222. display: flex;
  223. flex-direction: column;
  224. justify-content: space-between;
  225. padding: 20rpx 16rpx 16rpx 16rpx;
  226. }
  227. .title {
  228. font-size: 28rpx;
  229. font-weight: 600;
  230. margin-bottom: 8rpx;
  231. overflow: hidden;
  232. text-overflow: ellipsis;
  233. white-space: nowrap;
  234. }
  235. .desc {
  236. font-size: 24rpx;
  237. color: #888;
  238. margin-bottom: 12rpx;
  239. display: -webkit-box;
  240. -webkit-line-clamp: 2;
  241. -webkit-box-orient: vertical;
  242. overflow: hidden;
  243. }
  244. .price-row {
  245. display: flex;
  246. justify-content: space-between;
  247. align-items: center;
  248. }
  249. .price {
  250. color: #e74c3c;
  251. font-size: 28rpx;
  252. font-weight: bold;
  253. }
  254. .sales {
  255. font-size: 24rpx;
  256. color: #999;
  257. }
  258. .loading-container,
  259. .error-container {
  260. display: flex;
  261. flex-direction: column;
  262. align-items: center;
  263. justify-content: center;
  264. padding: 40rpx;
  265. }
  266. .error-text {
  267. color: #e74c3c;
  268. font-size: 28rpx;
  269. margin-bottom: 20rpx;
  270. }
  271. .retry-btn {
  272. background: #3498db;
  273. color: #fff;
  274. padding: 16rpx 32rpx;
  275. border-radius: 30rpx;
  276. font-size: 28rpx;
  277. }
  278. </style>