index.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. <template>
  2. <view class="cart-index">
  3. <view class="lement">
  4. <view>
  5. <checkbox-group @change="checkboxChangeAll">
  6. <checkbox class="round" style="transform:scale(0.7)" :checked="isAllChecked" />
  7. </checkbox-group>
  8. </view>
  9. <view>全选</view>
  10. <view style="display: flex;">
  11. <view>合计:</view>
  12. <view style="color: #FF1E1E;font-size: 28rpx;">¥{{totalPrice}}</view>
  13. </view>
  14. <view>免费配送</view>
  15. <view class="now" @click="submitOrder">立即结算</view>
  16. </view>
  17. <!-- 这里开始遍历商家和商品 -->
  18. <!-- 这一层是商家,val是商家的数据,k是商家的下标,可以在数组中用于定位商家 -->
  19. <view class="item" v-for="(val,k) in cartData" :key="k">
  20. <view class="title">
  21. <checkbox-group @change="checkboxChangeShop($event, k)">
  22. <checkbox class="round" style="transform:scale(0.7)" :checked="val.checked" />
  23. </checkbox-group>
  24. <image style="width:35rpx;height: 35rpx;margin-top: 7rpx;" :src="val.storeDto.storePic"></image>
  25. <text>{{val.storeDto.storeName}}</text>
  26. </view>
  27. <!-- 这一层是商品的遍历,item是商品信息,index是商品下标 -->
  28. <view v-for="(item,index) in val.shoppingTrolleyDtoList">
  29. <x-slideLeft @delItem="delItem(index,k)">
  30. <view class="cart-container">
  31. <view>
  32. <!-- 这一层是商品的勾选,我们可以传入商家和商品的下标,用于帮我们快速定位商品 -->
  33. <checkbox-group @change="checkboxChangeGood($event,item.id, k, index)">
  34. <checkbox class="round" style="transform:scale(0.7)" :checked="item.checked" />
  35. </checkbox-group>
  36. </view>
  37. <view style="background: #F2F2F2;">
  38. <image style="width:168rpx;height: 168rpx;" :src="item.goodsDto.goodsPicPathList[0]">
  39. </image>
  40. </view>
  41. <view class="message">
  42. <view style="font-size: 28rpx;">{{item.goodsDto.goodsName}}</view>
  43. <view class="guige">
  44. <view>{{message(index,k,item.goodsSpec)}}</view>
  45. <!-- <image style="width: 26rpx;height: 26rpx;" src="../../static/index/arrow.png"></image> -->
  46. </view>
  47. <view class="price">
  48. <view>¥{{item.goodsDto.price}}</view>
  49. <view>¥{{item.goodsPrice}}</view>
  50. <view class="amount">
  51. <view @click="sub(item,index,k)">-</view>
  52. <view>{{item.goodsCount}}</view>
  53. <view @click="add(item)">+</view>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </x-slideLeft>
  59. </view>
  60. </view>
  61. </view>
  62. </template>
  63. <script>
  64. export default {
  65. data() {
  66. return {
  67. isAllChecked: false, //是否全选
  68. totalPrice: 0, //总价
  69. cartData: [], //数据
  70. totalArr: []
  71. }
  72. },
  73. onLoad() {
  74. this.list();
  75. },
  76. //监听页面滚动s
  77. onPageScroll(res) {
  78. this.scrollTop = res.scrollTop
  79. },
  80. methods: {
  81. message(index, k, goodsSpec) {
  82. console.log(goodsSpec[1]);
  83. // console.log(goodsSpec[3]);
  84. let newGood1 = this.cartData[k].shoppingTrolleyDtoList[index].goodsSpecList[0].child;
  85. let newGood2 = this.cartData[k].shoppingTrolleyDtoList[index].goodsSpecList[1].child;
  86. console.log(newGood1.length);
  87. for (let i = 0; i < newGood1.length; i++) {
  88. if (goodsSpec[1] == i) {
  89. // break
  90. // console.log(newGood1[i].name);
  91. var specifications = newGood1[i].name;
  92. }
  93. }
  94. for (let j = 0; j < newGood2.length; j++) {
  95. if (goodsSpec[3] == j) {
  96. // break
  97. // console.log(newGood2[i].name);
  98. var color = newGood2[j].name;
  99. }
  100. }
  101. return [specifications, color];
  102. },
  103. async list() {
  104. let data = {
  105. userUuid: uni.getStorageSync('uuid'),
  106. currentPage: 1,
  107. pageSize: 10
  108. }
  109. // let res = await cartList({
  110. // header: {
  111. // token: uni.getStorageSync('token'),
  112. // clientType: 1
  113. // },
  114. // data: data
  115. // })
  116. // if (res.code == 200) {
  117. // this.cartData = res.data;
  118. // console.log(this.cartData)
  119. // }
  120. },
  121. async delItem(index, k) {
  122. console.log(this.cartData[k].shoppingTrolleyDtoList[index].goodsUuid, "goodsUuid");
  123. console.log(this.cartData[k].shoppingTrolleyDtoList[index].storeUuid, "storeUuid");
  124. let data = {
  125. isSelect: 0,
  126. shoppingTrolleyVoList: [{
  127. storeUuid: this.cartData[k].shoppingTrolleyDtoList[index].storeUuid,
  128. goodsUuid: this.cartData[k].shoppingTrolleyDtoList[index].goodsUuid
  129. }]
  130. }
  131. let res = await deleteCart({
  132. method: "POST",
  133. header: {
  134. token: uni.getStorageSync('token'),
  135. clientType: 1
  136. },
  137. data: data
  138. })
  139. console.log(res.data)
  140. if (res.code == 200) {
  141. this.list();
  142. this.cartData = res.data;
  143. }
  144. },
  145. add(item) {
  146. item.goodsCount++;
  147. },
  148. sub(item, index, k) {
  149. console.log(this.cartList, "this.cartList");
  150. if (item.goodsCount <= 1) {
  151. uni.showModal({
  152. title: '提示',
  153. content: '确定删除吗',
  154. success: (res) => {
  155. if (res.confirm) {
  156. console.log('用户点击确定');
  157. console.log(index);
  158. this.delItem(index, k);
  159. } else if (res.cancel) {
  160. console.log('用户点击取消');
  161. item.count == 0;
  162. }
  163. }
  164. });
  165. } else {
  166. item.goodsCount--;
  167. }
  168. },
  169. // delItem(index) {
  170. // console.log(this.cartList, "this.cartList");
  171. // this.cartList.splice(index, 1)
  172. // console.log("删除了", index);
  173. // },
  174. submitOrder() { // 提交购物车订单
  175. uni.navigateTo({
  176. url: '/pages/address/address'
  177. })
  178. },
  179. // this.cartData.forEach(item => {
  180. // item.checked = this.checkedAll;
  181. // if (item.shoppingTrolleyDtoList) {
  182. // item.shoppingTrolleyDtoList.forEach(citem => {
  183. // citem.checked = this.checkedAll;
  184. // })
  185. // }
  186. // })
  187. // 合计
  188. setCart() {
  189. let totalPrice = 0;
  190. this.cartList.forEach(shop => {
  191. if (shop.checked) {
  192. shop['shoppingTrolleyDtoList'].forEach(good => {
  193. totalPrice += good.count * good.price
  194. })
  195. }
  196. })
  197. this.totalPrice = totalPrice
  198. },
  199. // 全选
  200. // 需要联动商家、商品
  201. checkboxChangeAll(e) {
  202. console.log(e, "全选的e");
  203. const isChecked = e.detail.value.length > 0;
  204. // 联动商家、商品
  205. this.cartData.forEach(shop => {
  206. console.log(shop['shoppingTrolleyDtoList'], "shop");
  207. this.$set(shop, 'checked', isChecked);
  208. shop['shoppingTrolleyDtoList'].forEach(good => {
  209. this.$set(good, 'checked', isChecked)
  210. })
  211. })
  212. console.log('购物车数据', this.cartData);
  213. console.log('全选数据', this.isAllChecked);
  214. // this.isAllChecked = !this.isAllChecked
  215. // this.cartList.forEach(v => v.isChecked = this.isAllChecked)
  216. },
  217. // 勾选商家
  218. // 需要联动更新商品、全选
  219. checkboxChangeShop(e, shopIndex) {
  220. console.log(e, shopIndex);
  221. const shop = this.cartData[shopIndex];
  222. // e.detail.length > 0 则是勾选中
  223. const isChecked = e.detail.value.length > 0;
  224. this.$set(shop, 'checked', isChecked);
  225. // 联动商品
  226. const goods = this.cartData[shopIndex]['shoppingTrolleyDtoList']
  227. goods.forEach(g => {
  228. this.$set(g, 'checked', isChecked)
  229. });
  230. // 联动全选
  231. const shops = this.cartData
  232. if (isChecked) {
  233. let shopCheckSum = 0;
  234. // 判断是否所有商家都勾选
  235. shops.forEach(s => {
  236. if (s.checked) {
  237. shopCheckSum++
  238. }
  239. })
  240. this.isAllChecked = shopCheckSum === shops.length;
  241. } else {
  242. this.isAllChecked = false
  243. }
  244. console.log('购物车数据', this.cartData);
  245. console.log('全选数据', this.isAllChecked);
  246. },
  247. // 勾选商品
  248. // 需要联动更新商家、全选
  249. checkboxChangeGood(e, id, shopIndex, goodIndex) {
  250. console.log(e, id, shopIndex, goodIndex);
  251. // 找到商品,并修改相关状态
  252. // e.detail.length > 0 则是勾选中
  253. const good = this.cartData[shopIndex]['shoppingTrolleyDtoList'][goodIndex]; // 单个商品
  254. this.$set(good, 'checked', e.detail.value.length > 0);
  255. // 商品状态变更,那么再考虑商家状态
  256. // 判断当前商家下面有多少个勾选的商品,如果勾选总数和商家拥有的商品总数一致,那么商家勾选中
  257. const goods = this.cartData[shopIndex]['shoppingTrolleyDtoList']; // 多个商品
  258. const shop = this.cartData[shopIndex]; // 单个商店
  259. let goodCheckSum = 0;
  260. goods.forEach(g => {
  261. if (g.checked) {
  262. goodCheckSum++;
  263. }
  264. })
  265. this.$set(shop, 'checked', goodCheckSum === goods.length);
  266. // 之后再判断全选
  267. // 当所有商家都勾选,那么全选就勾选上
  268. const shops = this.cartData; // 所有商店
  269. let shopCheckSum = 0;
  270. shops.forEach(s => {
  271. if (s.checked) {
  272. shopCheckSum++
  273. }
  274. });
  275. this.isAllChecked = shopCheckSum === this.cartData.length;
  276. console.log('购物车数据', this.cartData);
  277. console.log('全选数据', this.isAllChecked);
  278. // console.log(e, id, this.cartData)
  279. // console.table({
  280. // '商家下标': shopIndex,
  281. // '商品下标': goodIndex
  282. // })
  283. // // var temp = []
  284. // // 找到被修改的商品对象,this.cartData是最外层的,代表着商家,里面一层才是商品
  285. // let index = this.cartData.findIndex(v => v.id === id)
  286. // // 选中状态取反
  287. // this.cartData[index].isChecked = !this.cartData[index].isChecked
  288. // temp = this.cartData.every(v => v.isChecked)
  289. // if (temp) {
  290. // this.isAllChecked = true
  291. // } else {a
  292. // this.isAllChecked = false
  293. // }
  294. // this.setCart()
  295. },
  296. },
  297. }
  298. </script>
  299. <style lang="stylus" scoped>
  300. .cart-index {
  301. background-color: #F7F7F7;
  302. .cart-container {
  303. display: flex;
  304. justify-content: space-between;
  305. align-items: center;
  306. margin-top: 30rpx;
  307. margin-left: -20rpx;
  308. }
  309. .lement {
  310. position fixed;
  311. // bottom 0;
  312. bottom: var(--window-bottom, 0);
  313. display flex;
  314. justify-content space-between;
  315. align-items center;
  316. padding 30rpx;
  317. font-size: 24rpx;
  318. background-color white;
  319. width: 93%;
  320. z-index: 1000;
  321. border-bottom: 1rpx solid #dadbde;
  322. .now {
  323. background: #FF1E1E;
  324. border-radius 38rpx;
  325. font-size: 30rpx;
  326. color: white;
  327. padding 20rpx 65rpx;
  328. }
  329. }
  330. .item {
  331. padding 0 30rpx 130rpx;
  332. background-color white;
  333. margin-top 15rpx;
  334. .title {
  335. display flex;
  336. text {
  337. font-size 28rpx;
  338. margin-left 5px;
  339. margin-top 5rpx
  340. }
  341. }
  342. .message {
  343. width 61%;
  344. display flex;
  345. flex-direction column;
  346. justify-content space-between view:nth-child(1) {
  347. // font-size: 28rpx;
  348. }
  349. .guige {
  350. display flex;
  351. justify-content space-around;
  352. align-items center;
  353. width 35%;
  354. background: #F3F3F3;
  355. border-radius 20rpx;
  356. color: #999999;
  357. font-size 24rpx;
  358. padding 7rpx;
  359. margin-top 10rpx
  360. }
  361. .price {
  362. display flex;
  363. justify-content space-between;
  364. margin-top 10rpx;
  365. view:nth-child(1) {
  366. font-size 30rpx;
  367. color: #FF1E1E;
  368. }
  369. view:nth-child(2) {
  370. font-size: 24rpx;
  371. color: #999999;
  372. text-decoration: line-through;
  373. margin-top 5rpx;
  374. margin-left -60rpx
  375. }
  376. }
  377. .amount {
  378. display flex;
  379. view:nth-child(2) {
  380. font-size: 28rpx;
  381. color: #333333;
  382. text-decoration: none;
  383. margin-left 20rpx;
  384. margin-right 20rpx
  385. }
  386. view:nth-child(1),
  387. view:nth-child(3) {
  388. width: 46rpx;
  389. height 46rpx;
  390. background-color #F4F4F4;
  391. text-align center;
  392. font-size 30rpx;
  393. color: black
  394. }
  395. }
  396. }
  397. }
  398. }
  399. </style>