index.vue 14 KB

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