index.vue 20 KB

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