<?php
namespace app\daili\controller;
use think\Controller;
use app\daili\model\Login as LoginModel;
use app\admin\model\Option as OptionModel;
use app\daili\model\Alipaysubmit as SubmitModel;
use app\index\model\Notify as NotifyModel;
class Login extends Controller {
	function _initialize() {
		$res=(new OptionModel())->getinfo();
		$this->assign('siteinfo',$res);
	}
	public function login() {
		if(request()->ispost()) {
			$res=(new LoginModel())->login($_POST);
			if($res['valid']) {
				$this->success($res['msg'],'/daili.php/index/index');
				exit;
			} else {
				$this->error($res['msg']);
				exit;
			}
		}
		return view();
	}
	public function reg() {
		if(request()->ispost()) {
			$username=xss_clean($_POST['username']);
			$email=xss_clean($_POST['email']);
			$paytype=$_POST['paytype'];
			if($username==""||$email=="") {
				$this->error('输入不规范,请检查');
				exit;
			} else {
				$finddl=(new LoginModel())->finddl($username);
				if($finddl) {
					$this->error('代理已存在');
					exit;
				} else {
					$siteinfo=(new OptionModel())->getinfo();
					$un_pw=$username;
					$email=$email;
					$zdy = $un_pw."--".$email;
					if($siteinfo['paytype']==2) {
						$bbspay_id = $siteinfo['bbspayid'];
						$bbspay_key = $siteinfo['bbspaykey'];
						$money = $siteinfo['opdl'];
						$paytype = $_POST['paytype'];
						$ddid = $un_pw."--".$email;
						
						$sign = md5($bbspay_id.$ddid.$zdy.$paytype.$money.$bbspay_key);
						$jxpayurl = "mid=".$bbspay_id."&payId=".$ddid.'&param='.$zdy.'&type='.$paytype."&price=".$money.'&sign='.$sign.'&notifyUrl='.$siteinfo['siteurl']."daili.php/login/regnotify".'&returnUrl='.$siteinfo['siteurl']."daili.php/login/regreturn".'&isHtml=1';
						header("Location:http://www.3mpay.com/createOrder?{$jxpayurl}");
						exit();
						
					} else if($siteinfo['paytype']==0) {
						$notify_url =$siteinfo['siteurl'].'daili.php/login/regnotify';
						$return_url =$siteinfo['siteurl'].'daili.php/login/regreturn';
						switch ($paytype) {
							case '1': $paytype='alipay';
							break;
							case '2': $paytype='qqpay';
							break;
							case '3': $paytype='wxpay';
							break;
							default: $paytype='alipay';
							break;
						}
						$parameter =array("pid" => trim($siteinfo['partner']), "type" => $paytype, "notify_url" => $notify_url, "return_url" => $return_url, "out_trade_no" => $un_pw."--".$email, "name" => '代理账户开通', "money" => $siteinfo['opdl'], "sitename" => $siteinfo['sitename'], );
						$alipayinfo=array('notify_url' =>$notify_url, 'return_url' =>$return_url, 'partner' =>$siteinfo['partner'], 'key' =>$siteinfo['key'], 'sign_type' =>strtoupper('MD5'), 'input_charset' =>strtolower('utf-8'), 'transport' =>'http', 'apiurl' =>$siteinfo['payapi'], );
						$alipaySubmit =new SubmitModel($alipayinfo);
						$html_text =$alipaySubmit->buildRequestForm($parameter);
						echo $html_text;
					}
				}
			}
		}
		return view();
	}
	public function regnotify() {
		$siteinfo=(new OptionModel())->getinfo();
		$bbspay_id = $siteinfo['bbspayid'];
		$bbspay_key = $siteinfo['bbspaykey'];

		if($siteinfo['paytype']==2) {
			$payId = $_GET['payId'];
			$param = $_GET['param'];
			$type = $_GET['type'];
			$price = $_GET['price'];
			$reallyPrice = $_GET['reallyPrice'];
			$sign = $_GET['sign'];
			$_sign =  md5($bbspay_id.$payId.$param.$type.$price.$reallyPrice.$bbspay_key);

			if ($_sign != $sign) {
				exit('fail');
			} else {
				$dlinfo=explode("--", $_GET['param']);
				$username=$dlinfo[0];
				$email=$dlinfo[1].'@qq.com';
				$regdl=(new LoginModel())->reg($username,$email);
				if($regdl) {
					echo 'success';
				} else {
					echo 'fail';
				}
			}
		} else if($siteinfo['paytype']==0) {
			$notify_url =$siteinfo['siteurl'].'daili.php/login/regnotify';
			$return_url =$siteinfo['siteurl'].'daili.php/login/regreturn';
			$siteinfo=(new OptionModel())->getinfo();
			$alipayinfo=array('notify_url' =>$notify_url, 'return_url' =>$return_url, 'partner' =>$siteinfo['partner'], 'key' =>$siteinfo['key'], 'sign_type' =>strtoupper('MD5'), 'input_charset' =>strtolower('utf-8'), 'transport' =>'http', 'apiurl' =>$siteinfo['payapi'], );
			$alipayNotify =new NotifyModel($alipayinfo);
			$verify_result =$alipayNotify->verifyNotify();
			if($verify_result) {
				$dailiid =$_GET['out_trade_no'];
				$money =$_GET['money'];
				if ($_GET['trade_status'] == 'TRADE_SUCCESS') {
					$dlinfo=explode("--", $_GET['out_trade_no']);
					$username=$dlinfo[0];
					$email=$dlinfo[1].'@qq.com';
					$regdl=(new LoginModel())->reg($username,$email);
				}
				echo 'success';
			} else {
				echo 'fail';
			}
		}
	}
	public function regreturn() {
		$siteinfo=(new OptionModel())->getinfo();
		$bbspay_id = $siteinfo['bbspayid'];
		$bbspay_key = $siteinfo['bbspaykey'];
		if($siteinfo['paytype']==2) {
			$payId = $_GET['payId'];
			$param = $_GET['param'];
			$type = $_GET['type'];
			$price = $_GET['price'];
			$reallyPrice = $_GET['reallyPrice'];
			$sign = $_GET['sign'];
			$_sign =  md5($bbspay_id . $payId . $param . $type . $price . $reallyPrice . $bbspay_key);
			if ($_sign != $sign) {
				exit('fail');
			} else {
				$this->success('开通成功,正在返回登录页面','daili/index/index');
				exit;
			}
		} else if($siteinfo['paytype']==0) {
			$notify_url =$siteinfo['siteurl'].'daili/login/regnotify';
			$return_url =$siteinfo['siteurl'].'daili/login/regreturn';
			$alipayinfo=array('notify_url' =>$notify_url, 'return_url' =>$return_url, 'partner' =>$siteinfo['partner'], 'key' =>$siteinfo['key'], 'sign_type' =>strtoupper('MD5'), 'input_charset' =>strtolower('utf-8'), 'transport' =>'http', 'apiurl' =>$siteinfo['payapi'], );
			$alipayNotify =new NotifyModel($alipayinfo);
			$verify_result =$alipayNotify->verifyReturn();
			if($verify_result) {
				$money =$_GET['money'];
				if($_GET['trade_status'] == 'TRADE_SUCCESS') {
				} else {
					echo "trade_status=".$_GET['trade_status'];
				}
				$this->success('开通成功,正在返回登录页面','daili/index/index');
				exit;
			} else {
				echo 'fail';
			}
		}
	}
}