392 lines
12 KiB
PHP
392 lines
12 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Merchant;
|
||
|
||
use App\Models\Bank;
|
||
use App\Models\BankRel;
|
||
use App\Models\Merchant;
|
||
use App\Models\MerchantRatio;
|
||
use App\Models\MerchantWallet;
|
||
use App\Models\Server;
|
||
use App\Services\SearchServices;
|
||
use Illuminate\Http\Request;
|
||
use App\Http\Controllers\Controller;
|
||
use Illuminate\Support\Facades\Session;
|
||
|
||
class MerchantController extends BaseDefaultController
|
||
{
|
||
public function __construct()
|
||
{
|
||
|
||
parent::__construct();
|
||
$this->middleware('proxy');
|
||
}
|
||
|
||
public function setPagesInfo()
|
||
{
|
||
$this->pages = [
|
||
'name' => '商户'
|
||
];
|
||
}
|
||
|
||
public function shareData($id = '')
|
||
{
|
||
$level = config('adconfig.merchant_level');
|
||
if (merchant('level') == 1) {
|
||
unset($level[1]);
|
||
}
|
||
if (merchant('level') == 2) {
|
||
unset($level[1]);
|
||
unset($level[2]);
|
||
}
|
||
return ['level' => $level, 'bank' => Bank::where(['is_checked' => 1])->pluck('name', 'id'), 'draw_type' => config('adconfig.draw_type')];
|
||
}
|
||
|
||
|
||
public function checkRule($id = '')
|
||
{
|
||
if (!$id) {
|
||
return [
|
||
'name' => 'required',
|
||
'email' => 'required|email|unique:merchants|max:255',
|
||
|
||
|
||
];
|
||
}
|
||
return [
|
||
'name' => 'required',
|
||
'email' => 'required|email',
|
||
|
||
|
||
];
|
||
}
|
||
|
||
public function extendValidate($request, $model, $id)
|
||
{
|
||
$level = $request->input('level');
|
||
$from_id = $request->input('from_id');
|
||
|
||
//判断身份
|
||
$proxy = merchant();
|
||
if ($level <= $proxy->level) {
|
||
return $this->errorJosn('你不能添加高于或等于你的等级商户');
|
||
}
|
||
|
||
|
||
if ($request->input('ratio') <= $proxy->ratio) {
|
||
return $this->errorJosn('设置费率,不能低于您的费率:' . $proxy->ratio);
|
||
}
|
||
|
||
|
||
//如果已经审核了。则不给修改
|
||
if ($id) {
|
||
if ($model->check_status != 2) {
|
||
return $this->errorJosn('你不能操作已经审核过的商户');
|
||
}
|
||
if ($model->from_id != $proxy->id) {
|
||
return $this->errorJosn('您不能操作其他人的商户');
|
||
}
|
||
}
|
||
|
||
|
||
}
|
||
|
||
public function getCheckStatusOn($level)
|
||
{
|
||
if ($level == 1) {
|
||
return config_cache('merchant_config.merchant_gudong_check');
|
||
}
|
||
if ($level == 2) {
|
||
return config_cache('merchant_config.merchant_daili_check');
|
||
}
|
||
}
|
||
|
||
protected function postDataDb($request, $id = '')
|
||
{
|
||
$data = $request->all();
|
||
if (!$id) {
|
||
//总后台生成
|
||
$data['app_key'] = date('YmdHis') . str_random(6);
|
||
$data['token'] = str_random(32);
|
||
}
|
||
//等待总后台审核
|
||
//是否
|
||
|
||
$data['check_status'] = $this->getCheckStatusOn(\merchant('level')) ? 2 : 1;
|
||
$proxy = merchant();
|
||
$data['from_id'] = $proxy->id;
|
||
$data['next_ratio'] = $proxy->ratio;//上级代理费率
|
||
|
||
if (!$request->input('password')) {
|
||
unset($data['password']);
|
||
} else {
|
||
$data['origin_password'] = $data['password'];
|
||
}
|
||
//开启事务
|
||
//添加的时候需要开启
|
||
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->crateWallte($request, $model);
|
||
//判断等级,
|
||
$level = $request->input('level');
|
||
$from_id = merchant('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);
|
||
}
|
||
}
|
||
}
|
||
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();
|
||
}
|
||
|
||
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();
|
||
|
||
$param['id_in'] = Merchant::subAllMerchant(merchant('id'));
|
||
|
||
$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['draw_money'] = $v->wallets['draw_money'];
|
||
$v['other_money'] = $v->wallets['other_money'];
|
||
$v['total_money'] = $v->wallets['total_money'];
|
||
$v['money'] = $v->wallets['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'] != merchant('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 == \merchant('id')) {
|
||
$back_btn = '';
|
||
}
|
||
//股东上面才有上下级
|
||
if ($v->level == 1) {
|
||
$btn = '<div class="layui-btn-group">' . $this->createGroupBtn($v->id, $v->from_id, 2, '下级代理') . $back_btn . '</div>';
|
||
}
|
||
if ($v->level == 2) {
|
||
|
||
$btn = '<div class="layui-btn-group">' . $this->createGroupBtn($v->id, $v->from_id, 3, '下级商户') . $back_btn . '
|
||
|
||
</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;
|
||
|
||
$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);
|
||
}
|
||
}
|