420 lines
19 KiB
PHP
420 lines
19 KiB
PHP
<?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 app\common\repositories\system\merchant\MerchantRepository;
|
||
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,9,10,11])) throw new ValidateException('非法请求,代理类型错误!');
|
||
if(empty($data['contact_name'])) throw new ValidateException('请输入联系人姓名!');
|
||
if(empty($data['contact_phone'])) throw new ValidateException('请输入联系人电话!');
|
||
if(!in_array((int)$data['agent_type'], [2,3,4,8,9,10,11]) && (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('商户已经存在,请勿重复申请!');
|
||
}
|
||
// 判断:上级邀请人员是否已经达到限制 类型:1=总部发起人,2=省公司发起人,3=省合伙人(外勤),4=省合伙人(内勤),5=区县运营商,6=区县合伙人,7=餐厅,8=配送商,9=总部外勤,10=总部内勤
|
||
$config = $this->repository->getConfig();
|
||
$inviteLimit = $config['invite_limit'] ?? [];
|
||
switch($data['agent_type']){
|
||
case 2:
|
||
// 判断:当前上级招募 [省公司发起人] 数量是否超出限制
|
||
$hasCount = $this->repository->getSearchModel(['pid'=>$data['pid'],'agent_type'=>2])->count();
|
||
if(($inviteLimit['invite_9_2'] ?? 0) <= $hasCount) {
|
||
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_9_2'] ?? 0).' 人');
|
||
}
|
||
break;
|
||
case 3:
|
||
// 判断:当前上级招募 [省公司外勤] 数量是否超出限制
|
||
$hasCount = $this->repository->getSearchModel(['pid'=>$data['pid'],'agent_type'=>3])->count();
|
||
if(($inviteLimit['invite_2_3'] ?? 0) <= $hasCount) {
|
||
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_2_3'] ?? 0).' 人');
|
||
}
|
||
break;
|
||
case 4:
|
||
// 判断:当前上级招募 [省公司内勤] 数量是否超出限制
|
||
$hasCount = $this->repository->getSearchModel(['pid'=>$data['pid'],'agent_type'=>4])->count();
|
||
if(($inviteLimit['invite_2_4'] ?? 0) <= $hasCount) {
|
||
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_2_4'] ?? 0).' 人');
|
||
}
|
||
break;
|
||
case 5:
|
||
// 判断:当前上级招募 [区县运营商] 数量是否超出限制
|
||
$hasCount = $this->repository->getSearchModel(['pid'=>$data['pid'],'agent_type'=>5])->count();
|
||
if(($inviteLimit['invite_3_5'] ?? 0) <= $hasCount) {
|
||
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_3_5'] ?? 0).' 人');
|
||
}
|
||
break;
|
||
case 6:
|
||
// 判断:当前上级招募 [区县合伙人] 数量是否超出限制
|
||
$hasCount = $this->repository->getSearchModel(['pid'=>$data['pid'],'agent_type'=>6])->count();
|
||
if(($inviteLimit['invite_5_6'] ?? 0) <= $hasCount) {
|
||
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_5_6'] ?? 0).' 人');
|
||
}
|
||
break;
|
||
case 7:
|
||
// 判断:当前上级招募 [餐厅] 数量是否超出限制
|
||
$hasCount = $this->repository->getSearchModel(['pid'=>$data['pid'],'agent_type'=>7])->count();
|
||
if(($inviteLimit['invite_6_7'] ?? 0) <= $hasCount) {
|
||
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_6_7'] ?? 0).' 家餐厅');
|
||
}
|
||
break;
|
||
case 8:
|
||
// 判断:当前上级招募 [配送商] 数量是否超出限制
|
||
$hasCount = $this->repository->getSearchModel(['pid'=>$data['pid'],'agent_type'=>8])->count();
|
||
if(($inviteLimit['invite_6_8'] ?? 0) <= $hasCount) {
|
||
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_6_8'] ?? 0).' 家配送商');
|
||
}
|
||
break;
|
||
case 9:
|
||
// 判断:当前上级招募 [总部外勤] 数量是否超出限制
|
||
$hasCount = $this->repository->getSearchModel(['pid'=>$data['pid'],'agent_type'=>9])->count();
|
||
if(($inviteLimit['invite_1_9'] ?? 0) <= $hasCount) {
|
||
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_1_9'] ?? 0).' 人');
|
||
}
|
||
break;
|
||
case 10:
|
||
// 判断:当前上级招募 [总部内勤] 数量是否超出限制
|
||
$hasCount = $this->repository->getSearchModel(['pid'=>$data['pid'],'agent_type'=>10])->count();
|
||
if(($inviteLimit['invite_1_10'] ?? 0) <= $hasCount) {
|
||
throw new ValidateException('邀请人招募数量超出限制!仅允许招募 '.($inviteLimit['invite_1_10'] ?? 0).' 人');
|
||
}
|
||
break;
|
||
}
|
||
// 信息是否重复
|
||
// $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']])
|
||
->where('status','<>', 1)
|
||
->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');
|
||
$wineList = (array)$this->repository->getIdentityList($uid,'wine');
|
||
// 省公司信息:仅显示省公司信息,如果存在多个则只显示一个,如果是内勤或者外勤则查询省公司信息
|
||
$newProvince = [];
|
||
foreach($provinceList as $k => $v){
|
||
// 判断:是否为省公司发起人信息 是则直接使用,否则查询省公司发起人信息
|
||
if($v['agent_type'] == 2) $currentUseInfo = $v;
|
||
else $currentUseInfo = $this->repository->getSearchModel(['id'=>$v['pid'],'agent_type'=>2])->findOrEmpty()->toArray();
|
||
// 判断:当前省公司发起人信息是否已经存在,不存在添加
|
||
if(!in_array($currentUseInfo['id'],array_column($newProvince,'id'))){
|
||
$currentUseInfo['agent_type_list'][$v['agent_type']] = $v['agent_type_text'];
|
||
$newProvince[$currentUseInfo['id']] = $currentUseInfo;
|
||
}else{
|
||
$newProvince[$currentUseInfo['id']]['agent_type_list'][$v['agent_type']] = $v['agent_type_text'];
|
||
}
|
||
}
|
||
$provinceList = array_values($newProvince);
|
||
// 区县运营商:仅显示公司信息,如果存在多个则只显示一个,如果是内勤或者外勤则查询省公司信息
|
||
$newCounty = [];
|
||
foreach($countyList as $k => $v){
|
||
// 判断:是否为餐厅,配送商 是则跳出处理
|
||
if(in_array($v['agent_type'],[7,8])){
|
||
$newCounty[$v['id']] = $v;
|
||
continue;
|
||
}
|
||
// 判断:是否为区县运营商信息 是则直接使用,否则查询区县运营商信息
|
||
if($v['agent_type'] == 5) $currentUseInfo = $v;
|
||
else $currentUseInfo = $this->repository->getSearchModel(['id'=>$v['pid'],'agent_type'=>5])->findOrEmpty()->toArray();
|
||
// 判断:当前省公司发起人信息是否已经存在,不存在添加
|
||
if(!in_array($currentUseInfo['id'],array_column($newCounty,'id'))){
|
||
$currentUseInfo['agent_type_list'][$v['agent_type']] = $v['agent_type_text'];
|
||
$newCounty[$currentUseInfo['id']] = $currentUseInfo;
|
||
}else{
|
||
$newCounty[$currentUseInfo['id']]['agent_type_list'][$v['agent_type']] = $v['agent_type_text'];
|
||
}
|
||
}
|
||
$countyList = array_values($newCounty);
|
||
// 排序
|
||
array_multisort(array_column($headquartersList,'id'), SORT_DESC, $headquartersList);
|
||
array_multisort(array_column($provinceList,'id'), SORT_DESC, $provinceList);
|
||
array_multisort(array_column($countyList,'id'), SORT_DESC, $countyList);
|
||
array_multisort(array_column($wineList,'id'), SORT_DESC, $wineList);
|
||
|
||
return app('json')->success([
|
||
'headquarters_list' => $headquartersList,
|
||
'province_list' => $provinceList,
|
||
'county_list' => $countyList,
|
||
'wine_list' => $wineList,
|
||
]);
|
||
}
|
||
/**
|
||
* Common: 获取指定角色及相关角色的信息
|
||
* Author: wu-hui
|
||
* Time: 2024/02/26 9:53
|
||
* @return mixed
|
||
* @throws \think\db\exception\DataNotFoundException
|
||
* @throws \think\db\exception\DbException
|
||
* @throws \think\db\exception\ModelNotFoundException
|
||
*/
|
||
public function getRoleAndCorrelationRole(){
|
||
// 参数获取
|
||
$agentId = $this->request->param('agent_id');
|
||
$uid = $this->request->uid();
|
||
// 获取当前代理信息
|
||
$agentInfo = $this->repository->getSingleInfo($agentId);
|
||
if(!$agentInfo) throw new ValidateException('不存在该代理信息!');
|
||
// 根据当前代理角色 获取全部相关角色信息(按照总部、省公司、区县划分)
|
||
// 类型:1=总部发起人,2=省公司发起人,3=省合伙人(外勤),4=省合伙人(内勤),5=区县运营商,6=区县合伙人,7=餐厅,8=配送商,9=总部外勤,10=总部内勤
|
||
if(in_array($agentInfo['agent_type'],[1,2,5])) {
|
||
$pid = $agentInfo['id'];
|
||
$corporateName = $agentInfo['corporate_name'];
|
||
}
|
||
else {
|
||
$pid = $agentInfo['pid'];
|
||
$corporateName = $this->repository->getSearchModel(['id'=>$pid])->value('corporate_name');
|
||
}
|
||
// 获取下级信息
|
||
$children = $this->repository
|
||
->getSearchModel([])
|
||
->where('pid|id', $pid)
|
||
->where('uid', $uid)
|
||
->select()
|
||
->toArray();
|
||
// 判断:是否存在和传递ID一致的角色信息 不存在默认第一个
|
||
if(!in_array($agentInfo['id'],array_column($children,'id'))) $agentInfo = $children[0];
|
||
|
||
return app('json')->success([
|
||
'agent_info' => $agentInfo,
|
||
'children' => $children,
|
||
'corporate_name' => in_array($agentInfo['agent_type'],[1,9,10]) ? '万马奔腾总部' : $corporateName
|
||
]);
|
||
}
|
||
/**
|
||
* Common: 获取我邀请的所有店铺
|
||
* Author: wu-hui
|
||
* Time: 2024/03/29 11:55
|
||
* @return mixed
|
||
*/
|
||
public function getMyInvite(){
|
||
$where = $this->request->params(['merchant_type', 'invite_agent_id']);
|
||
[$page, $limit] = $this->getPage();
|
||
$data = app()->make(MerchantRepository::class)->lst($where, $page, $limit);
|
||
|
||
return app('json')->success($data);
|
||
}
|
||
/**
|
||
* Common: 修改资源股东
|
||
* Author: wu-hui
|
||
* Time: 2024/04/26 14:38
|
||
* @return mixed
|
||
*/
|
||
public function updateShareholders(){
|
||
$params = $this->request->params(['mer_id', 'resource_shareholders_uid']);
|
||
|
||
Merchant::update(['resource_shareholders_uid'=>$params['resource_shareholders_uid']],['mer_id'=>$params['mer_id']]);
|
||
|
||
return app('json')->success('编辑成功');
|
||
}
|
||
|
||
|
||
|
||
}
|