index.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. <template>
  2. <view>
  3. <view class="car_list">
  4. <view v-for="(pItem, pIndex) in cartListByGroup" :key="pIndex">
  5. <view class="business_name" v-if="pItem.products.length !== 0">
  6. <image @click="selectBussiness(pIndex)" class="checkbox" :src="pItem.checked ? '../../static/icon/checked.png' : '../../static/icon/checkbox.png'"></image>&nbsp;
  7. <image src="../../static/icon/shop_fill.png" class="business_icon" />
  8. {{ pItem.business_name }}
  9. </view>
  10. <view class="product_group">
  11. <view v-for="(item, index) in pItem.products" @longpress="deleteCar(pIndex, index)" :key="index" style="margin-bottom: 20rpx">
  12. <SwipeAction @clickItem="clickItem" :index="index" ref="swipeAction" :pIndex="pIndex">
  13. <view class="car_item">
  14. <view class="check_label" @click="checkedItem(pIndex, index, item.id)">
  15. <image class="checkbox" src="@/static/icon/checkbox_disabled.png" style="width: 30rpx; height: 30rpx" v-if="item.product_status !== 0 || item.stock == 0" />
  16. <image class="checkbox" :src="item.checked ? '../../static/icon/checked.png' : '../../static/icon/checkbox.png'" v-else></image>
  17. </view>
  18. <view class="box_left">
  19. <navigator :url="'/pages/product/index?product_id=' + item.product_id">
  20. <image class="car_image" :src="item.thumb" mode=""></image>
  21. <view class="product_status" v-if="item.product_status !== 0">
  22. {{ item.stock == 0 ? "已售罄" : "已下架" }}
  23. </view>
  24. </navigator>
  25. </view>
  26. <view class="box_center">
  27. <navigator :url="'/pages/product/index?product_id=' + item.product_id" class="car_name">{{ item.name }}</navigator>
  28. <navigator :url="'/pages/product/index?product_id=' + item.product_id" class="car_spec">{{ item.spec }}</navigator>
  29. <view v-if="item.promo_title" class="promo_title">{{ item.promo_title }}</view>
  30. <navigator :url="'/pages/product/index?product_id=' + item.product_id" class="car_price">
  31. <text class="price">¥{{ item.price }}</text>
  32. <text class="market_price">¥{{ item.market_price }}</text>
  33. </navigator>
  34. </view>
  35. <view class="box_right">
  36. <view class="buy_num_box">
  37. <button class="buy_num_sub" @click="changeQuantity(pIndex, index, -1)" data-eventsync="true">
  38. <image class="sub_icon" src="../../static/icon/sub_icon.png" mode=""></image>
  39. </button>
  40. <input type="number" class="buy_num" placeholder="数量" v-model="item.buy_num" @blur="changeQuantity(pIndex, index, 0)" />
  41. <button class="buy_num_add" @click="changeQuantity(pIndex, index, +1)" data-eventsync="true">
  42. <image class="add_icon" src="../../static/icon/add_icon.png" mode=""></image>
  43. </button>
  44. </view>
  45. </view>
  46. </view>
  47. </SwipeAction>
  48. </view>
  49. </view>
  50. </view>
  51. </view>
  52. <Empty v-if="cartList.length == 0" text="----- 还没有产品啦 -----" />
  53. <view class="to_bottom" v-if="cartList.length"> -----到底啦-----</view>
  54. <view class="bottom_box">
  55. <view class="check_all_label" @click="checkAll()">
  56. <image class="checkbox" :src="checkedAll ? '../../static/icon/checked.png' : '../../static/icon/checkbox.png'"></image>
  57. <text class="checkall">全选</text>
  58. </view>
  59. <view class="price_box">
  60. 合计:<text class="price_total">¥{{ priceTotal }}</text>
  61. </view>
  62. <view class="to_order" @click="toOrder()">预约</view>
  63. </view>
  64. </view>
  65. </template>
  66. <script>
  67. import Empty from "@/components/Empty/Empty.vue";
  68. import SwipeAction from "@/components/SwipeAction/SwipeAction.vue";
  69. export default {
  70. components: { Empty, SwipeAction },
  71. data() {
  72. return {
  73. // 请求参数
  74. requestParam: {},
  75. // 是否全选
  76. checkedAll: false,
  77. // 总价
  78. priceTotal: "0.00",
  79. // 是否请求中
  80. isReqing: false,
  81. //购物车列表
  82. cartListByGroup: [],
  83. cartList: [],
  84. };
  85. },
  86. onLoad() {
  87. // #ifdef MP-WEIXIN
  88. //分享按钮
  89. uni.showShareMenu({
  90. withShareTicket: true,
  91. menus: ["shareAppMessage", "shareTimeline"],
  92. });
  93. // #endif
  94. },
  95. onShareAppMessage(obj) {
  96. // 获取分享信息
  97. let shareList = getApp().globalData.shareList;
  98. // 获取分享信息
  99. let shareObj = {
  100. title: "药优惠 得积分 兑豪礼",
  101. path: "/pages/index/index",
  102. imageUrl: "",
  103. };
  104. // 循环列表
  105. for (let i in shareList) {
  106. if (shareList[i].pages == "pages/car/index") {
  107. shareObj.path = shareList[i].path ? shareList[i].path : shareObj.path;
  108. shareObj.title = shareList[i].title ? shareList[i].title : shareObj.title;
  109. shareObj.imageUrl = shareList[i].image_url ? shareList[i].image_url : shareObj.imageUrl;
  110. }
  111. }
  112. // 返回分享信息
  113. return shareObj;
  114. },
  115. onShow() {
  116. // 登录提示
  117. if (!this.$checkAccess.alterLogin()) return;
  118. // 请求中,不允许刷新
  119. if (this.isReqing) return;
  120. // 设置请求中
  121. this.isReqing = true;
  122. // 非全选
  123. this.checkedAll = 0;
  124. // 请求列表
  125. this.$http.request("api/shop_cart/get_list", this.requestParam).then((re) => {
  126. // 设置非请求中
  127. this.isReqing = false;
  128. // 成功结果
  129. if (re.code == "success") {
  130. // 赋值
  131. this.cartListByGroup = this.formatGroupedData(re.data);
  132. this.cartList = re.data;
  133. // 计算价格
  134. this.priceHandler();
  135. }
  136. });
  137. },
  138. onPullDownRefresh() {
  139. // 登录提示
  140. if (!this.$checkAccess.alterLogin()) return;
  141. // 请求列表
  142. this.$http.request("api/shop_cart/get_list", this.requestParam).then((re) => {
  143. if (re.code == "success") {
  144. // 赋值
  145. this.cartListByGroup = this.formatGroupedData(re.data);
  146. this.cartList = re.data;
  147. // 计算价格
  148. this.priceHandler();
  149. }
  150. });
  151. uni.stopPullDownRefresh();
  152. },
  153. onReachBottom() {},
  154. methods: {
  155. // 转换为数组格式
  156. formatGroupedData(list) {
  157. const groupedData = list.reduce((result, item) => {
  158. const { business_id, business_name } = item;
  159. // 如果该 business_id 的分组不存在,则初始化该分组
  160. if (!result[business_id]) {
  161. result[business_id] = {
  162. business_id,
  163. business_name,
  164. products: [],
  165. checked: false,
  166. };
  167. }
  168. // 将商品添加到对应的 business_id 分组
  169. result[business_id].products.push(item);
  170. return result;
  171. }, {});
  172. return groupedData;
  173. },
  174. //选择商业公司下面的所有商品
  175. selectBussiness(index) {
  176. this.cartListByGroup[index].checked = !this.cartListByGroup[index].checked;
  177. this.cartListByGroup[index].products.forEach((item) => {
  178. item.checked = this.cartListByGroup[index].checked;
  179. });
  180. // 计算价格
  181. this.priceHandler();
  182. },
  183. // 数量调整
  184. changeQuantity(pIndex, index, number) {
  185. // 如果不是0.表示两侧按钮点击,0表示输入的修改
  186. if (number != 0) {
  187. // 计算个数
  188. this.cartListByGroup[pIndex].products[index].buy_num = this.cartListByGroup[pIndex].products[index].buy_num + number;
  189. }
  190. // 如果大于库存
  191. if (this.cartListByGroup[pIndex].products[index].buy_num > this.cartListByGroup[pIndex].products[index].stock) {
  192. // 设置为库存
  193. this.cartListByGroup[pIndex].products[index].buy_num = this.cartListByGroup[pIndex].products[index].stock;
  194. // 提示
  195. uni.showToast({
  196. title: "购买数量不能大于库存",
  197. icon: "none",
  198. });
  199. return;
  200. }
  201. // 如果小于1.设置为1
  202. if (this.cartListByGroup[pIndex].products[index].buy_num < 1) {
  203. // 恢复1
  204. this.cartListByGroup[pIndex].products[index].buy_num = 1;
  205. // 提示
  206. uni.showToast({
  207. title: "数量不可小于1",
  208. icon: "none",
  209. });
  210. return;
  211. }
  212. // 请求列表
  213. this.$http
  214. .request("api/shop_cart/edit", {
  215. id: this.cartListByGroup[pIndex].products[index].id,
  216. buy_num: this.cartListByGroup[pIndex].products[index].buy_num,
  217. })
  218. .then((re) => {
  219. if (re.code == "success") {
  220. // 计算价格
  221. this.priceHandler();
  222. } else {
  223. uni.showToast({
  224. title: re.msg,
  225. icon: "none",
  226. });
  227. }
  228. });
  229. },
  230. // 删除购物车
  231. deleteCar(pIndex, index) {
  232. uni.showModal({
  233. title: "是否删除?",
  234. success: (re) => {
  235. if (re.confirm) {
  236. // 请求列表
  237. this.$http
  238. .request("api/shop_cart/del", {
  239. id: this.cartListByGroup[pIndex].products[index].id,
  240. })
  241. .then((re) => {
  242. // 如果删除成功的话
  243. if (re.code == "success") {
  244. this.cartListByGroup[pIndex].products.splice(index, 1);
  245. // 计算价格
  246. this.priceHandler();
  247. if (this.$refs.swipeAction.length) {
  248. this.$refs.swipeAction[this.$refs.swipeAction.length - 1]?.reset();
  249. }
  250. }
  251. });
  252. } else {
  253. this.$refs.swipeAction[this.$refs.swipeAction.length - 1]?.reset();
  254. }
  255. },
  256. });
  257. },
  258. checkedItem(pIndex, index, product_id) {
  259. const product = this.cartListByGroup[pIndex].products[index];
  260. // 如果商品不可选中,直接返回
  261. if (product.product_status !== 0 || product.stock === 0) {
  262. return;
  263. }
  264. // 切换选中状态
  265. const isChecked = (product.checked = !product.checked);
  266. // 找到对应商家组中的商品并更新选中状态
  267. const productInGroup = this.cartListByGroup[pIndex].products.find((item) => item.id === product_id);
  268. if (productInGroup) {
  269. productInGroup.checked = isChecked;
  270. }
  271. // 检查该商家组内所有商品是否都已选中
  272. const checkedBussinessAll = this.cartListByGroup[pIndex].products.every((item) => item.checked);
  273. // 更新商家组的全选状态
  274. this.cartListByGroup[pIndex].checked = checkedBussinessAll;
  275. // 检查是否所有商品都已选中
  276. let checkedAll = true;
  277. for (const key in this.cartListByGroup) {
  278. if (!this.cartListByGroup[key].checked) {
  279. checkedAll = false;
  280. }
  281. }
  282. // 更新全选状态
  283. this.checkedAll = checkedAll ? 1 : 0;
  284. // 计算价格
  285. this.priceHandler();
  286. },
  287. checkAll() {
  288. // 设置全选/单选
  289. this.checkedAll = this.checkedAll ? 0 : 1;
  290. // 循环处理
  291. for (const index in this.cartListByGroup) {
  292. this.cartListByGroup[index].checked = this.checkedAll;
  293. this.cartListByGroup[index].products.forEach((item) => (item.checked = this.checkedAll));
  294. }
  295. // 计算价格
  296. this.priceHandler();
  297. },
  298. priceHandler() {
  299. // 总价格
  300. let priceTotal = 0;
  301. // 循环处理
  302. for (const index in this.cartListByGroup) {
  303. this.cartListByGroup[index].products.forEach((item) => {
  304. if (item.checked) {
  305. priceTotal = this.$decimal.add(priceTotal, this.$decimal.mul(item.price, item.buy_num));
  306. }
  307. });
  308. }
  309. // 小数点处理
  310. this.priceTotal = priceTotal.toFixed(2);
  311. },
  312. toOrder() {
  313. // 等待支付的信息
  314. let waitList = [];
  315. // 循环处理
  316. for (let index in this.cartListByGroup) {
  317. // 如果选中的
  318. for (const key in this.cartListByGroup[index].products) {
  319. if (this.cartListByGroup[index].products[key].checked) {
  320. // 如果库存不足
  321. if (this.cartListByGroup[index].products[key].buy_num < 1) {
  322. uni.showToast({ icon: "none", title: "选择的产品至少需要1个" });
  323. return;
  324. }
  325. // 如果库存不足
  326. if (this.cartListByGroup[index].products[key].buy_num > this.cartListByGroup[index].products[key].stock) {
  327. uni.showToast({ icon: "none", title: "产品库存不足" });
  328. return;
  329. }
  330. waitList.push(this.cartListByGroup[index].products[key].id);
  331. }
  332. }
  333. }
  334. // 如果没有选择
  335. if (!waitList.length) {
  336. uni.showToast({ icon: "none", title: "请选择需要结算的产品" });
  337. return;
  338. }
  339. // 如果没有选择
  340. if (waitList.length > 99) {
  341. uni.showToast({ icon: "none", title: "这么多产品一个预约单写不下哦" });
  342. return;
  343. }
  344. uni.navigateTo({
  345. url: "/pages/car/order?cart_ids=" + waitList.join(","),
  346. });
  347. },
  348. clickItem(e) {
  349. this.deleteCar(e.pIndex, e.index);
  350. },
  351. },
  352. };
  353. </script>
  354. <style lang="less">
  355. .car_list {
  356. display: block;
  357. overflow: hidden;
  358. margin: 0rpx auto;
  359. margin-top: 20rpx;
  360. padding-bottom: 110rpx;
  361. .business_name {
  362. padding: 8rpx 10rpx;
  363. border-bottom: 1px solid #f3f3f3;
  364. font-size: 32rpx;
  365. z-index: 1;
  366. display: flex;
  367. align-items: center;
  368. background-color: #fff;
  369. .checkbox {
  370. width: 40rpx;
  371. height: 40rpx;
  372. }
  373. .business_icon {
  374. width: 48rpx;
  375. height: 48rpx;
  376. margin-right: 10rpx;
  377. }
  378. }
  379. .car_item {
  380. height: 180rpx;
  381. display: block;
  382. background: #ffffff;
  383. margin: 0rpx auto;
  384. margin-bottom: 20rpx;
  385. padding: 20rpx 0rpx 0;
  386. position: relative;
  387. .delete_btn {
  388. position: absolute;
  389. top: 0;
  390. right: 0;
  391. bottom: 0;
  392. width: 80px;
  393. background-color: #f44336;
  394. color: #fff;
  395. display: flex;
  396. justify-content: center;
  397. align-items: center;
  398. }
  399. .swipe-content {
  400. transition: transform 0.3s ease;
  401. }
  402. .check_label {
  403. float: left;
  404. width: 40rpx;
  405. height: 40rpx;
  406. display: flex;
  407. align-items: center;
  408. justify-content: center;
  409. margin-top: 10rpx;
  410. padding: 50rpx 20rpx;
  411. .checkbox {
  412. float: left;
  413. width: 40rpx;
  414. height: 40rpx;
  415. }
  416. }
  417. .box_left {
  418. float: left;
  419. width: 140rpx;
  420. height: 200rpx;
  421. margin-top: 10rpx;
  422. position: relative;
  423. .car_image {
  424. width: 140rpx;
  425. height: 140rpx;
  426. border-radius: 5rpx;
  427. }
  428. .product_status {
  429. position: absolute;
  430. width: 100%;
  431. height: 40rpx;
  432. bottom: 60rpx;
  433. display: flex;
  434. justify-content: center;
  435. align-items: center;
  436. font-size: 24rpx;
  437. background-color: #999999;
  438. }
  439. }
  440. .box_center {
  441. float: left;
  442. width: 300rpx;
  443. margin-left: 25rpx;
  444. .car_name {
  445. max-height: 60rpx;
  446. font-size: 30rpx;
  447. line-height: 30rpx;
  448. overflow: hidden;
  449. white-space: nowrap; /* 不换行 */
  450. overflow: hidden; /* 隐藏超出的内容 */
  451. text-overflow: ellipsis; /* 用省略号表示被隐藏的部分 */
  452. }
  453. .promo_title {
  454. max-height: 80rpx;
  455. font-size: 20rpx;
  456. line-height: 40rpx;
  457. overflow: hidden;
  458. padding: 0rpx 0rpx;
  459. color: #dd524d;
  460. }
  461. .car_spec {
  462. color: #999999;
  463. font-size: 24rpx;
  464. max-height: 60rpx;
  465. line-height: 60rpx;
  466. overflow: hidden;
  467. }
  468. .car_price {
  469. font-size: 30rpx;
  470. line-height: 60rpx;
  471. .price {
  472. color: red;
  473. }
  474. .market_price {
  475. font-size: 24rpx;
  476. color: #999999;
  477. margin-left: 10rpx;
  478. padding-left: 10rpx;
  479. text-decoration: line-through;
  480. border-left: 2rpx solid #dddddd;
  481. }
  482. }
  483. }
  484. .box_right {
  485. float: right;
  486. width: 185rpx;
  487. padding-right: 15rpx;
  488. .buy_num_box {
  489. float: right;
  490. color: #333333;
  491. overflow: hidden;
  492. font-size: 24rpx;
  493. margin-top: 70rpx;
  494. text-align: center;
  495. .buy_num_sub {
  496. float: left;
  497. border: none;
  498. height: 36rpx;
  499. background: none;
  500. text-align: center;
  501. line-height: 36rpx;
  502. padding: 10rpx 10rpx;
  503. .sub_icon {
  504. width: 22rpx;
  505. height: 22rpx;
  506. display: block;
  507. }
  508. }
  509. .buy_num_sub::after {
  510. border: none;
  511. background: none;
  512. }
  513. .buy_num {
  514. float: left;
  515. width: 90rpx;
  516. height: 36rpx;
  517. font-size: 24rpx;
  518. min-height: 36rpx;
  519. line-height: 36rpx;
  520. padding: 0rpx 0rpx;
  521. border-radius: 8rpx;
  522. border: 2rpx solid #dddddd;
  523. }
  524. .buy_num_add {
  525. float: left;
  526. border: none;
  527. height: 36rpx;
  528. background: none;
  529. text-align: center;
  530. padding: 10rpx 10rpx;
  531. line-height: 36rpx;
  532. .add_icon {
  533. width: 22rpx;
  534. height: 22rpx;
  535. display: block;
  536. }
  537. }
  538. .buy_num_add::after {
  539. border: none;
  540. background: none;
  541. }
  542. }
  543. }
  544. }
  545. }
  546. .bottom_box {
  547. z-index: 999;
  548. left: 0rpx;
  549. width: 100%;
  550. height: 100rpx;
  551. display: block;
  552. position: fixed;
  553. overflow: hidden;
  554. background: #ffffff;
  555. padding: 0rpx 35rpx;
  556. bottom: var(--window-bottom);
  557. .check_all_label {
  558. float: left;
  559. width: 120rpx;
  560. height: 40rpx;
  561. font-size: 24rpx;
  562. line-height: 40rpx;
  563. padding: 30rpx 0rpx;
  564. .checkbox {
  565. float: left;
  566. width: 40rpx;
  567. height: 40rpx;
  568. }
  569. .checkbox.active {
  570. border: 2rpx solid red;
  571. .checkbox_active {
  572. background-color: #e03519;
  573. }
  574. }
  575. .checkall {
  576. float: left;
  577. height: 40rpx;
  578. display: block;
  579. margin-left: 10rpx;
  580. line-height: 40rpx;
  581. }
  582. }
  583. .price_box {
  584. float: left;
  585. width: 400rpx;
  586. display: block;
  587. color: #666666;
  588. font-size: 26rpx;
  589. text-align: right;
  590. line-height: 100rpx;
  591. margin-right: 20rpx;
  592. .price_total {
  593. color: red;
  594. font-size: 30rpx;
  595. }
  596. }
  597. .to_order {
  598. float: left;
  599. width: 140rpx;
  600. height: 60rpx;
  601. display: block;
  602. color: #ffffff;
  603. font-size: 28rpx;
  604. margin-top: 20rpx;
  605. line-height: 60rpx;
  606. padding: 0rpx 0rpx;
  607. text-align: center;
  608. border-radius: 30rpx;
  609. background-color: #e03519;
  610. }
  611. }
  612. </style>