CartList.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. <template>
  2. <view class="div cart-list-wrapper" :style="'padding-top:'+navHeight+'px'">
  3. <view class="div cart-list">
  4. <checkbox-group @change="changeSingleStatu" class="div list" v-for="(item, index) in cartList" :key="index">
  5. <view class="div list-checkbox">
  6. <checkbox
  7. class="checkbox"
  8. :id="'goods'+index"
  9. :value="item.pgoods_id"
  10. :checked="item.checked"
  11. :disabled="!isCheckedAll && item.pgoods_storage == 0"
  12. />
  13. <label class="label" :class="{'checked':item.checked}" :for="'goods'+index"><text class="span iconfont">&#xe69b;</text></label>
  14. </view>
  15. <view class="div list-item" @click="goDetail(item.pgoods_id)">
  16. <view class="div item">
  17. <view class="div ui-image">
  18. <image mode="aspectFit" class="img" :src="item.pgoods_image" />
  19. <text v-if="item.pgoods_storage == 0" class="span stock-info"
  20. >已售罄</text
  21. >
  22. <text
  23. v-if="
  24. item.pgoods_storage > 0 && item.pgoods_storage <= 10
  25. "
  26. class="span stock-info"
  27. >仅剩{{ item.pgoods_storage }}件</text
  28. >
  29. </view>
  30. <view class="div list-info">
  31. <view class="div product-header">
  32. <text
  33. class="h3 product-title"
  34. v-bind:class="{ 'disabled-list': item.pgoods_storage == 0 }"
  35. >
  36. {{ item.pgoods_name }}
  37. </text>
  38. </view>
  39. <text class="h3 property-info"></text>
  40. <view class="div info-price">
  41. <view class="p"
  42. v-bind:class="{ 'disabled-list': item.pgoods_storage == 0 }"
  43. >
  44. {{ item.pgoods_points }}积分
  45. </view>
  46. <view class="div ui-number">
  47. <view
  48. class="div reduce ui-common"
  49. @click.stop="reduceNumber(item.pcart_id, item.pgoods_choosenum,index)"
  50. v-bind:class="{ 'reduce-opacity': item.pgoods_choosenum <= 1 }"
  51. >
  52. -
  53. </view>
  54. <input
  55. type="number"
  56. min="1"
  57. class="number"
  58. value="1"
  59. v-model="item.pgoods_choosenum"
  60. readonly="true"
  61. />
  62. <view
  63. class="div add ui-common"
  64. @click.stop="
  65. addNumber(
  66. item.pcart_id,
  67. item.pgoods_choosenum,
  68. index
  69. )
  70. "
  71. >
  72. +
  73. </view>
  74. </view>
  75. </view>
  76. </view>
  77. </view>
  78. </view>
  79. </checkbox-group>
  80. </view>
  81. </view>
  82. </template>
  83. <script>
  84. import { urlencode } from '@/util/common'
  85. import { mapState, mapMutations } from 'vuex'
  86. import {
  87. cartGet,
  88. cartDelete,
  89. cartUpdate,
  90. cartQuantity
  91. } from '../../../../api/memberPointscart'
  92. export default {
  93. props: {
  94. isCheckedAll: {
  95. type: Boolean,
  96. default: false
  97. }
  98. },
  99. data () {
  100. return {
  101. navHeight:0,
  102. cartList: [], // 购物车列表
  103. indicator: { spinnerType: 'fading-circle' },
  104. totalPrice: 0, // 购物车总价
  105. cartId: '', // 购物车中选中的商品
  106. totalAmount: 0, // 购物车数量
  107. promosIds: [] // 促销信息IDS
  108. }
  109. },
  110. created () {
  111. this.getCartList(true)
  112. },
  113. mounted: function() {
  114. // #ifdef MP-WEIXIN
  115. this.navHeight = uni.getMenuButtonBoundingClientRect().top
  116. // #endif
  117. },
  118. methods: {
  119. ...mapMutations({
  120. getAmount: 'calculationAmount',
  121. getPrice: 'calculationPrice',
  122. setCartNumber: 'setCartNumber',
  123. saveSelectedCartGoods: 'saveSelectedCartGoods'
  124. }),
  125. /*
  126. * getCartList: 获取购物车列表
  127. */
  128. getCartList (value) {
  129. cartGet().then(res => {
  130. if (res && res.result.cart_array.length > 0) {
  131. this.cartList = Object.assign([], res.result.cart_array)
  132. this.addChecked(value)
  133. this.renderCart()
  134. } else {
  135. this.cartList = []
  136. this.getAmount(0)
  137. this.getPrice(0.0)
  138. }
  139. uni.$emit('list-is-empty', this.cartList)
  140. })
  141. },
  142. /*
  143. * addChecked: 为每个商品添加checked 属性
  144. * @param: isSelectedall 是否选中商品 Boolean
  145. */
  146. addChecked (isSelectedall) {
  147. let list = this.cartList
  148. for (var i in list) {
  149. if (list[i].pgoods_storage == 0 && !this.isCheckedAll) {
  150. list[i].checked = false
  151. } else {
  152. list[i].checked = isSelectedall
  153. }
  154. }
  155. this.cartList = Object.assign([], list)
  156. },
  157. /*
  158. * renderCart: 修改商品数量和点击是否选中后 重新计算商品价格和数量
  159. */
  160. renderCart () {
  161. let data = this.cartList
  162. this.promosIds = []
  163. let cartGoods = []
  164. let totalAmount = 0
  165. let totalPrice = 0
  166. for (var i in data) {
  167. if (data[i].checked) {
  168. totalAmount += parseInt(data[i].pgoods_choosenum)
  169. totalPrice += parseInt(data[i].pgoods_choosenum) * parseInt(data[i].pgoods_points)
  170. cartGoods.push(data[i].pcart_id + '|' + data[i].pgoods_choosenum)
  171. }
  172. }
  173. this.cartId = cartGoods.toString()
  174. this.totalPrice = totalPrice
  175. this.totalAmount = totalAmount
  176. uni.$emit('calcu-cart-data', { totalPrice: this.totalPrice, totalAmount: this.totalAmount, cartId: this.cartId })
  177. },
  178. /*
  179. * deleteSelected: 删除购物车数据
  180. */
  181. deleteSelected () {
  182. let data = this.cartList
  183. let deleteGoods = []
  184. this.promosIds = []
  185. for (var i in data) {
  186. if (data[i].checked) {
  187. deleteGoods.push(data[i].pcart_id)
  188. }
  189. }
  190. if (deleteGoods.length > 0) {
  191. deleteGoods = deleteGoods.toString()
  192. } else {
  193. uni.showToast({icon:'none',title: '当前没有可删除的商品'})
  194. return
  195. }
  196. uni.showLoading({ title: '加载中' })
  197. cartDelete(deleteGoods).then(res => {
  198. if (res) {
  199. this.getCartList(false)
  200. uni.hideLoading()
  201. }
  202. })
  203. },
  204. /*
  205. * changeSingleStatu: 改变单个商品是否选中的状态, 然后重新获取商品的件数和价格
  206. */
  207. changeSingleStatu (e) {
  208. var value=e.detail.value
  209. let list = this.cartList
  210. let length = 0
  211. let totalLength = 0
  212. let status = false
  213. for (var i in list) {
  214. if (value.indexOf(list[i].pgoods_id)>-1) {
  215. list[i].checked=true
  216. length = length + 1
  217. k++
  218. }else{
  219. list[i].checked = false
  220. }
  221. }
  222. uni.$emit('change-footer-status', status)
  223. if (!this.isCheckedAll) {
  224. this.renderCart()
  225. }
  226. this.cartList = Object.assign([], list)
  227. },
  228. /*
  229. * reduceNumber: 数量减少
  230. * @param: id 当前减少的购物车id
  231. * @param: amount 数量
  232. * @param: i 当前减少的购物车的index
  233. */
  234. reduceNumber (id, amount, i) {
  235. if (amount > 1) {
  236. amount--
  237. this.updateCartQuantity(id, amount, i)
  238. } else {
  239. uni.showToast({icon:'none',title: {
  240. message: '受不了了, 宝贝不能再少了'
  241. }})
  242. }
  243. },
  244. /*
  245. * addNumber: 数量增加
  246. * @param: id 当前增加的购物车id
  247. * @param: amount 数量
  248. * @param: i 当前增加的购物车组的index
  249. * @param: index 当前增加的购物车的index
  250. */
  251. addNumber (id, amount, i, index) {
  252. amount++
  253. this.updateCartQuantity(id, amount, i, index)
  254. },
  255. /*
  256. * updateCartQuantity: 商品数量加减更新数
  257. * @param: id 当前减少的购物车id
  258. * @param: amount 数量
  259. * @param: i 当前购物车的index
  260. */
  261. updateCartQuantity (id, amount, i) {
  262. uni.showLoading({ title: '加载中' })
  263. cartUpdate(id, amount).then(
  264. res => {
  265. if (res) {
  266. uni.hideLoading()
  267. this.cartList[i].pgoods_choosenum = amount
  268. this.renderCart()
  269. // this.getCartNumber()
  270. }
  271. },
  272. error => {
  273. uni.showToast({icon:'none',title: error.message})
  274. uni.hideLoading()
  275. }
  276. )
  277. },
  278. /*
  279. * getCartNumber: 获取购物车列表
  280. */
  281. getCartNumber () {
  282. cartQuantity().then(res => {
  283. if (res) {
  284. this.setCartNumber(res.quantity)
  285. }
  286. })
  287. },
  288. /*
  289. * goDetail: 跳转到详情
  290. */
  291. goDetail (id) {
  292. uni.navigateTo({ url: '/pages/home/pointsgoods/Detail'+'?'+urlencode( { pgoods_id: id } )})
  293. }
  294. }
  295. }
  296. </script>
  297. <style lang="scss" scoped>
  298. .cart-list-wrapper {
  299. overflow-y: auto;
  300. position: fixed;
  301. width: 100%;
  302. bottom: $footerHeight;
  303. padding-bottom: constant(safe-area-inset-bottom);/*兼容 IOS<11.2*/padding-bottom: env(safe-area-inset-bottom);/*兼容 IOS>11.2*/
  304. top: $headerHeight;
  305. margin-bottom:0.5rem;
  306. .cart-list{margin-bottom:$modelSpace;background:#fff;}
  307. .store-info{background:#fff;border-bottom:1px solid #e8eaed;display:flex;padding-left:0.6rem;align-content: center;align-items: center;
  308. .store-name{font-size:$h2;line-height: 2rem;flex:1;}
  309. }
  310. .list-checkbox {
  311. width:1rem;
  312. height:1rem;
  313. flex-basis:1rem;
  314. flex-shrink: 0;
  315. position: relative;
  316. margin-right:0.25rem;
  317. .label {
  318. padding:0;
  319. position: absolute;
  320. left:0;
  321. top: 0;
  322. width:1rem;
  323. height:1rem;
  324. display: inline-block;
  325. border-radius:50%;
  326. border:1px solid #333;
  327. box-sizing:border-box;
  328. .iconfont{display: none;line-height:1rem;text-align: center;}
  329. &.checked{
  330. border-color:$primaryColor;
  331. background-color:$primaryColor;
  332. .iconfont{display: block;color:#fff}
  333. }
  334. }
  335. .checkbox {
  336. position: relative;
  337. width:1rem;
  338. margin: 0;
  339. opacity: 0;
  340. background-color: #fff;
  341. }
  342. }
  343. .list {
  344. background-color: #fff;
  345. padding:0.6rem;
  346. display: flex;
  347. align-content: center;
  348. align-items: center;
  349. .list-item {
  350. display: flex;
  351. width: 100%;
  352. flex-direction: column;
  353. .div.item {
  354. display: flex;
  355. width: 100%;
  356. .div.ui-image {
  357. flex-shrink: 0;
  358. width:4.5rem;
  359. height:4.5rem;
  360. flex-basis:4.5rem;
  361. position: relative;
  362. .img {
  363. width: 100%;
  364. height: 100%;
  365. border-radius: 0.4rem;
  366. }
  367. .span.promos {
  368. position: absolute;
  369. width:1.8rem;
  370. height:1rem;
  371. color: #ffffff;
  372. font-size:$h6;
  373. top: 0;
  374. background-size: cover;
  375. font-weight: 100;
  376. line-height: 1rem;
  377. text-align: left;
  378. padding-left:0.25rem;
  379. }
  380. .span.stock-info {
  381. position: absolute;
  382. height:1rem;
  383. background: rgba(243, 244, 245, 1);
  384. line-height:1rem;
  385. text-align: center;
  386. font-size:$subFontSize;
  387. color: $primaryColor;
  388. width: 100%;
  389. bottom: 0;
  390. left: 0;
  391. }
  392. }
  393. .div.list-info {
  394. margin-left:0.5rem;
  395. width: 100%;
  396. display: flex;
  397. flex-direction: column;
  398. align-content: center;
  399. justify-content: space-between;
  400. .product-header {
  401. display: flex;
  402. align-items: center;
  403. .promos-icon {
  404. width:0.8rem;
  405. height:0.8rem;
  406. margin-right:0.2rem;
  407. }
  408. .product-title {
  409. font-size:$subFontSize;
  410. line-height:1rem;
  411. height:2rem;
  412. padding: 0;
  413. display: -webkit-box;
  414. -webkit-box-orient: vertical;
  415. -webkit-line-clamp: 2;
  416. overflow: hidden;
  417. &.disabled-list {
  418. color: #a4aab3;
  419. }
  420. }
  421. }
  422. .h3 {
  423. font-size:$subFontSize;
  424. color: rgba(78, 84, 93, 1);
  425. padding: 0;
  426. margin: 0;
  427. display: -webkit-box;
  428. -webkit-box-orient: vertical;
  429. -webkit-line-clamp: 2;
  430. overflow: hidden;
  431. &.disabled-list {
  432. color: #a4aab3;
  433. }
  434. }
  435. .h3.property-info {
  436. font-size:$fontSize;
  437. color: #7c7f88;
  438. }
  439. .div.info-price {
  440. width: 100%;
  441. display: flex;
  442. justify-content: space-between;
  443. align-content: flex-end;
  444. align-items: flex-end;
  445. .p {
  446. font-size:$mainFontSize;
  447. color: $primaryColor;
  448. padding: 0;
  449. margin: 0;
  450. display: inline-block;
  451. &.disabled-list {
  452. color: #a4aab3;
  453. }
  454. }
  455. }
  456. .div.ui-number {
  457. height:1.2rem;
  458. display: flex;
  459. border-radius: 0.15rem 0 0 0.15rem;
  460. input,
  461. .div {
  462. height:1.2rem;
  463. text-align: center;
  464. color: #404245;
  465. display: inline-block;
  466. padding: 0;
  467. margin: 0;
  468. border: 0;
  469. outline-offset: 0;
  470. }
  471. .ui-common {
  472. line-height:1.2rem;
  473. width:1.3rem;
  474. height:1.2rem;
  475. border: 1px solid #c7c7cd;
  476. cursor: pointer;
  477. }
  478. .reduce {
  479. border-right: 0;
  480. }
  481. .reduce-opacity {
  482. opacity: 0.4;
  483. }
  484. .add {
  485. border-left: 0;
  486. }
  487. .number {
  488. width:1.3rem;
  489. border: 1px solid #c7c7cd;
  490. border-radius: 0;
  491. border-image-width: 0;
  492. box-shadow: 0;
  493. vertical-align: bottom;
  494. &:focus {
  495. outline: none;
  496. }
  497. }
  498. }
  499. }
  500. }
  501. .p.list-promotion-info {
  502. margin-top:0.6rem;
  503. padding:0.4rem 0;
  504. line-height: auto;
  505. font-size:$h6;
  506. color: #000;
  507. background: #f8f8f8;
  508. width: 100%;
  509. .span {
  510. border: 1px solid $primaryColor;
  511. padding: 1px 0.2rem;
  512. border-radius:0.1rem;
  513. font-size:$h6;
  514. color: $primaryColor;
  515. margin: 0 0.5rem;
  516. text-align: center;
  517. }
  518. }
  519. }
  520. }
  521. }
  522. .has-bottom {
  523. bottom:4.7rem;
  524. }
  525. </style>