index.blade.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. @extends('base.base')
  2. @section('base')
  3. <meta name="csrf-token" content="{{ csrf_token() }}">
  4. <div class="container-fluid">
  5. <div class="row">
  6. <div class="col-12">
  7. <div class="card">
  8. <div class="card-header d-flex align-items-center justify-content-between">
  9. <h3 class="card-title mb-0">1234 权重配置 & 点击统计</h3>
  10. <small class="text-muted">配置 1/2/3/4 的权重(整数),前端按权重抽取,后台记录用户点击</small>
  11. </div>
  12. <div class="card-body">
  13. <ul class="nav nav-tabs mb-3" role="tablist">
  14. <li class="nav-item">
  15. <a class="nav-link active" data-toggle="tab" href="#tab-config" role="tab">
  16. <i class="mdi mdi-cog"></i> 权重配置
  17. </a>
  18. </li>
  19. <li class="nav-item">
  20. <a class="nav-link" data-toggle="tab" href="#tab-stats" role="tab">
  21. <i class="mdi mdi-chart-bar"></i> 点击统计
  22. </a>
  23. </li>
  24. </ul>
  25. <div class="tab-content">
  26. <div class="tab-pane fade show active" id="tab-config" role="tabpanel">
  27. @php
  28. $totalWeight = array_sum($config);
  29. @endphp
  30. <div class="alert alert-info">
  31. 当前总权重: <strong id="total-weight">{{ $totalWeight }}</strong>
  32. ,前端按权重比例随机返回 1/2/3/4
  33. </div>
  34. <div class="table-responsive">
  35. <table class="table table-striped table-hover align-middle mb-0" style="max-width: 600px;">
  36. <thead class="thead-light">
  37. <tr>
  38. <th style="width:80px;">ID</th>
  39. <th style="width:200px;">权重 (整数)</th>
  40. <th>占比</th>
  41. </tr>
  42. </thead>
  43. <tbody class="weight-form">
  44. @foreach([1,2,3,4] as $id)
  45. @php
  46. $w = intval($config[$id] ?? 0);
  47. $percent = $totalWeight > 0 ? round($w / $totalWeight * 100, 2) : 0;
  48. @endphp
  49. <tr>
  50. <td><span class="badge badge-primary">{{ $id }}</span></td>
  51. <td>
  52. <input type="number" min="0" class="form-control form-control-sm weight-input"
  53. data-id="{{ $id }}" name="weight[{{ $id }}]" value="{{ $w }}" />
  54. </td>
  55. <td>
  56. <span class="weight-percent" data-id="{{ $id }}">{{ $percent }}%</span>
  57. </td>
  58. </tr>
  59. @endforeach
  60. </tbody>
  61. </table>
  62. </div>
  63. <div class="d-flex align-items-center mt-4">
  64. <button class="btn btn-gradient-primary btn-sm" id="save-weights">
  65. <i class="mdi mdi-content-save"></i> 保存权重
  66. </button>
  67. <span class="save-status ml-3"></span>
  68. </div>
  69. </div>
  70. <div class="tab-pane fade" id="tab-stats" role="tabpanel">
  71. <div class="d-flex align-items-center justify-content-between mb-3">
  72. <h5 class="mb-0">总点击统计</h5>
  73. <button class="btn btn-sm btn-outline-danger" id="reset-stats">
  74. <i class="mdi mdi-delete"></i> 清除总统计
  75. </button>
  76. </div>
  77. @php
  78. $totalClicksAll = array_sum($totalClicks);
  79. @endphp
  80. <div class="table-responsive mb-4">
  81. <table class="table table-bordered align-middle" style="max-width: 600px;">
  82. <thead class="thead-light">
  83. <tr>
  84. <th>ID</th>
  85. <th>点击次数</th>
  86. <th>占比</th>
  87. </tr>
  88. </thead>
  89. <tbody>
  90. @foreach([1,2,3,4] as $id)
  91. @php
  92. $c = intval($totalClicks[$id] ?? 0);
  93. $p = $totalClicksAll > 0 ? round($c / $totalClicksAll * 100, 2) : 0;
  94. @endphp
  95. <tr>
  96. <td><span class="badge badge-info">{{ $id }}</span></td>
  97. <td>{{ $c }}</td>
  98. <td>{{ $p }}%</td>
  99. </tr>
  100. @endforeach
  101. <tr class="table-secondary">
  102. <td><strong>合计</strong></td>
  103. <td colspan="2"><strong>{{ $totalClicksAll }}</strong></td>
  104. </tr>
  105. </tbody>
  106. </table>
  107. </div>
  108. <h5 class="mb-3">最近 7 天每日点击</h5>
  109. <div class="table-responsive">
  110. <table class="table table-bordered align-middle" style="max-width: 800px;">
  111. <thead class="thead-light">
  112. <tr>
  113. <th>日期</th>
  114. <th>1</th>
  115. <th>2</th>
  116. <th>3</th>
  117. <th>4</th>
  118. <th>合计</th>
  119. </tr>
  120. </thead>
  121. <tbody>
  122. @foreach($dailyClicks as $row)
  123. @php
  124. $sum = intval($row[1]) + intval($row[2]) + intval($row[3]) + intval($row[4]);
  125. @endphp
  126. <tr>
  127. <td>{{ $row['date'] }}</td>
  128. <td>{{ $row[1] }}</td>
  129. <td>{{ $row[2] }}</td>
  130. <td>{{ $row[3] }}</td>
  131. <td>{{ $row[4] }}</td>
  132. <td><strong>{{ $sum }}</strong></td>
  133. </tr>
  134. @endforeach
  135. </tbody>
  136. </table>
  137. </div>
  138. </div>
  139. </div>
  140. </div>
  141. </div>
  142. </div>
  143. </div>
  144. </div>
  145. <script>
  146. $(function() {
  147. function refreshPercents() {
  148. let total = 0;
  149. $('.weight-input').each(function(){
  150. total += parseInt($(this).val() || 0);
  151. });
  152. $('#total-weight').text(total);
  153. $('.weight-input').each(function(){
  154. const id = $(this).data('id');
  155. const w = parseInt($(this).val() || 0);
  156. const p = total > 0 ? (w / total * 100).toFixed(2) : 0;
  157. $('.weight-percent[data-id="' + id + '"]').text(p + '%');
  158. });
  159. }
  160. $('.weight-input').on('input change', refreshPercents);
  161. $('#save-weights').click(function() {
  162. const $btn = $(this);
  163. const $status = $btn.siblings('.save-status');
  164. const config = {};
  165. $('.weight-input').each(function(){
  166. const id = $(this).data('id');
  167. let v = parseInt($(this).val() || 0);
  168. if (isNaN(v) || v < 0) v = 0;
  169. config[id] = v;
  170. });
  171. $btn.prop('disabled', true).html('<i class="fa fa-spinner fa-spin"></i> 保存中...');
  172. $status.removeClass('text-success text-danger').text('');
  173. $.post("{{ url('/admin/weight-config/update') }}", {
  174. config: JSON.stringify(config),
  175. _token: "{{ csrf_token() }}"
  176. }).done(function(res){
  177. $btn.prop('disabled', false).html('<i class="mdi mdi-content-save"></i> 保存权重');
  178. if (res.status === 'success') {
  179. $status.text('更新成功').addClass('text-success');
  180. } else {
  181. $status.text(res.message || '更新失败').addClass('text-danger');
  182. }
  183. setTimeout(function(){ $status.fadeOut(function(){ $(this).text('').show().removeClass('text-success text-danger'); }); }, 3000);
  184. }).fail(function(){
  185. $btn.prop('disabled', false).html('<i class="mdi mdi-content-save"></i> 保存权重');
  186. $status.text('系统错误').addClass('text-danger');
  187. });
  188. });
  189. $('#reset-stats').click(function(){
  190. if (!confirm('确认清除总点击统计?(每日明细仍保留)')) return;
  191. $.post("{{ url('/admin/weight-config/reset-stats') }}", {
  192. type: 'total',
  193. _token: "{{ csrf_token() }}"
  194. }).done(function(res){
  195. if (res.status === 'success') {
  196. location.reload();
  197. } else {
  198. alert(res.message || '操作失败');
  199. }
  200. }).fail(function(){
  201. alert('系统错误');
  202. });
  203. });
  204. });
  205. </script>
  206. <style>
  207. .table thead th { white-space: nowrap; }
  208. .table tbody td { vertical-align: middle; }
  209. .badge { font-size: .85rem; padding: .5em .6em; }
  210. </style>
  211. @endsection