MiniprogramController.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use Illuminate\Http\Request;
  5. class MiniprogramController extends Controller
  6. {
  7. public function actiondata(Request $request)
  8. {
  9. try {
  10. $this->validate($request, [
  11. 'start_at' => 'required',
  12. 'end_at' => 'required',
  13. 'public_id' => 'required'
  14. ]);
  15. $public_id = $request->get('public_id') ?? 0;
  16. $result = array(
  17. 'total' => [
  18. 'all' => [
  19. 'pv' => 0,
  20. 'uv' => 0,
  21. ],
  22. '1x' => [
  23. 'pv' => 0,
  24. 'uv' => 0,
  25. ],
  26. '2x' => [
  27. 'pv' => 0,
  28. 'uv' => 0,
  29. ],
  30. '3x' => [
  31. 'pv' => 0,
  32. 'uv' => 0,
  33. ],
  34. '4x' => [
  35. 'pv' => 0,
  36. 'uv' => 0,
  37. ],
  38. ],
  39. 'list' => [
  40. '1x' => [],
  41. '2x' => [],
  42. '3x' => [],
  43. '4x' => []
  44. ],
  45. );
  46. \DB::enableQueryLog();
  47. $uvall = \DB::select(
  48. "SELECT COUNT(DISTINCT(`uid`)) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `action_id` BETWEEN ? and ? and `created_at` BETWEEN ? and ?",
  49. [
  50. $public_id,
  51. 10000,
  52. 49999,
  53. strtotime($request->post('start_at')),
  54. strtotime($request->post('end_at')) + 86400
  55. ]
  56. );
  57. $uv1x = \DB::select(
  58. "SELECT COUNT(DISTINCT(`uid`)) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `action_id` BETWEEN ? and ? and `created_at` BETWEEN ? and ?",
  59. [
  60. $public_id,
  61. 10000,
  62. 19999,
  63. strtotime($request->post('start_at')),
  64. strtotime($request->post('end_at')) + 86400
  65. ]
  66. );
  67. $uv2x = \DB::select(
  68. "SELECT COUNT(DISTINCT(`uid`)) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `action_id` BETWEEN ? and ? and `created_at` BETWEEN ? and ?",
  69. [
  70. $public_id,
  71. 20000,
  72. 29999,
  73. strtotime($request->post('start_at')),
  74. strtotime($request->post('end_at')) + 86400
  75. ]
  76. );
  77. $uv3x = \DB::select(
  78. "SELECT COUNT(DISTINCT(`uid`)) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `action_id` BETWEEN ? and ? and `created_at` BETWEEN ? and ?",
  79. [
  80. $public_id,
  81. 30000,
  82. 39999,
  83. strtotime($request->post('start_at')),
  84. strtotime($request->post('end_at')) + 86400
  85. ]
  86. );
  87. $uv4x = \DB::select(
  88. "SELECT COUNT(DISTINCT(`uid`)) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `action_id` BETWEEN ? and ? and `created_at` BETWEEN ? and ?",
  89. [
  90. $public_id,
  91. 40000,
  92. 49999,
  93. strtotime($request->post('start_at')),
  94. strtotime($request->post('end_at')) + 86400
  95. ]
  96. );
  97. $result['total']['all']['uv'] = $uvall[0]->c;
  98. $result['total']['1x']['uv'] = $uv1x[0]->c;
  99. $result['total']['2x']['uv'] = $uv2x[0]->c;
  100. $result['total']['3x']['uv'] = $uv3x[0]->c;
  101. $result['total']['4x']['uv'] = $uv4x[0]->c;
  102. $pvs = \DB::select(
  103. "SELECT `action_id`, COUNT(*) as c FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `created_at` BETWEEN ? and ? GROUP BY `action_id`",
  104. [$public_id, strtotime($request->post('start_at')), strtotime($request->post('end_at')) + 86400]
  105. );
  106. foreach ($pvs as $v) {
  107. if ($v->action_id >= 10000 && $v->action_id <= 19999) {
  108. $result['total']['all']['pv'] += $v->c;
  109. $result['total']['1x']['pv'] += $v->c;
  110. $result['list']['1x'][$v->action_id]['pv'] = $v->c;
  111. }
  112. if ($v->action_id >= 20000 && $v->action_id <= 29999) {
  113. $result['total']['all']['pv'] += $v->c;
  114. $result['total']['2x']['pv'] += $v->c;
  115. $result['list']['2x'][$v->action_id]['pv'] = $v->c;
  116. }
  117. if ($v->action_id >= 30000 && $v->action_id <= 39999) {
  118. $result['total']['all']['pv'] += $v->c;
  119. $result['total']['3x']['pv'] += $v->c;
  120. $result['list']['3x'][$v->action_id]['pv'] = $v->c;
  121. }
  122. if ($v->action_id >= 40000 && $v->action_id <= 49999) {
  123. $result['total']['all']['pv'] += $v->c;
  124. $result['total']['4x']['pv'] += $v->c;
  125. $result['list']['4x'][$v->action_id]['pv'] = $v->c;
  126. }
  127. }
  128. $uvs = \DB::select(
  129. "SELECT `action_id`, COUNT(*) as c FROM( SELECT DISTINCT(`uid`), `action_id` FROM `kdgx_charge_fpdx_miniprogram_formid` WHERE `public_id`= ? and `created_at` BETWEEN ? and ?) as tab GROUP BY`action_id`",
  130. [$public_id, strtotime($request->post('start_at')), strtotime($request->post('end_at')) + 86400]
  131. );
  132. foreach ($uvs as $v) {
  133. if ($v->action_id >= 10000 && $v->action_id <= 19999) {
  134. $result['list']['1x'][$v->action_id]['uv'] = $v->c;
  135. }
  136. if ($v->action_id >= 20000 && $v->action_id <= 29999) {
  137. $result['list']['2x'][$v->action_id]['uv'] = $v->c;
  138. }
  139. if ($v->action_id >= 30000 && $v->action_id <= 39999) {
  140. $result['list']['3x'][$v->action_id]['uv'] = $v->c;
  141. }
  142. if ($v->action_id >= 40000 && $v->action_id <= 49999) {
  143. $result['list']['4x'][$v->action_id]['uv'] = $v->c;
  144. }
  145. }
  146. return array(
  147. 'code' => 200,
  148. 'message' => 'success',
  149. 'data' => $result
  150. );
  151. } catch (\Exception $e) {
  152. throw new \ApiException($e->getMessage(), 501);
  153. }
  154. }
  155. }