AppServiceProvider.php 6.1 KB

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