315 lines
9.8 KiB
PHP
315 lines
9.8 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Controllers\Web;
|
||
|
||
use App\Models\Merchant;
|
||
use App\Models\MerchantRatio;
|
||
use App\Models\MerchantRegist;
|
||
use App\Models\MerchantRegistToken;
|
||
use App\Models\MerchantWallet;
|
||
use App\Models\Order;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\DB;
|
||
use Illuminate\Support\Facades\Hash;
|
||
|
||
use Validator;
|
||
|
||
class IndexController extends BaseController
|
||
{
|
||
//
|
||
public function index()
|
||
{
|
||
$data = [
|
||
'title' => config_cache('config.sitename'),
|
||
'keyword' => config_cache('config.keyword'),
|
||
'description' => config_cache('config.description'),
|
||
];
|
||
|
||
return $this->display($data);
|
||
|
||
}
|
||
|
||
public function merchantRegist($token)
|
||
{
|
||
$token_model = MerchantRegistToken::where('regist_token', $token)->where('is_checked', 1)->first();
|
||
if (empty($token_model)) {
|
||
return abort(403, '注册地址无效');
|
||
}
|
||
$fid_model = Merchant::where('id', $token_model->merchant_id)->first();
|
||
if (empty($fid_model)) {
|
||
return abort(403, '注册链接地址失效');
|
||
|
||
}
|
||
//系统最低金额如果大于支付金额,则推广链接无效
|
||
$system_money=config_cache_default('merchant_config.merchant_regist_min_money',500);
|
||
|
||
if($system_money>($token_model->money+0))
|
||
{
|
||
return $this->alert('商户注册链接已过期');
|
||
}
|
||
$data = [
|
||
'token' => $token,
|
||
'token_model' => $token_model,
|
||
];
|
||
|
||
return view('web.merchantRegist', $data);
|
||
}
|
||
|
||
public function merchantRegistPost(Request $request)
|
||
{
|
||
$check_data = [
|
||
'email' => 'required|unique:merchants|max:255',
|
||
'password' => 'required|string',
|
||
'token' => 'required|string',
|
||
];
|
||
|
||
$error = $this->validatorForm2($request, $check_data, [], ['email' => '邮箱', 'password' => '密码']);
|
||
if (count($error) > 0) {
|
||
$r = $this->formError($error);
|
||
return $this->apiErrorJosn(['field' => $r['data'], 'field_msg' => $r['msg']], 1, '表单验证失败');
|
||
};
|
||
|
||
$token = $request->input('token');
|
||
$email = $request->input('email');
|
||
$password = $request->input('password');
|
||
$qq = $request->input('qq');
|
||
$mobile = $request->input('mobile');
|
||
$skype = $request->input('skype');
|
||
//注册模型TOKEN
|
||
$token_model = MerchantRegistToken::where('regist_token', $token)->where('is_checked', 1)->first();
|
||
if (empty($token_model)) {
|
||
return $this->apiErrorJosn('', 1, '注册链接地址失效');
|
||
}
|
||
//推广商户模型
|
||
$fid_model = Merchant::where('id', $token_model->merchant_id)->where('is_checked', 1)->first();
|
||
if (empty($fid_model)) {
|
||
return $this->apiErrorJosn('', 1, '注册链接地址失效');
|
||
}
|
||
DB::beginTransaction();
|
||
//发起注册申请
|
||
$data = [
|
||
'from_merchant_id' => $token_model->merchant_id,//推广人
|
||
'token_id' => $token_model->id,//注册token_id
|
||
'email' => $email,
|
||
'password' => $password,
|
||
'mobile' => $mobile,
|
||
'qq' => $qq,
|
||
'skype' => $skype,
|
||
'status' => 0,
|
||
'money'=>$token_model->money,
|
||
'ratio'=>$token_model->ratio,
|
||
'type'=>$token_model->type,
|
||
'level'=>$token_model->level
|
||
|
||
];
|
||
$r = MerchantRegist::create($data);
|
||
|
||
//注册类型,是否需要缴费
|
||
if ($token_model->type == 1) {
|
||
|
||
DB::commit();
|
||
$out_data = [
|
||
'pay_url' => route('web.merchant.order',['type'=>'merchant_regist','id'=>$r->id]),
|
||
'regist_id' => $r->id,
|
||
'token' => $token,
|
||
'is_pay' => 1,
|
||
];
|
||
DB::commit();
|
||
return $this->apiSuccessJosn($out_data, 0, '注册申请成功,需要缴费' . $token_model->money.'才能正式开通');
|
||
} else {
|
||
|
||
//发起商户注册
|
||
$m_r = Merchant::create([
|
||
'from_regist'=>2,//推广注册
|
||
'email'=>$email,
|
||
'next_ratio' => $fid_model->ratio,
|
||
'origin_password' => $r->password,
|
||
'password'=>($r->password),
|
||
'name' => $email,
|
||
'ip' => $request->getClientIp(),
|
||
'app_key' => date('YmdHis') . str_random(6),
|
||
'token' => str_random(32),
|
||
'draw_type' => 1,
|
||
'ratio' => $token_model->ratio,
|
||
'from_id' => $token_model->merchant_id,
|
||
'level' => $token_model->level,
|
||
'qq'=>$token_model->qq,
|
||
'mobile'=>$token_model->mobile,
|
||
'skype'=>$token_model->skype
|
||
]);
|
||
//生成推广商户关系表
|
||
$rel_r = $this->updateMerchantRatio($m_r);
|
||
|
||
if ($r && $m_r && $rel_r) {
|
||
//更新注册状态为成功
|
||
MerchantRegist::where('id', $r->id)->update(
|
||
[
|
||
'status' => 1,
|
||
'merchant_id'=>$m_r->id
|
||
]);
|
||
write_merchant();//写入配置文件
|
||
//写入费率信息
|
||
write_merchant_ratio();
|
||
//输出注册信息
|
||
$out_data = [
|
||
'admin_url' => route('merchant.admin.login'),
|
||
'email' => $email,
|
||
'password' => $password,
|
||
'is_pay' => 0,
|
||
'data' => '注册成功,商户后台地址' . route('merchant.admin.login'),
|
||
];
|
||
//更新注册信息
|
||
|
||
DB::commit();
|
||
return $this->apiSuccessJosn($out_data, 0, '注册成功');
|
||
}
|
||
|
||
}
|
||
|
||
}
|
||
|
||
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);
|
||
}
|
||
|
||
}
|
||
|
||
public function apiJosn($error, $code = 0, $msg, $data = [])
|
||
{
|
||
$json_data = [
|
||
'error' => $error,//0表示没有错误
|
||
'code' => $code,//业务代码
|
||
'msg' => $msg,
|
||
'data' => $data,
|
||
];
|
||
return response()->json($json_data);
|
||
}
|
||
|
||
/**
|
||
* 返回没有错误信息的JSON数据
|
||
* @param $code
|
||
* @param $msg
|
||
* @param $data
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function apiSuccessJosn($data = [], $code = 0, $msg = '获取成功')
|
||
{
|
||
return $this->apiJosn(0, $code, $msg, $data);
|
||
}
|
||
|
||
/**
|
||
* 返回有错误信息的JSON数据
|
||
* @param $code
|
||
* @param $msg
|
||
* @param $data
|
||
* @return \Illuminate\Http\JsonResponse
|
||
*/
|
||
public function apiErrorJosn($data = [], $code = 1, $msg = '获取失败')
|
||
{
|
||
return $this->apiJosn(1, $code, $msg, $data);
|
||
}
|
||
|
||
public function apiAutoJosn($data)
|
||
{
|
||
if (empty($data)) {
|
||
return $this->apiErrorJosn([]);
|
||
}
|
||
return $this->apiSuccessJosn($data, 0);
|
||
}
|
||
|
||
/**
|
||
* 验证表单
|
||
* @param $request 请求参数
|
||
* @param $check_data 验证规则
|
||
* @param $message_data 验证规则对应提示
|
||
* @param array $filed_name_data 全局字段名字对应关系名字
|
||
* @return array
|
||
*/
|
||
protected function validatorForm2($request, $check_data, $message_data = [], $filed_name_data = [])
|
||
{
|
||
|
||
|
||
$validator = Validator::make($request->all(), $check_data, $message_data, $filed_name_data);
|
||
if ($validator->fails()) {
|
||
|
||
return $validator->errors();
|
||
|
||
}
|
||
|
||
return [];
|
||
}
|
||
|
||
public function md5Code(Request $request)
|
||
{
|
||
$merchant = Merchant::first()->makeVisible('password')->toArray();
|
||
print_r($merchant);
|
||
|
||
}
|
||
|
||
public function scan(Request $request)
|
||
{
|
||
$this->order = Order::find(3);
|
||
$data = [
|
||
'url' => '',
|
||
'order' => $this->order,
|
||
'type_name' => config('adconfig.pay_type')[$this->order->pay_type],
|
||
];
|
||
return view('web.order.scan', $data);
|
||
|
||
}
|
||
|
||
public function toUrlParams($params)
|
||
{
|
||
$string = '';
|
||
if (!empty($params)) {
|
||
$array = [];
|
||
foreach ($params as $key => $value) {
|
||
$array[] = $key . '=' . $value;
|
||
}
|
||
$string = implode("&", $array);
|
||
}
|
||
return $string;
|
||
}
|
||
|
||
|
||
}
|