index.vue 12 KB

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