AppServiceProvider.php 6.1 KB

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