category2.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <template>
  2. <view class="content">
  3. <!-- 兼容小程序搜索 -->
  4. <!-- #ifdef MP -->
  5. <navigator url="/pages/product/search">
  6. <view class="input-box flex">
  7. <view class=" input-content flex">
  8. <view class="iconfont iconsearch"></view>
  9. <view class="input"><input disabled placeholder="请输入搜索内容" /></view>
  10. </view>
  11. </view>
  12. </navigator>
  13. <!-- #endif -->
  14. <!-- #ifdef H5 -->
  15. <!-- <view class="cate-bg"></view> -->
  16. <!-- #endif -->
  17. <div class="classBox">
  18. <scroll-view scroll-y class="left-aside">
  19. <view class="f-item b-b" :class="{ active: item.id === currentId }" v-for="(item, index) in flist" :key="item.id" @click="tabtap(item, index)">
  20. {{ item.cate_name }}
  21. </view>
  22. </scroll-view>
  23. <!-- <scroll-view scroll-y class="left-aside">
  24. <view class="" v-for="item in flist" :key="item.id">
  25. <view v-for="titem in item.children" :key="titem.id" class="f-item b-b" :class="{ active: titem.id === currentId }" @click="tabtap(titem)">
  26. {{ titem.cate_name }}
  27. </view>
  28. </view>
  29. </scroll-view> -->
  30. <scroll-view scroll-with-animation scroll-y class="right-aside" :scroll-top="tabScrollTop" @scrolltolower="getListMore">
  31. <!-- <view v-for="item in flist" :key="item.id" class="s-list" :id="'main-' + item.id">
  32. <text class="s-item">{{ item.cate_name }}</text> -->
  33. <view class="t-list " v-if="flist.length > 0">
  34. <view @click="navToDetailPage(titem)" class="t-item" v-for="titem in flist[actionClassInd].list" :key="titem.id" id="'main-' + item.id">
  35. <image :src="titem.image" mode="scaleToFill" :lazy-load="true"></image>
  36. <view class="font">
  37. <view class="title">{{ titem.store_name }}</view>
  38. <view class="font-bottom">
  39. <view class="price">¥{{ titem.price }}</view>
  40. <view class="shop"><view class="shop-text">马上购</view></view>
  41. </view>
  42. </view>
  43. </view>
  44. <view class="text-center"><uni-load-more :status="flist[actionClassInd].loadingType"></uni-load-more></view>
  45. </view>
  46. <!-- </view> -->
  47. </scroll-view>
  48. </div>
  49. <u-tabbar v-model="current" :list="tabbar" active-color="#FF0000" inactive-color="#666666"></u-tabbar>
  50. </view>
  51. </template>
  52. <script>
  53. import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
  54. import { getList, newgetList } from '@/api/category.js';
  55. import { tabbar } from '@/utils/tabbar.js';
  56. import { getProducts } from '@/api/product.js';
  57. export default {
  58. components: {
  59. uniLoadMore
  60. },
  61. data() {
  62. return {
  63. tabbar: tabbar,
  64. current: 0,
  65. sizeCalcState: false,
  66. tabScrollTop: 0,
  67. currentId: 5,
  68. flist: [],
  69. commodity: [], //商品列表
  70. current: 0,
  71. tabbar: tabbar,
  72. page: 1, //当前页数
  73. limit: 6, //每次加载数据条数
  74. loadingType: 'more', //加载更多状态
  75. actionClassInd: 0
  76. };
  77. },
  78. onLoad() {
  79. this.loadData();
  80. },
  81. //下拉刷新
  82. onPullDownRefresh() {
  83. this.loadData('refresh');
  84. },
  85. // 监听导航栏输入框点击事件
  86. onNavigationBarSearchInputClicked(e) {
  87. uni.navigateTo({
  88. url: '/pages/product/search'
  89. });
  90. },
  91. //监听页面是否滚动到底部加载更多
  92. onReachBottom() {
  93. this.loadData();
  94. },
  95. methods: {
  96. // 载入数据
  97. async loadData(type = 'add', loading) {
  98. let obj = this;
  99. let data = {
  100. page: obj.page,
  101. limit: obj.limit
  102. };
  103. //没有更多直接返回
  104. if (type === 'add') {
  105. if (obj.loadingType === 'nomore') {
  106. return;
  107. }
  108. obj.loadingType = 'loading';
  109. } else {
  110. obj.loadingType = 'more';
  111. }
  112. if (type === 'refresh') {
  113. // 清空数组
  114. obj.page = 1;
  115. }
  116. newgetList({})
  117. .then(({ data }) => {
  118. console.log('------',data);
  119. obj.flist = data.list.map(function(s) {
  120. s.list = []; //用于保存当前分类下商品对象的数据
  121. s.loadingType = 'more'; //判断当前商品分类数据是否已经加载完毕
  122. s.page = 1; //当前商品分类加载的商品页数
  123. s.limit = 10; //每次家在数据加载的数据条数
  124. return s;
  125. });
  126. console.log(obj.flist, '数据');
  127. obj.currentId = obj.flist[0].id;
  128. // 开始处理数据
  129. // for (var i = 0; i < obj.flist.length; i++) {
  130. // }
  131. obj.getProductsList(obj.flist[0]);
  132. })
  133. .catch(err => {
  134. console.log(err);
  135. });
  136. },
  137. //一级分类点击
  138. tabtap(item, index) {
  139. // 判断有没有初始化页面高度对象数据
  140. // if (!this.sizeCalcState) {
  141. // this.calcSize();
  142. // }
  143. // 获取当前点击的id
  144. this.currentId = item.id;
  145. //获取当前点击的下标
  146. this.actionClassInd = index;
  147. this.tabScrollTop = item.top;
  148. // 获取当前选中的对象
  149. let ls = this.flist[this.actionClassInd];
  150. // 判断当前数据是否已经加载完毕
  151. if (ls.loadingType == 'more') {
  152. this.getProductsList(ls);
  153. }
  154. },
  155. // 加载分类下商品数据
  156. getProductsList(item, type = 'add') {
  157. console.log(item);
  158. const obj = this;
  159. const requestData = {
  160. page: item.page,
  161. limit: item.limit,
  162. sid: item.id
  163. };
  164. // 判断数据是否加载中
  165. if (item.loadingType == 'nomore' || item.loadingType == 'loading') {
  166. return;
  167. }
  168. // 判断是否重新加载数据
  169. if(type=='refresh'){
  170. item.list=[];
  171. }
  172. // 设置数据加载中
  173. item.loadingType = 'loading';
  174. return new Promise((ok, erro) => {
  175. getProducts(requestData)
  176. .then(({ data }) => {
  177. // item.list = data.map((s,ind) => {
  178. // return s;
  179. // });
  180. data.forEach((sj,index) => {
  181. item.list.push(sj);
  182. })
  183. if (item.limit == data.length) {
  184. item.page++;
  185. item.loadingType = 'more';
  186. } else {
  187. item.loadingType = 'nomore';
  188. }
  189. if (type === 'refresh') {
  190. if (loading == 1) {
  191. uni.hideLoading();
  192. } else {
  193. uni.stopPullDownRefresh();
  194. }
  195. }
  196. })
  197. .catch(err => {
  198. console.log(err);
  199. });
  200. });
  201. },
  202. //下拉到底部后加载商品列表数据
  203. getListMore() {
  204. this.getProductsList(this.flist[this.actionClassInd]);
  205. },
  206. navToDetailPage(item) {
  207. // 点击导航跳转到详细页面
  208. uni.navigateTo({
  209. url: '/pages/product/product?id=' + item.id
  210. });
  211. }
  212. }
  213. };
  214. </script>
  215. <style lang="scss">
  216. .text-center {
  217. width: 100%;
  218. text-align: center;
  219. }
  220. /* #ifdef MP || APP-PLUS*/
  221. .input-box {
  222. padding: 25rpx;
  223. /* #ifdef APP-PLUS */
  224. margin-top: var(--status-bar-height);
  225. /* #endif */
  226. background-color: #fe8119;
  227. height: 44px;
  228. .iconsearch {
  229. font-size: 50rpx;
  230. }
  231. .input-content {
  232. border-radius: 99rpx;
  233. flex-grow: 1;
  234. padding: 5rpx 30rpx;
  235. // background-color: rgba(231, 231, 231, 0.7);
  236. background: #ffffff;
  237. .input {
  238. flex-grow: 1;
  239. input {
  240. font-size: $font-base;
  241. }
  242. }
  243. }
  244. .input-button {
  245. padding-left: 20rpx;
  246. font-size: $font-base;
  247. height: 100%;
  248. }
  249. }
  250. /* #endif */
  251. page,
  252. .content {
  253. height: 100%;
  254. background-color: #f8f8f8;
  255. }
  256. .content {
  257. .cate-bg {
  258. position: fixed;
  259. top: 0;
  260. width: 750rpx;
  261. height: 44px;
  262. z-index: 10;
  263. // background: linear-gradient(90deg, #FF8E00, #FE762F);
  264. background: #12ada9;
  265. }
  266. .classBox {
  267. // padding-top: 44px;
  268. display: flex;
  269. height: calc(100% - 44px);
  270. /* #ifdef APP-PLUS || MP */
  271. height: calc(100% - 88px);
  272. /* #endif */
  273. }
  274. }
  275. .left-aside {
  276. flex-shrink: 0;
  277. width: 180rpx;
  278. height: 100%;
  279. background-color: #fff;
  280. }
  281. .f-item {
  282. display: flex;
  283. align-items: center;
  284. justify-content: center;
  285. width: 100%;
  286. height: 100rpx;
  287. font-size: 28rpx;
  288. color: $font-color-base;
  289. position: relative;
  290. &.active {
  291. color: $base-color;
  292. background: #f8f8f8;
  293. &:before {
  294. content: '';
  295. position: absolute;
  296. left: 0;
  297. top: 50%;
  298. transform: translateY(-50%);
  299. height: 36rpx;
  300. width: 8rpx;
  301. background-color: $base-color;
  302. border-radius: 0 4px 4px 0;
  303. opacity: 0.8;
  304. }
  305. }
  306. }
  307. .right-aside {
  308. flex: 1;
  309. overflow: hidden;
  310. padding-left: 20rpx;
  311. padding-right: 20rpx;
  312. }
  313. .s-item {
  314. display: flex;
  315. align-items: center;
  316. font-weight: bold;
  317. height: 70rpx;
  318. padding-top: 8rpx;
  319. font-size: 35rpx;
  320. color: $font-color-dark;
  321. }
  322. .t-list {
  323. display: flex;
  324. flex-wrap: wrap;
  325. border-radius: 15rpx;
  326. width: 100%;
  327. background: #fff;
  328. padding-top: 12rpx;
  329. &:after {
  330. content: '';
  331. flex: 99;
  332. height: 0;
  333. }
  334. }
  335. .t-item {
  336. height: 220rpx;
  337. flex-shrink: 0;
  338. display: flex;
  339. justify-content: center;
  340. align-items: center;
  341. flex-direction: row;
  342. width: 500rpx;
  343. font-size: 26rpx;
  344. color: #666;
  345. padding-bottom: 20rpx;
  346. margin-right: 40rpx;
  347. &.active {
  348. display: none;
  349. }
  350. image {
  351. width: 200rpx;
  352. height: 200rpx;
  353. margin-top: 30rpx;
  354. margin-left: 20rpx;
  355. }
  356. .font {
  357. margin-left: 25rpx;
  358. margin-top: 30rpx;
  359. width: 500rpx;
  360. .title {
  361. font-weight: bold;
  362. font-size: $font-base;
  363. color: $font-color-dark;
  364. word-break: break-all;
  365. display: -webkit-box;
  366. -webkit-line-clamp: 2;
  367. -webkit-box-orient: vertical;
  368. overflow: hidden;
  369. }
  370. .font-bottom {
  371. height: 60rpx;
  372. width: 100%;
  373. display: flex;
  374. margin-top: 76rpx;
  375. justify-content: space-between;
  376. .shop {
  377. width: 137rpx;
  378. height: 52rpx;
  379. background: linear-gradient(270deg, #ff8149, #ff4e00);
  380. border-radius: 26rpx;
  381. .shop-text {
  382. font-size: 26rpx;
  383. font-family: PingFang SC;
  384. font-weight: 500;
  385. color: #ffffff;
  386. margin-left: 30rpx;
  387. margin-top: 10rpx;
  388. }
  389. }
  390. .price {
  391. margin-top: 14rpx;
  392. width: 88rpx;
  393. height: 26rpx;
  394. font-size: 34rpx;
  395. font-family: PingFang SC;
  396. font-weight: bold;
  397. color: #fb3a2f;
  398. line-height: 34rpx;
  399. }
  400. }
  401. }
  402. }
  403. </style>