index.vue 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. <template>
  2. <view class="wrap">
  3. <u-navbar :autoBack="true" title="课程" bgColor="rgba(255,255,255,255)" :placeholder="true" titleStyle="font-weight:bold;color:#121212"></u-navbar>
  4. <view class="search">
  5. <view class="b-input">
  6. <input class="s-input" :value="keyword" @input="onInput" @confirm="onSearch" placeholder="搜索课程" />
  7. <view class="s-submit iconfont icon-search" @click="onSearch"></view>
  8. </view>
  9. </view>
  10. <view class="category-list">
  11. <view class="b-row">
  12. <view class="b-all" @click="onCategorySelect('first', -1)" :class="{ 'f-active': -1 === firstCategoryIndex }">不限</view>
  13. <view class="b-items">
  14. <view
  15. class="s-item"
  16. v-for="(item, index) in categoryList"
  17. :key="index"
  18. @click="onCategorySelect('first', index)"
  19. :class="{ 'f-active': index === firstCategoryIndex }"
  20. >
  21. {{ item.name }}
  22. </view>
  23. </view>
  24. </view>
  25. <view class="b-row" v-if="secondCategoryList.length > 0">
  26. <view class="b-all" @click="onCategorySelect('second', -1)" :class="{ 'f-active': -1 === secondCategoryIndex }">不限</view>
  27. <view class="b-items">
  28. <view
  29. class="s-item"
  30. v-for="(item, index) in secondCategoryList"
  31. :key="index"
  32. @click="onCategorySelect('second', index)"
  33. :class="{ 'f-active': index === secondCategoryIndex }"
  34. >
  35. {{ item.name }}
  36. </view>
  37. </view>
  38. </view>
  39. <view class="b-row" v-if="thirdCategoryList.length > 0">
  40. <view class="b-all" @click="onCategorySelect('third', -1)" :class="{ 'f-active': -1 === thirdCategoryIndex }">不限</view>
  41. <view class="b-items">
  42. <view
  43. class="s-item"
  44. v-for="(item, index) in thirdCategoryList"
  45. :key="index"
  46. @click="onCategorySelect('third', index)"
  47. :class="{ 'f-active': index === thirdCategoryIndex }"
  48. >
  49. {{ item.name }}
  50. </view>
  51. </view>
  52. </view>
  53. </view>
  54. <view class="filter-box">
  55. <view class="b-item" v-for="(item, index) in groupList" :key="index" :class="{ 'f-active': index == groupIndex }" @click="onGroupSelect(index)">
  56. <view class="s-tit">{{ item.name }}</view>
  57. <view class="iconfont" v-if="item.sort && index == groupIndex" :class="{ 'icon-unfold': item.sort == 'asc', 'icon-fold': item.sort == 'desc' }"></view>
  58. </view>
  59. </view>
  60. <view class="course-list">
  61. <view class="b-items">
  62. <course-item v-for="item in courseList" :item="item" :key="item.id"></course-item>
  63. </view>
  64. <!-- 加载中 -->
  65. <load-more :loadingType="loadingType" :loadingText="loadingText"></load-more>
  66. </view>
  67. </view>
  68. </template>
  69. <script>
  70. // import { getCategoryList, getCourseList } from '@/service/api/course.js';
  71. // import mixinsCommon from '@/mixins/common.js';
  72. let that;
  73. export default {
  74. // mixins: [mixinsCommon],
  75. data() {
  76. return {
  77. groupIndex: 0,
  78. groupList: [
  79. {
  80. name: '全部',
  81. value: 'all',
  82. sort: ''
  83. },
  84. {
  85. name: '免费',
  86. value: 'free',
  87. sort: ''
  88. },
  89. {
  90. name: 'VIP',
  91. value: 'vip',
  92. sort: ''
  93. },
  94. {
  95. name: '热门',
  96. value: 'hot',
  97. sort: ''
  98. },
  99. {
  100. name: '价格',
  101. value: 'price',
  102. sort: 'asc'
  103. }
  104. ],
  105. categoryList: [],
  106. categoryValue: 0,
  107. firstCategoryIndex: -1,
  108. secondCategoryIndex: -1,
  109. thirdCategoryIndex: -1,
  110. courseList: [],
  111. keyword: '',
  112. loadingType: 1,
  113. loadingText: ''
  114. };
  115. },
  116. computed: {
  117. secondCategoryList() {
  118. if (this.firstCategoryIndex === -1) {
  119. return [];
  120. }
  121. return this.categoryList[this.firstCategoryIndex].children;
  122. },
  123. thirdCategoryList() {
  124. if (this.secondCategoryIndex === -1) {
  125. return [];
  126. }
  127. return this.categoryList[this.firstCategoryIndex].children[this.secondCategoryIndex].children;
  128. }
  129. },
  130. onLoad(options) {
  131. that = this;
  132. this.$api.getCategoryList({}, function (res) {
  133. console.log(res.data, 9999);
  134. that.categoryList = res.data;
  135. });
  136. // getCategoryList().then(([err, res]) => {
  137. // console.log('getCourseList', err, res);
  138. // if (!err) {
  139. // this.categoryList = res;
  140. // console.log(this.categoryList, 'this.categoryList');
  141. // }
  142. // });
  143. this.loadList(true);
  144. },
  145. onReachBottom() {
  146. if (this.loadingType !== 1 && this.loadingType !== 2) {
  147. this.loadList();
  148. }
  149. },
  150. methods: {
  151. loadList(refresh) {
  152. console.log('loadList', refresh);
  153. if (refresh) {
  154. this.page = 1;
  155. this.courseList = [];
  156. } else {
  157. this.page++;
  158. }
  159. this.loadingType = 1;
  160. this.loadingText = '';
  161. this.$api.getCourseList(
  162. {
  163. category_id: this.categoryValue,
  164. group: this.groupList[this.groupIndex].value,
  165. keyword: this.keyword,
  166. page: this.page,
  167. page_size: 10,
  168. sort: this.groupList[this.groupIndex].sort
  169. },
  170. function (res) {
  171. that.loadingType = -1;
  172. console.log('啦啦啦', res);
  173. that.loadingType = -1;
  174. if (res.code == 1) {
  175. if (res.data.items.length > 0) {
  176. that.courseList = [...that.courseList, ...res.data.items];
  177. } else {
  178. that.loadingType = 2;
  179. if (that.page == 1) {
  180. that.loadingText = '暂无课程数据';
  181. }
  182. that.page--; // 重置分页
  183. }
  184. } else {
  185. that.loadingType = 3;
  186. }
  187. }
  188. );
  189. // getCourseList(this.categoryValue, this.groupList[this.groupIndex].value, this.groupList[this.groupIndex].sort, this.keyword, this.page, 10).then(([err, res]) => {
  190. // console.log('getCourseList', err, res);
  191. // this.loadingType = -1;
  192. // if (!err) {
  193. // if (res.items.length > 0) {
  194. // this.courseList = [...this.courseList, ...res.items];
  195. // } else {
  196. // this.loadingType = 2;
  197. // if (this.page == 1) {
  198. // this.loadingText = '暂无课程数据';
  199. // }
  200. // this.page--; // 重置分页
  201. // }
  202. // } else {
  203. // this.loadingType = 3;
  204. // }
  205. // });
  206. },
  207. onInput(e) {
  208. this.keyword = e.detail.value;
  209. },
  210. onSearch() {
  211. if (this.keyword) {
  212. this.onCategorySelect('first', -1);
  213. }
  214. },
  215. onCategorySelect(level, index) {
  216. if (this.keyword && !(level === 'first' && index === -1)) {
  217. this.keyword = '';
  218. }
  219. if (level === 'first') {
  220. this.firstCategoryIndex = index;
  221. if (index === -1) {
  222. this.categoryValue = 0;
  223. } else {
  224. this.categoryValue = this.categoryList[index].id;
  225. }
  226. } else if (level === 'second') {
  227. this.secondCategoryIndex = index;
  228. if (index === -1) {
  229. this.categoryValue = this.categoryList[this.firstCategoryIndex].id;
  230. } else {
  231. this.categoryValue = this.categoryList[this.firstCategoryIndex].children[index].id;
  232. }
  233. } else if (level === 'third') {
  234. this.thirdCategoryIndex = index;
  235. if (index === -1) {
  236. this.categoryValue = this.categoryList[this.firstCategoryIndex].children[this.secondCategoryIndex].id;
  237. } else {
  238. this.categoryValue = this.categoryList[this.firstCategoryIndex].children[this.secondCategoryIndex].children[index].id;
  239. }
  240. }
  241. this.loadList(true);
  242. },
  243. // 分类点击事件
  244. onGroupSelect(index) {
  245. console.log(index);
  246. if (index == this.groupIndex) {
  247. const sort = this.groupList[this.groupIndex].sort;
  248. if (sort) {
  249. if (sort === 'asc') {
  250. this.groupList[this.groupIndex].sort = 'desc';
  251. } else {
  252. this.groupList[this.groupIndex].sort = 'asc';
  253. }
  254. }
  255. }
  256. this.groupIndex = index;
  257. this.loadList(true);
  258. }
  259. }
  260. };
  261. </script>
  262. <style>
  263. .search {
  264. margin: auto;
  265. padding: 0 30upx;
  266. width: 690upx;
  267. height: 90upx;
  268. background: #da5650;
  269. display: flex;
  270. align-items: center;
  271. }
  272. .search .b-input {
  273. padding: 0 20upx;
  274. flex: 1;
  275. height: 70upx;
  276. background: #fff;
  277. border-radius: 10upx;
  278. display: flex;
  279. align-items: center;
  280. }
  281. .search .b-input .s-input {
  282. flex: 1;
  283. color: #333;
  284. font-size: 26upx;
  285. }
  286. .search .b-input .s-submit {
  287. margin-left: 20upx;
  288. color: #c3c3c3;
  289. font-size: 40upx;
  290. }
  291. .category-list {
  292. background: #fff;
  293. }
  294. .category-list .b-row {
  295. padding: 0 30upx;
  296. display: flex;
  297. border-bottom: 2upx solid #eee;
  298. }
  299. .category-list .b-all {
  300. padding: 20upx 0;
  301. font-size: 28upx;
  302. }
  303. .category-list .b-all.f-active {
  304. font-weight: bold;
  305. }
  306. .category-list .b-items {
  307. margin-left: 20upx;
  308. flex: 1;
  309. display: flex;
  310. flex-wrap: wrap;
  311. }
  312. .category-list .b-items .s-item {
  313. padding: 20upx 20upx;
  314. font-size: 28upx;
  315. }
  316. .category-list .b-items .s-item.f-active {
  317. font-weight: bold;
  318. }
  319. .filter-box {
  320. background: #fff;
  321. display: flex;
  322. align-items: center;
  323. }
  324. .filter-box .b-item {
  325. padding: 20upx 30upx;
  326. display: flex;
  327. align-items: center;
  328. justify-content: center;
  329. font-size: 28upx;
  330. }
  331. .filter-box .b-item.f-active {
  332. font-weight: bold;
  333. }
  334. .filter-box .b-item .s-tit {
  335. color: #333;
  336. }
  337. .filter-box .b-item .iconfont {
  338. color: #999;
  339. font-size: 28upx;
  340. margin-left: 10upx;
  341. }
  342. .course-list {
  343. margin-top: 30upx;
  344. }
  345. .course-list .course-item {
  346. border-bottom: 1upx solid #eee;
  347. }
  348. .course-list .course-item:last-child {
  349. border-bottom: none;
  350. }
  351. </style>