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