index.vue 8.7 KB

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