buy.blade.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. @extends('user.layouts')
  2. @section('css')
  3. <link href="/assets/examples/css/pages/invoice.css" type="text/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('home.service_name')}}</th>
  14. <th>{{trans('home.service_desc')}} </th>
  15. <th>{{trans('home.service_price')}}</th>
  16. <th>{{trans('home.service_quantity')}}</th>
  17. </tr>
  18. </thead>
  19. <tbody>
  20. <tr>
  21. <td class="text-middle">{{$goods->name}} </td>
  22. <td>{{trans('home.service_days')}}
  23. <strong>{{$goods->type==1? $dataPlusDays:$goods->days}} {{trans('home.day')}}</strong>
  24. <br>
  25. <strong>{{$goods->traffic_label}}</strong> {{trans('home.bandwidth')}}
  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. @if($goods->type <= 2)
  34. <div class="row">
  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('home.coupon')}}"/>
  38. <div class="input-group-btn">
  39. <button type="submit" class="btn btn-info" onclick="redeemCoupon()"><i class="icon wb-loop" aria-hidden="true"></i> {{trans('home.redeem_coupon')}}</button>
  40. </div>
  41. </div>
  42. </div>
  43. <div class="col-lg-3 offset-lg-6 text-right">
  44. <p>{{trans('home.service_subtotal_price')}}
  45. <span>¥{{$goods->price}}</span>
  46. </p>
  47. <p class="page-invoice-amount">{{trans('home.service_total_price')}}
  48. <span class="grand-total">¥{{$goods->price}}</span>
  49. </p>
  50. </div>
  51. @endif
  52. <div class="col-md-12 mb-30">
  53. <div class="float-right">
  54. @include('user.components.purchase')
  55. @if($goods->type <= 2)
  56. <button class="btn btn-lg btn-primary" onclick="pay('balance','0')"> {{trans('home.service_pay_button')}} </button>
  57. @endif
  58. </div>
  59. </div>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. @endsection
  65. @section('script')
  66. <script type="text/javascript">
  67. // 校验优惠券是否可用
  68. function redeemCoupon() {
  69. const coupon_sn = $('#coupon_sn').val();
  70. const goods_price = '{{$goods->price}}';
  71. $.ajax({
  72. type: "POST",
  73. url: "/redeemCoupon",
  74. async: false,
  75. data: {_token: '{{csrf_token()}}', coupon_sn: coupon_sn, price: '{{$goods->price}}'},
  76. dataType: 'json',
  77. success: function (ret) {
  78. $(".input-group-prepend").remove();
  79. if (ret.status === 'success') {
  80. $("#coupon_sn").parent().prepend('<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>');
  81. // 根据类型计算折扣后的总金额
  82. let total_price = 0;
  83. if (ret.data.type === 2) {
  84. total_price = goods_price * (1 - ret.data.discount / 10);
  85. $(".page-invoice-amount").parent().prepend('<p>优惠码 - ' + ret.data.name + ' ' + ret.data.discount + '折<br> 优惠 <span>¥ - ' + total_price.toFixed(2) + '</span></p>');
  86. total_price = goods_price - total_price;
  87. } else {
  88. total_price = goods_price - ret.data.amount;
  89. total_price = total_price > 0 ? total_price : 0;
  90. if (ret.data.type === 1) {
  91. $(".page-invoice-amount").parent().prepend('优惠码-' + ret.data.name + ' <span>¥ - ' + ret.data.amount + '</span>');
  92. }
  93. }
  94. // 四舍五入,保留2位小数
  95. $(".grand-total").text('¥' + total_price.toFixed(2));
  96. swal.fire({
  97. title: ret.message,
  98. type: 'success',
  99. timer: 1300,
  100. showConfirmButton: false
  101. });
  102. } else {
  103. $(".grand-total").text("¥" + goods_price);
  104. $("#coupon_sn").parent().prepend('<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>');
  105. swal.fire({
  106. title: ret.title,
  107. text: ret.message,
  108. type: 'error'
  109. });
  110. }
  111. }
  112. });
  113. }
  114. // 检查预支付
  115. function pay(method, pay_type) {
  116. // 存在套餐 和 购买类型为套餐时 出现提示
  117. if ('{{$activePlan}}' === '1' && '{{$goods->type}}' === '2') {
  118. swal.fire({
  119. title: '套餐存在冲突',
  120. html: '<p>当前购买套餐将自动设置为 <code>预支付套餐</code><p><ol class="text-left"><li> 预支付套餐会在生效中的套餐失效后自动开通!</li><li> 您可以在支付后手动激活套餐!</li></ol>',
  121. type: 'info',
  122. showCancelButton: true,
  123. cancelButtonText: '返 回',
  124. confirmButtonText: '继 续',
  125. }).then((result) => {
  126. if (result.value) {
  127. contiousPay(method, pay_type);
  128. }
  129. })
  130. } else {
  131. contiousPay(method, pay_type);
  132. }
  133. }
  134. function contiousPay(method, pay_type) {
  135. const goods_id = '{{$goods->id}}';
  136. const coupon_sn = $('#coupon_sn').val();
  137. $.ajax({
  138. type: "POST",
  139. url: "/payment/purchase",
  140. async: false,
  141. data: {_token: '{{csrf_token()}}', goods_id: goods_id, coupon_sn: coupon_sn, method: method, pay_type: pay_type},
  142. dataType: 'json',
  143. success: function (ret) {
  144. if (ret.status === 'success') {
  145. swal.fire({
  146. title: ret.message,
  147. type: 'success',
  148. timer: 1300,
  149. showConfirmButton: false
  150. });
  151. if (method === 'balance') {
  152. swal.fire({title: ret.message, type: 'success', timer: 1000, showConfirmButton: false})
  153. .then(() => window.location.href = '/invoices')
  154. }
  155. if (ret.data) {
  156. window.location.href = '/payment/' + ret.data;
  157. } else if (ret.url) {
  158. window.location.href = ret.url;
  159. }
  160. } else if (ret.status === 'info') {
  161. swal.fire({title: ret.title, text: ret.message, type: 'question'});
  162. } else {
  163. swal.fire({
  164. title: ret.message,
  165. type: 'error'
  166. })
  167. }
  168. },
  169. error: function () {
  170. swal.fire('未知错误', '请开工单通知客服', 'error')
  171. }
  172. });
  173. }
  174. </script>
  175. @endsection