category.vue 5.6 KB

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