diff --git a/app/Admin/Controllers/AgentController.php b/app/Admin/Controllers/AgentController.php index 2f9fe6f..5aaec3e 100644 --- a/app/Admin/Controllers/AgentController.php +++ b/app/Admin/Controllers/AgentController.php @@ -56,9 +56,12 @@ class AgentController extends AdminController UserStatus::DISABLED => 'warning', ], 'primary'); - $grid->filter(function (Grid\Filter $filter) { - $filter->equal('id'); + $grid->filter(function ($filter) { + $filter->panel(); + $filter->equal('id')->width(2); + $filter->like('agent_name')->width(3); + $filter->equal('status', '用户状态')->select(UserStatus::array())->width(2); }); }); } @@ -72,7 +75,7 @@ class AgentController extends AdminController */ protected function detail($id) { - return Show::make($id, new Agent(), function (Show $show) { + return Show::make($id, new Agent('agentInfo'), function (Show $show) { $show->disableDeleteButton(); $show->field('id'); @@ -90,6 +93,21 @@ class AgentController extends AdminController $show->field('license_pic')->image(120, 120); $show->field('director'); $show->field('contact_phone'); + $show->field('agentInfo.about', '关于我们') + ->unescape() + ->as(function ($v) { + return preg_replace('/.*?<\/script>/is', '', $this->agentInfo->about); + }); + $show->field('agentInfo.reg_protocol', '注册协议') + ->unescape() + ->as(function ($v) { + return preg_replace('/.*?<\/script>/is', '', $this->agentInfo->reg_protocol); + }); + $show->field('agentInfo.buy_protocol', '购买协议') + ->unescape() + ->as(function ($v) { + return preg_replace('/.*?<\/script>/is', '', $this->agentInfo->buy_protocol); + }); $show->field('created_at')->as(fn($v) => date('Y-m-d H:i:s', $v)); $show->field('updated_at')->as(fn($v) => date('Y-m-d H:i:s', $v)); }); @@ -106,8 +124,15 @@ class AgentController extends AdminController $form->disableDeleteButton(); $form->display('id'); - $form->text('account')->required(); - $form->text('password')->required(); + //新增 + if ($form->isCreating()) { + $form->text('account')->required(); + $form->text('password')->required(); + } + else if ($form->isEditing()) { + $form->display('account'); + $form->text('password')->customFormat(fn() => ''); + } $form->text('agent_name')->required(); $form->text('appid')->required(); $form->text('appsecret')->required(); @@ -123,7 +148,9 @@ class AgentController extends AdminController $form->image('license_pic'); $form->text('director'); $form->text('contact_phone'); - $form->textarea('agentInfo.about'); + $form->editor('agentInfo.about', '关于我们');// 隐藏菜单用:->options(['menubar' => false]); + $form->editor('agentInfo.reg_protocol', '注册协议'); + $form->editor('agentInfo.buy_protocol', '购买协议'); })->saving(function (Form $form) { //不允许编辑的字段 if ($form->isEditing()) { @@ -132,10 +159,23 @@ class AgentController extends AdminController //过滤null字段 foreach($form->input() as $k => $v) { - if (is_null($form->$k)) { + if (is_null($v)) { $form->$k = ''; + } else if (is_array($v)) { + foreach ($v as $k2 => &$v2) { + if (is_null($v2)) { + $v2 = ''; //不能用$form->$k[$k2] = '',会报错,只能通过引用修改 + } else { + $v2 = preg_replace('/.*?<\/script>/is', '', $v2); + } + } } } + })->saved(function (Form $form) { + //如果状态是正常,插入初始数据 + if ($form->status == UserStatus::NORMAL) { + (new AuditAgent)->setKey($form->getKey())->pass(); + } }); } } diff --git a/app/Admin/Extensions/Grid/AuditAgent.php b/app/Admin/Extensions/Grid/AuditAgent.php index a9e5f68..6a893ad 100644 --- a/app/Admin/Extensions/Grid/AuditAgent.php +++ b/app/Admin/Extensions/Grid/AuditAgent.php @@ -5,6 +5,7 @@ use App\Common\UserStatus; use App\Models\Agent; use App\Models\Category; use App\Models\Channel; +use App\Models\Slide; use Dcat\Admin\Grid\RowAction; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; @@ -40,39 +41,74 @@ class AuditAgent extends RowAction } //通过 - private function pass() + public function pass() { $id = $this->getKey(); + $host = env('APP_URL'); + DB::beginTransaction(); try { $user = Agent::find($id); $user->status = UserStatus::NORMAL; $user->save(); - # 写入初始数据 + # 写入默认初始数据 //代理商产品分类 if (!Category::where('agent_id', $id)->first()) { - $defaultCategory = Category::where('agent_id', 0)->get()->toArray(); - $defaultCategory = array_map(function ($v) use ($id) { + $default_category = Category::where('agent_id', 0)->orderBy('id')->get()->toArray(); + + $default_category = array_map(function ($v) use ($id) { unset($v['id']); $v['agent_id'] = $id; return $v; - }, $defaultCategory); - Category::insert($defaultCategory); + }, $default_category); + Category::insert($default_category); + + //获取ID间隔,更新pid + $id_interval = Category::max('id') - count($default_category); + Category::where([ + ['agent_id', '=', $id], + ['pid', '<>', 0], + ])->increment('pid', $id_interval); } //代理商频道列表 if (!Channel::where('agent_id', $id)->first()) { - $defaultChannel = Channel::where('agent_id', $id)->get()->toArray(); - $autoIncrement = - $defaultChannel = array_map(function ($v) use ($id) { + $default_channel = Channel::where('agent_id', 0)->orderBy('id')->get()->toArray(); + + $default_channel = array_map(function ($v) use ($id, $host) { + unset($v['id']); + $v['agent_id'] = $id; + //insert和update,increment等批量更新时,不支持修改器,需要手动处理 + $v['icon'] = str_replace($host, '', $v['icon']); + return $v; + }, $default_channel); + Channel::insert($default_channel); + + //获取ID间隔,更新pid + $id_interval = Channel::max('id') - count($default_channel); + Channel::where([ + ['agent_id', '=', $id], + ['pid', '<>', 0], + ])->increment('pid', $id_interval); + } + + //首页轮播图 + if (!Slide::where('agent_id', $id)->first()) { + $default_slide = Slide::where('agent_id', 0)->orderBy('id')->get()->toArray(); + + $default_slide = array_map(function ($v) use ($id, $host) { unset($v['id']); $v['agent_id'] = $id; + //insert和update,increment等批量更新时,不支持修改器,需要手动处理 + $v['url'] = str_replace($host, '', $v['url']); return $v; - }, $defaultChannel); - Category::insert($defaultChannel); + }, $default_slide); + Slide::insert($default_slide); } + //TODO 还需要插入 演示产品、公告、专题等 + DB::commit(); return $this->response()->success("审核成功")->refresh(); } catch (\Exception $e) {