index.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <template>
  2. <view class="container">
  3. <view class="search-box">
  4. <uni-search-bar radius="5" placeholder="搜索" clearButton="auto" cancelButton="none" bgColor="#F8F8F8" @confirm="search"
  5. class="search-bar" />
  6. </view>
  7. <scroll-view show-scrollbar="true" scroll-y="true" class="sort-left-box">
  8. <view class="sort-left-row" :class="sortFirstCode == item.cateCode ? 'sort-left-row-active' : ''" v-for="(item,index) in sortFirstList"
  9. @click="checkFirstSort(item)" :key="index">
  10. <text>{{item.cateValue}}</text>
  11. </view>
  12. </scroll-view>
  13. <view class="sort-right-box">
  14. <scroll-view show-scrollbar="true" scroll-x="true" class="sort-right-row">
  15. <view class="sort-right-col" :class="sortSecondCode == item.cateCode ? 'sort-right-col-active' : ''" v-for="(item,index) in sortSecondList"
  16. :key="index" @click="checkSecondSort(item)">{{item.cateValue}}</view>
  17. </scroll-view>
  18. <k-scroll-view @onPullDown="handlePullDown" @onPullUp="handleLoadMore" class="goods-box">
  19. <view class="goods-row" v-for="(item, index) in goodsList" :key="index" @click="goToGoodDetails(item)">
  20. <cover-image class="goods-img" :src="item.imgPath"></cover-image>
  21. <view class="goods-info">
  22. <view class="goods-name">{{item.productName}}</view>
  23. <view class="goods-details">{{item.productDescribe}}</view>
  24. <view class="goods-number">
  25. <text class="goods-icon">¥</text>
  26. <text class="goods-spec">{{item.bizPrice}}/{{item.unit}}</text>
  27. <text class="goods-sales">{{item.sellCount}}人付款</text>
  28. </view>
  29. </view>
  30. <view class="goods-cart">
  31. <view class="cart-button" @click.stop="addCart(item)">
  32. <view class="iconfont icongouwuche"></view>
  33. </view>
  34. </view>
  35. </view>
  36. </k-scroll-view>
  37. </view>
  38. <u-modal v-model="modalShow" content="是否将该商品添加至购物车" @confirm="submitAddCart()" :async-close="true"
  39. :show-cancel-button="true"></u-modal>
  40. <u-top-tips ref="uTips"></u-top-tips>
  41. </view>
  42. </template>
  43. <script>
  44. const NET = require('../../utils/request')
  45. const API = require('../../config/api')
  46. import kScrollView from '@/components/k-scroll-view/k-scroll-view.vue'
  47. export default {
  48. components: {
  49. kScrollView
  50. },
  51. data() {
  52. return {
  53. pageIndex: 1,
  54. isOver: false,
  55. goodName: '',
  56. sortFirstCode: '',
  57. sortFirstList: [],
  58. sortSecondCode: '',
  59. sortSecondList: [],
  60. goodsList: [],
  61. modalShow: false,
  62. goodItem: null,
  63. }
  64. },
  65. onLoad() {
  66. NET.request(API.getSortList, {}, 'POST').then(res => {
  67. this.sortFirstList = res.data
  68. if (res.data.length) {
  69. this.checkFirstSort(res.data[0])
  70. } else {
  71. this.$refs.uTips.show({
  72. title: '获取商品分类失败',
  73. type: 'warning',
  74. })
  75. }
  76. }).catch(error => {
  77. this.$refs.uTips.show({
  78. title: '获取商品分类失败',
  79. type: 'warning',
  80. })
  81. })
  82. },
  83. methods: {
  84. // 切换一级分类
  85. checkFirstSort(item) {
  86. this.sortFirstCode = item.cateCode
  87. this.sortSecondList = []
  88. if (item.oneCategory.length) {
  89. this.sortSecondList = item.oneCategory
  90. this.checkSecondSort(item.oneCategory[0])
  91. }
  92. },
  93. // 切换二级分类
  94. checkSecondSort(item) {
  95. this.sortSecondCode = item.cateCode
  96. this.goodsList = []
  97. if (this.triggered = 'restore') {
  98. this.pageIndex = 1
  99. this.getGoodsList()
  100. }
  101. },
  102. // 搜索商品
  103. search(data) {
  104. this.goodName = data.value
  105. this.goodsList = []
  106. this.pageIndex = 1
  107. this.getGoodsList()
  108. },
  109. // 下拉刷新
  110. handlePullDown(stopLoad) {
  111. this.pageIndex = 1
  112. this.goodsList = []
  113. this.getGoodsList(stopLoad)
  114. stopLoad ? stopLoad() : '';
  115. },
  116. // 懒加载
  117. handleLoadMore(stopLoad) {
  118. if (!this.isOver) {
  119. this.pageIndex++
  120. this.getGoodsList()
  121. } else {
  122. stopLoad ? stopLoad({
  123. isEnd: true
  124. }) : '';
  125. }
  126. },
  127. // 获取商品
  128. getGoodsList(stopLoad) {
  129. NET.request(API.getGoodsBySort, {
  130. name: this.goodName,
  131. oneId: this.sortFirstCode,
  132. twoId: this.sortSecondCode,
  133. pageIndex: this.pageIndex,
  134. pageSize: 10,
  135. }, 'POST').then(res => {
  136. this.isOver = res.data.list.length != 10
  137. this.goodsList = this.goodsList.concat(res.data.list)
  138. }).catch(res => {
  139. this.$refs.uTips.show({
  140. title: '获取商品列表失败',
  141. type: 'warning',
  142. })
  143. })
  144. },
  145. // 添加购物车
  146. addCart(item) {
  147. this.goodItem = {
  148. productId: item.id,
  149. productName: item.productName,
  150. imgUrl: item.imgPath,
  151. bizPrice: item.bizPrice,
  152. originalPrice: item.originalPrice,
  153. productType: item.productType,
  154. tenantCode: item.tenantCode,
  155. buyNum: 1
  156. }
  157. this.modalShow = true
  158. },
  159. // 提交购物车
  160. submitAddCart() {
  161. NET.request(API.addCart, this.goodItem, 'POST').then(res => {
  162. this.$refs.uTips.show({
  163. title: '加入购物车成功',
  164. type: 'success',
  165. })
  166. this.modalShow = false
  167. }).catch(res => {
  168. this.modalShow = false
  169. this.$refs.uTips.show({
  170. title: '加入购物车失败',
  171. type: 'warning',
  172. })
  173. })
  174. },
  175. // 跳转商品详情
  176. goToGoodDetails(item) {
  177. uni.navigateTo({
  178. url: '/pagesGood/goodDetails?goodId=' + item.id
  179. });
  180. }
  181. }
  182. }
  183. </script>
  184. <style lang="less" scoped>
  185. page {
  186. width: 100%;
  187. height: 100%;
  188. }
  189. .container {
  190. width: 100%;
  191. height: 100%;
  192. position: absolute;
  193. .search-box {
  194. width: 100%;
  195. height: 58px;
  196. box-sizing: border-box;
  197. padding: 2px 12px 1px 12px;
  198. border-bottom: 1px solid #DDDDDD;
  199. /deep/.search-bar {
  200. .uni-searchbar__box {
  201. border-color: #F8F8F8;
  202. }
  203. }
  204. }
  205. .sort-left-box {
  206. width: 80px;
  207. height: calc(100% - 58px);
  208. border-right: 1px solid #DDDDDD;
  209. float: left;
  210. overflow-y: auto;
  211. .sort-left-row {
  212. width: 100%;
  213. height: 45px;
  214. box-sizing: border-box;
  215. padding-left: 9px;
  216. border-left: 3px solid #FFFFFF;
  217. font-size: 15px;
  218. font-family: PingFang SC;
  219. color: #333333;
  220. line-height: 16px;
  221. align-items: center;
  222. display: flex;
  223. }
  224. .sort-left-row-active {
  225. color: #51A539;
  226. border-left: 3px solid #51A539;
  227. }
  228. }
  229. .sort-right-box {
  230. width: calc(100% - 81px);
  231. height: calc(100% - 58px);
  232. float: left;
  233. .sort-right-row {
  234. width: 100%;
  235. height: 44px;
  236. padding: 9px 2px 0px 7px;
  237. box-sizing: border-box;
  238. white-space: nowrap;
  239. float: left;
  240. .sort-right-col {
  241. height: 26px;
  242. margin-right: 10px;
  243. display: inline-block;
  244. border: 1px solid #DCDCDC;
  245. border-radius: 13px;
  246. padding: 0 25px;
  247. box-sizing: border-box;
  248. font-size: 12px;
  249. font-family: PingFang SC;
  250. color: #666666;
  251. line-height: 24px;
  252. }
  253. .sort-right-col-active {
  254. color: #FFFFFF;
  255. background: #52A63A;
  256. border: 1px solid #51A539;
  257. }
  258. }
  259. .goods-box {
  260. width: 100%;
  261. height: calc(100% - 44px) !important;
  262. padding: 6px 0 0px 0;
  263. box-sizing: border-box;
  264. overflow-y: auto;
  265. float: left;
  266. .goods-row {
  267. width: calc(100% - 24px);
  268. height: 104px;
  269. padding: 11px 11px 11px 7px;
  270. box-sizing: border-box;
  271. margin: 4px 0 12px 12px;
  272. background: #FFFFFF;
  273. box-shadow: 0px 1px 5px 0px rgba(102, 102, 102, 0.43);
  274. border-radius: 15px;
  275. .goods-img {
  276. width: 84px;
  277. height: 84px;
  278. float: left;
  279. }
  280. .goods-info {
  281. width: calc(100% - 108px);
  282. height: 84px;
  283. box-sizing: border-box;
  284. padding-left: 6px;
  285. float: left;
  286. .goods-name {
  287. width: calc(100% + 24px);
  288. height: 18px;
  289. font-size: 15px;
  290. font-family: PingFang SC;
  291. color: #333333;
  292. line-height: 18px;
  293. white-space: nowrap;
  294. text-overflow: ellipsis;
  295. overflow: hidden;
  296. }
  297. .goods-details {
  298. height: 28px;
  299. font-size: 12px;
  300. font-family: PingFang SC;
  301. color: #999999;
  302. line-height: 14px;
  303. overflow: hidden;
  304. text-overflow: ellipsis;
  305. display: -webkit-box;
  306. -webkit-line-clamp: 2;
  307. -webkit-box-orient: vertical;
  308. word-wrap: break-word;
  309. margin: 4px 0 12px 0;
  310. }
  311. .goods-number {
  312. height: 16px;
  313. font-family: PingFang SC;
  314. line-height: 16px;
  315. .goods-icon {
  316. font-size: 12px;
  317. color: #52A63A;
  318. }
  319. .goods-spec {
  320. font-size: 15px;
  321. color: #52A63A;
  322. margin-right: 15px;
  323. }
  324. .goods-sales {
  325. font-size: 12px;
  326. color: #999999;
  327. }
  328. }
  329. }
  330. .goods-cart {
  331. width: 24px;
  332. height: 84px;
  333. float: left;
  334. .cart-button {
  335. width: 24px;
  336. height: 24px;
  337. margin-top: 30px;
  338. background-color: #52A63A;
  339. border-radius: 50%;
  340. text-align: center;
  341. .iconfont {
  342. color: #FFFFFF;
  343. font-size: 18px;
  344. line-height: 24px;
  345. }
  346. }
  347. }
  348. }
  349. }
  350. }
  351. }
  352. </style>