AppServiceProvider.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /*
  3. * This file is part of the Jiannei/lumen-api-starter.
  4. *
  5. * (c) Jiannei <longjian.huang@foxmail.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace App\Providers;
  11. use Carbon\Carbon;
  12. use Illuminate\Support\Facades\App;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. use Illuminate\Support\Facades\Validator;
  16. use Illuminate\Support\ServiceProvider;
  17. use Overtrue\EasySms\EasySms;
  18. class AppServiceProvider extends ServiceProvider
  19. {
  20. /**
  21. * Register any application services.
  22. */
  23. public function register()
  24. {
  25. }
  26. /**
  27. * Bootstrap any application services.
  28. *
  29. * @return void
  30. */
  31. public function boot()
  32. {
  33. $this->registerObservers(); // 注册观察者
  34. Validator::extend('mobile', function ($attribute, $value, $parameters) {
  35. return preg_match('/^1[3456789][0-9]{9}$/', $value);
  36. }, '手机号不合法');
  37. Validator::extend('password_level', function ($attribute, $value, $parameters) {
  38. return check_password($value, true);
  39. }, '密码太简单了【必须包含数字、字母、符号两种类型】');
  40. Validator::extend('bank_card_no', function ($attribute, $value, $parameters) {
  41. if (!preg_match('/^\d{16,19}$/', $value)) return false;
  42. $len = strlen($value);
  43. $all = [];
  44. $sum_odd = 0;
  45. $sum_even = 0;
  46. for ($i = 0; $i < $len; $i++) {
  47. $all[] = substr($value, $len - $i - 1, 1);
  48. }
  49. //all 里的偶数key都是我们要相加的奇数位
  50. for ($k = 0; $k < $len; $k++) {
  51. if ($k % 2 == 0) {
  52. $sum_odd += $all[$k];
  53. } else {
  54. //奇数key都是要相加的偶数和
  55. if ($all[$k] * 2 >= 10) {
  56. $sum_even += $all[$k] * 2 - 9;
  57. } else {
  58. $sum_even += $all[$k] * 2;
  59. }
  60. }
  61. }
  62. $total = $sum_odd + $sum_even;
  63. if ($total % 10 == 0) {
  64. return true;
  65. } else {
  66. return false;
  67. }
  68. }, '银行卡号不合法');
  69. Validator::extend('id_card', function ($attribute, $value, $parameters) {
  70. if (18 != strlen($value)) {
  71. return false;
  72. }
  73. $weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
  74. $code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
  75. $mode = 0;
  76. $ver = substr($value, -1);
  77. if ($ver == 'x') {
  78. $ver = 'X';
  79. }
  80. foreach ($weight as $key => $val) {
  81. if ($key == 17) {
  82. continue;
  83. }
  84. $digit = intval(substr($value, $key, 1));
  85. $mode += $digit * $val;
  86. }
  87. $mode %= 11;
  88. if ($ver != $code[$mode]) {
  89. return false;
  90. }
  91. $date = substr($value, 6, 8);
  92. $year = substr($date, 0, 4);
  93. $month = substr($date, 4, 2);
  94. $day = substr($date, 6);
  95. $check = checkdate($month, $day, $year);
  96. if (!$check) {
  97. return false;
  98. }
  99. $today = date('Ymd');
  100. $date = substr($value, 6, 8);
  101. if ($date >= $today) {
  102. return false;
  103. }
  104. return true;
  105. }, '身份证号不合法');
  106. //记录执行效率
  107. DB::listen(function ($query) {
  108. if ($query->time <= 1000) {
  109. return;
  110. }
  111. $location = collect(debug_backtrace())->filter(function ($trace) {
  112. if (!isset($trace['file'])) {
  113. return [
  114. 'file' => '',
  115. 'line' => 0,
  116. ];
  117. }
  118. return !str_contains($trace['file'], 'vendor/');
  119. })->first(); // grab the first element of non vendor/ calls
  120. if (!isset($location['file'])) {
  121. $location = [
  122. 'file' => '',
  123. 'line' => 0,
  124. ];
  125. }
  126. // $bindings = implode(", ", $query->bindings); // format the bindings as string
  127. try {
  128. if (count($query->bindings)) {
  129. $sql = vsprintf(str_replace("?", "'%s'", str_replace("%", '%%', $query->sql)), $query->bindings);
  130. } else {
  131. $sql = $query->sql;
  132. }
  133. } catch (\Exception $exception) {
  134. $bindings = implode(", ", $query->bindings);
  135. Log::info("
  136. ------ERROR------
  137. Sql: $query->sql
  138. Bindings: $bindings
  139. ------------
  140. ");
  141. Log::error($exception);
  142. return;
  143. }
  144. Log::info("
  145. ------------
  146. Sql: $sql
  147. Time: $query->time
  148. File: ${location['file']}
  149. Line: ${location['line']}
  150. ------------
  151. ");
  152. });
  153. }
  154. protected function registerObservers(): void
  155. {
  156. }
  157. }