index.vue 12 KB

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