category.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <template>
  2. <view class="wh-full">
  3. <view class="top-bg">
  4. <!-- <view class="top-kuang">
  5. <view class="top-title">分类</view>
  6. </view>
  7. <view style="width: 100%;height: 26rpx;"></view> -->
  8. <!-- 头部组件 -->
  9. <use-header :fixed="headerFixed" :placeholder="headerPlaceholder" :search-tip="searchTip" :search-auto="searchAuto"></use-header>
  10. </view>
  11. <!-- 分类 -->
  12. <view class="category dflex-s h-full padding-top-big" v-for="item in fdatas" :key="item.id">
  13. <!-- 左侧一级分类 -->
  14. <view class="h-full left">
  15. <scroll-view scroll-y class="h-full">
  16. <view class="item dflex-c" v-for="item in fdatas" :key="item.id"
  17. :class="{ active: item.id === cid }" @click="fSelect(item)">{{ item.name }}</view>
  18. </scroll-view>
  19. </view>
  20. <!-- 右侧 1二级分类 2商品列表 -->
  21. <scroll-view class="h-full right bg-main" scroll-with-animation scroll-y :scroll-top="top"
  22. :style="{ height: scrollHeight }" @scroll="onScroll">
  23. <!-- 右侧二级分类 -->
  24. <view class="dflex-s dflex-wrap-w" v-if="mode == 1">
  25. <view class="item padding-bottom-sm dflex dflex-flow-c" v-if="list.pid == cid"
  26. v-for="(list, listindex) in sdatas" :key="listindex" @click="togoodslist(list)">
  27. <image :lazy-load="true" :src="list.icon"></image>
  28. <text class="tac clamp margin-top-sm">{{ list.name }}</text>
  29. </view>
  30. </view>
  31. <!-- 右侧分类对应商品列表 -->
  32. <view v-if="mode == 2">
  33. <!-- 空白页 -->
  34. <use-empty v-if="empty" e-style="round" tip="无商品数据"></use-empty>
  35. <view v-else class="padding-lr" v-for="(list, listindex) in goodsDatas" :key="listindex"
  36. @click="togoods(list)">
  37. <view class="goods border-radius-sm padding margin-bottom-sm bg-main"
  38. style="padding-bottom: 15rpx;">
  39. <view class="goods-left">
  40. <image mode="aspectFill" :lazy-load="true" :src="list.imgs"></image>
  41. </view>
  42. <view class="margin-left-sm pos-r">
  43. <text class="clamp-2">{{ list.name }} {{ list.name_pw }}</text>
  44. <view class="pos-a price-box w-full">
  45. <text class="price">{{ list.price }}</text>
  46. <text class="m-price">{{ list.marketPrice }}</text>
  47. </view>
  48. </view>
  49. </view>
  50. </view>
  51. <!-- 上拉加载更多 -->
  52. <use-loadmore v-if="!empty && hasmore" :type="loadmoreType"></use-loadmore>
  53. <!-- 置顶 -->
  54. <use-totop ref="usetop" bottom="150" :style="{ marginBottom: navHeight + 'px' }" @to="totop"></use-totop>
  55. </view>
  56. </scroll-view>
  57. </view>
  58. <!-- 切换模式 1二级分类 2商品列表 -->
  59. <!-- <view class="fixed-top" :style="{ marginBottom: navHeight + 'px' }" @click="changeMode">
  60. <text class="iconfont iconpailie" v-if="mode == 1"></text>
  61. <text class="iconfont iconpailie02" v-if="mode == 2"></text>
  62. </view> -->
  63. <!-- <tabbar :current-page="1"></tabbar> -->
  64. </view>
  65. </template>
  66. <script>
  67. import {
  68. goodsCate
  69. } from '../../utils/api_category.js'
  70. import {
  71. goodslistlimit
  72. } from '../../utils/api_home.js'
  73. import useHeader from '../../components/use-header/use-header.vue'
  74. import useEmpty from '../../components/use-empty/use-empty.vue'
  75. import useLoadmore from '../../components/use-loadmore/use-loadmore.vue'
  76. import useTotop from '../../components/use-totop/use-totop.vue'
  77. // import tabbar from '../tabbar.vue'
  78. const _goods = 'usemall-goods'
  79. const _goodscategory = 'usemall-goods-category'
  80. export default {
  81. components:{
  82. useHeader,
  83. useEmpty,
  84. useLoadmore,
  85. useTotop,
  86. // tabbar
  87. },
  88. data() {
  89. return {
  90. // 1分类列表 2商品列表
  91. mode: 1,
  92. // 兼容支付宝 height 显示 bug
  93. scrollHeight: '100%',
  94. // 头部参数
  95. headerPlaceholder: 0,
  96. headerFixed: !0,
  97. searchAuto: !0,
  98. searchTip: '请输入搜索关键字',
  99. // 当前选中分类ID
  100. cid: 0,
  101. // 一级数据
  102. fdatas: [
  103. ],
  104. // 二级数据
  105. sdatas: [],
  106. // 商品列表
  107. goodsDatas: [],
  108. empty: false,
  109. hasmore: 0,
  110. loadmoreType: 'nomore',
  111. // 商品请求数据
  112. reqdata: {
  113. rows: 20,
  114. page: 1
  115. },
  116. top: 0,
  117. scrollTop: 0,
  118. navHeight: 0
  119. };
  120. },
  121. watch: {
  122. goodsDatas(e) {
  123. // 监听数据,呈现空白页
  124. let empty = e.length === 0;
  125. if (this.empty !== empty) {
  126. this.empty = empty;
  127. }
  128. }
  129. },
  130. onPageScroll(e) {
  131. //this.scrollTop = e.scrollTop;
  132. // this.$refs.usetop.change(e.scrollTop);
  133. },
  134. onLoad(option) {
  135. // #ifdef MP-ALIPAY
  136. // this.scrollHeight = (this.$env.windowHeight - this.$env.sis.titleBarHeight) + 'px';
  137. // #endif
  138. // 获取存储的模式
  139. // this.mode = uni.getStorageSync('category.mode') || 1;
  140. this.cid = option.cid;
  141. this.loadData(() => {
  142. if (this.mode == 2) {
  143. // 加载商品数据
  144. this.loadGoodsDatas()
  145. }
  146. });
  147. },
  148. // 下拉刷新
  149. onPullDownRefresh() {
  150. this.loadData(() => {
  151. uni.stopPullDownRefresh();
  152. });
  153. },
  154. methods: {
  155. loadData(callback) {
  156. //商品分类树形列表
  157. let _self = this;
  158. _self.fdatas = []
  159. goodsCate().then((res) => {
  160. if (res.success) {
  161. res.data.forEach(data => {
  162. _self.fdatas.push(data)
  163. })
  164. if (res.data.length > 0) {
  165. // _self.cid = _self.fdatas[0].id;
  166. // _self.sdatas=_self.fdatas[0].childCategory
  167. for(var i=0;i<_self.fdatas.length;i++){
  168. if(_self.cid==_self.fdatas[i].id){
  169. _self.sdatas=_self.fdatas[i].childCategory
  170. }
  171. }
  172. }
  173. if (typeof callback === 'function') {
  174. // 数据加载完成回调函数
  175. callback();
  176. }
  177. }else{
  178. _self.$message.warning('没有符合条件的数据!')
  179. }
  180. })
  181. },
  182. // 加载商品数据
  183. loadGoodsDatas() {
  184. let _self = this;
  185. if (_self.mode != 2) {
  186. return;
  187. }
  188. // 根据当前 cid 加载商品数据列表
  189. _self.reqdata.cid = _self.cid;
  190. _self.goodsDatas=[]
  191. var data='?categoryId='+_self.reqdata.cid+'&categoryLevel=1'
  192. goodslistlimit(data).then((res) => {
  193. if (res.success) {
  194. var total=res.data.totalCount
  195. data='categoryId='+_self.reqdata.cid+'&categoryLevel=1&pageSize='+total
  196. goodslistlimit(data).then((res) => {
  197. if (res.success) {
  198. res.data.list.forEach(data => {
  199. _self.goodsDatas.push(data)
  200. })
  201. }else{
  202. _self.$message.warning('没有符合条件的数据!')
  203. }
  204. })
  205. }
  206. })
  207. },
  208. totop(e) {
  209. this.top = e.scrollTop
  210. this.$nextTick(function() {
  211. this.top = 0
  212. });
  213. },
  214. // 一级分类
  215. fSelect(item) {
  216. for(var i=0;i<this.fdatas.length;i++){
  217. if(item.id==this.fdatas[i].id){
  218. this.sdatas=this.fdatas[i].childCategory
  219. }
  220. }
  221. this.cid = item.id;
  222. this.loadGoodsDatas();
  223. },
  224. // 切换模式 1分类模式 2商品模式
  225. changeMode() {
  226. this.mode = this.mode == 1 ? 2 : 1;
  227. this.loadGoodsDatas();
  228. },
  229. // 跳转商品详情
  230. togoods(item) {
  231. this.$api.togoods({
  232. id: item.id
  233. });
  234. },
  235. // 跳转商品列表
  236. togoodslist(item) {
  237. this.$api.togoodslist({
  238. cid: item.id,
  239. level:item.level,
  240. keyword:''//item.name
  241. });
  242. },
  243. },
  244. mounted() {
  245. // #ifdef H5 || MP-360
  246. this.navHeight = 50;
  247. // #endif
  248. }
  249. };
  250. </script>
  251. <style lang="scss">
  252. @import url('/packageShang/components/iconfont/iconfont.css');
  253. @import url('/packageShang/common/common.scss');
  254. page {
  255. height: 100%;
  256. background-color: $page-color-base;
  257. }
  258. // 顶部背景
  259. .top-bg{
  260. width: 100%;
  261. height: 100rpx;
  262. background: rgba(255, 255, 255, 1);
  263. .top-kuang{
  264. width: 100%;
  265. height: 176rpx;
  266. background: rgba(255, 255, 255, 1);
  267. box-shadow: 0px 2rpx 0px rgba(236, 236, 236, 1);
  268. .top-title{
  269. padding: 106rpx 0 0 0rpx;
  270. text-align: center;
  271. font-size: 36rpx;
  272. font-weight: 500;
  273. line-height: 52rpx;
  274. color: rgba(0, 0, 0, 1);
  275. }
  276. }
  277. }
  278. .category {
  279. overflow: hidden;
  280. .left {
  281. width: 200rpx;
  282. background-color: $page-color-base;
  283. .item {
  284. height: 100rpx;
  285. color: $font-color-base;
  286. position: relative;
  287. &.active {
  288. color: $uni-color-primary;
  289. background: #fff;
  290. &:before {
  291. content: '';
  292. position: absolute;
  293. left: 0;
  294. top: 50%;
  295. transform: translateY(-50%);
  296. height: 36rpx;
  297. width: 8rpx;
  298. background-color: $uni-color-primary;
  299. opacity: 0.8;
  300. }
  301. }
  302. }
  303. }
  304. .right {
  305. flex: 1;
  306. overflow: hidden;
  307. display: block;
  308. .item {
  309. flex-shrink: 0;
  310. width: 33.33%;
  311. font-size: $font-sm + 2upx;
  312. color: #666;
  313. image {
  314. width: 130rpx;
  315. height: 130rpx;
  316. }
  317. }
  318. }
  319. }
  320. .goods {
  321. display: flex;
  322. .goods-left {
  323. image {
  324. width: 120rpx;
  325. height: 120rpx;
  326. }
  327. }
  328. .price-box {
  329. bottom: 0;
  330. }
  331. }
  332. </style>