海南旅游SAAS
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.
 
 
 

234 lines
7.8 KiB

<?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(), function (Grid $grid) {
$grid->disableDeleteButton();
//如果是审核页面,多加where条件判断
if (strpos(Route::current()->uri, 'audit')) {
$grid->model()->where('status', UserStatus::UNAUDITED);
}
$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');
$grid->column('status', '状态')
->if(fn() => $this->status == UserStatus::UNAUDITED)
->display('')
->then(function ($column) {
$column->append((new AuditAgent(null, 1))->setKey($this->id))->append('&nbsp;');
$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('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('logo')->image('', 80, 80);
$show->field('address');
$show->field('license_pic')->image('', 80, 80);
$show->field('director');
$show->field('contact_phone');
$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->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->radio('type')
->options(AgentType::array())
->default(AgentType::OPERATOR)
->required();
$form->text('company_name');
$form->image('logo')->removable(false)->uniqueName();
$form->text('address');
$form->image('license_pic')->removable(false)->uniqueName();
$form->text('director');
$form->text('contact_phone');
$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字段
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, $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());
}
}
}
}
});
}
}