484 lines
16 KiB
PHP
484 lines
16 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Admin;
|
||
|
||
use App\Models\Bank;
|
||
use App\Models\BankRel;
|
||
use App\Models\DrawMoney;
|
||
use App\Models\Gateway;
|
||
use App\Models\Merchant;
|
||
use App\Models\MerchantRatio;
|
||
use App\Models\MerchantWallet;
|
||
use App\Models\Order;
|
||
use App\Models\Server;
|
||
use App\Services\SearchServices;
|
||
use Illuminate\Http\Request;
|
||
use App\Http\Controllers\Controller;
|
||
|
||
class MerchantController extends BaseDefaultController
|
||
{
|
||
public function setPagesInfo()
|
||
{
|
||
$this->pages = [
|
||
'name' => '商户'
|
||
];
|
||
}
|
||
|
||
public function shareData($id = '')
|
||
{
|
||
write_merchant_ratio();
|
||
$gateways = Gateway::where('is_checked', 1)->orderBy('ename', 'desc')->orderBy('client_type', 'desc')->get();//
|
||
$arr = [];
|
||
if (count($gateways) > 0) {
|
||
foreach ($gateways as $k => $v) {
|
||
$arr[$v->ename]['name']=config('adconfig.pay_type')[$v->ename];
|
||
$arr[$v->ename]['data'][]=$v;
|
||
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
//上级是否存在
|
||
$from_id = \request()->input('from_id');
|
||
$from_name = '';
|
||
if ($from_id) {
|
||
//
|
||
$uobj = Merchant::find($from_id);
|
||
$from_name = $uobj['name'] ?: '';
|
||
}
|
||
return [
|
||
'gateways' => $arr,
|
||
'level' => config('adconfig.merchant_level'),
|
||
'bank' => Bank::where(['is_checked' => 1])->pluck('name', 'id'),
|
||
'draw_type' => config('adconfig.draw_type'),
|
||
'from_id' => $from_id,
|
||
'from_name' => $from_name
|
||
];
|
||
}
|
||
|
||
|
||
public function checkRule($id = '')
|
||
{
|
||
if (!$id) {
|
||
return [
|
||
'name' => 'required',
|
||
'email' => 'required|unique:merchants|max:255',
|
||
'total_price' => 'required',
|
||
|
||
//'ratio' => 'required',
|
||
|
||
];
|
||
}
|
||
return [
|
||
'name' => 'required',
|
||
'email' => 'required',
|
||
'total_price' => 'required',
|
||
//'ratio' => 'required',
|
||
|
||
|
||
];
|
||
}
|
||
|
||
public function extendValidate($request, $model, $id)
|
||
{
|
||
$level = $request->input('level');
|
||
$from_id = $request->input('from_id');
|
||
|
||
//禁止使用qq
|
||
/* if (stripos($request->input('email'), '@qq.com')) {
|
||
return $this->errorJosn('禁止使用QQ邮箱');
|
||
}*/
|
||
|
||
if ($from_id) {
|
||
//等级判断股东下不能再有上级
|
||
if ($level == 1) {
|
||
return $this->errorJosn('股东下不能再有上级');
|
||
}
|
||
//判断上级费率,不能低于
|
||
$proxy = Merchant::find($request->input('from_id'));
|
||
if ($request->input('ratio') <= $proxy->ratio) {
|
||
return $this->errorJosn('设置费率,不能低于上级费率:' . $proxy->ratio);
|
||
}
|
||
//股东不能再添加股东
|
||
//代理不能再添加代理
|
||
if ($level == $proxy->level) {
|
||
return $this->errorJosn('同级别是不能再添加同级别的');
|
||
}
|
||
//身份低的不能添加高的
|
||
if ($level < $proxy->level) {
|
||
return $this->errorJosn('身份等级不匹配,可能是代理或股东是不能再添加代理或股东的');
|
||
}
|
||
|
||
|
||
}
|
||
if ($id) {
|
||
|
||
if ($id == $request->input('from_id')) {
|
||
return $this->errorJosn('不能是自己');
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
protected function postDataDb($request, $id = '')
|
||
{
|
||
$data = $request->all();
|
||
if (!$id) {
|
||
$data['app_key'] = date('YmdHis') . str_random(6);
|
||
$data['token'] = str_random(32);
|
||
}
|
||
//如果上级代理不是0,则需要计算上级代理的费率
|
||
if ($data['from_id'] != 0) {
|
||
$proxy = Merchant::find($data['from_id']);
|
||
$data['next_ratio'] = $proxy->ratio;//上级代理费率
|
||
}
|
||
if (!$request->input('password')) {
|
||
unset($data['password']);
|
||
} else {
|
||
$data['origin_password'] = $data['password'];
|
||
}
|
||
|
||
if (isset($data['gateways'])) {
|
||
$data['gateways_id'] = implode(",", $data['gateways']);
|
||
}else
|
||
{
|
||
$data['gateways_id']=0;
|
||
}
|
||
//全部不选则为0
|
||
if(isset($data['all_waygate']))
|
||
{
|
||
if($data['all_waygate']==1)
|
||
{
|
||
$data['gateways_id']=0;
|
||
}
|
||
}
|
||
//添加的时候需要开启
|
||
if (!$id) {
|
||
$data['begindb'] = 1;//开启事务
|
||
}
|
||
|
||
|
||
return $data;
|
||
}
|
||
|
||
protected function updateMerchantRatio($merchant)
|
||
{
|
||
//取得费率表信息
|
||
$ratio = MerchantRatio::where('merchant_id', $merchant->id)->where('is_self', 1)->first();
|
||
|
||
//判断上级是否有上级
|
||
$upObj = Merchant::find($merchant->from_id);
|
||
$hierarchy = 1;//默认1级,表示顶级
|
||
if ($upObj) {
|
||
|
||
if ($upObj->from_id != 0) {
|
||
$hierarchy = 3;
|
||
} else {
|
||
//没有上级,则只有2级别
|
||
$hierarchy = 2;
|
||
}
|
||
}
|
||
//层级不一样了,判断谁的层级高一点
|
||
//全部删除,重新添加
|
||
MerchantRatio::where('merchant_id', $merchant->id)->delete();
|
||
//重新添加
|
||
|
||
|
||
//判断层级
|
||
if ($hierarchy == 3) {
|
||
//3级关系级别
|
||
return MerchantRatio::addDefault($merchant->from_id, $merchant->id, $merchant->ratio);
|
||
} elseif ($hierarchy == 2) {
|
||
//代理级别
|
||
if($merchant->level==2)
|
||
{
|
||
//代理级别
|
||
return MerchantRatio::addProxyLevel($merchant->from_id, $merchant->id, $merchant->ratio);
|
||
}else
|
||
{
|
||
//商户级别
|
||
return MerchantRatio::add($merchant->from_id, $merchant->level, 2, $merchant->id, $merchant->ratio);
|
||
}
|
||
} elseif ($hierarchy == 1) {
|
||
//普通级别
|
||
return MerchantRatio::add(0, $merchant->level, 1, $merchant->id, $merchant->ratio);
|
||
}
|
||
|
||
/* //判断层级是否一样且父没有更换,如果是的话,只需要更新,无需删除和添加操作
|
||
if($hierarchy==$ratio->hierarchy && $merchant->from_id==$ratio->parent_id)
|
||
{
|
||
|
||
MerchantRatio::where('merchant_id',$merchant->id)->update(['hierarchy'=>$hierarchy,'ratio'=>$merchant->ratio,'parent_id'=>$merchant->from_id,'level'=>$merchant->level]);//
|
||
}else
|
||
{
|
||
|
||
|
||
}*/
|
||
|
||
|
||
}
|
||
|
||
protected function saveAfter($request, $model, $id = '')
|
||
{
|
||
//$this->insertBank($request->input('bank'),$model->id,$id);
|
||
|
||
//创建钱包记录
|
||
$this->crateWallte($request, $model);
|
||
|
||
//判断等级,
|
||
$level = $request->input('level');
|
||
$from_id = $request->input('from_id');
|
||
//添加操作
|
||
if ($id) {
|
||
//更新
|
||
//当前的身份等级
|
||
$this->updateMerchantRatio($model);
|
||
|
||
} else {
|
||
//上级ID存在
|
||
if ($from_id) {
|
||
//等级判断
|
||
if ($level == 2) {
|
||
//代理级别添加
|
||
return MerchantRatio::addProxyLevel($from_id, $model->id, $request->input('ratio'));
|
||
}
|
||
//如果添加是商户
|
||
if ($level == 3) {
|
||
return MerchantRatio::addDefault($from_id, $model->id, $model->ratio);
|
||
}
|
||
} else {
|
||
//普通用户,普通代理,普通股东
|
||
return MerchantRatio::add(0, $level, 1, $model->id, $request->input('ratio'));
|
||
}
|
||
}
|
||
write_merchant();//写入配置文件
|
||
//写入费率信息
|
||
write_merchant_ratio();
|
||
|
||
|
||
}
|
||
|
||
public function crateWallte(Request $request, $model)
|
||
{
|
||
$obj = new MerchantWallet();
|
||
//判断是否存在,如果存在不需要再创建
|
||
$has = $obj->where('merchant_id', $model->id)->first();
|
||
if (!$has) {
|
||
MerchantWallet::firstOrCreate(['merchant_id' => $model->id]);
|
||
}
|
||
}
|
||
|
||
public function insertBank($arr, $id = '', $is_del = 0)
|
||
{
|
||
if (count($arr) <= 0) {
|
||
return false;
|
||
}
|
||
|
||
$insert_m_data = [];
|
||
if ($is_del) {
|
||
//删除之前的
|
||
BankRel::where(['model_id' => $id, 'model_type' => 'merchant', 'is_default' => 1])->delete();
|
||
}
|
||
//插入关联表
|
||
$insert_m_data = $arr;
|
||
$insert_m_data['model_id'] = $id;
|
||
$insert_m_data['model_type'] = 'merchant';
|
||
$insert_m_data['is_default'] = 1;
|
||
|
||
BankRel::insert($insert_m_data);
|
||
|
||
}
|
||
|
||
|
||
public function setErrorMsg()
|
||
{
|
||
$messages = [
|
||
|
||
];
|
||
return $messages;
|
||
}
|
||
|
||
public function setModel()
|
||
{
|
||
return new Merchant();
|
||
}
|
||
|
||
protected function getShow($id)
|
||
{
|
||
$show = $this->setModel()->find($id);
|
||
if (!$show) {
|
||
return abort(404);
|
||
}
|
||
view()->share('show', $show);
|
||
|
||
//申请提现资金
|
||
$data['draw_money_wait_number'] = DrawMoney::getDrawMoney(['status_in' => [0, 2]]);
|
||
$day_seizure = $this->daySeizure($id);
|
||
$data['today_money'] = $day_seizure['day_total_money'];
|
||
$data['day_seizure_money'] = $day_seizure['day_seizure_money'];
|
||
$data = $data + $this->shareData();
|
||
return $this->display($data);
|
||
}
|
||
|
||
//每天扣押款
|
||
public function daySeizure($merchant_id)
|
||
{
|
||
//统计今天成就结算总额
|
||
$to_day_total_money = Order::getMerchantTotalToDayMoney($merchant_id, 1);
|
||
$day_seizure_money = 0;
|
||
//每天押款多少比率
|
||
if ($to_day_total_money > 0) {
|
||
$day_seizure_ratio = config_cache('draw_config.draw_day_money_ratio') ? config_cache('draw_config.draw_day_money_ratio') : 0;
|
||
$day_seizure_money = round($to_day_total_money * $day_seizure_ratio / 100, 2);
|
||
}
|
||
return ['day_total_money' => $to_day_total_money, 'day_seizure_money' => $day_seizure_money];
|
||
}
|
||
|
||
public function createGroupBtn($id, $prev_id, $level, $msg, $color = 'default')
|
||
{
|
||
return '<button class="layui-btn layui-btn-sm layui-btn-' . $color . '" lay-event="soso" data-id="' . $id . '" data-pid="' . $prev_id . '" data-level="' . $level . '"> ' . $msg . ' </button>';
|
||
}
|
||
|
||
public function createGroupBackBtn($id, $prev_id, $level, $msg, $color = 'default')
|
||
{
|
||
return '<button class="layui-btn layui-btn-sm layui-btn-' . $color . '" lay-event="soso" data-id="' . $id . '" data-pid="' . $prev_id . '" data-level="' . $level . '"> ' . $msg . ' </button>';
|
||
}
|
||
|
||
public function createGroupAddBtn($id, $msg, $color = 'danger')
|
||
{
|
||
return '<button class="layui-btn layui-btn-sm layui-btn-' . $color . '" lay-event="add" data-id="' . $id . '" > ' . $msg . ' </button>';
|
||
}
|
||
|
||
public function apiJson(Request $request)
|
||
{
|
||
$offset = $request->input('page', 1);
|
||
$pagesize = $request->input('limit', 1);
|
||
$offset = ($offset - 1) * $pagesize;
|
||
$order_id = $request->input('sort', 'id');
|
||
$order_type = $request->input('order', 'desc');
|
||
$debug = $request->input('debug', 0);
|
||
|
||
$param = $request->all();
|
||
$prev_id = $request->input('prev_id', 0);
|
||
$level = $request->input('level', '');
|
||
|
||
//搜索集合地
|
||
$search = (new SearchServices($this->setModel(), $param));
|
||
$model = $search->getList();
|
||
$total = $model->count();
|
||
$result = $model->with(['wallets', 'froms' => function ($query) {
|
||
//再来嵌套一个
|
||
$query->with('froms');
|
||
}])->skip($offset)->orderBy($order_id, $order_type)->orderBy('id', 'desc')->take($pagesize)->get();;
|
||
$narr = array();
|
||
|
||
foreach ($result as $k => $v) {
|
||
$v['edit_url'] = action($this->route['controller_name'] . '@edit', ['id' => $v->id]);
|
||
$v['edit_post_url'] = action($this->route['controller_name'] . '@update', ['id' => $v->id]);
|
||
$v['show_url'] = action($this->route['controller_name'] . '@show', ['id' => $v->id]);
|
||
/* $v['bank_name']=$v->getDefaultBank['name'];
|
||
$v['bank_account']=$v->getDefaultBank['account'];
|
||
$v['bank_realname']=$v->getDefaultBank['realname'];*/
|
||
$v['money'] = $v->wallets['money'];
|
||
$v['draw_money'] = $v->wallets['draw_money'];
|
||
$v['other_money'] = $v->wallets['other_money'];
|
||
$v['total_money'] = $v->wallets['total_money'];
|
||
$v['check_status_name2'] = $v->check_status != 1 ? '<span class="text-red">' . $v->CheckStatusName . '</span>' : $v->CheckStatusName;
|
||
|
||
$v['name_tpl'] = $v->name;
|
||
$from_name = '';
|
||
if ($v->froms['name']) {
|
||
$from_name = '<p class="text-blue ">上级:' . $v->froms['name'] . '</p>';
|
||
if ($v->froms['froms']['name']) {
|
||
$from_name .= '<p class="text-color-lvse">顶级:' . $v->froms['froms']['name'] . '</p>';
|
||
}
|
||
}
|
||
$btn = '';
|
||
$back_id = $prev_id;
|
||
if ($v->level == 3) {
|
||
$level = $v->level - 1;
|
||
}
|
||
if ($v->level == 2) {
|
||
$back_id = 0;
|
||
}
|
||
$back_btn = $this->createGroupBackBtn($back_id, $v->from_id, $level, '返回');
|
||
//全部列表不显示按钮
|
||
if (!$request->input('is_all')) {
|
||
if (!$v->from_id) {
|
||
$back_btn = '';
|
||
}
|
||
//股东上面才有上下级
|
||
if ($v->level == 1) {
|
||
$btn = '<div class="layui-btn-group">' . $this->createGroupBtn($v->id, $v->from_id, 2, '下级代理') . $back_btn
|
||
. $this->createGroupBtn($v->id, $v->from_id, 3, '下级商户') . $this->createGroupAddBtn($v->id, '添加下级') . '</div>';
|
||
}
|
||
if ($v->level == 2) {
|
||
|
||
$btn = '<div class="layui-btn-group">' . $this->createGroupBtn($v->id, $v->from_id, 3, '下级商户') . $back_btn . $this->createGroupAddBtn($v->id, '添加下级') . '
|
||
|
||
</div>';
|
||
}
|
||
if ($v->level == 3) {
|
||
if ($v->from_id) {
|
||
$btn = '<div class="layui-btn-group">' . $back_btn . '
|
||
|
||
</div>';
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
$color = 'layui-bg-green';
|
||
if ($v->level == 1) {
|
||
$color = 'layui-bg-cyan';
|
||
} elseif ($v->level == 2) {
|
||
$color = 'layui-bg-blue';
|
||
}
|
||
$v['level_name_label'] = '<span class="layui-badge ' . $color . '">' . $v->levelName . '</span> ';
|
||
$v['name'] = '<p>' . $v->name . '</p>' . $from_name . '<p>邮箱:' . $v->email . '</p>' . $btn;
|
||
|
||
$v['post_check_url'] = action($this->route['controller_name'] . '@checkHandle', ['id' => $v->id]);
|
||
|
||
|
||
$narr[] = $v;
|
||
}
|
||
$json = [
|
||
"pid" => $prev_id,
|
||
"level" => $level,
|
||
"status" => 1,
|
||
'code' => $total > 0 ? 0 : 1,
|
||
'msg' => $total > 0 ? '请求数据成功' : '暂无数据',
|
||
'count' => $total,
|
||
'data' => $narr
|
||
];
|
||
|
||
if ($debug) {
|
||
return $this->jsonDebug($json);
|
||
}
|
||
return response()->json($json);
|
||
}
|
||
|
||
public function checkHandle($id, Request $request)
|
||
{
|
||
$value = $request->input('value');
|
||
$data = [
|
||
'check_status' => $value
|
||
];
|
||
$r = $this->setModel()::where('id', $id)->update($data);
|
||
if ($r) {
|
||
|
||
//更新
|
||
write_merchant();
|
||
$this->insertLog('审核商户申请状态,状态设置为', $value);
|
||
return $this->jsonMsg(0, '审核操作成功');
|
||
} else {
|
||
$this->insertLog('审核商户申请状态,状态设置为', $value);
|
||
return $this->jsonMsg(1, '审核操作失败');
|
||
|
||
}
|
||
}
|
||
}
|