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. <scroll-view class="goods-box" scroll-y="true" @scrolltolower="handleLoadMore()">
  19. <view class="goods-row" v-for="(item, index) in goodsList" :key="index" @click="goToGoodDetails(item)">
  20. <image class="goods-img" :src="item.imgPath"></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. </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.getBaseData()
  67. },
  68. onPullDownRefresh() {
  69. this.getBaseData()
  70. uni.stopPullDownRefresh();
  71. },
  72. methods: {
  73. // 获取全部数据
  74. getBaseData() {
  75. this.pageIndex = 1
  76. this.sortFirstList = []
  77. this.sortSecondList = []
  78. this.goodsList = []
  79. NET.request(API.getSortList, {}, 'POST').then(res => {
  80. this.sortFirstList = res.data
  81. if (res.data.length) {
  82. this.checkFirstSort(res.data[0])
  83. } else {
  84. this.$refs.uTips.show({
  85. title: '获取商品分类失败',
  86. type: 'warning',
  87. })
  88. }
  89. }).catch(error => {
  90. this.$refs.uTips.show({
  91. title: '获取商品分类失败',
  92. type: 'warning',
  93. })
  94. })
  95. },
  96. // 切换一级分类
  97. checkFirstSort(item) {
  98. this.sortFirstCode = item.cateCode
  99. this.sortSecondList = []
  100. if (item.oneCategory.length) {
  101. this.sortSecondList = item.oneCategory
  102. this.checkSecondSort(item.oneCategory[0])
  103. }
  104. },
  105. // 切换二级分类
  106. checkSecondSort(item) {
  107. this.sortSecondCode = item.cateCode
  108. this.goodsList = []
  109. if (this.triggered = 'restore') {
  110. this.pageIndex = 1
  111. this.getGoodsList()
  112. }
  113. },
  114. // 搜索商品
  115. search(data) {
  116. this.goodName = data.value
  117. this.goodsList = []
  118. this.pageIndex = 1
  119. this.getGoodsList()
  120. },
  121. // 懒加载
  122. handleLoadMore(stopLoad) {
  123. if (!this.isOver) {
  124. this.pageIndex++
  125. this.getGoodsList()
  126. }
  127. },
  128. // 获取商品
  129. getGoodsList() {
  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. 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>