buy.blade.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. <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('home.coupon')}}"/>
  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('home.redeem_coupon')}}
  41. </button>
  42. </div>
  43. </div>
  44. </div>
  45. <div class="col-lg-3 offset-lg-6 text-right">
  46. <p>{{trans('home.service_subtotal_price')}}
  47. <span>¥{{$goods->price}}</span>
  48. </p>
  49. <p class="page-invoice-amount">{{trans('home.service_total_price')}}
  50. <span class="grand-total">¥{{$goods->price}}</span>
  51. </p>
  52. </div>
  53. @endif
  54. <div class="col-md-12 mb-30">
  55. <div class="float-right">
  56. <div class="btn-group btn-group-lg">
  57. @include('user.components.purchase')
  58. @if($goods->type <= 2)
  59. <button class="btn btn-flat" onclick="pay('credit','0')">
  60. <img src="/assets/images/payment/creditpay.svg" height="48px" alt="{{trans('home.service_pay_button')}}"/>
  61. </button>
  62. @endif
  63. </div>
  64. </div>
  65. </div>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. @endsection
  71. @section('script')
  72. <script type="text/javascript">
  73. // 校验优惠券是否可用
  74. function redeemCoupon() {
  75. const coupon_sn = $('#coupon_sn').val();
  76. const goods_price = '{{$goods->price}}';
  77. $.ajax({
  78. method: 'POST',
  79. url: '{{route('redeemCoupon')}}',
  80. async: false,
  81. data: {_token: '{{csrf_token()}}', coupon_sn: coupon_sn, price: '{{$goods->price}}'},
  82. dataType: 'json',
  83. success: function(ret) {
  84. $('.input-group-prepend').remove();
  85. if (ret.status === 'success') {
  86. $('#coupon_sn').
  87. parent().
  88. prepend(
  89. '<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>');
  90. // 根据类型计算折扣后的总金额
  91. let total_price = 0;
  92. if (ret.data.type === 2) {
  93. total_price = goods_price * (1 - ret.data.value / 100);
  94. $('.page-invoice-amount').
  95. parent().
  96. prepend('<p>优惠码 - ' + ret.data.name + ' ' + ret.data.value + '折<br> 优惠 <span>¥ - ' +
  97. total_price.toFixed(2) + '</span></p>');
  98. total_price = goods_price - total_price;
  99. }
  100. else {
  101. total_price = goods_price - ret.data.value;
  102. total_price = total_price > 0 ? total_price : 0;
  103. if (ret.data.type === 1) {
  104. $('.page-invoice-amount').
  105. parent().
  106. prepend('优惠码-' + ret.data.name + ' <span>¥ - ' + ret.data.value + '</span>');
  107. }
  108. }
  109. // 四舍五入,保留2位小数
  110. $('.grand-total').text('¥' + total_price.toFixed(2));
  111. swal.fire({
  112. title: ret.message,
  113. type: 'success',
  114. timer: 1300,
  115. showConfirmButton: false,
  116. });
  117. }
  118. else {
  119. $('.grand-total').text('¥' + goods_price);
  120. $('#coupon_sn').
  121. parent().
  122. prepend(
  123. '<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>');
  124. swal.fire({
  125. title: ret.title,
  126. text: ret.message,
  127. type: 'error',
  128. });
  129. }
  130. },
  131. });
  132. }
  133. // 检查预支付
  134. function pay(method, pay_type) {
  135. // 存在套餐 和 购买类型为套餐时 出现提示
  136. if ('{{$activePlan}}' === '1' && '{{$goods->type}}' === '2') {
  137. swal.fire({
  138. title: '套餐存在冲突',
  139. html: '<p>当前购买套餐将自动设置为 <code>预支付套餐</code><p><ol class="text-left"><li> 预支付套餐会在生效中的套餐失效后自动开通!</li><li> 您可以在支付后手动激活套餐!</li></ol>',
  140. type: 'info',
  141. showCancelButton: true,
  142. cancelButtonText: '返 回',
  143. confirmButtonText: '继 续',
  144. }).then((result) => {
  145. if (result.value) {
  146. contiousPay(method, pay_type);
  147. }
  148. });
  149. }
  150. else {
  151. contiousPay(method, pay_type);
  152. }
  153. }
  154. function contiousPay(method, pay_type) {
  155. const goods_id = '{{$goods->id}}';
  156. const coupon_sn = $('#coupon_sn').val();
  157. $.ajax({
  158. method: 'POST',
  159. url: '{{route('purchase')}}',
  160. async: false,
  161. data: {
  162. _token: '{{csrf_token()}}',
  163. goods_id: goods_id,
  164. coupon_sn: coupon_sn,
  165. method: method,
  166. pay_type: pay_type,
  167. },
  168. dataType: 'json',
  169. success: function(ret) {
  170. if (ret.status === 'success') {
  171. swal.fire({
  172. title: ret.message,
  173. type: 'success',
  174. timer: 1300,
  175. showConfirmButton: false,
  176. });
  177. if (method === 'credit') {
  178. swal.fire({title: ret.message, type: 'success', timer: 1000, showConfirmButton: false}).then(() => window.location.href = '{{route('invoice')}}');
  179. }
  180. if (ret.data) {
  181. window.location.href = '{{route('orderDetail', '')}}/' + ret.data;
  182. }
  183. else if (ret.url) {
  184. window.location.href = ret.url;
  185. }
  186. }
  187. else if (ret.status === 'info') {
  188. swal.fire({title: ret.title, text: ret.message, type: 'question'});
  189. }
  190. else {
  191. swal.fire({
  192. title: ret.message,
  193. type: 'error',
  194. });
  195. }
  196. },
  197. error: function() {
  198. swal.fire('未知错误', '请开工单通知客服', 'error');
  199. },
  200. });
  201. }
  202. </script>
  203. @endsection