index.vue 20 KB

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