shopping-cart.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. <template>
  2. <view class="shopping-cart">
  3. <view class="header">
  4. <view class="title">
  5. 我的购物车
  6. </view>
  7. <view style="position: relative;">
  8. <view class="sub-title">购物车订单</view>
  9. </view>
  10. </view>
  11. <view v-if="cart.length===0" style="margin: 20upx auto;text-align: center; font-size: 28upx;color: #999999;">
  12. 暂未添加任何商品
  13. </view>
  14. <view class="shopping-cart-item" v-for="seller in cart">
  15. <view class="shopping-cart-item-head">
  16. <view class="select">
  17. <checkbox class="checkbox" :checked="seller.checked" @tap="selectSeller(seller.seller_id)" />
  18. </view>
  19. <view class="factory-name">{{seller.seller_nickname?seller.seller_nickname:'省心直供(该厂家暂未设置昵称)'}}</view>
  20. </view>
  21. <view class="shopping-cart-info">
  22. <view class="product-item" v-for="product in seller.products"
  23. @longpress="longpress(product.id,product.spec_index)">
  24. <view class="select">
  25. <checkbox class="checkbox" :checked="product.checked"
  26. @tap="select(product.id,product.spec_index)" />
  27. </view>
  28. <view class="product-image">
  29. <image class="image" :src="product.spec_image|imagesFilter" mode="scaleToFill"></image>
  30. </view>
  31. <view class="product-info">
  32. <view class="row row-1">
  33. <text class="title"><text class="sxzg-icon">省心直供</text>{{product.name}}</text>
  34. </view>
  35. <view class="row">
  36. <view class="spec">{{product.spec_name}}</view>
  37. </view>
  38. <view class="row row-2">
  39. <text class="org-price">¥{{product.spec_org_price}}</text>
  40. </view>
  41. <view class="row">
  42. <text class="sxj-icon">省心价</text>
  43. <text class="price">¥{{product.spec_price}}</text>
  44. </view>
  45. </view>
  46. <view class="counter">
  47. <view class="sub" @tap="subnum(product.id,product.num,product.spec_index)">
  48. <image style="width: 42upx;height: 42upx;" src="../../static/minus.png" mode="scaleToFill"></image>
  49. </view>
  50. <input class="num" type="text" v-model="product.num" />
  51. <view class="plus" @tap="plusnum(product.id,product.num,product.spec_index)">
  52. <image style="width: 42upx;height: 42upx;" src="../../static/add.png" mode="scaleToFill"></image>
  53. </view>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. <view class="footer">
  59. <view class="select-all">
  60. <label @tap="selectAll()">
  61. <checkbox color="#ff5d5b" class="checkbox" :checked="checkall" />全选
  62. </label>
  63. </view>
  64. <view class="total">合计 <text>¥{{total}}</text></view>
  65. <view class="settlement"><button class="settlement-btn" type="default" @tap="buy">结算</button></view>
  66. </view>
  67. </view>
  68. </template>
  69. <script>
  70. import {
  71. mapState
  72. } from 'vuex'
  73. export default {
  74. data() {
  75. return {
  76. checkall: false
  77. }
  78. },
  79. computed: mapState({
  80. cart: state => state.cart,
  81. total() {
  82. return this.$store.getters['cart/total']
  83. }
  84. }),
  85. onLoad() {
  86. // uni.removeStorageSync("cart")
  87. },
  88. methods: {
  89. selectSeller(seller_id) {
  90. this.$store.commit("cart/selectSeller", {
  91. seller_id: seller_id
  92. })
  93. },
  94. select(id, spec_index) {
  95. this.$store.commit("cart/select", {
  96. id: id,
  97. spec_index: spec_index
  98. })
  99. },
  100. selectAll() {
  101. this.checkall = !this.checkall;
  102. this.$store.commit("cart/selectAll", {
  103. checked: this.checkall
  104. })
  105. },
  106. subnum(id, num, spec_index) {
  107. if (num === 1) {
  108. uni.showModal({
  109. title: "移除商品",
  110. content: "确认移除该商品?",
  111. success: (res) => {
  112. if (res.confirm) {
  113. this.$store.commit("cart/remove", {
  114. id: id,
  115. num: num,
  116. spec_index: spec_index
  117. })
  118. }
  119. }
  120. })
  121. } else {
  122. num--
  123. }
  124. this.$store.commit("cart/numchange", {
  125. id: id,
  126. num: num,
  127. spec_index: spec_index
  128. })
  129. },
  130. plusnum(id, num, spec_index) {
  131. num++
  132. this.$store.commit("cart/numchange", {
  133. id: id,
  134. num: num,
  135. spec_index: spec_index
  136. })
  137. },
  138. longpress(id, spec_index) {
  139. uni.showActionSheet({
  140. itemList: ['删除'],
  141. itemColor: "#FF5D5B",
  142. success: (res)=> {
  143. if (res.tapIndex === 0) {
  144. this.$store.commit("cart/remove", {
  145. id: id,
  146. spec_index: spec_index
  147. })
  148. uni.showToast({
  149. icon: "none",
  150. title: "删除成功"
  151. })
  152. }
  153. },
  154. })
  155. },
  156. buy() {
  157. let i = 0;
  158. let postData = {}
  159. for (let seller of this.cart) {
  160. for (let product of seller.products) {
  161. if (product.checked) {
  162. postData[`products[${i}]`] = JSON.stringify({
  163. id: product.id,
  164. spec_index: product.spec_index,
  165. num: product.num
  166. })
  167. i++;
  168. }
  169. }
  170. }
  171. this.$http.post({
  172. url: '/order/create',
  173. data: postData,
  174. success: (res) => {
  175. for (const [key, value] of Object.entries(postData)) {
  176. console.log(value)
  177. this.$store.commit("cart/remove", JSON.parse(value))
  178. }
  179. uni.navigateTo({
  180. url: "/pages/order/cashier?order_no=" + res.data.data
  181. })
  182. }
  183. })
  184. }
  185. }
  186. }
  187. </script>
  188. <style lang="scss">
  189. .header {
  190. margin: 20upx;
  191. background: white;
  192. text-align: center;
  193. border-radius: 20upx;
  194. padding-bottom: 10upx;
  195. .title {
  196. height: 100upx;
  197. font-size: 32upx;
  198. line-height: 100upx;
  199. // font-weight: bold;
  200. }
  201. .sub-title {
  202. font-size: 28upx;
  203. color: #999999;
  204. }
  205. }
  206. .shopping-cart {
  207. padding-bottom: 80upx;
  208. overflow: hidden;
  209. position: relative;
  210. }
  211. .shopping-cart-item {
  212. padding: 20upx;
  213. background: white;
  214. // padding-left: 100upx;
  215. margin: 20upx;
  216. border-radius: 20upx;
  217. .shopping-cart-item-head {
  218. margin-bottom: 20upx;
  219. font-size: 28upx;
  220. font-weight: bold;
  221. position: relative;
  222. display: flex;
  223. }
  224. .select {
  225. align-self: center;
  226. position: relative;
  227. margin-top: -4upx;
  228. margin-right: 20upx;
  229. }
  230. .checkbox {
  231. transform: scale(0.7);
  232. /deep/ .uni-checkbox-input {
  233. border: 2upx solid #CCCCCC;
  234. &.uni-checkbox-input-checked {
  235. color: $primary-color !important;
  236. }
  237. }
  238. /deep/ .uni-checkbox-input:hover {
  239. border: 2upx solid #CCCCCC;
  240. }
  241. }
  242. .product-item {
  243. display: flex;
  244. position: relative;
  245. margin-bottom: 20upx;
  246. .checkbox::before {
  247. top: 70upx;
  248. }
  249. .product-image {
  250. margin-right: 20upx;
  251. width: 180upx;
  252. height: 180upx;
  253. .image {
  254. width: 180upx;
  255. height: 180upx;
  256. margin-top: 5upx;
  257. }
  258. }
  259. .title {
  260. font-size: 24rpx;
  261. display: inline-block;
  262. white-space: normal;
  263. display: -webkit-box;
  264. -webkit-box-orient: vertical;
  265. -webkit-line-clamp: 2;
  266. overflow: hidden;
  267. height: 68upx;
  268. }
  269. .spec {
  270. font-size: 22upx;
  271. background: #CCCCCC;
  272. color: white;
  273. padding: 0 5upx;
  274. border-radius: 10upx;
  275. margin-top: 10upx;
  276. // display: inline-block;
  277. }
  278. .counter {
  279. position: absolute;
  280. bottom: 0upx;
  281. right: 10upx;
  282. display: flex;
  283. border: 2upx solid #CCCCCC;
  284. font-size: 28upx;
  285. align-items: center;
  286. border-radius: 10upx;
  287. .sub,
  288. .plus {
  289. // transform: scale(0.5);
  290. font-weight: 0;
  291. width: 42upx;
  292. text-align: center;
  293. display: flex;
  294. align-items: center;
  295. &:active {
  296. background: rgba(0, 0, 0, .1);
  297. }
  298. }
  299. .num {
  300. text-align: center;
  301. font-size: 24upx;
  302. border-left: 2upx solid #EEEEEE;
  303. border-right: 2upx solid #EEEEEE;
  304. width: 50upx;
  305. }
  306. }
  307. }
  308. }
  309. .sxzg-icon {
  310. color: $primary-color;
  311. font-size: 16rpx;
  312. width: 80rpx;
  313. text-align: center;
  314. line-height: normal;
  315. border: 2rpx solid $primary-color;
  316. border-radius: 20rpx;
  317. display: inline-block;
  318. position: relative;
  319. top: -4rpx;
  320. margin-right: 10upx;
  321. // transform: scale(0.9);
  322. }
  323. .sxj-icon {
  324. background: $primary-color;
  325. color: white;
  326. font-size: 20upx;
  327. padding: 0 5upx;
  328. border-radius: 5upx;
  329. vertical-align: middle;
  330. }
  331. .row {
  332. display: flex;
  333. align-items: center;
  334. }
  335. .org-price {
  336. text-decoration: line-through;
  337. font-size: 26rpx;
  338. color: #cccccc;
  339. }
  340. .price {
  341. font-size: 26rpx;
  342. color: $primary-color;
  343. font-weight: bold;
  344. }
  345. .footer {
  346. display: flex;
  347. position: fixed;
  348. height: 100upx;
  349. background: $primary-color;
  350. bottom: 0;
  351. width: 100%;
  352. box-shadow: 0 0 10upx #999999;
  353. align-items: center;
  354. /* #ifdef H5 */
  355. bottom: 90upx;
  356. /* #endif */
  357. color: white;
  358. .select-all {
  359. width: 200upx;
  360. flex: 0 0 200upx;
  361. position: relative;
  362. margin-left: 20upx;
  363. .checkbox {
  364. transform: scale(0.7);
  365. position: relative;
  366. top: -4upx
  367. }
  368. /deep/ .uni-checkbox-input {
  369. border: none;
  370. }
  371. /deep/ .uni-checkbox-input:hover {
  372. border: none;
  373. }
  374. }
  375. .total {
  376. flex-grow: 1;
  377. text-align: right;
  378. }
  379. .settlement {
  380. flex-grow: 0;
  381. text-align: right;
  382. margin: 0 20upx;
  383. }
  384. .settlement-btn {
  385. background: #ffa82e;
  386. padding: 15upx 60upx;
  387. color: white;
  388. line-height: normal;
  389. font-size: 32upx;
  390. border-radius: 50upx;
  391. }
  392. }
  393. </style>