<?php
/**
 * User: 呆呆
 * Agreement: 禁止使用本软件（系统）用于任何违法违规业务或项目,造成的任何法律后果允由使用者（或运营者）承担
 * Date: 2021/3/3
 * Time: 14:34
 */
namespace app\common\mq_gvxyd\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'];//通道账号
        
        $cache = tohc('Heart');
		$post = array(
            'game_dm' => 'wx_gck',//通道标识
            'type' => 'WXHeartBeat',//传出类型
            'post' => array(//传出数据
                    'wxsb' => $zhanghao,//微信设备
                )
        );
		$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'];
            }
		}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_gvxyd');//通道缓存
        $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['ck']!='无数据'){
                        if($json['money']==$atad['price']){
                            return $this->getReturn(1,'交易成功', $atad);
                        }else{
                            return $this->getReturn(-1,'交易失败', $atad);
                        }
                    }else{
                        if($json['trade_no']==$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']=='weixin'){//微信
            $data['money'] = $data['price'];//实际金额
            if(mb_strpos($data['mid_url'],';base64,') !== false){
                $data['src'] = $data['mid_url'];//二维码
            }else{
                $data['src'] = url("enQrcode","","html",true).'?url='.$data['mid_url'];//二维码
            }
            // 模板变量赋值
            $data['luji'] = iphtml('pay');//模板路径
            $this->assign('data',$data);
            return $this->fetch(ippublic('pay').'/index.html');
        }
        
    }
    
    public function exec($api,$atad,$money,$type)//拉起支付
    {
        $cache = tohc('Heart');
        $orderId = Db::table('pay_order')->where("pid",$atad["pid"])->where("state",1)->count();
        $orderId = 'WX'.date("YmdHms").$orderId.rand(10, 99).rand(11, 89).rand(12, 79).rand(13, 69);
        if($atad['ck']!='无数据'){
            $atad['data'] = $atad['ck'];//通道监控数据
            $atad['pay_id']  = $orderId;//通道订单号
            $atad['mid_url'] = urlencode($atad['ck']);//通道二维码
            return $this->getReturn(1, $atad, $atad['data']);
        }else{
            $post = array(
                'game_dm' => 'mq_gvxyd',//通道标识
                'type' => 'exec',//传出类型:支付类型:exec||监控类型:cehsald
                'post' => array(//传出数据
                            'type' => $type,//支付类型
                            'wxsb' => $atad['zhanghao'],//微信设备
                            'bf' => $atad['bf'],//免输模式
                            'money' => $money,//支付金额
                            'bz' => $orderId,//支付备注
                        )
            );
            $ret = curls($cache->get('ds_todaorul'),http_build_query(ydqu($post)));
            $json = json_decode($ret,TRUE);
            if($json['code']=="-1"){
                return $this->getReturn(-1, $atad['typec_name'].$json['msg']);
            }else{
                $atad['pay_id'] = $json['data']['orderId'];//通道订单号
                $atad['mid_url'] = $json['data']['url'];//通道二维码
                return $this->getReturn(1, $atad, $ret);
            }
        }
    }
    
}