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.
		
		
		
		
		
			
		
			
				
					
					
						
							270 lines
						
					
					
						
							10 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							270 lines
						
					
					
						
							10 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\Admin\Controllers;
							 | 
						|
								
							 | 
						|
								use App\Admin\Extensions\Grid\AuditAgent;
							 | 
						|
								use App\Admin\Extensions\Grid\MiniProgramAuditStatus;
							 | 
						|
								use App\Admin\Extensions\Grid\MiniProgramReg;
							 | 
						|
								use App\Admin\Extensions\Grid\MiniProgramUpload;
							 | 
						|
								use App\Admin\Repositories\Agent;
							 | 
						|
								use App\Common\AgentType;
							 | 
						|
								use App\Common\UserStatus;
							 | 
						|
								use App\Models\MiniProgramTemplate;
							 | 
						|
								use App\Models\Supplier;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								use Illuminate\Support\Facades\DB;
							 | 
						|
								use Illuminate\Support\Facades\Route;
							 | 
						|
								
							 | 
						|
								class AgentController extends AdminController
							 | 
						|
								{
							 | 
						|
								    /**
							 | 
						|
								     * Make a grid builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Grid
							 | 
						|
								     */
							 | 
						|
								    protected function grid()
							 | 
						|
								    {
							 | 
						|
								        return Grid::make(new Agent(['miniUpload']), function (Grid $grid) {
							 | 
						|
											$grid->disableDeleteButton();
							 | 
						|
								
							 | 
						|
											//如果是审核页面,多加where条件判断
							 | 
						|
											if (strpos(Route::current()->uri, 'audit')) {
							 | 
						|
												$grid->model()->where('status', UserStatus::UNAUDITED);
							 | 
						|
											}
							 | 
						|
											$grid->model()->where('id', '>', 1); //隐藏ID=1的领峰云管理员
							 | 
						|
								
							 | 
						|
								            $grid->column('id')->sortable();
							 | 
						|
								            $grid->column('username');
							 | 
						|
								            $grid->column('name');
							 | 
						|
								            $grid->column('type')->using(AgentType::array());
							 | 
						|
								            $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('rate')->editable()->help('分成百分比,如10%,则输入10');
							 | 
						|
											$grid->column('created_at');
							 | 
						|
								
							 | 
						|
											$last_template_id = MiniProgramTemplate::max('template_id');
							 | 
						|
											$grid->column('status', '状态')
							 | 
						|
												->style('min-width:7em;')
							 | 
						|
												->help('新用户入驻时,请先注册小程序。待企业法人认证通过之后,再上传小程序即可')
							 | 
						|
												->using(UserStatus::array())
							 | 
						|
												->dot([
							 | 
						|
													UserStatus::NORMAL => 'success',
							 | 
						|
													UserStatus::UNAUDITED => '',
							 | 
						|
													UserStatus::REFUSE => 'danger',
							 | 
						|
													UserStatus::DISABLED => 'warning',
							 | 
						|
												], 'primary')
							 | 
						|
												->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));
							 | 
						|
												})
							 | 
						|
												->if(fn() => $this->status == UserStatus::NORMAL)
							 | 
						|
												->display('')
							 | 
						|
												->then(function ($column) use ($last_template_id) {
							 | 
						|
													$is_success = $this->miniUpload->is_success ?? null;
							 | 
						|
													$template_id = $this->miniUpload->template_id ?? null;
							 | 
						|
								
							 | 
						|
													if (empty($this->appid)) { //注册小程序
							 | 
						|
														$column->append((new MiniProgramReg(null, 1))->setKey($this->id))->append(' ');
							 | 
						|
														$column->append((new MiniProgramReg(null, 2))->setKey($this->id));
							 | 
						|
													} else if ($is_success === 0 && $template_id === $last_template_id) {
							 | 
						|
														$column->append("已发布(模板:{$template_id})");
							 | 
						|
													} else if (in_array($is_success, [-1, 2, 4])) { //如果状态是-1未检查过,或2审核中,或4审核延后,则显示审核状态按钮
							 | 
						|
														$column->append((new MiniProgramAuditStatus(null, 1))->setKey($this->id));
							 | 
						|
													} else {
							 | 
						|
														$column->append((new MiniProgramUpload(null, 1))->setKey($this->id));
							 | 
						|
														$statusArr = [0 => '审核成功', 1 => '审核被拒绝', 2 => '审核中', 3 => '已撤回', 4 => '审核延后'];
							 | 
						|
														if (isset($is_success, $statusArr[$is_success])) {
							 | 
						|
															$column->append('<br>(' . $statusArr[$is_success] . ')');
							 | 
						|
														}
							 | 
						|
													}
							 | 
						|
												});
							 | 
						|
								
							 | 
						|
								            $grid->filter(function ($filter) {
							 | 
						|
												$filter->panel();
							 | 
						|
								
							 | 
						|
								                $filter->equal('id')->width(2);
							 | 
						|
								                $filter->like('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('username');
							 | 
						|
								            $show->field('name');
							 | 
						|
								            $show->field('appid');
							 | 
						|
								//            $show->field('appsecret');
							 | 
						|
								//            $show->field('mchid');
							 | 
						|
								//            $show->field('mchkey');
							 | 
						|
								            $show->field('status')->using(UserStatus::array());
							 | 
						|
											$show->field('type')->using(AgentType::array());
							 | 
						|
								            $show->field('company_name');
							 | 
						|
								            $show->field('credit_codes');
							 | 
						|
								            $show->field('logo')->image('', 80, 80);
							 | 
						|
								            $show->field('address');
							 | 
						|
								            $show->field('license_pic')->image('', 80, 80);
							 | 
						|
								            $show->field('director');
							 | 
						|
								            $show->field('contact_phone');
							 | 
						|
								            $show->field('legal_persona_name');
							 | 
						|
								            $show->field('legal_persona_wechat');
							 | 
						|
								            //$show->field('rate');
							 | 
						|
											$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');
							 | 
						|
								            $show->field('updated_at');
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * 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('username')->required();
							 | 
						|
												$form->text('password')->required();
							 | 
						|
											}
							 | 
						|
								            else if ($form->isEditing()) {
							 | 
						|
												$form->display('username');
							 | 
						|
												$form->text('password')->customFormat(fn() => '');
							 | 
						|
											}
							 | 
						|
								            $form->text('name')->required();
							 | 
						|
								            $form->display('appid')->help('无需填写,注册小程序后会自动生成');
							 | 
						|
								//            $form->text('appsecret')->help('无需填写,注册小程序后会自动生成');
							 | 
						|
								//            $form->text('mchid')->required();
							 | 
						|
								//            $form->text('mchkey')->required();
							 | 
						|
											$form->select('status')
							 | 
						|
												->default(UserStatus::NORMAL)
							 | 
						|
												->options(UserStatus::array())
							 | 
						|
												->required();
							 | 
						|
											$form->radio('type')
							 | 
						|
												->options(AgentType::array())
							 | 
						|
												->default(AgentType::OPERATOR)
							 | 
						|
												->required();
							 | 
						|
											$form->text('company_name')->required()->help('请正确填写,保持跟营业执照上的完全一致,如:海南易游天下供应链有限公司');
							 | 
						|
											$form->text('credit_codes')->required()->help('请正确填写,保持跟营业执照上的完全一致,如:91440300577652919M');
							 | 
						|
											$form->distpicker(['province_id', 'city_id', 'area_id'], '请选择区域')->required();
							 | 
						|
								            $form->image('logo')->removable(false)->uniqueName();
							 | 
						|
								            $form->text('address');
							 | 
						|
								            $form->image('license_pic')->removable(false)->uniqueName();
							 | 
						|
								            $form->text('director');
							 | 
						|
								            $form->text('contact_phone');
							 | 
						|
											$form->text('legal_persona_name')->required()->maxLength(20)->help('很重要,注册小程序时需要用到,用于实名认证');
							 | 
						|
											$form->text('legal_persona_wechat')->required()->maxLength(100)->help('很重要,用于接收实名认证信息,不能是手机号或QQ号,微信号请前往微信“我的”页面查看');
							 | 
						|
								            //$form->number('rate')->min(0)->max(100)->help('分成百分比,如10%,则输入10');
							 | 
						|
											$form->editor('agentInfo.about', '关于我们');// 隐藏菜单用:->options(['menubar' => false]);
							 | 
						|
											$form->editor('agentInfo.reg_protocol', '注册协议');
							 | 
						|
											$form->editor('agentInfo.buy_protocol', '购买协议');
							 | 
						|
								        })->saving(function (Form $form) {
							 | 
						|
											//判断账号是否唯一
							 | 
						|
											if ($form->isCreating()) {
							 | 
						|
												if ($form->repository()->model()->where('username', $form->username)->exists()) {
							 | 
						|
													return $form->response()->error($form->username . ' 的账号已经存在');
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											////抽成比例
							 | 
						|
											//if ($form->rate < 0 || $form->rate > 100) {
							 | 
						|
											//	return $form->response()->error('抽成比例在 0 ~ 100 之间');
							 | 
						|
											//}
							 | 
						|
								
							 | 
						|
								        	//不允许编辑的字段
							 | 
						|
								        	if ($form->isEditing()) {
							 | 
						|
												$form->ignore(['id', 'username', 'created_at', 'updated_at', 'deleted_at']);
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
								        	//过滤null字段
							 | 
						|
											$allow_null = ['avatar', 'remember_token', 'appid', 'appsecret', 'mchid', 'mchkey'];
							 | 
						|
								        	foreach($form->input() as $k => $v) {
							 | 
						|
								        		if (is_null($v) && !in_array($k, $allow_null)) {
							 | 
						|
													$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, $result) {
							 | 
						|
											//如果状态是正常,插入初始数据
							 | 
						|
											if ($result && $form->status == UserStatus::NORMAL) {
							 | 
						|
												(new AuditAgent)->setKey($form->getKey())->pass();
							 | 
						|
								
							 | 
						|
												//如果是供应商版旅行社,同时插入供应商会员表
							 | 
						|
												if ($form->type == AgentType::SUPPLIER) {
							 | 
						|
													if (Supplier::query()->where('username', $form->model()->username)->doesntExist()) {
							 | 
						|
														DB::beginTransaction();
							 | 
						|
														try {
							 | 
						|
															//插入用户表
							 | 
						|
															$supplier_id = Supplier::query()->insertGetId([
							 | 
						|
																'username' => $form->model()->username,
							 | 
						|
																'password' => $form->model()->password, //密码不用转换,原样插入
							 | 
						|
																'name' => $form->model()->name,
							 | 
						|
																'avatar' => $form->model()->avatar,
							 | 
						|
																'status' => $form->model()->status,
							 | 
						|
																'company_name' => $form->model()->company_name,
							 | 
						|
																'logo' => $form->model()->logo,
							 | 
						|
																'address' => $form->model()->address,
							 | 
						|
																'license_pic' => $form->model()->license_pic,
							 | 
						|
																'director' => $form->model()->director,
							 | 
						|
																'contact_phone' => $form->model()->contact_phone,
							 | 
						|
																//'rate' => $form->model()->password,
							 | 
						|
															]);
							 | 
						|
															//插入权限表
							 | 
						|
															DB::table(config('admin-supplier.database.role_users_table'))
							 | 
						|
																->insertOrIgnore(['role_id' => 2, 'user_id' => $supplier_id]);
							 | 
						|
															DB::commit();
							 | 
						|
														} catch (\Exception $e) {
							 | 
						|
															DB::rollBack();
							 | 
						|
															return $form->response()->error('保存成功,但插入供应商会员失败,失败为:' . $e->getMessage());
							 | 
						|
														}
							 | 
						|
													}
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
										});
							 | 
						|
								    }
							 | 
						|
								}
							 |