* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ namespace App\Providers; use App\Observers\BillObserver; use App\Observers\OrderObserver; use App\Observers\UserObserver; use App\Repositories\Models\Base\Setting; use App\Repositories\Models\Car\Bill; use App\Repositories\Models\Car\Order; use App\Repositories\Models\User; use App\Support\Sdk\Ai\AiCAS; 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(); // 注册观察者 $this->app->singleton('AiCAS', function ($app) { return new AiCAS(); }); Validator::extend('mobile', function ($attribute, $value, $parameters) { return preg_match('/^1[3456789][0-9]{9}$/', $value); }, '手机号不合法'); Validator::extend('password_level', function ($attribute, $value, $parameters) { return check_password($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']; $setting = Setting::byCodesGetSettings(['system_baidu_sms_AccessKey', 'system_baidu_sms_SecretKey', 'system_baidu_sms_signatureId']); $config['gateways']['baidu']['ak'] = $setting['system_baidu_sms_AccessKey']; $config['gateways']['baidu']['sk'] = $setting['system_baidu_sms_SecretKey']; $config['gateways']['baidu']['invoke_id'] = $setting['system_baidu_sms_signatureId']; 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 { } }