<?php
/**
 * User: 呆呆
 * Agreement: 禁止使用本软件（系统）用于任何违法违规业务或项目,造成的任何法律后果允由使用者（或运营者）承担
 * Date: 2021/3/3
 * Time: 14:34
 */
namespace app\common\mq_gzfbyd\controller;//跟目录路径一样
use think\Db;
use think\Controller;

class Index extends Controller
{
    public function getReturn($code = 1, $data = "成功", $json = null)//系统需，要不要动
    {
        return array("code" => $code, "data" => $data, "json" => $json);
    }

    public function Cookie($atad)//监控心跳
    {
        error_reporting(0);
        $zhanghao = $atad['zhanghao'];//通道账号
        $Cookie = $atad['ck'];//通道CK数据
        
        $cache = tohc('Heart');
		$post = array(
            'game_dm' => 'zfb_gck',//通道标识
            'type' => 'zfbHeartBeat',//传出类型
            'post' => array(//传出数据
                    'zfbsb' => $Cookie,//支付宝设备
                )
        );
		$ret = curl_post($cache->get('ds_todaorul'),http_build_query(ydqu($post)));
		$json = json_decode($ret,true);
		if($ret){
		    if($json['code']==-1){
                $code = -1;
                $data = $json['msg'];
            }else{
                $code = 1;
                $data = $json['msg']."余额：".$json['data']['money']." 元";
            }
		}else{
		    $code = 1;
            $data = '服务器终端无响应';
		}
        return $this->getReturn($code,$data,array("time"=>time(),"cookie"=>$Cookie));
    }
    
    public function cehsald($atad)//监控回调
    {
        $sz = Db::name("pay_sz")->find();//系统需，要不要动
        $row = Db::name("pay_gfg")->where("pid", $atad['pid'])->where("mid",$atad['mid'])->find();//系统需，要不要动
        $cache = tohc('mq_gzfbyd');//通道缓存
        $res = $cache->get($row['zhanghao']);
        if(!$res){
            return $this->getReturn(-1,'交易失败', $atad); 
        }
        $json = json_decode($res,TRUE);
        if($json['code']=="1"){
            if($json['data']['data']){
                foreach ($json['data']['data'] as $json) {
                    if($row['bf']!='无数据'){
                        if($json['type']=='在线支付'){
                            if(strtotime($json['pay_date']) > $atad['create_date']){
                                if($json['order_id']!=$atad['pay_id'] and $json['money']==$atad['price']){
                                    $rof = Db::name("pay_order")->where("pid",$atad['pid'])->where("price",$json['money'])->where("order_id",$atad['order_id'])->where("state",1)->update(array("pay_id"=>$json['order_id']));
                                    if($rof){
                                        return $this->getReturn(1,'交易成功', $atad);
                                    }else{
                                        return $this->getReturn(-1,'交易失败', $atad);
                                    }
                                }
                            }else{
                                return $this->getReturn(-1,'交易失败', $atad);
                            }
                        }
                    }else{
                        if($json['type']=='转账'){
                            if($json['pay_id']==$atad['pay_id']){
                                return $this->getReturn(1,'交易成功', $atad);
                            }else{
                                return $this->getReturn(-1,'交易失败', $atad);
                            }
                        }
                    }
                }
            }else{
                return $this->getReturn(-1,'交易失败', $atad);
            }
        }else{
           return $this->getReturn(-1,$json['msg'], $atad); 
        }
    }
    
    public function fukyem($data)//支付页面
    {
        $sz = Db::name("pay_sz")->find();//系统需，要不要动
        $atad= Db::name("pay_gfg")->where("pid", $data['pid'])->where("mid", $data['mid'])->find();//系统需，要不要动
        $user = Db::name("pay_user")->where("id", $data['pid'])->find();//系统需，要不要动
        $data['timeOut'] = $user['bug_close'];//系统需，要不要动
        
        if($data['type']=='alipay'){//支付宝
        
            if($atad['ms']==0){//H5
                $data['money'] = $data['price'];//实际金额
                $data['mid_url'] = "https://ds.alipay.com/?from=mobilecodec&scheme=".urlencode("alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=".http_ssl().http_pay().url('index/pay').'?Ali_PayH5='.$data['order_id']);//H5转跳链接
                // 模板变量赋值
                $data['luji'] = iphtml('pay');//模板路径
                $this->assign('data',$data);
                return $this->fetch(ippublic('pay').'/h5.html');
            }else{//扫码
                $data['money'] = $data['price'];//实际金额
                $data['src'] = url('enQrcode').'?url='.http_ssl().http_pay().url('index/pay').'?Ali_PayH5='.$data['order_id'];//二维码
                $data['mid_url'] = "https://ds.alipay.com/?from=mobilecodec&scheme=".urlencode("alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=".http_ssl().http_pay().url('index/pay').'?Ali_PayH5='.$data['order_id']);//H5转跳链接
                // 模板变量赋值
                $data['luji'] = iphtml('pay');//模板路径
                $this->assign('data',$data);
                return $this->fetch(ippublic('pay').'/index.html');
            }
        }
    }
    
    public function exec($api,$atad,$money,$type)//拉起支付
    {
        $orderId = Db::table('pay_order')->where("pid",$atad["pid"])->where("state",1)->count();
        $orderId = 'ZFB'.date("YmdHms").$orderId.rand(10, 99).rand(11, 89).rand(12, 79).rand(13, 69);
        if($atad['bf']!='无数据'){
            $atad['data'] = $atad['bf'];//通道监控数据
            $atad['pay_id']  = $orderId;//通道订单号
            $atad['mid_url'] = urlencode($atad['bf']);//通道二维码
            return $this->getReturn(1, $atad, $atad['data']);
        }else{
            $atad['pay_id'] = $orderId;//通道订单号
            $atad['mid_url'] = urlencode("https://ds.alipay.com/?from=mobilecodec&scheme=".urlencode("alipays://platformapi/startapp?saId=10000007&clientVersion=3.7.0.0718&qrcode=".urlencode('alipays://platformapi/startapp?appId=20000123&actionType=scan&biz_data={"s":"money","u":"'.$atad['zhanghao'].'","a":"'.$money.'","m":"'.$orderId.'"}')));//通道二维码
            return $this->getReturn(1, $atad, $url);
        }
    }
    
}