123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 |
- <?php
- /*
- * This file is part of the Jiannei/lumen-api-starter.
- *
- * (c) Jiannei <longjian.huang@foxmail.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace App\Providers;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\Validator;
- use Illuminate\Support\ServiceProvider;
- use Overtrue\EasySms\EasySms;
- class AppServiceProvider extends ServiceProvider
- {
- /**
- * Register any application services.
- */
- public function register()
- {
- }
- /**
- * Bootstrap any application services.
- *
- * @return void
- */
- public function boot()
- {
- $this->registerObservers(); // 注册观察者
- Validator::extend('mobile', function ($attribute, $value, $parameters) {
- return preg_match('/^1[3456789][0-9]{9}$/', $value);
- }, '手机号不合法');
- Validator::extend('mobileTW', function ($attribute, $value, $parameters) {
- return preg_match('/^[0]{1}[9]{1}\d{8}$/', $value);
- }, '手机号不合法');
- Validator::extend('mobileOM', function ($attribute, $value, $parameters) {
- return preg_match('/^[6]\d{7}$/', $value);
- }, '手机号不合法');
- Validator::extend('mobileXG', function ($attribute, $value, $parameters) {
- return preg_match('/^([6|9|5])\d{7}$/', $value);
- }, '手机号不合法');
- Validator::extend('password_level', function ($attribute, $value, $parameters) {
- return check_password($value);
- }, '密码太简单了【必须包含数字、字母、符号两种类型】');
- Validator::extend('zh', function ($attribute, $value, $parameters) {
- return preg_match('/^[\x{4e00}-\x{9fa5}]+$/u', $value);
- }, '必须为中文');
- Validator::extend('bank_card_no', function ($attribute, $value, $parameters) {
- if (!preg_match('/^\d{16,19}$/', $value)) return false;
- $len = strlen($value);
- $all = [];
- $sum_odd = 0;
- $sum_even = 0;
- for ($i = 0; $i < $len; $i++) {
- $all[] = substr($value, $len - $i - 1, 1);
- }
- //all 里的偶数key都是我们要相加的奇数位
- for ($k = 0; $k < $len; $k++) {
- if ($k % 2 == 0) {
- $sum_odd += $all[$k];
- } else {
- //奇数key都是要相加的偶数和
- if ($all[$k] * 2 >= 10) {
- $sum_even += $all[$k] * 2 - 9;
- } else {
- $sum_even += $all[$k] * 2;
- }
- }
- }
- $total = $sum_odd + $sum_even;
- if ($total % 10 == 0) {
- return true;
- } else {
- return false;
- }
- }, '银行卡号不合法');
- Validator::extend('id_card', function ($attribute, $value, $parameters) {
- if (18 != strlen($value)) {
- return false;
- }
- $weight = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
- $code = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
- $mode = 0;
- $ver = substr($value, -1);
- if ($ver == 'x') {
- $ver = 'X';
- }
- foreach ($weight as $key => $val) {
- if ($key == 17) {
- continue;
- }
- $digit = intval(substr($value, $key, 1));
- $mode += $digit * $val;
- }
- $mode %= 11;
- if ($ver != $code[$mode]) {
- return false;
- }
- $date = substr($value, 6, 8);
- $year = substr($date, 0, 4);
- $month = substr($date, 4, 2);
- $day = substr($date, 6);
- $check = checkdate($month, $day, $year);
- if (!$check) {
- return false;
- }
- $today = date('Ymd');
- $date = substr($value, 6, 8);
- if ($date >= $today) {
- return false;
- }
- return true;
- }, '身份证号不合法');
- //短信
- $this->app->singleton('easy_sms', function ($app) {
- $config = $app->config['sms'];
- return (new EasySms($config));
- });
- //记录执行效率
- DB::listen(function ($query) {
- if ($query->time <= 1000) {
- return;
- }
- $location = collect(debug_backtrace())->filter(function ($trace) {
- if (!isset($trace['file'])) {
- return [
- 'file' => '',
- 'line' => 0,
- ];
- }
- return !str_contains($trace['file'], 'vendor/');
- })->first(); // grab the first element of non vendor/ calls
- if (!isset($location['file'])) {
- $location = [
- 'file' => '',
- 'line' => 0,
- ];
- }
- // $bindings = implode(", ", $query->bindings); // format the bindings as string
- try {
- if (count($query->bindings)) {
- $sql = vsprintf(str_replace("?", "'%s'", str_replace("%", '%%', $query->sql)), $query->bindings);
- } else {
- $sql = $query->sql;
- }
- } catch (\Exception $exception) {
- $bindings = implode(", ", $query->bindings);
- Log::info("
- ------ERROR------
- Sql: $query->sql
- Bindings: $bindings
- ------------
- ");
- Log::error($exception);
- return;
- }
- Log::info("
- ------------
- Sql: $sql
- Time: $query->time
- File: ${location['file']}
- Line: ${location['line']}
- ------------
- ");
- });
- }
- protected function registerObservers(): void
- {
- }
- }
|