You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							181 lines
						
					
					
						
							5.7 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							181 lines
						
					
					
						
							5.7 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\Admin\Controllers;
							 | 
						|
								
							 | 
						|
								use App\Admin\Extensions\Grid\AuditAgent;
							 | 
						|
								use App\Admin\Repositories\Agent;
							 | 
						|
								use App\Common\UserStatus;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								use Illuminate\Support\Facades\Route;
							 | 
						|
								
							 | 
						|
								class AgentController extends AdminController
							 | 
						|
								{
							 | 
						|
								    /**
							 | 
						|
								     * Make a grid builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Grid
							 | 
						|
								     */
							 | 
						|
								    protected function grid()
							 | 
						|
								    {
							 | 
						|
								        return Grid::make(new Agent(), function (Grid $grid) {
							 | 
						|
											$grid->disableDeleteButton();
							 | 
						|
								
							 | 
						|
											//如果是审核页面,多加where条件判断
							 | 
						|
											if (strpos(Route::current()->uri, 'audit')) {
							 | 
						|
												$grid->model()->where('status', UserStatus::UNAUDITED);
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
								            $grid->column('id')->sortable();
							 | 
						|
								            $grid->column('account');
							 | 
						|
								            $grid->column('agent_name');
							 | 
						|
								            $grid->column('company_name');
							 | 
						|
								            $grid->column('logo')->image(60, 60);
							 | 
						|
								            $grid->column('address');
							 | 
						|
								            $grid->column('license_pic')->image(60, 60);
							 | 
						|
								            $grid->column('director');
							 | 
						|
								            $grid->column('contact_phone');
							 | 
						|
											$grid->column('created_at')->display(fn($v) => $v);
							 | 
						|
											$grid->column('updated_at')->display(fn($v) => $v);
							 | 
						|
								
							 | 
						|
											$grid->column('status', '状态')
							 | 
						|
												->if(fn() => $this->status == UserStatus::UNAUDITED)
							 | 
						|
												->display('')
							 | 
						|
												->then(function ($column) {
							 | 
						|
													$column->append((new AuditAgent(null, 1))->setKey($this->id))->append(' ');
							 | 
						|
													$column->append((new AuditAgent(null, 2))->setKey($this->id));
							 | 
						|
												})
							 | 
						|
												->else()
							 | 
						|
												->using(UserStatus::array())
							 | 
						|
												->dot([
							 | 
						|
													UserStatus::NORMAL => 'success',
							 | 
						|
													UserStatus::UNAUDITED => '',
							 | 
						|
													UserStatus::REFUSE => 'danger',
							 | 
						|
													UserStatus::DISABLED => 'warning',
							 | 
						|
												], 'primary');
							 | 
						|
								
							 | 
						|
								            $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);
							 | 
						|
								            });
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a show builder.
							 | 
						|
								     *
							 | 
						|
								     * @param mixed $id
							 | 
						|
								     *
							 | 
						|
								     * @return Show
							 | 
						|
								     */
							 | 
						|
								    protected function detail($id)
							 | 
						|
								    {
							 | 
						|
								        return Show::make($id, new Agent('agentInfo'), function (Show $show) {
							 | 
						|
											$show->disableDeleteButton();
							 | 
						|
								
							 | 
						|
								            $show->field('id');
							 | 
						|
								            $show->field('account');
							 | 
						|
								            $show->field('password');
							 | 
						|
								            $show->field('agent_name');
							 | 
						|
								            $show->field('appid');
							 | 
						|
								            $show->field('appsecret');
							 | 
						|
								            $show->field('mchid');
							 | 
						|
								            $show->field('mchkey');
							 | 
						|
								            $show->field('status')->using(UserStatus::array());
							 | 
						|
								            $show->field('company_name');
							 | 
						|
								            $show->field('logo')->image(120, 120);
							 | 
						|
								            $show->field('address');
							 | 
						|
								            $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.*?>.*?<\/script>/is', '', $this->agentInfo->about);
							 | 
						|
												});
							 | 
						|
											$show->field('agentInfo.reg_protocol', '注册协议')
							 | 
						|
												->unescape()
							 | 
						|
												->as(function ($v) {
							 | 
						|
													return preg_replace('/<script.*?>.*?<\/script>/is', '', $this->agentInfo->reg_protocol);
							 | 
						|
												});
							 | 
						|
											$show->field('agentInfo.buy_protocol', '购买协议')
							 | 
						|
												->unescape()
							 | 
						|
												->as(function ($v) {
							 | 
						|
													return preg_replace('/<script.*?>.*?<\/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));
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a form builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Form
							 | 
						|
								     */
							 | 
						|
								    protected function form()
							 | 
						|
								    {
							 | 
						|
								        return Form::make(new Agent('agentInfo'), function (Form $form) {
							 | 
						|
											$form->disableDeleteButton();
							 | 
						|
								
							 | 
						|
								            $form->display('id');
							 | 
						|
								            //新增
							 | 
						|
								            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();
							 | 
						|
								            $form->text('mchid')->required();
							 | 
						|
								            $form->text('mchkey')->required();
							 | 
						|
											$form->select('status')
							 | 
						|
												->default(UserStatus::NORMAL)
							 | 
						|
												->options(UserStatus::array())
							 | 
						|
												->required();
							 | 
						|
								            $form->text('company_name');
							 | 
						|
								            $form->image('logo');
							 | 
						|
								            $form->text('address');
							 | 
						|
								            $form->image('license_pic');
							 | 
						|
								            $form->text('director');
							 | 
						|
								            $form->text('contact_phone');
							 | 
						|
											$form->editor('agentInfo.about', '关于我们');// 隐藏菜单用:->options(['menubar' => false]);
							 | 
						|
											$form->editor('agentInfo.reg_protocol', '注册协议');
							 | 
						|
											$form->editor('agentInfo.buy_protocol', '购买协议');
							 | 
						|
								        })->saving(function (Form $form) {
							 | 
						|
								        	//不允许编辑的字段
							 | 
						|
								        	if ($form->isEditing()) {
							 | 
						|
												$form->ignore(['id', 'account', 'created_at', 'updated_at']);
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
								        	//过滤null字段
							 | 
						|
								        	foreach($form->input() as $k => $v) {
							 | 
						|
								        		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.*?>.*?<\/script>/is', '', $v2);
							 | 
						|
														}
							 | 
						|
													}
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
										})->saved(function (Form $form) {
							 | 
						|
											//如果状态是正常,插入初始数据
							 | 
						|
											if ($form->status == UserStatus::NORMAL) {
							 | 
						|
												(new AuditAgent)->setKey($form->getKey())->pass();
							 | 
						|
											}
							 | 
						|
										});
							 | 
						|
								    }
							 | 
						|
								}
							 |