201 lines
6.6 KiB
PHP
201 lines
6.6 KiB
PHP
<?php
|
||
|
||
namespace Database\Seeders;
|
||
|
||
use Dcat\Admin\Models\Permission;
|
||
use Illuminate\Database\Seeder;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Log;
|
||
|
||
class AdminPermissionSeeder extends Seeder
|
||
{
|
||
/**
|
||
* Run the database seeds.
|
||
*
|
||
* @return void
|
||
*/
|
||
public function run()
|
||
{
|
||
//
|
||
$permissions = [
|
||
/** 示例 **/
|
||
// 'test'=> [
|
||
// 'name' => '测试权限',
|
||
// 'curd' => true, //true默认全部,false为不需要, ['index']为仅需要的
|
||
// 'children' => [
|
||
// 'show' => [
|
||
// 'name' => '详情',
|
||
// ],
|
||
// ],
|
||
// ],
|
||
'management'=> [
|
||
'name' => '系统管理',
|
||
'curd' => false,
|
||
'children' => [
|
||
'administrators' => [
|
||
'name' => '管理员',
|
||
'curd' => true,
|
||
],
|
||
'roles' => [
|
||
'name' => '角色',
|
||
'curd' => true,
|
||
],
|
||
'permissions' => [
|
||
'name' => '权限',
|
||
'curd' => true,
|
||
],
|
||
'menus' => [
|
||
'name' => '菜单',
|
||
'curd' => true,
|
||
],
|
||
],
|
||
],
|
||
'ad_addresses'=>[
|
||
'name'=>'广告位',
|
||
'curd' => ['index', 'create', 'store', 'edit', 'update', 'destroy'],
|
||
],
|
||
'ads'=>[
|
||
'name'=>'广告内容',
|
||
'curd' => ['index', 'create', 'store', 'edit', 'update', 'destroy'],
|
||
],
|
||
'article_categories' =>[
|
||
'name'=>'文章分类',
|
||
'curd' => ['index', 'create', 'store', 'edit', 'update', 'destroy'],
|
||
],
|
||
'articles'=>[
|
||
'name'=>'文章',
|
||
'curd' => true,
|
||
],
|
||
'product_categories'=>[
|
||
'name' =>'商品分类',
|
||
'curd'=>['index', 'create', 'store', 'edit', 'update', 'destroy'],
|
||
],
|
||
'product_groups'=>[
|
||
'name' =>'商品分组',
|
||
'curd'=>['index', 'create', 'store', 'edit', 'update', 'destroy'],
|
||
],
|
||
'product_features'=>[
|
||
'name' =>'商品特点',
|
||
'curd' =>['index', 'create', 'store', 'edit', 'update', 'destroy'],
|
||
],
|
||
'product_buynotes'=>[
|
||
'name' =>'商品购买须知',
|
||
'curd'=>['index', 'create', 'store', 'edit', 'update', 'destroy'],
|
||
],
|
||
'product_spus'=>[
|
||
'name' =>'主商品',
|
||
'curd'=> true,
|
||
'children'=>[
|
||
'list'=>[
|
||
'name'=>'子商品列表',
|
||
],
|
||
'setting_specs'=>[
|
||
'name'=>'设置规格',
|
||
],
|
||
'init_sku_by_specs'=>[
|
||
'name' =>'初始化SKU',
|
||
],
|
||
'add_sku'=>[
|
||
'name' =>'添加子商品',
|
||
],
|
||
],
|
||
],
|
||
'product_skus'=>[
|
||
'name' =>'子商品',
|
||
'curd' =>['index', 'edit', 'update', 'destroy'],
|
||
'children'=>[
|
||
'batch_release_up' => [
|
||
'name'=>'批量上架申请',
|
||
],
|
||
'batch_release_down'=>[
|
||
'name' =>'批量下架',
|
||
],
|
||
'batch_release_cacel'=>[
|
||
'name' =>'批量取消申请',
|
||
],
|
||
'batch_sku_sync_spu'=>[
|
||
'name' =>'批量同步主商品',
|
||
],
|
||
],
|
||
],
|
||
'product_sku_verifies'=>[
|
||
'name' =>'商品审核',
|
||
'curd'=>['index', 'destroy'],
|
||
'children' => [
|
||
'verify'=>[
|
||
'name' =>'审核商品',
|
||
],
|
||
'batch_verify'=>[
|
||
'name' =>'批量审核',
|
||
],
|
||
],
|
||
],
|
||
];
|
||
try {
|
||
DB::begintransaction();
|
||
$this->createPermissionData($permissions);
|
||
DB::commit();
|
||
} catch (\Exception $e) {
|
||
Log::error('权限录入:'.$e->getMessage());
|
||
DB::rollBack();
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 插入权限
|
||
*
|
||
* @param array $permissions
|
||
* @param string $key
|
||
* @param integer $pId
|
||
*/
|
||
public function createPermissionData(array $permissions, string $key = '', int $pId = 0)
|
||
{
|
||
$curdArr = [
|
||
'index'=>[
|
||
'name' => '列表',
|
||
],
|
||
'create'=>[
|
||
'name' => '新增',
|
||
],
|
||
'store' =>[
|
||
'name' => '保存',
|
||
],
|
||
'edit' =>[
|
||
'name' => '修改',
|
||
],
|
||
'update'=>[
|
||
'name' => '更新',
|
||
],
|
||
'destroy'=>[
|
||
'name' => '删除',
|
||
],
|
||
'show'=>[
|
||
'name' => '详情',
|
||
],
|
||
];
|
||
foreach ($permissions as $slug => $permission) {
|
||
//是否已存在该权限
|
||
$slugKey = 'dcat.admin.'.($key ? $key.'.'.$slug : $slug);
|
||
$pper = Permission::updateOrCreate(['slug'=>$slugKey], ['name' => $permission['name'], 'parent_id' => $pId]);
|
||
|
||
if (!isset($permission['children'])) {
|
||
$permission['children'] = [];
|
||
}
|
||
//判断是否默认插入curd权限
|
||
if (isset($permission['curd']) && $permission['curd']) {
|
||
if (is_array($permission['curd'])) {
|
||
foreach ($permission['curd'] as $value) {
|
||
$permission['children'][$value] = $curdArr[$value];
|
||
}
|
||
} else {
|
||
$permission['children'] = array_merge($curdArr, $permission['children']);
|
||
}
|
||
}
|
||
if (count($permission['children']) > 0) {
|
||
$_key = $permission['curd'] ? ($key ? $key.'.'.$slug : $slug) : $key;
|
||
$this->createPermissionData($permission['children'], $_key??$slug, $pper->id);
|
||
}
|
||
}
|
||
}
|
||
}
|