index.vue 9.3 KB

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