index.vue 8.8 KB

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