category.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <template>
  2. <view class="content">
  3. <view class="" style="height: 20rpx;"></view>
  4. <template v-for="item in flist">
  5. <view class="cate-item" v-if="item.pic" @click="navto('/pages/product/list?fid='+item.id)">
  6. <image :src="item.pic" mode="widthFix"></image>
  7. </view>
  8. </template>
  9. </view>
  10. </template>
  11. <script>
  12. import { getCategoryList,getProducts } from '@/api/product.js';
  13. export default {
  14. data() {
  15. return {
  16. sizeCalcState: false,
  17. tabScrollTop: 0,
  18. currentId: 9,
  19. flist: [],
  20. height: '',
  21. currentIndex: 0,
  22. list: [],
  23. page: 1,
  24. limit: 20,
  25. loadingType: 'more',
  26. loaded: false,
  27. };
  28. },
  29. onLoad() {
  30. this.loadData();
  31. },
  32. // 监听导航栏输入框点击事件
  33. onNavigationBarSearchInputClicked(e) {
  34. uni.navigateTo({
  35. url: '/pages/product/search'
  36. });
  37. },
  38. onReady(res) {
  39. },
  40. methods: {
  41. leftClick(index) {
  42. this.currentIndex = index
  43. this.getProducts('reload')
  44. },
  45. getProducts(type) {
  46. let index = this.flist[this.currentIndex].id
  47. if(type == 'reload') {
  48. this.list = []
  49. this.page = 1
  50. this.loadingType = 'more'
  51. this.loaded = false
  52. }
  53. if(this.loadingType == 'loading' ) {
  54. return
  55. }
  56. if( this.loadingType == 'noMore') {
  57. if(this.currentIndex < (this.flist.length - 1)) {
  58. console.log(this.currentIndex)
  59. this.currentIndex++
  60. this.list = []
  61. this.page = 1
  62. this.loadingType = 'more'
  63. this.loaded = false
  64. this.getProducts()
  65. }else {
  66. console.log('ddd')
  67. }
  68. }
  69. this.loadingType = 'loading'
  70. getProducts({
  71. cid: index
  72. }).then(res => {
  73. this.list = this.list.concat(res.data)
  74. if(this.limit == res.data.length) {
  75. this.loadingType = 'more'
  76. }else {
  77. this.loadingType = 'noMore'
  78. }
  79. this.loaded = true
  80. })
  81. },
  82. // 载入数据
  83. async loadData() {
  84. let obj = this;
  85. getCategoryList({})
  86. .then(({ data }) => {
  87. obj.flist = data.map(function(s) {
  88. return s;
  89. });
  90. // obj.getProducts()
  91. })
  92. .catch(err => {
  93. console.log(err);
  94. });
  95. },
  96. //一级分类点击
  97. tabtap(item) {
  98. console.log(item);
  99. // 判断有没有初始化页面高度对象数据
  100. if (!this.sizeCalcState) {
  101. this.calcSize();
  102. }
  103. // 获取当前点击的id
  104. this.currentId = item.id;
  105. console.log(item.top);
  106. this.tabScrollTop = item.top;
  107. console.log(this.tabScrollTop);
  108. },
  109. //右侧栏滚动
  110. asideScroll(e) {
  111. // 判断有没有初始化页面高度对象数据
  112. if (!this.sizeCalcState) {
  113. this.calcSize();
  114. }
  115. let scrollTop = e.detail.scrollTop;
  116. let box = 0; //列表包裹框高度初始化
  117. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  118. // 查询当前页面对象
  119. let view = uni.createSelectorQuery().select('.content');
  120. view.fields(
  121. {
  122. id: true,
  123. dataset: true,
  124. rect: true,
  125. size: true,
  126. scrollOffset: true
  127. },
  128. function(e) {
  129. // 保存包裹框高度
  130. box = e.height;
  131. }
  132. ).exec();
  133. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  134. let tabs = this.flist.filter(item =>( item.top-10) <= scrollTop).reverse();
  135. if (tabs.length > 0) {
  136. // 判断是否已经到达滚轮底部
  137. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  138. this.currentId = this.flist[this.flist.length - 1].id;
  139. } else {
  140. this.currentId = tabs[0].id;
  141. }
  142. }
  143. },
  144. //计算右侧栏每个tab的高度等信息
  145. calcSize() {
  146. let h = 0;
  147. this.flist.forEach(item => {
  148. let view = uni.createSelectorQuery().select('#main-' + item.id);
  149. view.fields(
  150. {
  151. size: true
  152. },
  153. data => {
  154. item.top = h;
  155. h += data.height;
  156. item.bottom = h;
  157. }
  158. ).exec();
  159. });
  160. this.sizeCalcState = true;
  161. },
  162. navToList(sid, tid) {
  163. // 点击导航跳转到详细页面
  164. uni.navigateTo({
  165. url: '/pages/product/list?fid='+this.currentId+'&sid='+sid+'&tid='+tid
  166. });
  167. },
  168. navto(url) {
  169. uni.navigateTo({
  170. url
  171. })
  172. }
  173. }
  174. };
  175. </script>
  176. <style lang="scss">
  177. page {
  178. height: auto;
  179. min-height: 100%;
  180. background-color: #fff;
  181. }
  182. .cate-item {
  183. width: 717rpx;
  184. margin:0 auto 30rpx;
  185. image {
  186. width: 100%;
  187. }
  188. }
  189. </style>