InitSettingCommand.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Console\Commands\Base;
  3. use App\Repositories\Models\Base\Admin;
  4. use App\Repositories\Models\Base\Dict;
  5. use App\Repositories\Models\Base\DictDetail;
  6. use App\Repositories\Models\Base\Role;
  7. use App\Repositories\Models\Base\Setting;
  8. use Illuminate\Console\Command;
  9. use Illuminate\Support\Arr;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Hash;
  12. /**
  13. * Class PresenterCommand
  14. * @package Prettus\Repository\Generators\Commands
  15. * @author Anderson Andrade <contato@andersonandra.de>
  16. */
  17. class InitSettingCommand extends Command
  18. {
  19. /**
  20. * The name of command.
  21. *
  22. * @var string
  23. */
  24. protected $signature = 'init:setting';
  25. /**
  26. * The description of command.
  27. *
  28. * @var string
  29. */
  30. protected $description = 'init setting';
  31. /**
  32. * The type of class being generated.
  33. *
  34. * @var string
  35. */
  36. protected $type = 'setting';
  37. /**
  38. * Execute the command.
  39. *
  40. * @return void
  41. * @see fire()
  42. */
  43. public function handle()
  44. {
  45. // $this->clear();
  46. $this->initSetting();
  47. $this->initDict();
  48. $this->initRole();
  49. $this->initUser();
  50. $this->line('配置初始化成功');
  51. }
  52. public function clear()
  53. {
  54. DB::statement('SET FOREIGN_KEY_CHECKS = 0'); // 禁用外键约束
  55. DB::table('base_admins')->truncate();
  56. DB::table('base_auth')->truncate();
  57. DB::table('base_banners')->truncate();
  58. DB::table('base_departments')->truncate();
  59. DB::table('base_dict_detail')->truncate();
  60. DB::table('base_dicts')->truncate();
  61. DB::table('base_model_has_roles')->truncate();
  62. DB::table('base_model_has_permissions')->truncate();
  63. DB::table('base_permissions')->truncate();
  64. DB::table('base_resources')->truncate();
  65. DB::table('base_role_has_permissions')->truncate();
  66. DB::table('base_roles')->truncate();
  67. DB::table('base_roles_data')->truncate();
  68. DB::table('base_roles_menus')->truncate();
  69. DB::table('base_settings')->truncate();
  70. DB::table('base_tasks')->truncate();
  71. DB::table('base_users')->truncate();
  72. // DB::table('base_category_settings')->truncate();
  73. DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // 启用外键约束
  74. }
  75. public function initUser()
  76. {
  77. $admin = Admin::query()->updateOrCreate([
  78. 'username' => 'superadmin',
  79. 'mobile' => '15225006562',
  80. ], [
  81. 'username' => 'superadmin',
  82. 'name' => '超级管理员',
  83. 'mobile' => '15225006562',
  84. 'type' => '1',
  85. 'email' => '751066209@qq.com',
  86. 'password' => Hash::make('Q1w2e3r4!'),
  87. ]);
  88. $admin->syncRoles('admin');
  89. }
  90. /**
  91. * 初始化字典
  92. * @return void
  93. */
  94. public function initDict()
  95. {
  96. $dicts = [];
  97. // $dicts[] = [
  98. // 'name' => '违章类型',
  99. // 'code' => 'VIOLATION_TYPE',
  100. // 'is_system' => 1,
  101. // 'options' => [
  102. // [
  103. // 'name' => '乱停乱放',
  104. // 'value' => 1,
  105. // ],
  106. // ]
  107. // ];
  108. DB::transaction(function () use ($dicts) {
  109. foreach ($dicts as $data) {
  110. $dict = Dict::query()->updateOrCreate(['code' => $data['code']], Arr::only($data, ['name', 'code', 'is_system']));
  111. foreach ($data['options'] as $option) {
  112. DictDetail::query()->updateOrCreate(['dict_id' => $dict['id'], 'name' => $option['name']], ['dict_id' => $dict['id'], 'name' => $option['name'], 'value' => $option['value']]);
  113. }
  114. }
  115. });
  116. }
  117. /**
  118. * 初始化配置
  119. * @return void
  120. */
  121. public function initSetting()
  122. {
  123. $settings[] = [
  124. 'name' => '系统上传图片允许格式',
  125. 'code' => 'system_upload_img_type',
  126. 'value' => 'png,jpg,gif,jpeg',
  127. 'is_system' => 1,
  128. ];
  129. $settings[] = [
  130. 'name' => '系统上传文件允许格式',
  131. 'code' => 'system_upload_file_type',
  132. 'value' => arr2str(["doc", 'docx', "xls", "pdf", 'xlsx', 'mp4', 'webvtt', 'vtt', "png", "jpg", "gif", 'jpeg'], ','),
  133. 'is_system' => 1,
  134. ];
  135. $settings[] = [
  136. 'name' => '锁屏时间',
  137. 'code' => 'system_auto_lock_minutes',
  138. 'value' => 30,
  139. 'is_system' => 1,
  140. ];
  141. $settings[] = [
  142. 'name' => '关于我们',
  143. 'code' => 'system_article_about_me',
  144. 'value' => '关于我们',
  145. 'is_system' => 1,
  146. ];
  147. // $settings[] = [
  148. // 'name' => '用户隐私协议',
  149. // 'code' => 'system_article_user_hidden',
  150. // 'value' => '用户隐私协议',
  151. // 'is_system' => 1,
  152. // ];
  153. //
  154. // $settings[] = [
  155. // 'name' => '用户协议',
  156. // 'code' => 'system_article_user',
  157. // 'value' => '用户协议',
  158. // 'is_system' => 1,
  159. // ];
  160. DB::transaction(function () use ($settings) {
  161. foreach ($settings as $setting) {
  162. Setting::query()->updateOrCreate(['code' => $setting['code']], $setting);
  163. }
  164. });
  165. }
  166. public function initRole()
  167. {
  168. $roles[] = [
  169. 'name' => 'admin',
  170. 'nickname' => '管理员',
  171. 'guard_name' => 'admins',
  172. 'is_system' => 1,
  173. ];
  174. $roles[] = [
  175. 'name' => 'user',
  176. 'nickname' => '巡检员',
  177. 'guard_name' => 'admins',
  178. 'is_system' => 1,
  179. ];
  180. DB::statement('SET FOREIGN_KEY_CHECKS = 0'); // 禁用外键约束
  181. DB::table('base_roles')->truncate();
  182. DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // 启用外键约束
  183. DB::transaction(function () use ($roles) {
  184. foreach ($roles as $role) {
  185. Role::query()->updateOrCreate(['name' => $role['name']], $role);
  186. }
  187. });
  188. }
  189. }