index.vue 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. <template>
  2. <view class="cart-page">
  3. <!-- 购物车列表 -->
  4. <view class="cart-list">
  5. <u-swipe-action :show="item.show" :index="index" v-for="(item, index) in cartList" :key="item.id"
  6. @click="clickAction" @open="openAction" :options="actionOptions">
  7. <cart-item :item="item" @check="handleCheck" @changeNum="handleChangeNum" @reduce="handleReduce"
  8. @selectCondition="onSelectCondition"></cart-item>
  9. </u-swipe-action>
  10. </view>
  11. <!-- 品相切换弹窗 -->
  12. <condition-popup ref="conditionPopup" @select="handleConditionUpdate"></condition-popup>
  13. <!-- 空状态 -->
  14. <view class="empty-cart" v-if="cartList.length === 0 && !loading" style="margin:15% 0">
  15. <u-empty text="购物车空空如也" mode="car"></u-empty>
  16. </view>
  17. <!-- 为你推荐 -->
  18. <view class="recommend-section">
  19. <view class="section-title">
  20. <text class="line"></text>
  21. <text class="text">为你推荐</text>
  22. <text class="line"></text>
  23. </view>
  24. <view class="recommend-grid">
  25. <view class="grid-item" v-for="(item, index) in recommendList" :key="index">
  26. <image :src="item.cover" mode="aspectFill" class="cover"></image>
  27. <view class="title">{{ item.title }}</view>
  28. </view>
  29. </view>
  30. </view>
  31. <!-- 底部占位 -->
  32. <view style="height: 120rpx;"></view>
  33. <!-- 底部结算栏 -->
  34. <view class="bottom-fixed" v-if="cartList.length > 0">
  35. <view class="left-part">
  36. <view class="checkbox-wrap" @click="toggleSelectAll">
  37. <u-checkbox v-model="isAllSelected" shape="circle" active-color="#38C148" :disabled="false"
  38. @change="onAllCheckChange">全选</u-checkbox>
  39. </view>
  40. <!-- 清空按钮 -->
  41. <view class="clear-btn" @click="handleClearCart"
  42. style="margin-left: 20rpx; font-size: 24rpx; color: #999;">
  43. 清空
  44. </view>
  45. </view>
  46. <view class="right-part">
  47. <view class="total-info">
  48. <view class="reduced-tip" v-if="totalReduced > 0">已降 ¥{{ totalReduced }}</view>
  49. <view class="total-price">
  50. <text class="label">合计:</text>
  51. <text class="price">¥{{ totalPrice }}</text>
  52. </view>
  53. </view>
  54. <view class="checkout-btn" @click="checkout">
  55. 结算({{ selectedCount }})
  56. </view>
  57. </view>
  58. </view>
  59. <!-- 减钱弹窗 -->
  60. <price-reduction-popup ref="reducePopup" @share="handleShare" @scan="handleScan"></price-reduction-popup>
  61. <common-dialog ref="reduceDialog" title="温馨提示" @confirm="onNext">
  62. <text>购物车中有可减钱的商品,如您提交订单,则失去该资格哦~</text>
  63. </common-dialog>
  64. </view>
  65. </template>
  66. <script>
  67. import CartItem from '../components/cart-item.vue';
  68. import PriceReductionPopup from '../components/price-reduction-popup.vue';
  69. import CommonDialog from '@/components/common-dialog.vue';
  70. import ConditionPopup from '../components/condition-popup.vue';
  71. export default {
  72. components: {
  73. CartItem,
  74. PriceReductionPopup,
  75. CommonDialog,
  76. ConditionPopup
  77. },
  78. data() {
  79. return {
  80. loading: true,
  81. currentItem: null,
  82. isAllSelected: false,
  83. cartList: [],
  84. actionOptions: [
  85. {
  86. text: '删除',
  87. style: {
  88. backgroundColor: '#fa3534'
  89. }
  90. }
  91. ],
  92. recommendList: [
  93. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  94. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  95. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  96. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  97. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' },
  98. { title: '工程数学线性代数第六版', cover: 'https://k.sinaimg.cn/n/sinakd20116/234/w1000h1634/20251003/b66b-587c9ff400fcf01be52c6693594b6a6d.jpg/w700d1q75cms.jpg' }
  99. ]
  100. };
  101. },
  102. onShow() {
  103. this.loadData();
  104. },
  105. mounted() {
  106. this.loadData();
  107. },
  108. computed: {
  109. selectedItems() {
  110. return this.cartList.filter(item => item.checked && item.stockStatus !== 3 && item.availableStock > 0);
  111. },
  112. totalPrice() {
  113. return this.selectedItems.reduce((sum, item) => sum + item.productPrice * item.quantity, 0).toFixed(2);
  114. },
  115. totalReduced() {
  116. return this.selectedItems.reduce((sum, item) => sum + (item.currReduceMoney || 0) * item.quantity, 0).toFixed(2);
  117. },
  118. selectedCount() {
  119. return this.selectedItems.reduce((sum, item) => sum + item.quantity, 0);
  120. }
  121. },
  122. watch: {
  123. cartList: {
  124. handler(val) {
  125. const validItems = val.filter(item => item.stockStatus !== 3 && item.availableStock > 0);
  126. if (validItems.length === 0) {
  127. this.isAllSelected = false;
  128. return;
  129. }
  130. this.isAllSelected = validItems.every(item => item.checked);
  131. },
  132. deep: true
  133. }
  134. },
  135. methods: {
  136. clickAction(index, index1) {
  137. if (this.actionOptions[index1].text == '删除') {
  138. const item = this.cartList[index];
  139. this.handleDelete(item);
  140. }
  141. },
  142. openAction(index) {
  143. this.cartList[index].show = true;
  144. this.cartList.map((val, idx) => {
  145. if (index != idx) this.cartList[idx].show = false;
  146. })
  147. },
  148. loadData() {
  149. this.loading = true;
  150. this.$u.api.getShopCartListAjax({}).then(res => {
  151. this.loading = false;
  152. // 本地添加选中状态属性
  153. const list = res.rows || [];
  154. // 如果可能,与现有的选中状态合并,或者重置?
  155. // 为简单起见,如果 id 匹配,则重置或保持选中状态。
  156. // 或者直接初始化 checked=false。
  157. this.cartList = list.map(item => {
  158. return {
  159. ...item,
  160. checked: false,
  161. show: false
  162. };
  163. });
  164. }).catch(() => {
  165. this.loading = false;
  166. });
  167. },
  168. handleCheck({ id, checked }) {
  169. const item = this.cartList.find(i => i.id === id);
  170. if (item) {
  171. item.checked = checked;
  172. }
  173. },
  174. handleChangeNum({ id, num }) {
  175. const item = this.cartList.find(i => i.id === id);
  176. if (item) {
  177. // 乐观更新还是等待服务器?
  178. // 通常是服务器优先。
  179. this.$u.api.updateCartNumAjax({
  180. id: id,
  181. quantity: num
  182. }).then(() => {
  183. item.quantity = num;
  184. // 通过 computed 重新计算总额
  185. }).catch(() => {
  186. // 如果是乐观更新,出错时需要回滚,但这里我们还没有更新?
  187. // 实际上数字输入框更新了 v-model。如果在子组件中使用 v-model,它会更新父组件。
  188. // 但我们触发了 changeNum。
  189. // 在子组件中:v-model="item.quantity" -> 直接更新 prop(Vue 2 会警告,但对象属性变更有效)。
  190. // 如果子组件更新了 prop,除了调用 API 我们不需要做任何事。
  191. // 但良好的实践是子组件触发事件,父组件更新。
  192. // cart-item 使用 v-model 绑定 props.item.quantity。这直接修改了 cartList 中的对象。
  193. // 所以 item.quantity 已经是 `num` 了。
  194. // 我们只需要调用 API。
  195. // 注意:cart-item.vue 使用 v-model="item.quantity"。
  196. // 这会直接修改对象。
  197. // 所以 API 调用应该使用新值。
  198. // 如果 API 失败,我们应该回滚。
  199. // 但让我们假设成功,或者通过重新加载列表来处理错误。
  200. });
  201. }
  202. },
  203. handleReduce(item) {
  204. const popupData = {
  205. price: item.productPrice,
  206. reducedPrice: (item.productPrice - item.reduceMoney).toFixed(2), // 假设 reduceMoney 是单价减少额
  207. startTime: new Date().getTime(),
  208. endTime: new Date().getTime() + (item.restTime || 0) * 1000, // restTime 单位是秒?
  209. upsellCode: 'mock-code'
  210. };
  211. this.$refs.reducePopup.bookInfo = popupData;
  212. this.$refs.reducePopup.showPopup = true;
  213. this.$refs.reducePopup.inviteUsers = [];
  214. },
  215. onAllCheckChange(e) {
  216. const checked = e.value;
  217. this.cartList.forEach(item => {
  218. if (item.stockStatus !== 3 && item.availableStock > 0) {
  219. item.checked = checked;
  220. }
  221. });
  222. },
  223. toggleSelectAll() {
  224. // 由 u-checkbox 处理
  225. },
  226. handleClearCart() {
  227. uni.showModal({
  228. title: '提示',
  229. content: '确定要清空购物车吗?',
  230. success: (res) => {
  231. if (res.confirm) {
  232. this.$u.api.clearCartAjax().then(() => {
  233. this.$u.toast('清空成功');
  234. this.cartList = [];
  235. });
  236. }
  237. }
  238. });
  239. },
  240. checkout() {
  241. if (this.selectedCount === 0) {
  242. uni.showToast({ title: '请选择商品', icon: 'none' });
  243. return;
  244. }
  245. // 如果需要,检查减价商品逻辑
  246. // 如果有选中的商品可以减价,显示对话框?
  247. const hasReduceItem = this.selectedItems.some(item => (item.reduceNum < item.maxReduceNum) && (item.reduceMoney > 0));
  248. if (hasReduceItem) {
  249. this.$refs.reduceDialog.openPopup();
  250. } else {
  251. this.onNext();
  252. }
  253. },
  254. onNext() {
  255. // 提交订单 - 传递选中的 ID
  256. if (this.selectedItems.length === 0) {
  257. this.$u.toast('请选择商品');
  258. return;
  259. }
  260. const ids = this.selectedItems.map(item => item.id);
  261. // 调用预提交接口检查
  262. uni.showLoading({ title: '处理中' });
  263. this.$u.api.preSubmitOrderAjax({ cartIdList: ids }).then(res => {
  264. uni.hideLoading();
  265. if (res.code === 200) {
  266. if (res.data.code === 1) {
  267. // 成功,跳转到确认订单页面
  268. uni.navigateTo({
  269. url: `/pages-home/pages/book-order?cartIds=${ids}`
  270. });
  271. } else if (res.data.code === 2) {
  272. // 失败,显示错误信息
  273. this.$u.toast('库存不足');
  274. } else if (res.data.code === 3) {
  275. // 失败,显示错误信息
  276. this.$u.toast('图书已下架');
  277. }
  278. } else {
  279. // 失败,显示错误信息
  280. this.$u.toast(res.msg || '无法提交订单');
  281. }
  282. }).catch(() => {
  283. uni.hideLoading();
  284. });
  285. },
  286. handleShare() {
  287. console.log('share');
  288. },
  289. handleScan() {
  290. console.log('scan');
  291. },
  292. handleDelete(item) {
  293. uni.showModal({
  294. title: '提示',
  295. content: '确定要删除该商品吗?',
  296. success: (res) => {
  297. if (res.confirm) {
  298. this.$u.api.deleteCartItemAjax(item.id).then(() => {
  299. this.$u.toast('删除成功');
  300. // 从本地列表中移除
  301. const index = this.cartList.findIndex(i => i.id === item.id);
  302. if (index > -1) {
  303. this.cartList.splice(index, 1);
  304. }
  305. });
  306. } else {
  307. item.show = false;
  308. }
  309. }
  310. });
  311. },
  312. onSelectCondition(item) {
  313. this.currentItem = item;
  314. // 获取商品详情中的 SKU 列表
  315. // 注意:这里使用 isbn,假设 item 中包含 isbn
  316. if (!item.isbn) {
  317. this.$u.toast('无法获取商品信息');
  318. return;
  319. }
  320. uni.showLoading({ title: '加载中' });
  321. this.$u.http.get('/token/shop/bookDetail', { isbn: item.isbn }).then(res => {
  322. uni.hideLoading();
  323. if (res.code === 200 && res.data && res.data.skuList) {
  324. this.$refs.conditionPopup.open(res.data.skuList, item.conditionType);
  325. } else {
  326. this.$u.toast('获取品相信息失败');
  327. }
  328. }).catch(() => {
  329. uni.hideLoading();
  330. this.$u.toast('网络请求失败');
  331. });
  332. },
  333. handleConditionUpdate(sku) {
  334. if (!this.currentItem) return;
  335. uni.showLoading({
  336. title: '更新中'
  337. });
  338. this.$u.api.updateCartConditionAjax({
  339. id: this.currentItem.id,
  340. conditionType: sku.conditionType
  341. }).then(() => {
  342. uni.hideLoading();
  343. this.$u.toast('更新成功');
  344. // 在列表中查找并更新,确保视图刷新
  345. const index = this.cartList.findIndex(i => i.id === this.currentItem.id);
  346. if (index > -1) {
  347. const targetItem = this.cartList[index];
  348. // 确保转换为数字类型
  349. const newType = Number(sku.conditionType);
  350. this.$set(targetItem, 'conditionType', newType);
  351. this.$set(targetItem, 'productPrice', sku.price);
  352. if (sku.reduceMoney !== undefined) {
  353. this.$set(targetItem, 'reduceMoney', sku.reduceMoney);
  354. }
  355. // 强制更新 currentItem 引用(虽然它指向同一个对象,但为了保险)
  356. this.currentItem = targetItem;
  357. } else {
  358. // 如果找不到(极少情况),重新加载
  359. this.loadData();
  360. }
  361. }).catch(() => {
  362. uni.hideLoading();
  363. });
  364. }
  365. }
  366. }
  367. </script>
  368. <style lang="scss" scoped>
  369. .cart-page {
  370. min-height: 100vh;
  371. background-color: #f5f5f5;
  372. padding-bottom: 120rpx; // 底部栏的留白
  373. }
  374. .ad-banner {
  375. background-color: #d1f2d6; // 浅绿色
  376. padding: 20rpx 30rpx;
  377. display: flex;
  378. justify-content: space-between;
  379. align-items: center;
  380. color: #666;
  381. font-size: 26rpx;
  382. }
  383. .cart-list {
  384. padding: 20rpx;
  385. }
  386. .recommend-section {
  387. padding: 0 20rpx;
  388. .section-title {
  389. display: flex;
  390. align-items: center;
  391. justify-content: center;
  392. margin: 30rpx 0;
  393. .line {
  394. width: 60rpx;
  395. height: 2rpx;
  396. background-color: #ccc;
  397. }
  398. .text {
  399. margin: 0 20rpx;
  400. font-size: 28rpx;
  401. color: #333;
  402. font-weight: bold;
  403. }
  404. }
  405. .recommend-grid {
  406. display: flex;
  407. flex-wrap: wrap;
  408. justify-content: space-between;
  409. .grid-item {
  410. width: 32%; // 3 列
  411. background-color: #fff;
  412. border-radius: 12rpx;
  413. margin-bottom: 20rpx;
  414. padding: 20rpx;
  415. box-sizing: border-box;
  416. display: flex;
  417. flex-direction: column;
  418. align-items: center;
  419. .cover {
  420. width: 100%;
  421. height: 220rpx;
  422. border-radius: 8rpx;
  423. background-color: #eee;
  424. }
  425. .title {
  426. margin-top: 10rpx;
  427. font-size: 24rpx;
  428. color: #333;
  429. line-height: 1.4;
  430. display: -webkit-box;
  431. -webkit-box-orient: vertical;
  432. -webkit-line-clamp: 2;
  433. overflow: hidden;
  434. width: 100%;
  435. }
  436. }
  437. }
  438. }
  439. .bottom-fixed {
  440. position: fixed;
  441. bottom: 0;
  442. /* #ifdef H5 */
  443. bottom: 50px;
  444. /* #endif */
  445. left: 0;
  446. width: 100%;
  447. background-color: #fff;
  448. display: flex;
  449. align-items: center;
  450. justify-content: space-between;
  451. padding: 24rpx 30rpx;
  452. box-sizing: border-box;
  453. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  454. z-index: 99;
  455. .left-part {
  456. display: flex;
  457. align-items: center;
  458. }
  459. .right-part {
  460. display: flex;
  461. align-items: center;
  462. .total-info {
  463. text-align: right;
  464. margin-right: 20rpx;
  465. display: flex;
  466. align-items: center;
  467. .reduced-tip {
  468. font-size: 22rpx;
  469. color: #38C148;
  470. border: 1rpx solid #38C148;
  471. border-radius: 4rpx;
  472. padding: 0 6rpx;
  473. margin-right: 10rpx;
  474. }
  475. .total-price {
  476. display: flex;
  477. align-items: center;
  478. .label {
  479. font-size: 28rpx;
  480. color: #333;
  481. }
  482. .price {
  483. font-size: 36rpx;
  484. color: #e02020;
  485. font-weight: bold;
  486. }
  487. }
  488. }
  489. .checkout-btn {
  490. background-color: #e02020;
  491. color: #fff;
  492. font-size: 30rpx;
  493. padding: 16rpx 40rpx;
  494. border-radius: 40rpx;
  495. }
  496. }
  497. }
  498. </style>