5 changed files with 316 additions and 0 deletions
			
			
		- 
					141app/Admin/Controllers/AgentController.php
 - 
					107app/Admin/Extensions/Grid/AuditAgent.php
 - 
					16app/Admin/Repositories/Agent.php
 - 
					27app/Models/Agent.php
 - 
					25resources/lang/zh_CN/agent.php
 
@ -0,0 +1,141 @@ | 
				
			|||
<?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 (Grid\Filter $filter) { | 
				
			|||
                $filter->equal('id'); | 
				
			|||
 | 
				
			|||
            }); | 
				
			|||
        }); | 
				
			|||
    } | 
				
			|||
 | 
				
			|||
    /** | 
				
			|||
     * Make a show builder. | 
				
			|||
     * | 
				
			|||
     * @param mixed $id | 
				
			|||
     * | 
				
			|||
     * @return Show | 
				
			|||
     */ | 
				
			|||
    protected function detail($id) | 
				
			|||
    { | 
				
			|||
        return Show::make($id, new Agent(), 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('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'); | 
				
			|||
            $form->text('account')->required(); | 
				
			|||
            $form->text('password')->required(); | 
				
			|||
            $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->textarea('agentInfo.about'); | 
				
			|||
        })->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($form->$k)) { | 
				
			|||
					$form->$k = ''; | 
				
			|||
				} | 
				
			|||
			} | 
				
			|||
		}); | 
				
			|||
    } | 
				
			|||
} | 
				
			|||
@ -0,0 +1,107 @@ | 
				
			|||
<?php | 
				
			|||
 | 
				
			|||
namespace App\Admin\Extensions\Grid; | 
				
			|||
use App\Common\UserStatus; | 
				
			|||
use App\Models\Agent; | 
				
			|||
use App\Models\Category; | 
				
			|||
use App\Models\Channel; | 
				
			|||
use Dcat\Admin\Grid\RowAction; | 
				
			|||
use Illuminate\Http\Request; | 
				
			|||
use Illuminate\Support\Facades\DB; | 
				
			|||
 | 
				
			|||
/** | 
				
			|||
 * 供应商审核 | 
				
			|||
 * Class AuditSupplier | 
				
			|||
 * @package App\Admin\Extensions\Grid | 
				
			|||
 */ | 
				
			|||
class AuditAgent extends RowAction | 
				
			|||
{ | 
				
			|||
	private $action; | 
				
			|||
 | 
				
			|||
	public function __construct($title = null, $action = 1) | 
				
			|||
	{ | 
				
			|||
		parent::__construct($title); | 
				
			|||
		$this->action = $action; //$action:1=通过;2=拒绝
 | 
				
			|||
		$this->title = $action == 1 ? '通过' : '拒绝'; | 
				
			|||
	} | 
				
			|||
 | 
				
			|||
	protected function html() | 
				
			|||
	{ | 
				
			|||
		$class = $this->action == 1 ? 'btn btn-sm btn-success' : 'btn btn-sm btn-danger'; | 
				
			|||
		$this->appendHtmlAttribute('class', $class); | 
				
			|||
		$this->defaultHtmlAttribute('href', 'javascript:;'); | 
				
			|||
 | 
				
			|||
		return "<a {$this->formatHtmlAttributes()}>{$this->title}</a>"; | 
				
			|||
	} | 
				
			|||
 | 
				
			|||
	public function handle(Request $request) | 
				
			|||
	{ | 
				
			|||
		return $request->action == 1 ? $this->pass() : $this->refuse(); | 
				
			|||
	} | 
				
			|||
 | 
				
			|||
	//通过
 | 
				
			|||
	private function pass() | 
				
			|||
	{ | 
				
			|||
		$id = $this->getKey(); | 
				
			|||
		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) { | 
				
			|||
					unset($v['id']); | 
				
			|||
					$v['agent_id'] = $id; | 
				
			|||
					return $v; | 
				
			|||
				}, $defaultCategory); | 
				
			|||
				Category::insert($defaultCategory); | 
				
			|||
			} | 
				
			|||
 | 
				
			|||
			//代理商频道列表
 | 
				
			|||
			if (!Channel::where('agent_id', $id)->first()) { | 
				
			|||
				$defaultChannel = Channel::where('agent_id', $id)->get()->toArray(); | 
				
			|||
				$autoIncrement = | 
				
			|||
				$defaultChannel = array_map(function ($v) use ($id) { | 
				
			|||
					unset($v['id']); | 
				
			|||
					$v['agent_id'] = $id; | 
				
			|||
					return $v; | 
				
			|||
				}, $defaultChannel); | 
				
			|||
				Category::insert($defaultChannel); | 
				
			|||
			} | 
				
			|||
 | 
				
			|||
			DB::commit(); | 
				
			|||
			return $this->response()->success("审核成功")->refresh(); | 
				
			|||
		} catch (\Exception $e) { | 
				
			|||
			DB::rollBack(); | 
				
			|||
			return $this->response()->error($e->getMessage()); | 
				
			|||
		} | 
				
			|||
	} | 
				
			|||
 | 
				
			|||
	//拒绝
 | 
				
			|||
	private function refuse() | 
				
			|||
	{ | 
				
			|||
		try { | 
				
			|||
			$user = Agent::find($this->getKey()); | 
				
			|||
			$user->status = UserStatus::REFUSE; | 
				
			|||
			$user->save(); | 
				
			|||
 | 
				
			|||
			return $this->response()->success("审核成功")->refresh(); | 
				
			|||
		} catch (\Exception $e) { | 
				
			|||
			return $this->response()->error($e->getMessage()); | 
				
			|||
		} | 
				
			|||
	} | 
				
			|||
 | 
				
			|||
	public function confirm() | 
				
			|||
	{ | 
				
			|||
		return ['确定要'.$this->title.'该用户吗?', '']; | 
				
			|||
	} | 
				
			|||
 | 
				
			|||
	public function parameters() | 
				
			|||
	{ | 
				
			|||
		return ['action' => $this->action]; | 
				
			|||
	} | 
				
			|||
} | 
				
			|||
@ -0,0 +1,16 @@ | 
				
			|||
<?php | 
				
			|||
 | 
				
			|||
namespace App\Admin\Repositories; | 
				
			|||
 | 
				
			|||
use App\Models\Agent as Model; | 
				
			|||
use Dcat\Admin\Repositories\EloquentRepository; | 
				
			|||
 | 
				
			|||
class Agent extends EloquentRepository | 
				
			|||
{ | 
				
			|||
    /** | 
				
			|||
     * Model. | 
				
			|||
     * | 
				
			|||
     * @var string | 
				
			|||
     */ | 
				
			|||
    protected $eloquentClass = Model::class; | 
				
			|||
} | 
				
			|||
@ -0,0 +1,25 @@ | 
				
			|||
<?php  | 
				
			|||
return [ | 
				
			|||
    'labels' => [ | 
				
			|||
        'Agent' => '代理商', | 
				
			|||
        'agent' => '代理商', | 
				
			|||
    ], | 
				
			|||
    'fields' => [ | 
				
			|||
        'account' => '账号', | 
				
			|||
        'password' => '密码', | 
				
			|||
        'agent_name' => '代理商名称', | 
				
			|||
        'appid' => '微信AppID', | 
				
			|||
        'appsecret' => '微信AppSecret', | 
				
			|||
        'mchid' => '微信支付mch_id', | 
				
			|||
        'mchkey' => '微信支付key', | 
				
			|||
        'status' => '状态', | 
				
			|||
        'company_name' => '公司名称', | 
				
			|||
        'logo' => 'LOGO', | 
				
			|||
        'address' => '公司地址', | 
				
			|||
        'license_pic' => '营业执照', | 
				
			|||
        'director' => '负责人', | 
				
			|||
        'contact_phone' => '联系电话', | 
				
			|||
    ], | 
				
			|||
    'options' => [ | 
				
			|||
    ], | 
				
			|||
]; | 
				
			|||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue