文件
xionggeshop/application/admin/controller/shopro/regional_agent/Agent.php
可乐加冰 a6415f7753 ```
feat(regional_agent): 完善代理佣金配置逻辑和状态管理
2026-09-12 20:49:38 +08:00

302 行
10 KiB
PHP

<?php
namespace app\admin\controller\shopro\regional_agent;
use app\admin\controller\shopro\Base;
use app\admin\model\shopro\Area;
use app\admin\model\shopro\regional_agent\Level as LevelModel;
class Agent extends Base
{
protected $model = null;
public function _initialize()
{
parent::_initialize();
$this->model = new \app\admin\model\shopro\regional_agent\Agent;
}
public function index()
{
$this->request->filter(['strip_tags']);
if ($this->request->isAjax()) {
$nobuildfields = ['nickname', 'mobile', 'area_name'];
list($where, $sort, $order, $offset, $limit) = $this->custombuildparams(null, $nobuildfields);
$total = $this->model
->with(['user' => function ($q) {
$q->field('id,avatar,nickname,mobile');
}, 'agentLevel'])
->where($where)
->order($sort, $order)
->count();
$list = $this->model
->with(['user' => function ($q) {
$q->field('id,avatar,nickname,mobile');
}, 'agentLevel'])
->where($where)
->order($sort, $order)
->limit($offset, $limit)
->select();
return $this->success('区域公司列表', null, ['total' => $total, 'rows' => $list]);
}
return $this->view->fetch();
}
public function update()
{
$id = $this->request->post('id');
$status = $this->request->post('status');
$level = $this->request->post('level');
$areaId = $this->request->post('area_id');
$areaLevel = $this->request->post('area_level');
$agent = $this->model->get($id);
if (!$agent) {
$this->error('公司不存在');
}
if ($status) {
$agent->status = $status;
if ($status === 'normal' && !$agent->becometime) {
$agent->becometime = time();
}
}
if ($level) {
$agent->level = $level;
}
if ($areaId && $areaLevel) {
$exists = $this->model->where('status', 'normal')
->where('area_id', $areaId)
->where('area_level', $areaLevel)
->where('id', '<>', $agent->id)
->find();
if ($exists) {
$this->error('该区域已有正式公司');
}
$area = Area::get($areaId);
$agent->area_id = $areaId;
$agent->area_level = $areaLevel;
$agent->area_name = $area ? $area['name'] : '';
if (!$level) {
$levelRow = LevelModel::where('area_level', $areaLevel)->order('level asc')->find();
if ($levelRow) {
$agent->level = $levelRow['level'];
}
}
}
$this->applyCommissionFields($agent, $this->request->post());
$agent->save();
$this->success('更新成功');
}
public function options()
{
$levels = LevelModel::order('level asc')->select();
$this->success('获取成功', null, ['levels' => $levels]);
}
public function edit($ids = null)
{
$row = $this->model->get($ids);
if (!$row) {
$this->error('公司不存在');
}
$this->assignconfig('row', $row);
return $this->view->fetch();
}
/**
* 审核区域公司申请
*/
public function audit()
{
$id = $this->request->post('id');
$action = $this->request->post('action');
$agent = $this->model->get($id);
if (!$agent) {
$this->error('公司不存在');
}
switch ($action) {
case 'approve':
if ($agent->status !== 'pending') {
$this->error('当前状态不可审核通过');
}
$exists = $this->model->where('status', 'normal')
->where('area_id', $agent->area_id)
->where('area_level', $agent->area_level)
->where('id', '<>', $agent->id)
->find();
if ($exists) {
$this->error('该区域已有正式公司');
}
$agent->status = 'normal';
$agent->becometime = time();
$agent->save();
if (class_exists('\\addons\\shopro_huifu\\library\\onboard\\HuifuOnboardGate')) {
\addons\shopro_huifu\library\onboard\HuifuOnboardGate::submitOnBusinessApproved(
$agent['user_id'],
'regional_agent',
$agent['id']
);
}
$this->notifyAgentAudit($agent, 'normal');
$this->success('审核通过,已提交汇付进件');
break;
case 'reject':
if ($agent->status !== 'pending') {
$this->error('当前状态不可驳回');
}
$this->notifyAgentAudit($agent, 'reject');
$agent->delete();
$this->success('已驳回申请');
break;
case 'forbidden':
$agent->status = 'forbidden';
$agent->save();
$this->success('已禁用');
break;
case 'normal':
$exists = $this->model->where('status', 'normal')
->where('area_id', $agent->area_id)
->where('area_level', $agent->area_level)
->where('id', '<>', $agent->id)
->find();
if ($exists) {
$this->error('该区域已有正式公司');
}
$agent->status = 'normal';
if (!$agent->becometime) {
$agent->becometime = time();
}
$agent->save();
$this->success('已启用');
break;
default:
$this->error('无效操作');
}
}
public function add()
{
if ($this->request->isPost()) {
$params = $this->request->post('row/a');
if (empty($params['user_id']) || empty($params['area_id']) || empty($params['area_level'])) {
$this->error('参数不完整');
}
$exists = $this->model->available()
->where('area_id', $params['area_id'])
->where('area_level', $params['area_level'])
->find();
if ($exists) {
$this->error('该区域已有公司');
}
$area = Area::get($params['area_id']);
$params['area_name'] = $area ? $area['name'] : '';
if (empty($params['level'])) {
$levelRow = LevelModel::where('area_level', $params['area_level'])->order('level asc')->find();
$params['level'] = $levelRow ? $levelRow['level'] : 3;
}
$params = array_merge($params, $this->buildCommissionData($params));
unset($params['use_custom_commission']);
$params['status'] = 'normal';
$params['becometime'] = time();
$this->model->create($params);
$this->success('添加成功');
}
return $this->view->fetch();
}
public function del($ids = null)
{
$agent = $this->model->get($ids);
if ($agent) {
$agent->status = 'forbidden';
$agent->save();
}
$this->success('操作成功');
}
protected function notifyAgentAudit($agent, $action)
{
try {
$user = \addons\shopro\model\User::get($agent['user_id']);
if ($user) {
$user->notify(new \addons\shopro\notifications\RegionalAgent([
'event' => 'regional_agent_apply',
'action' => $action,
'agent' => $agent->toArray(),
]));
}
} catch (\Exception $e) {
\think\Log::write('区域公司审核通知失败: ' . $e->getMessage());
}
}
/**
* 保存单个公司自定义差价(空=跟随等级通用配置)
*/
protected function applyCommissionFields($agent, $data)
{
$commission = $this->buildCommissionData($data);
$agent->commission_type = $commission['commission_type'];
$agent->commission_rate = $commission['commission_rate'];
$agent->commission_money = $commission['commission_money'];
}
protected function buildCommissionData($data)
{
if (array_key_exists('use_custom_commission', $data)) {
$useCustom = filter_var($data['use_custom_commission'], FILTER_VALIDATE_BOOLEAN);
} else {
$hasRate = isset($data['commission_rate']) && $data['commission_rate'] !== '' && $data['commission_rate'] !== null;
$hasMoney = isset($data['commission_money']) && $data['commission_money'] !== '' && $data['commission_money'] !== null;
$useCustom = !empty($data['commission_type']) && ($hasRate || $hasMoney);
}
if (!$useCustom) {
return [
'commission_type' => null,
'commission_rate' => null,
'commission_money' => null,
];
}
$type = $data['commission_type'] ?? 'rate';
if (!in_array($type, ['rate', 'money'])) {
$this->error('差价类型无效');
}
if ($type === 'rate') {
$rate = $data['commission_rate'] ?? null;
if ($rate === '' || $rate === null) {
$this->error('请填写自定义差价比例');
}
return [
'commission_type' => 'rate',
'commission_rate' => round(floatval($rate), 2),
'commission_money' => null,
];
}
$money = $data['commission_money'] ?? null;
if ($money === '' || $money === null) {
$this->error('请填写自定义固定差价');
}
return [
'commission_type' => 'money',
'commission_rate' => null,
'commission_money' => round(floatval($money), 2),
];
}
}