123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209 |
- <?php
- namespace App\Console\Commands\Base;
- use App\Repositories\Models\Base\Admin;
- use App\Repositories\Models\Base\Dict;
- use App\Repositories\Models\Base\DictDetail;
- use App\Repositories\Models\Base\Role;
- use App\Repositories\Models\Base\Setting;
- use Illuminate\Console\Command;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Hash;
- /**
- * Class PresenterCommand
- * @package Prettus\Repository\Generators\Commands
- * @author Anderson Andrade <contato@andersonandra.de>
- */
- 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);
- }
- });
- }
- }
|