index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  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. }
  185. },
  186. handleReduce(item) {
  187. const popupData = {
  188. price: item.productPrice,
  189. reducedPrice: (item.productPrice - item.reduceMoney).toFixed(2), // 假设 reduceMoney 是单价减少额
  190. startTime: new Date().getTime(),
  191. endTime: new Date().getTime() + (item.restTime || 0) * 1000, // restTime 单位是秒?
  192. upsellCode: 'mock-code'
  193. };
  194. this.$refs.reducePopup.bookInfo = popupData;
  195. this.$refs.reducePopup.showPopup = true;
  196. this.$refs.reducePopup.inviteUsers = [];
  197. },
  198. onAllCheckChange(e) {
  199. const checked = e.value;
  200. this.cartList.forEach(item => {
  201. if (item.stockStatus !== 3 && item.availableStock > 0) {
  202. item.checked = checked;
  203. }
  204. });
  205. },
  206. toggleSelectAll() {
  207. // 由 u-checkbox 处理
  208. },
  209. handleClearCart() {
  210. uni.showModal({
  211. title: '提示',
  212. content: '确定要清空购物车吗?',
  213. success: (res) => {
  214. if (res.confirm) {
  215. this.$u.api.clearCartAjax().then(() => {
  216. this.$u.toast('清空成功');
  217. this.cartList = [];
  218. });
  219. }
  220. }
  221. });
  222. },
  223. checkout() {
  224. if (this.selectedCount === 0) {
  225. uni.showToast({ title: '请选择商品', icon: 'none' });
  226. return;
  227. }
  228. // 如果需要,检查减价商品逻辑
  229. // 如果有选中的商品可以减价,显示对话框?
  230. const hasReduceItem = this.selectedItems.some(item => (item.reduceNum < item.maxReduceNum) && (item.reduceMoney > 0));
  231. if (hasReduceItem) {
  232. this.$refs.reduceDialog.openPopup();
  233. } else {
  234. this.onNext();
  235. }
  236. },
  237. onNext() {
  238. // 提交订单 - 传递选中的 ID
  239. if (this.selectedItems.length === 0) {
  240. this.$u.toast('请选择商品');
  241. return;
  242. }
  243. const ids = this.selectedItems.map(item => item.id);
  244. // 调用预提交接口检查
  245. uni.showLoading({ title: '处理中' });
  246. this.$u.api.preSubmitOrderAjax({ cartIdList: ids }).then(res => {
  247. uni.hideLoading();
  248. if (res.code === 200) {
  249. if (res.data.code === 2) {
  250. return this.$u.toast('库存不足');
  251. } else if (res.data.code === 3) {
  252. return this.$u.toast('图书已下架');
  253. }
  254. uni.navigateTo({
  255. url: `/pages-car/pages/confirm-order`
  256. });
  257. //存储提交的数据
  258. res.data.cartIdList = ids;
  259. uni.setStorageSync('preSubmitOrderData', res.data);
  260. } else {
  261. // 失败,显示错误信息
  262. this.$u.toast(res.msg || '无法提交订单');
  263. }
  264. }).catch(() => {
  265. uni.hideLoading();
  266. });
  267. },
  268. handleShare() {
  269. console.log('share');
  270. },
  271. handleScan() {
  272. console.log('scan');
  273. },
  274. handleDelete(item) {
  275. uni.showModal({
  276. title: '提示',
  277. content: '确定要删除该商品吗?',
  278. success: (res) => {
  279. if (res.confirm) {
  280. this.$u.api.deleteCartItemAjax(item.id).then(() => {
  281. this.$u.toast('删除成功');
  282. // 从本地列表中移除
  283. const index = this.cartList.findIndex(i => i.id === item.id);
  284. if (index > -1) {
  285. this.cartList.splice(index, 1);
  286. }
  287. });
  288. } else {
  289. item.show = false;
  290. }
  291. }
  292. });
  293. },
  294. onSelectCondition(item) {
  295. this.currentItem = item;
  296. // 获取商品详情中的 SKU 列表
  297. // 注意:这里使用 isbn,假设 item 中包含 isbn
  298. if (!item.isbn) {
  299. this.$u.toast('无法获取商品信息');
  300. return;
  301. }
  302. uni.showLoading({ title: '加载中' });
  303. this.$u.http.get('/token/shop/bookDetail', { isbn: item.isbn }).then(res => {
  304. uni.hideLoading();
  305. if (res.code === 200 && res.data && res.data.skuList) {
  306. this.$refs.conditionPopup.open(res.data.skuList, item.conditionType);
  307. } else {
  308. this.$u.toast('获取品相信息失败');
  309. }
  310. }).catch(() => {
  311. uni.hideLoading();
  312. this.$u.toast('网络请求失败');
  313. });
  314. },
  315. handleConditionUpdate(sku) {
  316. if (!this.currentItem) return;
  317. uni.showLoading({
  318. title: '更新中'
  319. });
  320. this.$u.api.updateCartConditionAjax({
  321. id: this.currentItem.id,
  322. conditionType: sku.conditionType
  323. }).then(() => {
  324. uni.hideLoading();
  325. this.$u.toast('更新成功');
  326. // 在列表中查找并更新,确保视图刷新
  327. const index = this.cartList.findIndex(i => i.id === this.currentItem.id);
  328. if (index > -1) {
  329. const targetItem = this.cartList[index];
  330. // 确保转换为数字类型
  331. const newType = Number(sku.conditionType);
  332. this.$set(targetItem, 'conditionType', newType);
  333. this.$set(targetItem, 'productPrice', sku.price);
  334. if (sku.reduceMoney !== undefined) {
  335. this.$set(targetItem, 'reduceMoney', sku.reduceMoney);
  336. }
  337. // 强制更新 currentItem 引用(虽然它指向同一个对象,但为了保险)
  338. this.currentItem = targetItem;
  339. } else {
  340. // 如果找不到(极少情况),重新加载
  341. this.loadData();
  342. }
  343. }).catch(() => {
  344. uni.hideLoading();
  345. });
  346. }
  347. }
  348. }
  349. </script>
  350. <style lang="scss" scoped>
  351. .cart-page {
  352. min-height: 100vh;
  353. background-color: #f5f5f5;
  354. padding-bottom: 120rpx; // 底部栏的留白
  355. }
  356. .ad-banner {
  357. background-color: #d1f2d6; // 浅绿色
  358. padding: 20rpx 30rpx;
  359. display: flex;
  360. justify-content: space-between;
  361. align-items: center;
  362. color: #666;
  363. font-size: 26rpx;
  364. }
  365. .cart-list {
  366. padding: 20rpx;
  367. }
  368. .recommend-section {
  369. padding: 0 20rpx;
  370. .section-title {
  371. display: flex;
  372. align-items: center;
  373. justify-content: center;
  374. margin: 30rpx 0;
  375. .line {
  376. width: 60rpx;
  377. height: 2rpx;
  378. background-color: #ccc;
  379. }
  380. .text {
  381. margin: 0 20rpx;
  382. font-size: 28rpx;
  383. color: #333;
  384. font-weight: bold;
  385. }
  386. }
  387. .recommend-grid {
  388. display: flex;
  389. flex-wrap: wrap;
  390. justify-content: space-between;
  391. .grid-item {
  392. width: 32%; // 3 列
  393. background-color: #fff;
  394. border-radius: 12rpx;
  395. margin-bottom: 20rpx;
  396. padding: 20rpx;
  397. box-sizing: border-box;
  398. display: flex;
  399. flex-direction: column;
  400. align-items: center;
  401. .cover {
  402. width: 100%;
  403. height: 220rpx;
  404. border-radius: 8rpx;
  405. background-color: #eee;
  406. }
  407. .title {
  408. margin-top: 10rpx;
  409. font-size: 24rpx;
  410. color: #333;
  411. line-height: 1.4;
  412. display: -webkit-box;
  413. -webkit-box-orient: vertical;
  414. -webkit-line-clamp: 2;
  415. overflow: hidden;
  416. width: 100%;
  417. }
  418. }
  419. }
  420. }
  421. .bottom-fixed {
  422. position: fixed;
  423. bottom: 0;
  424. /* #ifdef H5 */
  425. bottom: 50px;
  426. /* #endif */
  427. left: 0;
  428. width: 100%;
  429. background-color: #fff;
  430. display: flex;
  431. align-items: center;
  432. justify-content: space-between;
  433. padding: 24rpx 30rpx;
  434. box-sizing: border-box;
  435. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  436. z-index: 99;
  437. .left-part {
  438. display: flex;
  439. align-items: center;
  440. }
  441. .right-part {
  442. display: flex;
  443. align-items: center;
  444. .total-info {
  445. text-align: right;
  446. margin-right: 20rpx;
  447. display: flex;
  448. align-items: center;
  449. .reduced-tip {
  450. font-size: 22rpx;
  451. color: #38C148;
  452. border: 1rpx solid #38C148;
  453. border-radius: 4rpx;
  454. padding: 0 6rpx;
  455. margin-right: 10rpx;
  456. }
  457. .total-price {
  458. display: flex;
  459. align-items: center;
  460. .label {
  461. font-size: 28rpx;
  462. color: #333;
  463. }
  464. .price {
  465. font-size: 36rpx;
  466. color: #e02020;
  467. font-weight: bold;
  468. }
  469. }
  470. }
  471. .checkout-btn {
  472. background-color: #e02020;
  473. color: #fff;
  474. font-size: 30rpx;
  475. padding: 16rpx 40rpx;
  476. border-radius: 40rpx;
  477. }
  478. }
  479. }
  480. </style>