buy.blade.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. @extends('user.layouts')
  2. @section('css')
  3. <link href="/assets/css/invoice.min.css" rel="stylesheet">
  4. @endsection
  5. @section('content')
  6. <div class="page-content">
  7. <div class="panel">
  8. <div class="panel-body container-fluid">
  9. <div class="page-invoice-table table-responsive">
  10. <table class="table table-hover text-md-center">
  11. <thead>
  12. <tr>
  13. <th>{{trans('user.shop.service')}}</th>
  14. <th>{{trans('user.shop.description')}} </th>
  15. <th>{{trans('user.shop.price')}}</th>
  16. <th>{{trans('user.shop.quantity')}}</th>
  17. </tr>
  18. </thead>
  19. <tbody>
  20. <tr>
  21. <td class="text-middle">{{$goods->name}} </td>
  22. <td>{{trans('common.available_date')}}
  23. <strong>{{$goods->type === 1? $dataPlusDays:$goods->days}} {{trans('validation.attributes.day')}}</strong>
  24. <br>
  25. <strong>{{$goods->traffic_label}}</strong> {{trans('user.attribute.data')}}
  26. </td>
  27. <td class="text-middle"> ¥{{$goods->price}} </td>
  28. <td class="text-middle"> x 1</td>
  29. </tr>
  30. </tbody>
  31. </table>
  32. </div>
  33. <div class="row">
  34. @if($goods->type <= 2)
  35. <div class="col-lg-3 pl-30">
  36. <div class="input-group">
  37. <input type="text" class="form-control" name="coupon_sn" id="coupon_sn" placeholder="{{trans('user.coupon.attribute')}}"/>
  38. <div class="input-group-btn">
  39. <button type="submit" class="btn btn-info" onclick="redeemCoupon()">
  40. <i class="icon wb-loop" aria-hidden="true"></i> {{trans('common.apply')}}
  41. </button>
  42. </div>
  43. </div>
  44. </div>
  45. <div class="col-lg-3 offset-lg-6 text-right">
  46. <p id="discount"></p>
  47. <p>{{trans('user.shop.subtotal')}}
  48. <span>¥{{$goods->price}}</span>
  49. </p>
  50. <p class="page-invoice-amount">{{trans('user.shop.total')}}
  51. <span class="grand-total">¥{{$goods->price}}</span>
  52. </p>
  53. </div>
  54. @endif
  55. <div class="col-md-12 mb-30">
  56. <div class="float-right">
  57. @include('user.components.purchase')
  58. @if($goods->type <= 2)
  59. <button class="btn btn-flat mt-2 mx-0 p-0" onclick="pay('credit','0')">
  60. <img src="/assets/images/payment/creditpay.svg" height="48px" alt="{{trans('user.shop.pay_credit')}}"/>
  61. </button>
  62. @endif
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. @endsection
  70. @section('javascript')
  71. <script>
  72. // 校验优惠券是否可用
  73. function redeemCoupon() {
  74. const coupon_sn = $('#coupon_sn').val();
  75. const goods_price = '{{$goods->price}}';
  76. $.ajax({
  77. method: 'POST',
  78. url: '{{route('redeemCoupon')}}',
  79. dataType: 'json',
  80. data: {_token: '{{csrf_token()}}', coupon_sn: coupon_sn, price: '{{$goods->price}}'},
  81. success: function(ret) {
  82. $('.input-group-prepend').remove();
  83. if (ret.status === 'success') {
  84. $('#coupon_sn').parent().prepend(
  85. '<div class="input-group-prepend"><span class="input-group-text bg-green-700"><i class="icon wb-check white" aria-hidden="true"></i></span></div>');
  86. // 根据类型计算折扣后的总金额
  87. let total_price = 0;
  88. let coupon_text = document.getElementById('discount');
  89. if (ret.data.type === 2) {
  90. total_price = goods_price * (1 - ret.data.value / 100);
  91. coupon_text.innerHTML = '【{{trans('user.coupon.attribute')}}】:' + ret.data.name + ' - '
  92. + (100 - ret.data.value) + '%<br> {{trans('user.coupon.discount')}} - ¥' + total_price.toFixed(2);
  93. total_price = goods_price - total_price;
  94. } else {
  95. total_price = goods_price - ret.data.value;
  96. total_price = total_price > 0 ? total_price : 0;
  97. if (ret.data.type === 1) {
  98. coupon_text.innerHTML = '【{{trans('user.coupon.voucher')}}】:' + ret.data.name + ' -¥' + ret.data.value;
  99. }
  100. }
  101. // 四舍五入,保留2位小数
  102. $('.grand-total').text('¥' + total_price.toFixed(2));
  103. swal.fire({
  104. title: ret.message,
  105. icon: 'success',
  106. timer: 1300,
  107. showConfirmButton: false,
  108. });
  109. } else {
  110. $('.grand-total').text('¥' + goods_price);
  111. $('#coupon_sn').parent().prepend(
  112. '<div class="input-group-prepend"><span class="input-group-text bg-red-700"><i class="icon wb-close white" aria-hidden="true"></i></span></div>');
  113. swal.fire({
  114. title: ret.title,
  115. text: ret.message,
  116. icon: 'error',
  117. });
  118. }
  119. },
  120. });
  121. }
  122. // 检查预支付
  123. function pay(method, pay_type) {
  124. // 存在套餐 和 购买类型为套餐时 出现提示
  125. if ('{{$activePlan}}' === '1' && '{{$goods->type}}' === '2') {
  126. swal.fire({
  127. title: '{{trans('user.shop.conflict')}}',
  128. html: '{!! trans('user.shop.conflict_tips') !!}',
  129. icon: 'info',
  130. showCancelButton: true,
  131. cancelButtonText: '{{trans('common.back')}}',
  132. confirmButtonText: '{{trans('common.continues')}}',
  133. }).then((result) => {
  134. if (result.value) {
  135. contiousPay(method, pay_type);
  136. }
  137. });
  138. } else {
  139. contiousPay(method, pay_type);
  140. }
  141. }
  142. function contiousPay(method, pay_type) {
  143. const goods_id = '{{$goods->id}}';
  144. const coupon_sn = $('#coupon_sn').val();
  145. $.ajax({
  146. method: 'POST',
  147. url: '{{route('purchase')}}',
  148. dataType: 'json',
  149. data: {
  150. _token: '{{csrf_token()}}',
  151. goods_id: goods_id,
  152. coupon_sn: coupon_sn,
  153. method: method,
  154. pay_type: pay_type,
  155. },
  156. success: function(ret) {
  157. if (ret.status === 'success') {
  158. swal.fire({
  159. title: ret.message,
  160. icon: 'success',
  161. timer: 1300,
  162. showConfirmButton: false,
  163. });
  164. if (method === 'credit') {
  165. swal.fire({title: ret.message, icon: 'success', timer: 1000, showConfirmButton: false}).then(() => window.location.href = '{{route('invoice')}}');
  166. }
  167. if (ret.data) {
  168. window.location.href = '{{route('orderDetail', '')}}/' + ret.data;
  169. } else if (ret.url) {
  170. window.location.href = ret.url;
  171. }
  172. } else if (ret.status === 'info') {
  173. swal.fire({title: ret.title, text: ret.message, icon: 'question'});
  174. } else {
  175. swal.fire({
  176. title: ret.message,
  177. icon: 'error',
  178. });
  179. }
  180. },
  181. error: function() {
  182. swal.fire('{{trans('user.unknown').trans('common.error')}}', '{{trans('user.shop.call4help')}}', 'error');
  183. },
  184. });
  185. }
  186. </script>
  187. @endsection