cart.vue 12 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">
  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 v-if="shopList.length == 0" class="no-data-box">
  47. <image src="/static/images/no_data.png" mode="aspectFit"></image>
  48. <view class="empty-title">暂无数据</view>
  49. </view>
  50. <!-- 底部按钮 -->
  51. <view class="btn-foot">
  52. <view class="left">
  53. <label>
  54. <checkbox :checked="checkAll" @click="handleCheckAll()" />
  55. </label>
  56. <text class="text">全选</text>
  57. <text class="text" @click="delCart()">删除</text>
  58. </view>
  59. <view class="right">
  60. <view class="total">
  61. <text class="label">合计:</text>
  62. <view class="price">
  63. <text class="unit">¥</text>
  64. <text class="num">{{totalMoney.toFixed(2)}}</text>
  65. </view>
  66. </view>
  67. <view class="btn" @click="submit">结算</view>
  68. </view>
  69. </view>
  70. </view>
  71. </template>
  72. <script>
  73. import {
  74. queryLiveCartList, // 查询购物车列表
  75. exportLiveCartList, // 导出购物车列表
  76. liveCartDetails, //获取购物车详情
  77. // addLiveCart, //新增购物车
  78. modifyLiveCart, //修改购物车
  79. delLiveCart, // 删除购物车
  80. liveOrderKey // 生成订单key
  81. } from "@/api/order.js"
  82. // import {getCarts,cartNum,delCart} from '@/api/product'
  83. // import likeProduct from './components/likeProduct.vue'
  84. export default {
  85. // components: {
  86. // likeProduct
  87. // },
  88. data() {
  89. return {
  90. shopList: [],
  91. totalMoney: 0.00,
  92. // carts: [],
  93. checkAll: false,
  94. }
  95. },
  96. onLoad() {
  97. // this.getCarts();
  98. },
  99. onReachBottom() {
  100. // this.$refs.product.getGoodsProducts();
  101. },
  102. mounted() {
  103. this.queryLiveCart();
  104. },
  105. methods: {
  106. // 获得key
  107. getKey() {
  108. liveOrderKey().then(res => {
  109. if (res.code == 200) {
  110. console.log("下订单的key>>>>", res)
  111. this.orderKey = res.orderKey
  112. console.log("key>>>>", this.orderKey)
  113. uni.navigateTo({
  114. url: '/pages_shop/confirmCreateOrder?type=cart&orderKey=' + this.orderKey
  115. });
  116. } else {
  117. uni.showToast({
  118. title: res.msg,
  119. icon: 'none'
  120. });
  121. }
  122. },
  123. rej => {}
  124. );
  125. },
  126. // 修改购物车
  127. modifyLiveCart(item) {
  128. let data = {
  129. checked: item.checked,
  130. cartId: item.cartId,
  131. cartNum: item.cartNum
  132. };
  133. modifyLiveCart(data).then(
  134. res => {
  135. if (res.code == 200) {
  136. this.computedMoney();
  137. } else {
  138. uni.showToast({
  139. icon: 'none',
  140. title: res.msg,
  141. });
  142. }
  143. },
  144. rej => {}
  145. );
  146. },
  147. // modifyLiveCartList() {
  148. // modifyLiveCart().then(
  149. // res => {
  150. // if (res.code == 200) {
  151. // this.shopList = res.rows;
  152. // console.log(this.shopList)
  153. // console.log(res.rows)
  154. // this.shopList.forEach(item => {
  155. // item.checked == 1 ? true : false
  156. // });
  157. // // this.computedMoney();
  158. // } else {
  159. // uni.showToast({
  160. // icon: 'none',
  161. // title: "请求失败",
  162. // });
  163. // }
  164. // },
  165. // rej => {}
  166. // );
  167. // },
  168. // 查询购物车列表
  169. queryLiveCart() {
  170. queryLiveCartList().then(
  171. res => {
  172. if (res.code == 200) {
  173. this.shopList = res.rows;
  174. this.shopList.forEach(item => {
  175. item.checked == 1 ? true : false
  176. });
  177. // this.computedMoney();
  178. } else {
  179. uni.showToast({
  180. icon: 'none',
  181. title: "请求失败",
  182. });
  183. }
  184. },
  185. rej => {}
  186. );
  187. },
  188. // 删除购物车
  189. delCart() {
  190. // 直接过滤已选商品
  191. const data = this.shopList
  192. .filter(item => item.checked)
  193. .map(item => item.cartId);
  194. if (data.length === 0) {
  195. uni.showToast({
  196. icon: 'none',
  197. title: "请选择商品删除"
  198. });
  199. return;
  200. }
  201. delLiveCart(data).then(res => {
  202. if (res.code === 200) {
  203. uni.showToast({
  204. icon: 'success',
  205. title: "操作成功"
  206. });
  207. this.queryLiveCart(); // 重新加载数据
  208. } else {
  209. uni.showToast({
  210. icon: 'none',
  211. title: res.msg
  212. });
  213. }
  214. });
  215. },
  216. // 计算价格
  217. computedMoney() {
  218. this.totalMoney = this.shopList.reduce((total, item) => {
  219. return total + (item.checked ? item.price * item.cartNum : 0);
  220. }, 0);
  221. },
  222. // 全选
  223. handleCheckAll() {
  224. this.checkAll = !this.checkAll;
  225. this.shopList.forEach(item => {
  226. item.checked = this.checkAll; // 直接操作商品
  227. });
  228. this.computedMoney();
  229. },
  230. //选择
  231. checkChange(item, shop) {
  232. item.checked = !item.checked;
  233. // console.log(item.checked)
  234. this.computedMoney(item.checked);
  235. },
  236. // 改数量
  237. changeNum(e, item) {
  238. item.cartNum = e.detail.value.replace(/\D/g, '')
  239. if (item.cartNum <= 1) {
  240. uni.showToast({
  241. title: "已经是底线啦!",
  242. icon: "none",
  243. duration: 2000
  244. });
  245. return;
  246. }
  247. if (item.cartNum < 1) {
  248. item.cartNum = 1
  249. }
  250. if (item.cartNum >= item.stock) {
  251. item.cartNum = item.stock;
  252. }
  253. this.modifyLiveCart(item)
  254. },
  255. // 购物车减法
  256. delNum(item) {
  257. if (item.cartNum <= 1) {
  258. uni.showToast({
  259. title: "已经是底线啦!",
  260. icon: "none",
  261. duration: 2000
  262. });
  263. return;
  264. }
  265. item.cartNum--
  266. if (item.cartNum < 1) {
  267. item.cartNum = 1
  268. }
  269. this.modifyLiveCart(item)
  270. },
  271. // 购物车加法
  272. addNum(item) {
  273. // console.log(item)
  274. item.cartNum++
  275. if (item.cartNum >= item.stock) {
  276. item.cartNum = item.stock;
  277. }
  278. this.modifyLiveCart(item)
  279. },
  280. // 结算
  281. submit() {
  282. const selectedItems = this.shopList.filter(item => item.checked);
  283. if (selectedItems.length === 0) {
  284. uni.showToast({
  285. icon: 'none',
  286. title: "请选择商品"
  287. });
  288. return;
  289. }
  290. this.getKey()
  291. },
  292. showProduct(item) {
  293. uni.navigateTo({
  294. url: './goods?productId=' + item.productId
  295. })
  296. },
  297. }
  298. }
  299. </script>
  300. <style lang="scss">
  301. page {
  302. height: 100%;
  303. }
  304. .content {
  305. height: 100%;
  306. padding: 20upx;
  307. .shopbox {
  308. background: #FFFFFF;
  309. border-radius: 16rpx;
  310. margin-bottom: 20rpx;
  311. }
  312. .no-data-box {
  313. display: flex;
  314. flex-direction: column;
  315. align-items: center;
  316. }
  317. .shopbox-name {
  318. padding: 30rpx 30rpx 0 30rpx;
  319. font-family: PingFang SC, PingFang SC;
  320. font-weight: 400;
  321. font-size: 30rpx;
  322. color: #111;
  323. overflow: hidden;
  324. white-space: nowrap;
  325. text-overflow: ellipsis;
  326. }
  327. .goods-list {
  328. padding-bottom: 140rpx;
  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>