*/ class InitSettingCommand extends Command { /** * The name of command. * * @var string */ protected $signature = 'init:setting'; /** * The description of command. * * @var string */ protected $description = 'init setting'; /** * The type of class being generated. * * @var string */ protected $type = 'setting'; /** * Execute the command. * * @return void * @see fire() */ public function handle() { // $this->clear(); $this->initSetting(); $this->initDict(); $this->initRole(); $this->initUser(); $this->line('配置初始化成功'); } public function clear() { DB::statement('SET FOREIGN_KEY_CHECKS = 0'); // 禁用外键约束 DB::table('base_admins')->truncate(); DB::table('base_auth')->truncate(); DB::table('base_banners')->truncate(); DB::table('base_departments')->truncate(); DB::table('base_dict_detail')->truncate(); DB::table('base_dicts')->truncate(); DB::table('base_model_has_roles')->truncate(); DB::table('base_model_has_permissions')->truncate(); DB::table('base_permissions')->truncate(); DB::table('base_resources')->truncate(); DB::table('base_role_has_permissions')->truncate(); DB::table('base_roles')->truncate(); DB::table('base_roles_data')->truncate(); DB::table('base_roles_menus')->truncate(); DB::table('base_settings')->truncate(); DB::table('base_tasks')->truncate(); DB::table('base_users')->truncate(); // DB::table('base_category_settings')->truncate(); DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // 启用外键约束 } public function initUser() { $admin = Admin::query()->updateOrCreate([ 'username' => 'superadmin', 'mobile' => '15225006562', ], [ 'username' => 'superadmin', 'name' => '超级管理员', 'mobile' => '15225006562', 'type' => '1', 'email' => '751066209@qq.com', 'password' => Hash::make('Q1w2e3r4!'), ]); $admin->syncRoles('admin'); } /** * 初始化字典 * @return void */ public function initDict() { $dicts = []; // $dicts[] = [ // 'name' => '违章类型', // 'code' => 'VIOLATION_TYPE', // 'is_system' => 1, // 'options' => [ // [ // 'name' => '乱停乱放', // 'value' => 1, // ], // ] // ]; DB::transaction(function () use ($dicts) { foreach ($dicts as $data) { $dict = Dict::query()->updateOrCreate(['code' => $data['code']], Arr::only($data, ['name', 'code', 'is_system'])); foreach ($data['options'] as $option) { DictDetail::query()->updateOrCreate(['dict_id' => $dict['id'], 'name' => $option['name']], ['dict_id' => $dict['id'], 'name' => $option['name'], 'value' => $option['value']]); } } }); } /** * 初始化配置 * @return void */ public function initSetting() { $settings[] = [ 'name' => '系统上传图片允许格式', 'code' => 'system_upload_img_type', 'value' => 'png,jpg,gif,jpeg', 'is_system' => 1, ]; $settings[] = [ 'name' => '系统上传文件允许格式', 'code' => 'system_upload_file_type', 'value' => arr2str(["doc", 'docx', "xls", "pdf", 'xlsx', 'mp4', 'webvtt', 'vtt', "png", "jpg", "gif", 'jpeg'], ','), 'is_system' => 1, ]; $settings[] = [ 'name' => '锁屏时间', 'code' => 'system_auto_lock_minutes', 'value' => 30, 'is_system' => 1, ]; $settings[] = [ 'name' => '关于我们', 'code' => 'system_article_about_me', 'value' => '关于我们', 'is_system' => 1, ]; // $settings[] = [ // 'name' => '用户隐私协议', // 'code' => 'system_article_user_hidden', // 'value' => '用户隐私协议', // 'is_system' => 1, // ]; // // $settings[] = [ // 'name' => '用户协议', // 'code' => 'system_article_user', // 'value' => '用户协议', // 'is_system' => 1, // ]; DB::transaction(function () use ($settings) { foreach ($settings as $setting) { Setting::query()->updateOrCreate(['code' => $setting['code']], $setting); } }); } public function initRole() { $roles[] = [ 'name' => 'admin', 'nickname' => '管理员', 'guard_name' => 'admins', 'is_system' => 1, ]; $roles[] = [ 'name' => 'user', 'nickname' => '巡检员', 'guard_name' => 'admins', 'is_system' => 1, ]; DB::statement('SET FOREIGN_KEY_CHECKS = 0'); // 禁用外键约束 DB::table('base_roles')->truncate(); DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // 启用外键约束 DB::transaction(function () use ($roles) { foreach ($roles as $role) { Role::query()->updateOrCreate(['name' => $role['name']], $role); } }); } }