cart.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. <template>
  2. <view class="container">
  3. <view class="service-policy">
  4. <view class="item">30天无忧退货</view>
  5. <view class="item">48小时快速退款</view>
  6. <view class="item">满88元免邮费</view>
  7. </view>
  8. <view class="no-cart" v-if="cartGoods.length <= 0">
  9. <view class="c">
  10. <view class="title-box">
  11. 购物车空空如也~
  12. </view>
  13. <view class="to-index-btn" @tap="toIndexPage">
  14. 去逛逛
  15. </view>
  16. </view>
  17. </view>
  18. <view class="cart-view" v-if="cartGoods.length > 0">
  19. <view class="list">
  20. <view class="group-item">
  21. <view class="goods">
  22. <view :class="'item ' + (isEditCart ? 'edit' : '')" v-for="(item, index) in cartGoods" :key="item.id">
  23. <view :class="'checkbox ' + (item.checked ? 'checked' : '')" @tap="checkedItem" :data-item-index="index"></view>
  24. <view class="cart-goods">
  25. <image class="img" :src="item.list_pic_url"></image>
  26. <view class="info">
  27. <view class="t">
  28. <text class="name">{{item.goods_name}}</text>
  29. <text class="num">x{{item.number}}</text>
  30. </view>
  31. <view class="attr">{{ isEditCart ? '已选择:' : ''}}{{item.goods_specifition_name_value||''}}</view>
  32. <view class="b">
  33. <text class="price">¥{{item.retail_price}}</text>
  34. <view class="selnum">
  35. <view class="cut" @tap="cutNumber" :data-item-index="index">-</view>
  36. <input :value="item.number" class="number" :disabled="true" type="number" />
  37. <view class="add" @tap="addNumber" :data-item-index="index">+</view>
  38. </view>
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. <view class="cart-bottom">
  47. <view :class="'checkbox ' + (checkedAllStatus ? 'checked' : '')" @tap="checkedAll">全选({{cartTotal.checkedGoodsCount}})</view>
  48. <view class="total">{{!isEditCart ? '¥'+cartTotal.checkedGoodsAmount : ''}}</view>
  49. <view class="delete" @tap="editCart">{{!isEditCart ? '编辑' : '完成'}}</view>
  50. <view class="checkout" @tap="deleteCart" v-if="isEditCart">删除所选</view>
  51. <view class="checkout" @tap="checkoutOrder" v-if="!isEditCart">下单</view>
  52. </view>
  53. </view>
  54. </view>
  55. </template>
  56. <script>
  57. const util = require("@/utils/util.js")
  58. const api = require('@/utils/api.js');
  59. export default {
  60. data() {
  61. return {
  62. cartGoods: [],
  63. cartTotal: {
  64. "goodsCount": 0,
  65. "goodsAmount": 0.00,
  66. "checkedGoodsCount": 0,
  67. "checkedGoodsAmount": 0.00
  68. },
  69. isEditCart: false,
  70. checkedAllStatus: true,
  71. editCartList: []
  72. }
  73. },
  74. methods: {
  75. getCartList: function() {
  76. let that = this;
  77. util.request(api.CartList).then(function(res) {
  78. if (res.errno === 0) {
  79. that.cartGoods = res.data.cartList
  80. that.cartTotal = res.data.cartTotal
  81. }
  82. that.checkedAllStatus = that.isCheckedAll()
  83. });
  84. },
  85. isCheckedAll: function() {
  86. //判断购物车商品已全选
  87. return this.cartGoods.every(function(element, index, array) {
  88. if (element.checked == true) {
  89. return true;
  90. } else {
  91. return false;
  92. }
  93. });
  94. },
  95. checkedItem: function(event) {
  96. let itemIndex = event.target.dataset.itemIndex;
  97. let that = this;
  98. if (!that.isEditCart) {
  99. util.request(api.CartChecked, {
  100. productIds: that.cartGoods[itemIndex].product_id,
  101. isChecked: that.cartGoods[itemIndex].checked ? 0 : 1
  102. }, "POST", "application/json").then(function(res) {
  103. if (res.errno === 0) {
  104. that.cartGoods = res.data.cartList
  105. that.cartTotal = res.data.cartTotal
  106. }
  107. that.checkedAllStatus = that.isCheckedAll()
  108. });
  109. } else {
  110. //编辑状态
  111. let tmpCartData = that.cartGoods.map(function(element, index, array) {
  112. if (index == itemIndex) {
  113. element.checked = !element.checked;
  114. }
  115. return element;
  116. });
  117. that.cartGoods = tmpCartData
  118. that.cartTotal.checkedGoodsCount = that.getCheckedGoodsCount()
  119. that.checkedAllStatus = that.isCheckedAll()
  120. }
  121. },
  122. getCheckedGoodsCount: function() {
  123. let checkedGoodsCount = 0;
  124. this.cartGoods.forEach(function(v) {
  125. if (v.checked === true) {
  126. checkedGoodsCount += v.number;
  127. }
  128. });
  129. return checkedGoodsCount;
  130. },
  131. checkedAll: function() {
  132. let that = this;
  133. if (!that.isEditCart) {
  134. var productIds = that.cartGoods.map(function(v) {
  135. return v.product_id;
  136. });
  137. util.request(api.CartChecked, {
  138. productIds: productIds.join(','),
  139. isChecked: that.isCheckedAll() ? 0 : 1
  140. }, "POST", "application/json").then(function(res) {
  141. if (res.errno === 0) {
  142. that.cartGoods = res.data.cartList
  143. that.cartTotal = res.data.cartTotal
  144. }
  145. that.checkedAllStatus = that.isCheckedAll()
  146. });
  147. } else {
  148. //编辑状态
  149. let checkedAllStatus = that.isCheckedAll();
  150. let tmpCartData = that.cartGoods.map(function(v) {
  151. v.checked = !checkedAllStatus;
  152. return v;
  153. });
  154. that.cartGoods = tmpCartData
  155. that.cartTotal.checkedGoodsCount = that.getCheckedGoodsCount()
  156. that.checkedAllStatus = that.isCheckedAll()
  157. }
  158. },
  159. editCart: function() {
  160. var that = this;
  161. if (that.isEditCart) {
  162. that.getCartList();
  163. that.isEditCart = !that.isEditCart
  164. } else {
  165. //编辑状态
  166. let tmpCartList = that.cartGoods.map(function(v) {
  167. v.checked = false;
  168. return v;
  169. });
  170. that.editCartList = that.cartGoods
  171. that.isEditCart = !that.isEditCart
  172. that.cartGoods = tmpCartList
  173. that.cartTotal.checkedGoodsCount = that.getCheckedGoodsCount()
  174. that.checkedAllStatus = that.isCheckedAll()
  175. }
  176. },
  177. toIndexPage: function() {
  178. uni.switchTab({
  179. url: "/pages/index/index"
  180. });
  181. },
  182. updateCart: function(productId, goodsId, number, id) {
  183. let that = this;
  184. util.request(api.CartUpdate, {
  185. productId: productId,
  186. goodsId: goodsId,
  187. number: number,
  188. id: id
  189. }).then(function(res) {
  190. that.checkedAllStatus = that.isCheckedAll()
  191. });
  192. },
  193. cutNumber: function(event) {
  194. let itemIndex = event.target.dataset.itemIndex;
  195. let cartItem = this.cartGoods[itemIndex];
  196. let number = (cartItem.number - 1 > 1) ? cartItem.number - 1 : 1;
  197. cartItem.number = number;
  198. this.cartGoods = this.cartGoods
  199. this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);
  200. },
  201. addNumber: function(event) {
  202. let itemIndex = event.target.dataset.itemIndex;
  203. let cartItem = this.cartGoods[itemIndex];
  204. let number = cartItem.number + 1;
  205. cartItem.number = number;
  206. this.cartGoods = this.cartGoods
  207. this.updateCart(cartItem.product_id, cartItem.goods_id, number, cartItem.id);
  208. },
  209. checkoutOrder: function() {
  210. //获取已选择的商品
  211. let that = this;
  212. var checkedGoods = that.cartGoods.filter(function(element, index, array) {
  213. if (element.checked == true) {
  214. return true;
  215. } else {
  216. return false;
  217. }
  218. });
  219. if (checkedGoods.length <= 0) {
  220. return false;
  221. }
  222. uni.navigateTo({
  223. url: '../shopping/checkout/checkout'
  224. })
  225. },
  226. deleteCart: function() {
  227. //获取已选择的商品
  228. let that = this;
  229. let productIds = that.cartGoods.filter(function(element, index, array) {
  230. if (element.checked == true) {
  231. return true;
  232. } else {
  233. return false;
  234. }
  235. });
  236. if (productIds.length <= 0) {
  237. return false;
  238. }
  239. productIds = productIds.map(function(element, index, array) {
  240. if (element.checked == true) {
  241. return element.product_id;
  242. }
  243. });
  244. util.request(api.CartDelete, {
  245. productIds: productIds.join(',')
  246. }, 'POST', 'application/json').then(function(res) {
  247. if (res.errno === 0) {
  248. let cartList = res.data.cartList.map(v => {
  249. v.checked = false;
  250. return v;
  251. });
  252. that.cartGoods = cartList
  253. that.cartTotal = res.data.cartTotal
  254. }
  255. that.checkedAllStatus = that.isCheckedAll()
  256. });
  257. }
  258. },
  259. onShow: function() {
  260. // 页面显示
  261. this.getCartList();
  262. },
  263. onLoad: function() {}
  264. }
  265. </script>
  266. <style lang="scss">
  267. page {
  268. height: 100%;
  269. min-height: 100%;
  270. background: #f4f4f4;
  271. }
  272. .container {
  273. background: #f4f4f4;
  274. width: 100%;
  275. height: auto;
  276. min-height: 100%;
  277. overflow: hidden;
  278. }
  279. .service-policy {
  280. width: 750rpx;
  281. height: 73rpx;
  282. background: #f4f4f4;
  283. padding: 0 31.25rpx;
  284. display: flex;
  285. flex-flow: row nowrap;
  286. align-items: center;
  287. justify-content: space-between;
  288. }
  289. .service-policy .item {
  290. background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/servicePolicyRed-518d32d74b.png) 0 center no-repeat;
  291. background-size: 10rpx;
  292. padding-left: 15rpx;
  293. display: flex;
  294. align-items: center;
  295. font-size: 25rpx;
  296. color: #666;
  297. }
  298. .no-cart {
  299. width: 100%;
  300. height: auto;
  301. margin: 0 auto;
  302. }
  303. .no-cart .c {
  304. width: 100%;
  305. height: auto;
  306. margin-top: 200rpx;
  307. }
  308. .no-cart .c image {
  309. margin: 0 auto;
  310. display: block;
  311. text-align: center;
  312. width: 258rpx;
  313. height: 258rpx;
  314. }
  315. .no-cart .c text {
  316. margin: 0 auto;
  317. display: block;
  318. width: 258rpx;
  319. height: 29rpx;
  320. line-height: 29rpx;
  321. text-align: center;
  322. font-size: 29rpx;
  323. color: #999;
  324. }
  325. .title-box {
  326. width: 100%;
  327. padding-top: 330rpx;
  328. text-align: center;
  329. font-size: 28rpx;
  330. color: #999;
  331. background: url(https://platform-wxmall.oss-cn-beijing.aliyuncs.com/upload/20180727/15015039793f4e.png) no-repeat center 205rpx;
  332. background-size: 100rpx auto;
  333. margin-bottom: 50rpx;
  334. }
  335. .to-index-btn {
  336. color: #fff;
  337. background: #e64340;
  338. border-radius: 6px;
  339. width: 300rpx;
  340. height: auto;
  341. line-height: 70rpx;
  342. text-align: center;
  343. font-size: 28rpx;
  344. margin: 0 auto;
  345. display: block;
  346. }
  347. .cart-view {
  348. width: 100%;
  349. height: auto;
  350. overflow: hidden;
  351. }
  352. .cart-view .list {
  353. height: auto;
  354. width: 100%;
  355. overflow: hidden;
  356. margin-bottom: 120rpx;
  357. }
  358. .cart-view .group-item {
  359. height: auto;
  360. width: 100%;
  361. background: #fff;
  362. margin-bottom: 18rpx;
  363. }
  364. .cart-view .item {
  365. height: 164rpx;
  366. width: 100%;
  367. overflow: hidden;
  368. }
  369. .cart-view .item .checkbox {
  370. float: left;
  371. height: 34rpx;
  372. width: 34rpx;
  373. margin: 65rpx 18rpx 65rpx 26rpx;
  374. background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/checkbox-0e09baa37e.png) no-repeat;
  375. background-size: 34rpx;
  376. }
  377. .cart-view .item .checkbox.checked {
  378. background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/checkbox-checked-822e54472a.png) no-repeat;
  379. background-size: 34rpx;
  380. }
  381. .cart-view .item .cart-goods {
  382. float: right;
  383. height: 164rpx;
  384. width: 672rpx;
  385. border-bottom: 1px solid #f4f4f4;
  386. }
  387. .cart-view .item .img {
  388. float: left;
  389. height: 125rpx;
  390. width: 125rpx;
  391. background: #f4f4f4;
  392. margin: 19.5rpx 18rpx 19.5rpx 0;
  393. }
  394. .cart-view .item .info {
  395. float: left;
  396. height: 125rpx;
  397. width: 503rpx;
  398. margin: 19.5rpx 26rpx 19.5rpx 0;
  399. }
  400. .cart-view .item .t {
  401. margin: 8rpx 0;
  402. height: 28rpx;
  403. font-size: 25rpx;
  404. color: #333;
  405. overflow: hidden;
  406. }
  407. .cart-view .item .name {
  408. height: 28rpx;
  409. max-width: 310rpx;
  410. line-height: 28rpx;
  411. font-size: 25rpx;
  412. color: #333;
  413. overflow: hidden;
  414. }
  415. .cart-view .item .num {
  416. height: 28rpx;
  417. line-height: 28rpx;
  418. float: right;
  419. }
  420. .cart-view .item .attr {
  421. margin-bottom: 17rpx;
  422. height: 24rpx;
  423. line-height: 24rpx;
  424. font-size: 22rpx;
  425. color: #666;
  426. overflow: hidden;
  427. }
  428. .cart-view .item .b {
  429. height: 28rpx;
  430. line-height: 28rpx;
  431. font-size: 25rpx;
  432. color: #333;
  433. overflow: hidden;
  434. }
  435. .cart-view .item .price {
  436. float: left;
  437. }
  438. .cart-view .item .open {
  439. height: 28rpx;
  440. width: 150rpx;
  441. display: block;
  442. float: right;
  443. background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/arrowDown-d48093db25.png) right center no-repeat;
  444. background-size: 25rpx;
  445. font-size: 25rpx;
  446. color: #333;
  447. }
  448. .cart-view .item.edit .t {
  449. display: none;
  450. }
  451. .cart-view .item.edit .attr {
  452. text-align: right;
  453. background: url(http://yanxuan.nosdn.127.net/hxm/yanxuan-wap/p/20161201/style/img/icon-normal/arrow-right1-e9828c5b35.png) right center no-repeat;
  454. padding-right: 25rpx;
  455. background-size: 12rpx 20rpx;
  456. margin-bottom: 24rpx;
  457. height: 39rpx;
  458. line-height: 39rpx;
  459. font-size: 24rpx;
  460. color: #999;
  461. overflow: hidden;
  462. }
  463. .cart-view .item.edit .b {
  464. display: flex;
  465. height: 52rpx;
  466. overflow: hidden;
  467. }
  468. .cart-view .item.edit .price {
  469. line-height: 52rpx;
  470. height: 52rpx;
  471. flex: 1;
  472. }
  473. .cart-view .item .selnum {
  474. display: none;
  475. }
  476. .cart-view .item.edit .selnum {
  477. width: 235rpx;
  478. height: 52rpx;
  479. border: 1rpx solid #ccc;
  480. display: flex;
  481. }
  482. .selnum .cut {
  483. width: 70rpx;
  484. height: 100%;
  485. text-align: center;
  486. line-height: 50rpx;
  487. }
  488. .selnum .number {
  489. flex: 1;
  490. height: 100%;
  491. text-align: center;
  492. line-height: 68.75rpx;
  493. border-left: 1px solid #ccc;
  494. border-right: 1px solid #ccc;
  495. float: left;
  496. }
  497. .selnum .add {
  498. width: 80rpx;
  499. height: 100%;
  500. text-align: center;
  501. line-height: 50rpx;
  502. }
  503. .cart-view .group-item .header {
  504. width: 100%;
  505. height: 94rpx;
  506. line-height: 94rpx;
  507. padding: 0 26rpx;
  508. border-bottom: 1px solid #f4f4f4;
  509. }
  510. .cart-view .promotion .icon {
  511. display: inline-block;
  512. height: 24rpx;
  513. width: 15rpx;
  514. }
  515. .cart-view .promotion {
  516. margin-top: 25.5rpx;
  517. float: left;
  518. height: 43rpx;
  519. width: 480rpx;
  520. /*margin-right: 84rpx;*/
  521. line-height: 43rpx;
  522. font-size: 0;
  523. }
  524. .cart-view .promotion .tag {
  525. border: 1px solid #f48f18;
  526. height: 37rpx;
  527. line-height: 31rpx;
  528. padding: 0 9rpx;
  529. margin-right: 10rpx;
  530. color: #f48f18;
  531. font-size: 24.5rpx;
  532. }
  533. .cart-view .promotion .txt {
  534. height: 43rpx;
  535. line-height: 43rpx;
  536. padding-right: 10rpx;
  537. color: #333;
  538. font-size: 29rpx;
  539. overflow: hidden;
  540. }
  541. .cart-view .get {
  542. margin-top: 18rpx;
  543. float: right;
  544. height: 58rpx;
  545. padding-left: 14rpx;
  546. border-left: 1px solid #d9d9d9;
  547. line-height: 58rpx;
  548. font-size: 29rpx;
  549. color: #333;
  550. }
  551. .cart-bottom {
  552. position: fixed;
  553. bottom: 0;
  554. left: 0;
  555. height: 100rpx;
  556. width: 100%;
  557. background: #fff;
  558. display: flex;
  559. }
  560. .cart-bottom .checkbox {
  561. height: 34rpx;
  562. padding-left: 60rpx;
  563. line-height: 34rpx;
  564. margin: 33rpx 18rpx 33rpx 26rpx;
  565. background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/checkbox-0e09baa37e.png) no-repeat;
  566. background-size: 34rpx;
  567. font-size: 29rpx;
  568. }
  569. .cart-bottom .checkbox.checked {
  570. background: url(http://nos.netease.com/mailpub/hxm/yanxuan-wap/p/20150730/style/img/icon-normal/checkbox-checked-822e54472a.png) no-repeat;
  571. background-size: 34rpx;
  572. }
  573. .cart-bottom .total {
  574. height: 34rpx;
  575. flex: 1;
  576. margin: 33rpx 10rpx;
  577. font-size: 29rpx;
  578. }
  579. .cart-bottom .delete {
  580. height: 34rpx;
  581. width: auto;
  582. margin: 33rpx 18rpx;
  583. font-size: 29rpx;
  584. }
  585. .cart-bottom .checkout {
  586. height: 100rpx;
  587. width: 210rpx;
  588. text-align: center;
  589. line-height: 100rpx;
  590. font-size: 29rpx;
  591. background: #b4282d;
  592. color: #fff;
  593. }
  594. </style>