shopping-cart.vue 8.1 KB

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