| 
						 | 
						<?php
namespace App\Admin\Controllers;
use App\Admin\Extensions\Grid\AuditAgent;use App\Admin\Repositories\Agent;use App\Common\AgentType;use App\Common\UserStatus;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', 'settledOrder']), function (Grid $grid) {			$grid->disableDeleteButton();			$grid->disableRowSelector();
			//如果是审核页面,多加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('type')->using(AgentType::array());            $grid->column('company_name');			$grid->column('对公账号')->display(fn() => $this->corporate_account . ' / ' . $this->deposit_bank)->limit(15);            $grid->column('license')->image('', 60, 60);            $grid->column('business_license')->image('', 60, 60);            $grid->column('director');            $grid->column('contact_phone');			$grid->column('contract')->image('', 60, 60);			$grid->column('cost', '入驻费')				->help('该代理商已支付的入驻费(入驻时微信支付的费用)')				->if(fn() => !empty($this->settledOrder->paid_money) && floatval($this->settledOrder->paid_money) != 0)				->display(fn() => $this->settledOrder->paid_money ?? 0)->label('success');			$grid->column('created_at');
			$grid->column('status', '状态')				->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));				});
            $grid->filter(function (Grid\Filter $filter) {				$filter->panel();
                $filter->equal('id')->width(2);                $filter->like('company_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('address');            $show->field('credit_codes');            $show->field('license')->image('', 80, 80);            $show->field('business_license')->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('corporate_account');			$show->field('deposit_bank');			$show->field('contract')->image('', 60, 60);            $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::CLUSTER)				->required();			$form->text('company_name')->required()->help('请正确填写,保持跟营业执照上的完全一致,如:海南易游天下供应链有限公司');			$form->text('credit_codes')->required()->help('请正确填写,保持跟营业执照上的完全一致,如:91440300577652919M');			$form->text('address');			$form->distpicker(['province_id', 'city_id', 'area_id'], '请选择区域')->required();            $form->multipleImage('license')->removable(false)->uniqueName();            $form->image('business_license')->removable(false)->uniqueName();            $form->text('director')->required();			$form->text('contact_phone')->required()->maxLength(15);			$form->text('contact_mobile')->required()->maxLength(11)->help('此号码将用于接收短信提醒(如订单提醒、付款提醒、核销提醒等)');			$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,								'license' => $form->model()->license,								'address' => $form->model()->address,								'business_license' => $form->model()->business_license,								'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());						}					}				}			}		});    }}
  |