cart.vue 9.8 KB

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