cart.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. <template>
  2. <view class="content">
  3. <!-- 商品列表 -->
  4. <!-- <view class="shopbox" v-for="(shop,idx) in shopList" :key="idx"> -->
  5. <!-- <view class="shopbox-name" v-if="shop.storeName && shop.storeName != 'null'">
  6. <label style="margin-right: 30upx;">
  7. <checkbox :value="shop.checked" :checked="shop.checked" @click="checkShopChange(shop)" />
  8. </label>
  9. <text>{{shop.storeName}}</text>
  10. </view> -->
  11. <view class="shopbox" v-if="shopList.length !== 0">
  12. <view class="goods-list" >
  13. <view class="item" v-for="(item,index) in shopList" :key="index">
  14. <label style="margin-right: 30upx;">
  15. <checkbox :value="item.checked" :checked="item.checked" @click="checkChange(item,shop)" />
  16. </label>
  17. <image class="goods-img" @click="showProduct(item)" :src="item.imgUrl" mode="aspectFit"></image>
  18. <view class="info-box">
  19. <view>
  20. <view class="title-box">
  21. <!-- <view class="tag">{{utils.getDictLabelName("storeProductType",item.productType)}}</view> -->
  22. <view class="title ellipsis">{{ item.productName }}</view>
  23. </view>
  24. <!-- <view class="intro ellipsis">{{item.productAttrName}}</view> -->
  25. </view>
  26. <view class="price-num">
  27. <view class="price">
  28. <text class="unit">¥</text>
  29. <text class="text">{{item.price}}</text>
  30. </view>
  31. <view class="num-box">
  32. <view class="img-box" @click="delNum(item)">
  33. <image v-if="item.cartNum <= 1" src="/static/images/jian.png" mode=""></image>
  34. <image v-else src="/static/images/jian2.png" mode=""></image>
  35. </view>
  36. <input type="number" @change="changeNum($event,item)" :value="item.cartNum" />
  37. <view class="img-box" @click="addNum(item)">
  38. <image src="/static/images/add.png" mode=""></image>
  39. </view>
  40. </view>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. <view style="height: 140rpx;width: 100%;"></view>
  47. <view v-if="shopList.length == 0" class="no-data-box">
  48. <image src="/static/images/no_data.png" mode="aspectFit"></image>
  49. <view class="empty-title">暂无数据</view>
  50. </view>
  51. <!-- 底部按钮 -->
  52. <view class="btn-foot">
  53. <view class="left">
  54. <label>
  55. <checkbox :checked="checkAll" @click="handleCheckAll()" />
  56. </label>
  57. <text class="text">全选</text>
  58. <text class="text" @click="delCart()">删除</text>
  59. </view>
  60. <view class="right">
  61. <view class="total">
  62. <text class="label">合计:</text>
  63. <view class="price">
  64. <text class="unit">¥</text>
  65. <text class="num">{{totalMoney.toFixed(2)}}</text>
  66. </view>
  67. </view>
  68. <view class="btn" @click="submit">结算</view>
  69. </view>
  70. </view>
  71. </view>
  72. </template>
  73. <script>
  74. import {
  75. queryLiveCartList, // 查询购物车列表
  76. exportLiveCartList, // 导出购物车列表
  77. liveCartDetails, //获取购物车详情
  78. // addLiveCart, //新增购物车
  79. modifyLiveCart, //修改购物车
  80. delLiveCart, // 删除购物车
  81. liveOrderKey // 生成订单key
  82. } from "@/api/order.js"
  83. // import {getCarts,cartNum,delCart} from '@/api/product'
  84. // import likeProduct from './components/likeProduct.vue'
  85. export default {
  86. // components: {
  87. // likeProduct
  88. // },
  89. data() {
  90. return {
  91. shopList: [],
  92. totalMoney: 0.00,
  93. // carts: [],
  94. checkAll: false,
  95. }
  96. },
  97. onLoad() {
  98. // this.getCarts();
  99. },
  100. onReachBottom() {
  101. // this.$refs.product.getGoodsProducts();
  102. },
  103. mounted() {
  104. this.queryLiveCart();
  105. this.computedMoney();
  106. },
  107. methods: {
  108. // 获得key
  109. getKey() {
  110. liveOrderKey().then(res => {
  111. if (res.code == 200) {
  112. console.log("下订单的key>>>>", res)
  113. this.orderKey = res.orderKey
  114. console.log("key>>>>", this.orderKey)
  115. uni.navigateTo({
  116. url: '/pages_shop/confirmCreateOrder?type=cart&orderKey=' + this.orderKey
  117. });
  118. } else {
  119. uni.showToast({
  120. title: res.msg,
  121. icon: 'none'
  122. });
  123. }
  124. },
  125. rej => {}
  126. );
  127. },
  128. // 修改购物车
  129. modifyLiveCart(item) {
  130. let data = {
  131. checked: item.checked == true ? 1 : 0,
  132. cartId: item.cartId,
  133. cartNum: item.cartNum
  134. };
  135. modifyLiveCart(data).then(
  136. res => {
  137. if (res.code == 200) {
  138. this.computedMoney();
  139. } else {
  140. uni.showToast({
  141. icon: 'none',
  142. title: res.msg,
  143. });
  144. }
  145. },
  146. rej => {}
  147. );
  148. },
  149. // modifyLiveCartList() {
  150. // modifyLiveCart().then(
  151. // res => {
  152. // if (res.code == 200) {
  153. // this.shopList = res.rows;
  154. // console.log(this.shopList)
  155. // console.log(res.rows)
  156. // this.shopList.forEach(item => {
  157. // item.checked == 1 ? true : false
  158. // });
  159. // // this.computedMoney();
  160. // } else {
  161. // uni.showToast({
  162. // icon: 'none',
  163. // title: "请求失败",
  164. // });
  165. // }
  166. // },
  167. // rej => {}
  168. // );
  169. // },
  170. // 查询购物车列表
  171. queryLiveCart() {
  172. queryLiveCartList().then(
  173. res => {
  174. if (res.code == 200) {
  175. this.shopList = res.rows;
  176. this.shopList.forEach(item => {
  177. item.checked = item.checked == 1 ? true : false; // 这里改为赋值
  178. });
  179. this.computedMoney(); // 数据加载完成后再计算价格
  180. } else {
  181. uni.showToast({
  182. icon: 'none',
  183. title: "请求失败",
  184. });
  185. }
  186. },
  187. rej => {}
  188. );
  189. },
  190. // 删除购物车
  191. delCart() {
  192. // 直接过滤已选商品
  193. const data = this.shopList
  194. .filter(item => item.checked)
  195. .map(item => item.cartId);
  196. if (data.length === 0) {
  197. uni.showToast({
  198. icon: 'none',
  199. title: "请选择商品删除"
  200. });
  201. return;
  202. }
  203. delLiveCart(data).then(res => {
  204. if (res.code === 200) {
  205. uni.showToast({
  206. icon: 'success',
  207. title: "操作成功"
  208. });
  209. this.queryLiveCart(); // 重新加载数据
  210. } else {
  211. uni.showToast({
  212. icon: 'none',
  213. title: res.msg
  214. });
  215. }
  216. });
  217. },
  218. // 计算价格
  219. computedMoney() {
  220. this.totalMoney = this.shopList.reduce((total, item) => {
  221. return total + (item.checked ? item.price * item.cartNum : 0);
  222. }, 0);
  223. },
  224. // 全选
  225. handleCheckAll() {
  226. this.checkAll = !this.checkAll;
  227. this.shopList.forEach(item => {
  228. item.checked = this.checkAll;
  229. this.modifyLiveCart(item);
  230. });
  231. this.computedMoney(); // 全选后重新计算
  232. },
  233. //选择
  234. checkChange(item, shop) {
  235. item.checked = !item.checked;
  236. this.computedMoney(); // 这里去掉参数
  237. this.modifyLiveCart(item)
  238. },
  239. // 改数量
  240. changeNum(e, item) {
  241. item.cartNum = e.detail.value.replace(/\D/g, '')
  242. if (item.cartNum <= 1) {
  243. uni.showToast({
  244. title: "已经是底线啦!",
  245. icon: "none",
  246. duration: 2000
  247. });
  248. return;
  249. }
  250. if (item.cartNum < 1) {
  251. item.cartNum = 1
  252. }
  253. if (item.cartNum >= item.stock) {
  254. item.cartNum = item.stock;
  255. }
  256. this.modifyLiveCart(item)
  257. },
  258. // 购物车减法
  259. delNum(item) {
  260. if (item.cartNum <= 1) {
  261. uni.showToast({
  262. title: "已经是底线啦!",
  263. icon: "none",
  264. duration: 2000
  265. });
  266. return;
  267. }
  268. item.cartNum--
  269. if (item.cartNum < 1) {
  270. item.cartNum = 1
  271. }
  272. this.modifyLiveCart(item)
  273. },
  274. // 购物车加法
  275. addNum(item) {
  276. item.cartNum++
  277. if (item.cartNum >= item.stock) {
  278. item.cartNum = item.stock;
  279. }
  280. this.modifyLiveCart(item)
  281. },
  282. // 结算
  283. submit() {
  284. const selectedItems = this.shopList.filter(item => item.checked);
  285. if (selectedItems.length === 0) {
  286. uni.showToast({
  287. icon: 'none',
  288. title: "请选择商品"
  289. });
  290. return;
  291. }
  292. this.getKey()
  293. },
  294. showProduct(item) {
  295. uni.navigateTo({
  296. url: './goods?productId=' + item.productId
  297. })
  298. },
  299. }
  300. }
  301. </script>
  302. <style lang="scss">
  303. page {
  304. height: 100%;
  305. }
  306. .content {
  307. height: 100%;
  308. padding: 20upx;
  309. .shopbox {
  310. background: #FFFFFF;
  311. border-radius: 16rpx;
  312. }
  313. .no-data-box {
  314. display: flex;
  315. flex-direction: column;
  316. align-items: center;
  317. }
  318. .shopbox-name {
  319. padding: 30rpx 30rpx 0 30rpx;
  320. font-family: PingFang SC, PingFang SC;
  321. font-weight: 400;
  322. font-size: 30rpx;
  323. color: #111;
  324. overflow: hidden;
  325. white-space: nowrap;
  326. text-overflow: ellipsis;
  327. }
  328. .goods-list {
  329. .item {
  330. box-sizing: border-box;
  331. height: 221upx;
  332. background: #FFFFFF;
  333. border-radius: 16upx;
  334. margin-bottom: 20upx;
  335. padding: 30upx;
  336. display: flex;
  337. align-items: center;
  338. &:last-child {
  339. margin-bottom: 0;
  340. }
  341. .goods-img {
  342. width: 160upx;
  343. height: 160upx;
  344. background: #FFFFFF;
  345. margin-right: 30upx;
  346. flex-shrink: 0;
  347. }
  348. .info-box {
  349. height: 160upx;
  350. display: flex;
  351. flex-direction: column;
  352. justify-content: space-between;
  353. width: calc(100% - 255upx);
  354. .title-box {
  355. width: 100%;
  356. display: flex;
  357. align-items: center;
  358. .tag {
  359. padding: 0 6upx;
  360. height: 30upx;
  361. line-height: 30upx;
  362. font-size: 22upx;
  363. font-family: PingFang SC;
  364. font-weight: bold;
  365. color: #FFFFFF;
  366. background: linear-gradient(90deg, #66b2ef 0%, #0bb3f2 100%);
  367. border-radius: 4upx;
  368. margin-right: 10upx;
  369. flex-shrink: 0;
  370. }
  371. .title {
  372. flex: 1;
  373. font-size: 28upx;
  374. font-family: PingFang SC;
  375. font-weight: 500;
  376. color: #111111;
  377. line-height: 1;
  378. }
  379. }
  380. .intro {
  381. font-size: 24upx;
  382. font-family: PingFang SC;
  383. font-weight: 500;
  384. color: #999999;
  385. margin-top: 22upx;
  386. line-height: 1;
  387. }
  388. .price-num {
  389. display: flex;
  390. align-items: center;
  391. justify-content: space-between;
  392. .price {
  393. display: flex;
  394. align-items: flex-end;
  395. .unit {
  396. font-size: 24upx;
  397. font-family: PingFang SC;
  398. font-weight: 500;
  399. color: #FF6633;
  400. line-height: 1.2;
  401. margin-right: 4upx;
  402. }
  403. .text {
  404. font-size: 32upx;
  405. font-family: PingFang SC;
  406. font-weight: bold;
  407. color: #FF6633;
  408. line-height: 1;
  409. }
  410. }
  411. .num-box {
  412. display: flex;
  413. align-items: center;
  414. .img-box {
  415. width: 60upx;
  416. height: 60upx;
  417. // border-radius: 4upx;
  418. border: 1px solid #dddddd;
  419. display: flex;
  420. align-items: center;
  421. justify-content: center;
  422. image {
  423. width: 25rpx;
  424. height: 25rpx;
  425. }
  426. }
  427. input {
  428. width: 60upx;
  429. height: 60upx;
  430. line-height: 60upx;
  431. font-size: 28upx;
  432. font-family: PingFang SC;
  433. font-weight: 500;
  434. color: #111111;
  435. // border-radius: 4upx;
  436. border-top: 1px solid #dddddd;
  437. border-bottom: 1px solid #dddddd;
  438. text-align: center;
  439. // margin: 0 16upx;
  440. }
  441. }
  442. }
  443. }
  444. }
  445. }
  446. .like-product {
  447. padding-bottom: 120upx;
  448. }
  449. .btn-foot {
  450. box-sizing: border-box;
  451. width: 100%;
  452. height: 121upx;
  453. background: #FFFFFF;
  454. padding: 16upx 30upx 16upx 60upx;
  455. display: flex;
  456. align-items: center;
  457. justify-content: space-between;
  458. position: fixed;
  459. left: 0;
  460. bottom: 0;
  461. z-index: 99;
  462. .left {
  463. display: flex;
  464. align-items: center;
  465. .text {
  466. margin-left: 14upx;
  467. font-size: 28upx;
  468. font-family: PingFang SC;
  469. font-weight: 500;
  470. color: #666666;
  471. line-height: 1;
  472. }
  473. }
  474. .right {
  475. display: flex;
  476. align-items: center;
  477. .total {
  478. display: flex;
  479. align-items: flex-end;
  480. margin-right: 36upx;
  481. .label {
  482. font-size: 26upx;
  483. font-family: PingFang SC;
  484. font-weight: 500;
  485. color: #999999;
  486. line-height: 1.5;
  487. }
  488. .price {
  489. display: flex;
  490. align-items: flex-end;
  491. .unit {
  492. font-size: 32upx;
  493. font-family: PingFang SC;
  494. font-weight: bold;
  495. color: #FF6633;
  496. line-height: 1.2;
  497. margin-right: 10upx;
  498. }
  499. .num {
  500. font-size: 30upx;
  501. font-family: PingFang SC;
  502. font-weight: bold;
  503. color: #FF6633;
  504. line-height: 1;
  505. }
  506. }
  507. }
  508. .btn {
  509. width: 200upx;
  510. height: 88upx;
  511. line-height: 88upx;
  512. text-align: center;
  513. font-size: 30upx;
  514. font-family: PingFang SC;
  515. font-weight: bold;
  516. color: #FFFFFF;
  517. background: #0bb3f2;
  518. border-radius: 44upx;
  519. }
  520. }
  521. }
  522. }
  523. </style>