index.vue 20 KB

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