cart.vue 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <template>
  2. <view class="cart-page">
  3. <!-- 自定义头部 -->
  4. <view class="custom-header" :style="{paddingTop: statusBarHeight + 'px'}">
  5. <view class="header-content">
  6. <view class="back-btn" @click="goBack">
  7. <u-icon name="arrow-left" color="#fff" size="20"></u-icon>
  8. </view>
  9. <text class="title">购物车</text>
  10. <view class="placeholder"></view>
  11. </view>
  12. </view>
  13. <scroll-view scroll-y class="cart-scroll" :style="{marginTop: (statusBarHeight + 44) + 'px'}">
  14. <view class="cart-list">
  15. <view v-if="carts.length === 0" class="empty-cart">
  16. <image src="/static/images/empty.png" mode="aspectFit"
  17. style="width: 300rpx; height: 300rpx;"></image>
  18. <text>购物车是空的</text>
  19. <button class="go-btn" @click="goHome">去逛逛</button>
  20. </view>
  21. <view v-else class="cart-item" v-for="(item, index) in carts" :key="index">
  22. <view class="checkbox">
  23. <u-checkbox :name="item.id" :checked="item.checked" shape="circle" activeColor="#2583EB"
  24. @change="checkChange(item)"></u-checkbox>
  25. </view>
  26. <image :src="item.productAttrImage || item.productImage" mode="aspectFill" class="goods-img">
  27. </image>
  28. <view class="item-right">
  29. <text class="title ellipsis2">{{item.productName}}</text>
  30. <text class="attr">{{item.productAttrName}}</text>
  31. <view class="price-box">
  32. <text class="price">¥{{item.price}}</text>
  33. <view class="number-box">
  34. <view class="icon-btn" @click="delNum(item)">
  35. <u-icon name="minus" size="12" color="#333"></u-icon>
  36. </view>
  37. <input class="num-input" type="number" v-model="item.cartNum"
  38. @blur="changeNum($event, item)" />
  39. <view class="icon-btn" @click="addNum(item)">
  40. <u-icon name="plus" size="12" color="#333"></u-icon>
  41. </view>
  42. </view>
  43. </view>
  44. </view>
  45. </view>
  46. </view>
  47. </scroll-view>
  48. <view class="action-section" v-if="carts.length > 0">
  49. <view class="left-action">
  50. <u-checkbox :checked="checkAll" shape="circle" activeColor="#2583EB"
  51. @change="handleCheckAll">全选</u-checkbox>
  52. <text class="del-btn" @click="delCart">删除</text>
  53. </view>
  54. <view class="right-action">
  55. <view class="total-box">
  56. <text class="label">合计:</text>
  57. <text class="price">¥{{totalMoney.toFixed(2)}}</text>
  58. </view>
  59. <button class="confirm-btn" @click="submit">结算</button>
  60. </view>
  61. </view>
  62. </view>
  63. </template>
  64. <script>
  65. import {
  66. getCarts,
  67. cartNum,
  68. delCart
  69. } from '@/api/product'
  70. export default {
  71. data() {
  72. return {
  73. statusBarHeight: 20,
  74. totalMoney: 0.00,
  75. carts: [],
  76. checkAll: false,
  77. }
  78. },
  79. created() {
  80. this.statusBarHeight = uni.getSystemInfoSync().statusBarHeight;
  81. this.getCarts();
  82. },
  83. methods: {
  84. goBack() {
  85. uni.showTabBar()
  86. uni.switchTab({
  87. url: '/pages_im/pages/conversation/conversationList/index'
  88. })
  89. },
  90. goHome() {
  91. uni.switchTab({
  92. url: '/pages_mall/index'
  93. });
  94. },
  95. // 获取购物车列表
  96. getCarts() {
  97. getCarts().then(res => {
  98. if (res.code == 200) {
  99. this.carts = res.carts || [];
  100. this.carts.forEach(item => {
  101. // 确保checked属性存在
  102. if (typeof item.checked === 'undefined') {
  103. this.$set(item, 'checked', false);
  104. }
  105. });
  106. this.computedMoney();
  107. } else {
  108. uni.showToast({
  109. icon: 'none',
  110. title: "请求失败",
  111. });
  112. }
  113. });
  114. },
  115. // 计算总价
  116. computedMoney() {
  117. let money = 0;
  118. this.carts.forEach(item => {
  119. if (item.checked) {
  120. money += item.price * item.cartNum;
  121. }
  122. });
  123. this.totalMoney = money;
  124. // 更新全选状态
  125. this.checkAll = this.carts.length > 0 && this.carts.every(item => item.checked);
  126. },
  127. // 单选
  128. checkChange(item) {
  129. item.checked = !item.checked;
  130. this.computedMoney();
  131. },
  132. // 全选
  133. handleCheckAll() {
  134. this.checkAll = !this.checkAll;
  135. this.carts.forEach(item => {
  136. item.checked = this.checkAll;
  137. });
  138. this.computedMoney();
  139. },
  140. // 修改数量接口
  141. changeCartNum(item) {
  142. let data = {
  143. number: item.cartNum,
  144. id: item.id
  145. };
  146. cartNum(data).then(res => {
  147. if (res.code == 200) {
  148. this.computedMoney();
  149. } else {
  150. uni.showToast({
  151. icon: 'none',
  152. title: res.msg || "操作失败",
  153. });
  154. }
  155. });
  156. },
  157. // 减少数量
  158. delNum(item) {
  159. if (item.cartNum <= 1) {
  160. uni.showToast({
  161. title: "已经是底线啦!",
  162. icon: "none"
  163. });
  164. return;
  165. }
  166. item.cartNum--;
  167. this.changeCartNum(item);
  168. },
  169. // 增加数量
  170. addNum(item) {
  171. if (item.stock && item.cartNum >= item.stock) {
  172. item.cartNum = item.stock;
  173. uni.showToast({
  174. title: "库存不足",
  175. icon: "none"
  176. });
  177. return;
  178. }
  179. item.cartNum++;
  180. this.changeCartNum(item);
  181. },
  182. // 输入框修改数量
  183. changeNum(e, item) {
  184. let val = parseInt(e.detail.value);
  185. if (!val || val < 1) val = 1;
  186. if (item.stock && val > item.stock) val = item.stock;
  187. item.cartNum = val;
  188. this.changeCartNum(item);
  189. },
  190. // 删除购物车
  191. delCart() {
  192. let selectCarts = this.carts.filter(ele => ele.checked).map(ele => ele.id);
  193. if (selectCarts.length == 0) {
  194. uni.showToast({
  195. icon: 'none',
  196. title: "请选择商品删除",
  197. });
  198. return;
  199. }
  200. uni.showModal({
  201. title: '提示',
  202. content: '确定要删除选中的商品吗?',
  203. success: (res) => {
  204. if (res.confirm) {
  205. delCart({
  206. ids: selectCarts
  207. }).then(res => {
  208. if (res.code == 200) {
  209. uni.showToast({
  210. title: "删除成功"
  211. });
  212. this.getCarts();
  213. this.checkAll = false;
  214. } else {
  215. uni.showToast({
  216. icon: 'none',
  217. title: res.msg
  218. });
  219. }
  220. });
  221. }
  222. }
  223. });
  224. },
  225. // 结算
  226. submit() {
  227. let selectCarts = this.carts.filter(ele => ele.checked).map(ele => ele.id);
  228. if (selectCarts.length == 0) {
  229. uni.showToast({
  230. icon: 'none',
  231. title: "请选择商品",
  232. });
  233. return;
  234. }
  235. uni.navigateTo({
  236. url: '/pages_mall/confirmOrder?type=cart&cartIds=' + selectCarts.toString()
  237. });
  238. }
  239. }
  240. }
  241. </script>
  242. <style scoped lang="scss">
  243. .cart-page {
  244. height: 100%;
  245. display: flex;
  246. flex-direction: column;
  247. background-color: #F5F7FA;
  248. }
  249. .custom-header {
  250. background: linear-gradient(to right, #2583EB, #4FACFE);
  251. position: fixed;
  252. top: 0;
  253. left: 0;
  254. right: 0;
  255. z-index: 10000;
  256. .header-content {
  257. height: 44px;
  258. display: flex;
  259. align-items: center;
  260. justify-content: space-between;
  261. padding: 0 30rpx;
  262. .title {
  263. color: #fff;
  264. font-size: 36rpx;
  265. font-weight: bold;
  266. }
  267. .back-btn,
  268. .placeholder {
  269. width: 40rpx;
  270. height: 40rpx;
  271. display: flex;
  272. align-items: center;
  273. justify-content: center;
  274. }
  275. }
  276. }
  277. .cart-scroll {
  278. flex: 1;
  279. height: 0;
  280. padding-bottom: 120rpx; // Space for action bar
  281. }
  282. .empty-cart {
  283. padding-top: 100rpx;
  284. display: flex;
  285. flex-direction: column;
  286. align-items: center;
  287. text {
  288. color: #999;
  289. font-size: 30rpx;
  290. margin-bottom: 30rpx;
  291. margin-top: 20rpx;
  292. }
  293. .go-btn {
  294. width: 240rpx;
  295. height: 80rpx;
  296. line-height: 80rpx;
  297. background: linear-gradient(to right, #2583EB, #4FACFE);
  298. color: #fff;
  299. font-size: 30rpx;
  300. border-radius: 40rpx;
  301. box-shadow: 0 8rpx 16rpx rgba(37, 131, 235, 0.3);
  302. }
  303. }
  304. .cart-item {
  305. display: flex;
  306. padding: 30rpx;
  307. background: #fff;
  308. margin: 20rpx;
  309. border-radius: 20rpx;
  310. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.04);
  311. align-items: center;
  312. .checkbox {
  313. margin-right: 20rpx;
  314. }
  315. .goods-img {
  316. width: 180rpx;
  317. height: 180rpx;
  318. border-radius: 12rpx;
  319. background: #eee;
  320. flex-shrink: 0;
  321. }
  322. .item-right {
  323. flex: 1;
  324. display: flex;
  325. flex-direction: column;
  326. padding-left: 24rpx;
  327. height: 180rpx;
  328. justify-content: space-between;
  329. overflow: hidden;
  330. .title {
  331. font-size: 30rpx;
  332. color: #333;
  333. line-height: 1.4;
  334. font-weight: 500;
  335. }
  336. .attr {
  337. font-size: 24rpx;
  338. color: #999;
  339. background: #f8f8f8;
  340. padding: 4rpx 12rpx;
  341. border-radius: 6rpx;
  342. align-self: flex-start;
  343. margin-top: 8rpx;
  344. }
  345. .price-box {
  346. display: flex;
  347. justify-content: space-between;
  348. align-items: center;
  349. .price {
  350. font-size: 34rpx;
  351. color: #FF5000;
  352. font-weight: bold;
  353. }
  354. .number-box {
  355. display: flex;
  356. align-items: center;
  357. background: #f5f5f5;
  358. border-radius: 8rpx;
  359. .icon-btn {
  360. width: 50rpx;
  361. height: 50rpx;
  362. display: flex;
  363. align-items: center;
  364. justify-content: center;
  365. }
  366. .num-input {
  367. width: 70rpx;
  368. text-align: center;
  369. font-size: 28rpx;
  370. color: #333;
  371. }
  372. }
  373. }
  374. }
  375. }
  376. .action-section {
  377. position: fixed;
  378. bottom: 100rpx; // Above TabBar (which is 100rpx height)
  379. left: 0;
  380. right: 0;
  381. height: 100rpx;
  382. background: #fff;
  383. display: flex;
  384. align-items: center;
  385. justify-content: space-between;
  386. padding: 0 30rpx;
  387. box-shadow: 0 -4rpx 16rpx rgba(0, 0, 0, 0.05);
  388. z-index: 999;
  389. .left-action {
  390. display: flex;
  391. align-items: center;
  392. .del-btn {
  393. margin-left: 30rpx;
  394. color: #FF5000;
  395. font-size: 28rpx;
  396. }
  397. }
  398. .right-action {
  399. display: flex;
  400. align-items: center;
  401. .total-box {
  402. margin-right: 20rpx;
  403. .label {
  404. font-size: 28rpx;
  405. color: #333;
  406. }
  407. .price {
  408. font-size: 32rpx;
  409. color: #FF5000;
  410. font-weight: bold;
  411. }
  412. }
  413. .confirm-btn {
  414. margin: 0;
  415. background: linear-gradient(to right, #2583EB, #4FACFE);
  416. color: #fff;
  417. font-size: 28rpx;
  418. border-radius: 40rpx;
  419. padding: 0 40rpx;
  420. height: 70rpx;
  421. line-height: 70rpx;
  422. }
  423. }
  424. }
  425. .ellipsis2 {
  426. overflow: hidden;
  427. text-overflow: ellipsis;
  428. display: -webkit-box;
  429. -webkit-line-clamp: 2;
  430. -webkit-box-orient: vertical;
  431. }
  432. </style>