category.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. <template>
  2. <view class="container">
  3. <view class="cate-nav">
  4. <scroll-view scroll-x="true" class="cate-nav-body" style="width: 750rpx;" :scroll-left="scrollLeft">
  5. <view v-for="(item, index) in navList" :key="index" :class="'item ' + (id == item.id ? 'active' : '')" :data-id="item.id"
  6. :data-index="index" @tap="switchCate">
  7. <view class="name">{{item.name}}</view>
  8. </view>
  9. </scroll-view>
  10. </view>
  11. <scroll-view scroll-y="true" :scroll-top="scrollTop" :style="{height:scrollHeight}">
  12. <view class="cate-item">
  13. <view class="h">
  14. <text class="name">{{currentCategory.name||''}}</text>
  15. <text class="desc">{{currentCategory.front_name||''}}</text>
  16. </view>
  17. <view class="b">
  18. <navigator :class="'item '+((iindex + 1) % 2 == 0 ? 'item-b' : '')" :url="'/pages/goods/goods?id='+iitem.id" v-for="(iitem, iindex) in goodsList"
  19. :key="iindex">
  20. <image class="img" :src="iitem.list_pic_url" background-size="cover"></image>
  21. <text class="name">{{iitem.name||''}}</text>
  22. <text class="price">¥{{iitem.retail_price||''}}</text>
  23. </navigator>
  24. </view>
  25. </view>
  26. <view v-if="goodsList.length>4" class="loadmore">
  27. <block v-if="nomore">
  28. <text>{{nomoreText}}</text>
  29. </block>
  30. <block v-else>
  31. <text class="iconfont icon-loading loading" space="nbsp"></text>
  32. <text> {{loadmoreText}}</text>
  33. </block>
  34. </view>
  35. </scroll-view>
  36. </view>
  37. </template>
  38. <script>
  39. const util = require("@/utils/util.js");
  40. const api = require('@/utils/api.js');
  41. export default {
  42. data() {
  43. return {
  44. navList: [],
  45. goodsList: [],
  46. id: 0,
  47. currentCategory: {},
  48. scrollLeft: 0,
  49. scrollTop: 0,
  50. scrollHeight: 0,
  51. page: 1,
  52. size: 10,
  53. loadmoreText: '正在加载更多数据',
  54. nomoreText: '全部加载完成',
  55. nomore: false,
  56. totalPages: 1
  57. }
  58. },
  59. methods: {
  60. getCategoryInfo: function() {
  61. let that = this;
  62. util.request(api.GoodsCategory, {
  63. id: this.id
  64. }).then(function(res) {
  65. if (res.errno == 0) {
  66. that.navList = res.data.brotherCategory
  67. that.currentCategory = res.data.currentCategory
  68. //nav位置
  69. let currentIndex = 0;
  70. let navListCount = that.navList.length;
  71. for (let i = 0; i < navListCount; i++) {
  72. currentIndex += 1;
  73. if (that.navList[i].id == that.id) {
  74. break;
  75. }
  76. }
  77. if (currentIndex > navListCount / 2 && navListCount > 5) {
  78. that.scrollLeft = currentIndex * 60
  79. }
  80. that.getGoodsList();
  81. } else {
  82. //显示错误信息
  83. }
  84. });
  85. },
  86. getGoodsList: function() {
  87. var that = this;
  88. if (that.totalPages <= that.page - 1) {
  89. that.nomore = true
  90. return;
  91. }
  92. util.request(api.GoodsList, {
  93. categoryId: that.id,
  94. page: that.page,
  95. size: that.size
  96. }).then(function(res) {
  97. that.goodsList = that.goodsList.concat(res.data.goodsList)
  98. that.page = res.data.currentPage + 1
  99. that.totalPages = res.data.totalPages
  100. });
  101. },
  102. switchCate: function(event) {
  103. if (this.id == event.currentTarget.dataset.id) {
  104. return false;
  105. }
  106. var that = this;
  107. var clientX = event.detail.x;
  108. var currentTarget = event.currentTarget;
  109. if (clientX < 60) {
  110. that.scrollLeft = currentTarget.offsetLeft - 60
  111. } else if (clientX > 330) {
  112. that.scrollLeft = currentTarget.offsetLeft
  113. }
  114. this.id = event.currentTarget.dataset.id
  115. this.page = 1
  116. this.totalPages = 1
  117. this.goodsList = []
  118. this.nomore = false
  119. this.getCategoryInfo();
  120. }
  121. },
  122. /**
  123. * 页面上拉触底事件的处理函数
  124. */
  125. onReachBottom: function() {
  126. this.getGoodsList()
  127. },
  128. onLoad: function(options) {
  129. // 页面初始化 options为页面跳转所带来的参数
  130. var that = this;
  131. if (options.id) {
  132. that.id = parseInt(options.id);
  133. }
  134. uni.getSystemInfo({
  135. success: function(res) {
  136. that.scrollHeight = res.windowHeight
  137. }
  138. });
  139. this.getCategoryInfo();
  140. }
  141. }
  142. </script>
  143. <style lang="scss">
  144. .container {
  145. background: #f9f9f9;
  146. }
  147. .cate-nav {
  148. position: fixed;
  149. left: 0;
  150. top: 0;
  151. z-index: 1000;
  152. }
  153. .cate-nav-body {
  154. height: 84rpx;
  155. white-space: nowrap;
  156. background: #fff;
  157. border-top: 1px solid rgba(0, 0, 0, .15);
  158. overflow: hidden;
  159. }
  160. .cate-nav .item {
  161. display: inline-block;
  162. height: 84rpx;
  163. min-width: 130rpx;
  164. padding: 0 15rpx;
  165. }
  166. .cate-nav .item .name {
  167. display: block;
  168. height: 84rpx;
  169. padding: 0 20rpx;
  170. line-height: 84rpx;
  171. color: #333;
  172. font-size: 30rpx;
  173. width: auto;
  174. }
  175. .cate-nav .item.active .name {
  176. color: #ab2b2b;
  177. border-bottom: 2px solid #ab2b2b;
  178. }
  179. .cate-item {
  180. margin-top: 94rpx;
  181. height: auto;
  182. overflow: hidden;
  183. }
  184. .cate-item .h {
  185. height: 145rpx;
  186. width: 750rpx;
  187. display: flex;
  188. flex-direction: column;
  189. align-items: center;
  190. justify-content: center;
  191. }
  192. .cate-item .h .name {
  193. display: block;
  194. height: 35rpx;
  195. margin-bottom: 18rpx;
  196. font-size: 30rpx;
  197. color: #333;
  198. }
  199. .cate-item .h .desc {
  200. display: block;
  201. height: 24rpx;
  202. font-size: 24rpx;
  203. color: #999;
  204. }
  205. .cate-item .b {
  206. width: 750rpx;
  207. padding: 0 6.25rpx;
  208. height: auto;
  209. overflow: hidden;
  210. }
  211. .cate-item .b .item {
  212. float: left;
  213. background: #fff;
  214. width: 365rpx;
  215. margin-bottom: 6.25rpx;
  216. padding-bottom: 33.333rpx;
  217. height: auto;
  218. overflow: hidden;
  219. text-align: center;
  220. }
  221. .cate-item .b .item-b {
  222. margin-left: 6.25rpx;
  223. }
  224. .cate-item .item .img {
  225. width: 302rpx;
  226. height: 302rpx;
  227. }
  228. .cate-item .item .name {
  229. display: block;
  230. width: 365.625rpx;
  231. height: 35rpx;
  232. margin: 11.5rpx 0 22rpx 0;
  233. text-align: center;
  234. overflow: hidden;
  235. padding: 0 20rpx;
  236. font-size: 30rpx;
  237. color: #333;
  238. }
  239. .cate-item .item .price {
  240. display: block;
  241. width: 365.625rpx;
  242. height: 30rpx;
  243. text-align: center;
  244. font-size: 30rpx;
  245. color: #b4282d;
  246. }
  247. .loadmore {
  248. height: 100rpx;
  249. width: 100%;
  250. line-height: 80rpx;
  251. text-align: center;
  252. margin-top: 0rpx;
  253. }
  254. .loadmore text {
  255. color: #999;
  256. }
  257. </style>