new-admin-api/app/controller/api/Agent.php

240 lines
9.0 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\controller\api;
use app\common\model\system\merchant\Merchant;
use app\common\repositories\marketing\AgentApplyRepository;
use app\common\repositories\marketing\AgentBrokerageRepository;
use app\common\repositories\marketing\AgentRepository;
use crmeb\basic\BaseController;
use think\App;
use think\exception\ValidateException;
class Agent extends BaseController{
protected $repository;
public function __construct(App $app, AgentRepository $repository){
parent::__construct($app);
$this->repository = $repository;
}
/**
* Common: 获取代理列表
* Author: wu-hui
* Time: 2024/01/29 16:00
*/
public function agentList(){
// 参数处理
[$page, $limit] = $this->getPage();
$params = $this->request->params(['uid','not_page','pid', 'id']);
// 信息列表获取
if((int)$params['not_page'] > 0) $data = $this->repository->getAllList((array)$params);
else $data = $this->repository->getList((array)$params,(int)$page,(int)$limit);
return app('json')->success($data);
}
/**
* Common: 二维码 - 供应商入驻二维码
* Author: wu-hui
* Time: 2024/01/29 17:40
* @return mixed
*/
public function qrCodeInviteSupplier(){
// 参数获取
$params = $this->request->params(['agent_id','type','level']);
$qrcode = $this->repository->createQrCode($params);
return app('json')->success(['qr_code' => $qrcode]);
}
/**
* Common: 获取单条代理人员信息
* Author: wu-hui
* Time: 2024/01/30 9:27
* @param $id
* @return mixed
*/
public function singleAgentInfo($id){
$res = $this->repository->getSingleInfo($id);
return app('json')->success($res);
}
/**
* Common: 获取配置信息
* Author: wu-hui
* Time: 2024/01/31 15:49
* @return mixed
*/
public function getConfig(){
$config = $this->repository->getConfig();
return app('json')->success($config);
}
/**
* Common: 申请成为代理
* Author: wu-hui
* Time: 2024/02/01 15:14
* @return mixed
*/
public function apply(){
// 获取申请参数
$params = $this->applyCheckParams();
// 处理数据
$params['user_info'] = $this->request->userInfo();
$params['is_app'] = $this->request->isApp();
$res = app()->make(AgentApplyRepository::class)->editApplyInfo($params);
if($res) return $res;
else return app('json')->success("操作成功");
}
/**
* Common: 申请参数获取 & 参数校验
* Author: wu-hui
* Time: 2024/02/01 13:41
* @return array
*/
public function applyCheckParams():array{
// 参数获取
$agentApplyId = (int)$this->request->param('id');
$data = $this->request->params([
['pid',0],
['agent_type',0],
'contact_name',
'contact_phone',
['province_id',0],
['city_id', 0],
['area_id', 0],
['street_id', 0],
'address',
['mer_name', ''],
['enterprise_name', ''],
['mer_class_id', 0],
['mer_type_id', 0],
['mer_images', []],
// 支付相关
'pay_type',
'return_url'
]);
$data['uid'] = $this->request->uid();
// 信息验证
if((int)$data['pid'] <= 0) throw new ValidateException('非法请求,无邀请信息!');
if(!in_array((int)$data['agent_type'], [2,3,4,5,6,7,8])) throw new ValidateException('非法请求,代理类型错误!');
if(empty($data['contact_name'])) throw new ValidateException('请输入联系人姓名!');
if(empty($data['contact_phone'])) throw new ValidateException('请输入联系人电话!');
if((int)$data['province_id'] <= 0) throw new ValidateException('请选择地区!');
if(in_array((int)$data['agent_type'], [5,6,7])){
if((int)$data['city_id'] <= 0) throw new ValidateException('请选择地区!');
if((int)$data['area_id'] <= 0) throw new ValidateException('请选择地区!');
}
if((int)$data['agent_type'] == 7){
if((int)$data['street_id'] <= 0) throw new ValidateException('请选择地区!');
if(empty($data['address'])) throw new ValidateException('请输入详细地址!');
if(empty($data['mer_name'])) throw new ValidateException('请输入商户名称!');
if((int)$data['mer_class_id'] <= 0) throw new ValidateException('请选择商户分类!');
if((int)$data['mer_type_id'] <= 0) throw new ValidateException('请选择商户类型!');
if(count($data['mer_images']) <= 0) throw new ValidateException('请上传资质证明图片!');
// 判断:商户名称是否重复
$merHas = Merchant::where('mer_name',$data['mer_name'])->count();
if($merHas > 0) throw new ValidateException('商户已经存在!');
// 判断:商户名称是否重复
$isHas = app()->make(AgentApplyRepository::class)
->getSearchModel([])
->where('mer_name', $data['mer_name'])
->when($agentApplyId > 0,function($query) use ($agentApplyId){
$query->where('id','<>',$agentApplyId);
})
->count();
if($isHas > 0) throw new ValidateException('商户已经存在,请勿重复申请!');
}
// 信息是否重复
$isHas = (int)app()->make(AgentApplyRepository::class)
->getSearchModel(['contact_phone'=>$data['contact_phone']])
->when($agentApplyId > 0,function($query) use ($agentApplyId){
$query->where('id','<>',$agentApplyId);
})->count();
if($isHas > 0) throw new ValidateException('联系人电话已经存在,请勿重复申请!');
$isHas = (int)$this->repository
->getSearchModel(['contact_phone'=>$data['contact_phone']])
->count();
if($isHas > 0) throw new ValidateException('联系人电话已经存在,请勿重复申请!');
// 不能 重复成为当前等级的用户
$isHas = (int)$this->repository
->getSearchModel(['agent_type'=>$data['agent_type'],'uid'=>$data['uid']])
->count();
if($isHas > 0) throw new ValidateException('代理身份信息已经存在!');
$isHas = (int)app()->make(AgentApplyRepository::class)
->getSearchModel(['agent_type'=>$data['agent_type'],'uid'=>$data['uid']])
->when($agentApplyId > 0,function($query) use ($agentApplyId){
$query->where('id','<>',$agentApplyId);
})->count();
if($isHas > 0) throw new ValidateException('代理身份信息已经存在,请勿重复申请!');
return compact("agentApplyId", "data");
}
/**
* Common: 获取申请记录
* Author: wu-hui
* Time: 2024/02/01 18:02
* @return mixed
*/
public function applyRecord(){
[$page, $limit] = $this->getPage();
$params = $this->request->params([]);
$params['uid'] = $this->request->uid();
$data = app()->make(AgentApplyRepository::class)->getList((array)$params,(int)$page,(int)$limit);
return app('json')->success($data);
}
/**
* Common: 单条申请信息
* Author: wu-hui
* Time: 2024/02/01 18:27
* @return mixed
*/
public function applyInfo(){
$applyId = $this->request->param('apply_id');
$data = app()->make(AgentApplyRepository::class)->getSearchModel(['id'=>$applyId])->findOrEmpty()->toArray();
return app('json')->success($data);
}
/**
* Common: 佣金列表获取
* Author: wu-hui
* Time: 2024/02/02 17:22
* @return mixed
*/
public function commissionList(){
[$page, $limit] = $this->getPage();
$agentId = $this->request->param('agent_id');
$data = app()->make(AgentBrokerageRepository::class)->getAgentCommissionList((int)$agentId,(int)$page,(int)$limit);
return app('json')->success($data);
}
/**
* Common: 获取用户身份信息列表
* Author: wu-hui
* Time: 2024/02/19 17:47
* @return mixed
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function getIdentityList(){
$uid = $this->request->uid();
// headquarters=总部province=省公司county=区县
$headquartersList = (array)$this->repository->getIdentityList($uid);
$provinceList = (array)$this->repository->getIdentityList($uid,'province');
$countyList = (array)$this->repository->getIdentityList($uid,'county');
return app('json')->success([
'headquarters_list' => $headquartersList,
'province_list' => $provinceList,
'county_list' => $countyList,
]);
}
}