category.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. <template>
  2. <view class="content">
  3. <view class="dddd" style="height: 10rpx;"></view>
  4. <view class="flex cate-wrap" :style="{'height': height}">
  5. <scroll-view scroll-y="true" class="left-wrap" :style="{'height': height}">
  6. <view class="left-item" v-for="(leftitem,leftindex) in flist"
  7. :class="{'action-item': currentIndex == leftindex}" @click="leftClick(leftindex)">
  8. {{leftitem.cate_name}}
  9. </view>
  10. </scroll-view>
  11. <scroll-view scroll-y="true" class="right-wrap" :style="{'height': height}" @scrolltolower="getProducts()">
  12. <view class="right-item" v-for="gooditem in list"
  13. @click="navto('/pages/product/product?id=' + gooditem.id)">
  14. <image :src="gooditem.image" mode="" class="good-img"></image>
  15. <view class="good-info flex">
  16. <view class="good-name clamp2">
  17. {{gooditem.store_name}}
  18. </view>
  19. <view class="good-price">
  20. ¥{{gooditem.price}}
  21. </view>
  22. </view>
  23. </view>
  24. <uni-load-more :status="loadingType"></uni-load-more>
  25. </scroll-view>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. import {
  31. getCategoryList,
  32. getProducts
  33. } from '@/api/product.js';
  34. export default {
  35. data() {
  36. return {
  37. sizeCalcState: false,
  38. tabScrollTop: 0,
  39. currentId: 9,
  40. flist: [],
  41. height: '',
  42. currentIndex: 0,
  43. list: [],
  44. page: 1,
  45. limit: 20,
  46. loadingType: 'more',
  47. loaded: false,
  48. };
  49. },
  50. onLoad() {
  51. this.loadData();
  52. },
  53. // 监听导航栏输入框点击事件
  54. onNavigationBarSearchInputClicked(e) {
  55. uni.navigateTo({
  56. url: '/pages/product/search'
  57. });
  58. },
  59. onReady(res) {
  60. var _this = this;
  61. uni.getSystemInfo({
  62. success: resu => {
  63. const query = uni.createSelectorQuery();
  64. query.select('.cate-wrap').boundingClientRect();
  65. query.exec(function(res) {
  66. _this.height = resu.windowHeight - res[0].top + 'px';
  67. console.log('打印页面的剩余高度', _this.height);
  68. });
  69. },
  70. fail: res => {}
  71. });
  72. },
  73. methods: {
  74. leftClick(index) {
  75. this.currentIndex = index
  76. this.getProducts('reload')
  77. },
  78. getProducts(type) {
  79. let index = this.flist[this.currentIndex].id
  80. if (type == 'reload') {
  81. this.list = []
  82. this.page = 1
  83. this.loadingType = 'more'
  84. this.loaded = false
  85. }
  86. if (this.loadingType == 'loading' || this.loadingType == 'noMore') {
  87. return
  88. }
  89. this.loadingType = 'loading'
  90. getProducts({
  91. cid: index,
  92. page: this.page,
  93. limit: this.limit
  94. }).then(res => {
  95. this.list = this.list.concat(res.data)
  96. if (this.limit == res.data.length) {
  97. this.page++
  98. this.loadingType = 'more'
  99. } else {
  100. this.loadingType = 'noMore'
  101. }
  102. this.loaded = true
  103. })
  104. },
  105. // 载入数据
  106. async loadData() {
  107. let obj = this;
  108. getCategoryList({})
  109. .then(({
  110. data
  111. }) => {
  112. obj.flist = data.map(function(s) {
  113. return s;
  114. });
  115. obj.getProducts()
  116. })
  117. .catch(err => {
  118. console.log(err);
  119. });
  120. },
  121. //一级分类点击
  122. tabtap(item) {
  123. console.log(item);
  124. // 判断有没有初始化页面高度对象数据
  125. if (!this.sizeCalcState) {
  126. this.calcSize();
  127. }
  128. // 获取当前点击的id
  129. this.currentId = item.id;
  130. console.log(item.top);
  131. this.tabScrollTop = item.top;
  132. console.log(this.tabScrollTop);
  133. },
  134. //右侧栏滚动
  135. asideScroll(e) {
  136. // 判断有没有初始化页面高度对象数据
  137. if (!this.sizeCalcState) {
  138. this.calcSize();
  139. }
  140. let scrollTop = e.detail.scrollTop;
  141. let box = 0; //列表包裹框高度初始化
  142. let bottom = 10; //距离页面底部多少像素左侧列表切换到最后一个一级分类
  143. // 查询当前页面对象
  144. let view = uni.createSelectorQuery().select('.content');
  145. view.fields({
  146. id: true,
  147. dataset: true,
  148. rect: true,
  149. size: true,
  150. scrollOffset: true
  151. },
  152. function(e) {
  153. // 保存包裹框高度
  154. box = e.height;
  155. }
  156. ).exec();
  157. // 获取所有距离顶部大于滚轮距离页面高度的所有分类
  158. let tabs = this.flist.filter(item => (item.top - 10) <= scrollTop).reverse();
  159. if (tabs.length > 0) {
  160. // 判断是否已经到达滚轮底部
  161. if (box + scrollTop + bottom >= e.detail.scrollHeight) {
  162. this.currentId = this.flist[this.flist.length - 1].id;
  163. } else {
  164. this.currentId = tabs[0].id;
  165. }
  166. }
  167. },
  168. //计算右侧栏每个tab的高度等信息
  169. calcSize() {
  170. let h = 0;
  171. this.flist.forEach(item => {
  172. let view = uni.createSelectorQuery().select('#main-' + item.id);
  173. view.fields({
  174. size: true
  175. },
  176. data => {
  177. item.top = h;
  178. h += data.height;
  179. item.bottom = h;
  180. }
  181. ).exec();
  182. });
  183. this.sizeCalcState = true;
  184. },
  185. navToList(sid, tid) {
  186. // 点击导航跳转到详细页面
  187. uni.navigateTo({
  188. url: '/pages/product/list?fid=' + this.currentId + '&sid=' + sid + '&tid=' + tid
  189. });
  190. },
  191. navto(url) {
  192. uni.navigateTo({
  193. url
  194. })
  195. }
  196. }
  197. };
  198. </script>
  199. <style lang="scss">
  200. .cate-wrap {
  201. width: 750rpx;
  202. .left-wrap {
  203. width: 180rpx;
  204. flex-shrink: 0;
  205. background-color: #f2f2f2;
  206. .left-item {
  207. width: 180rpx;
  208. height: 100rpx;
  209. line-height: 100rpx;
  210. font-size: 28rpx;
  211. font-weight: 500;
  212. color: #666666;
  213. text-align: center;
  214. }
  215. .action-item {
  216. background-color: #fff;
  217. font-weight: bold;
  218. position: relative;
  219. &::before {
  220. content: '';
  221. position: absolute;
  222. top: 0;
  223. left: 0;
  224. width: 3rpx;
  225. height: 100rpx;
  226. background: #418c78;
  227. }
  228. }
  229. }
  230. .right-wrap {
  231. flex-wrap: 1;
  232. background-color: #fff;
  233. .right-item {
  234. width: 520rpx;
  235. height: 240rpx;
  236. padding: 30rpx 0;
  237. margin: 0 auto;
  238. border-bottom: 1px solid #eee;
  239. display: flex;
  240. justify-content: flex-start;
  241. align-items: center;
  242. .good-img {
  243. flex-shrink: 0;
  244. width: 180rpx;
  245. height: 180rpx;
  246. border-radius: 20rpx;
  247. }
  248. .good-info {
  249. flex-direction: column;
  250. justify-content: space-between;
  251. align-items: flex-start;
  252. height: 100%;
  253. padding-left: 25rpx;
  254. .good-name {
  255. font-size: 30rpx;
  256. font-weight: bold;
  257. color: #333333;
  258. }
  259. .good-price {
  260. font-size: 34rpx;
  261. font-weight: bold;
  262. color: #FF4C4C;
  263. }
  264. }
  265. }
  266. }
  267. }
  268. </style>