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

class Index extends Controller
{
    
    public function getReturn($code = 1, $msg = "成功", $data = null)
    {
        return array("code" => $code, "msg" => $msg, "data" => $data);
    }
    public function dernout()
    {
        if(config('url')!=$_SERVER['HTTP_HOST'] || config('url_api')!=$_SERVER['HTTP_HOST'] || config('url_jk')!=$_SERVER['HTTP_HOST']){
            if(config('url')==config('url_api') || config('url')==config('url_jk') || config('url_api')==config('url') || config('url_api')==config('url_jk') || config('url_jk')==config('url') || config('url_jk')==config('url_api')){
                return response()->code(404)->allowCache(false);
            }
        }
    }
    public function index()
    {
        if($dernout = $this->dernout()){
           return $dernout;//域名配置
        }
        return response()->code(404)->allowCache(false);
    }
    
    public function cehsald($user='')//监控支付回调
    {
       if($dernout = $this->dernout()){
           return $dernout;//域名配置
       }
       
       $user = xss($user);
       if (!$user || $user == "") {
            $this->success('404',null,'user（附加信息：请输入管理员账号）参数错误');
       }
       $data = Db::name("pay_user")->where("user", $user)->where("type", 2)->cache(true)->find();
       if (!$data) {
            $this->success('404',null,'user（附加信息：输入管理员账号不正确）');
       }
       $this->Heart('cehsald');//监控回调心跳检测
       $rows = Db::table("pay_order")->where("state", 1)->cursor();
       if(!$rows){
            return '未检测到正在付款的订单';
       }
       foreach ($rows as $row){
            $cache = tohc('Heart');
            if($cache->get('pcyjk')>=date('Y-m-d H:i:s',time())){
                if($row['mid_dm']!='mq_gvxyjk'){//云监控(微信)
                }else{
                  continue;
                }
            }
            $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['mid_id'])->find();
            $cls = common($ree['game_dm']);//支付通道路径
            $run = $cls->cehsald($row);//支付通道数据
            if($run['code']==1){
                $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
                $data = Db::name("pay_user")->where("id", $row['pid'])->find();
                url_notify($eer,$row,$data);
            }
            echo $run['data'];
       }
       return '检测订单监听中...';
    }
    
    public function Cookie()//检测系统功能
    {
        jiankong();//检测监控状态
    }
    
    public function notify()//异步回调
    {
      return "success";
    }
    
    public function Heart($type)//监控回调心跳检测
    {
        if($dernout = $this->dernout()){
           return $dernout;//域名配置
        }
        
        //$type = xss(input('type'));
        if (!$type || $type == "") {
            return json($this->getReturn(-1, "请输入您要监控心跳的类型"));
        }
        
        if($type=='appjkd'){//APP监控端
            $user = xss(input("username"));
            if (!$user || $user == "") {
                return json($this->getReturn(-1, "请输入登录账号"));
            }
            $pass = xss(input("password"));
            if (!$pass || $pass == "") {
                return json($this->getReturn(-1, "请输入登录密码"));
            }
            $atad = Db::name("pay_user")->where("user", $user)->cache(120)->find();
            if ($user != $atad["user"]) {
                return json($this->getReturn(-1, "账号或密码错误"));
            }else
            if (!password_verify($pass, $atad["pass"])) {
                return json($this->getReturn(-1, "账号或密码错误"));
            }
            if($atad['active']==1 and $atad['type']!=2){
                return json($this->getReturn(-1, "您登录的账号已被禁用，请联系客服咨询原因"));
            }
            $cache = tohc('Heart');
            $cache->set('appjkd',date('Y-m-d H:i:s',time()+60*2),60*2);
            $rows = Db::table("pay_gfg")->where("pid", $atad['id'])->cursor();
            foreach ($rows as $row){
                $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['typec_id'])->cache(true)->find();
                if($ree['game_dm']=='mq_gzfbjkd'||$ree['game_dm']=='mq_gvxjkd'||$ree['game_dm']=='mq_gqqjkd'||$ree['game_dm']=='gm_gjkd'){//监控端(微信)||监控端(支付宝)||监控端(QQ)||监控端(固码)
                    Db::name("pay_gfg")->where("pid", $row['pid'])->where("typec_id",$ree['id'])->update(array("lastheart"=>time(),"jkstate"=>1));
                }else{
                    continue;
                }
            }
            return json($this->getReturn(1, "心跳正常"));
        }else if($type=='cehsald'){//宝塔链接监控回调
            $atad = Db::name("pay_sz")->find();
            $cache = tohc('Heart');
            if($atad['ds_todaorul']){//云端网关
                $cache->set('ds_todaorul',$atad['ds_todaorul'],60*2);
                $cache->set('cehsald',date('Y-m-d H:i:s',time()+60*2),60*2);
                $rows = Db::table("pay_gfg")->cursor();
                foreach ($rows as $row){
                    $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['typec_id'])->cache(true)->find();
                    if($ree['game_dm']!='mq_gzfbjkd'and$ree['game_dm']!='mq_gvxjkd'and$ree['game_dm']!='mq_gqqjkd'and$ree['game_dm']!='gm_gjkd'and$ree['game_dm']!='mq_gzfbyjk'and$ree['game_dm']!='mq_gvxyjk'and$ree['game_dm']!='mq_gqqyjk'){//除了监控端(微信)||监控端(支付宝)||监控端(Q Q)||监控端(固码)||云监控(支付宝)||云监控(微信)||云监控(Q Q)
                        Db::name("pay_gfg")->where("pid", $row['pid'])->where("typec_id",$ree['id'])->update(array("lastheart"=>time(),"jkstate"=>1));
                    }else{
                        continue;
                    }
                }
            }
            if($atad['ds_yjkrul']){//云监控网关
                $cache->set('ds_yjkrul',$atad['ds_yjkrul'],60*2);
                $cache->set('pcyjk',date('Y-m-d H:i:s',time()+60*2),60*2);
                $rows = Db::table("pay_gfg")->cursor();
                foreach ($rows as $row){
                    $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['typec_id'])->find();
                    if($ree['game_dm']=='mq_gvxyjk' || $ree['game_dm']=='mq_gzfbyjk' || $ree['game_dm']=='mq_gqqyjk'){//云监控(微信)||云监控(支付宝)||云监控(Q Q)
                        Db::name("pay_gfg")->where("pid", $row['pid'])->where("typec_id",$ree['id'])->update(array("lastheart"=>time(),"jkstate"=>1));
                    }else{
                        continue;
                    }
                }
            }
            closeEndOrder();//检测过期订单
        }else{
            return json($this->getReturn(-1, "心跳失败"));
        }
        
    }
    
    public function appdds()//APP监控端推送付款数据
    {
        if($dernout = $this->dernout()){
           return $dernout;//域名配置
        }
       
        $user = xss(input("username"));
        if (!$user || $user == "") {
            return json($this->getReturn(-1, "请输入登录账号"));
        }
        $pass = xss(input("password"));
        if (!$pass || $pass == "") {
            return json($this->getReturn(-1, "请输入登录密码"));
        }
        $game_dm = xss(input("game_dm"));
        if (!$game_dm || $game_dm == "") {
            return json($this->getReturn(-1, "请输入通道标识"));
        }
        $type = xss(input("type"));
        if (!$type || $type == "") {
            return json($this->getReturn(-1, "请输入付款类型"));
        }
        $money = xss(input("money"));
        if (!$money || $money == "") {
            return json($this->getReturn(-1, "请输入付款金额"));
        }
        $res = Db::name("pay_user")->where("user", $user)->find();
        if ($user != $res["user"]) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }else
        if (!password_verify($pass, $res["pass"])) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }
        if($res['active']==1 and $res['type']!=2){
            return json($this->getReturn(-1, "您登录的账号已被禁用，请联系客服咨询原因"));
        }
        $rows = Db::name("pay_jie")->where("pid",$res['id'])->where("game_dm",$game_dm)->where("state", 1)->find();
        if (!$rows){
            return json($this->getReturn(-1, "该通道已关闭"));
        }
        $row = Db::name("pay_order")->where("pid",$res['id'])->where("mid_id",$rows['id'])->where("type",$type)->where("price",$money)->where("state", 1)->find();
        if($row){
            $eer = Db::name("pay_gfg")->where("pid", $row['pid'])->where("mid",$row['mid'])->find();
            $data = Db::name("pay_user")->where("id", $res['id'])->find();
            if(url_notify($eer,$row,$data)){
                return json($this->getReturn(1, $row['order_id'],"回调成功"));
            }else{
                return json($this->getReturn(1, $row['order_id'],"回调失败"));
            }
        }else{
           return json($this->getReturn(-1, "未检测到正在付款的订单"));
        }
    }
    
    public function shanghu()//商户登录
    {
        if($dernout = $this->dernout()){
           return $dernout;//域名配置
        }
       
        $user = xss(input("username"));
        if (!$user || $user == "") {
            return json($this->getReturn(-1, "请输入登录账号"));
        }
        $pass = xss(input("password"));
        if (!$pass || $pass == "") {
            return json($this->getReturn(-1, "请输入登录密码"));
        }
        $res = Db::name('pay_user')->where('user',$user)->find();
        if ($user != $res["user"]) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }else
        if (!password_verify($pass, $res["pass"])) {
            return json($this->getReturn(-1, "账号或密码错误"));
        }
        if($res['active']==1 and $res['type']!=2){
            return json($this->getReturn(-1, "您登录的账号已被禁用，请联系客服咨询原因"));
        }else{
            return json($this->getReturn(1, "登录成功"));
        }
    }
    
    public function appHeart()//V免签监控端心跳检测
    {
        if($dernout = $this->dernout()){
           return $dernout;//域名配置
        }
       
        $sign = xss(input("sign"));
        if (!$sign || $sign == "") {
            return json($this->getReturn(-1, "请输入通信KEY"));
        }
        $cache = tohc('Heart');
        $cache->set('appjkd',date('Y-m-d H:i:s',time()+60*5),60*5);
        $rows = Db::table("pay_user")->where("active", 0)->cursor();
        foreach ($rows as $row){
            if ($sign == md5(xss(input("t")).$row["key"])) {
                $rows = Db::table("pay_gfg")->where("pid", $row['id'])->cursor();
                foreach ($rows as $row){
                    $ree = Db::name("pay_jie")->where("pid", $row['pid'])->where("id",$row['typec_id'])->cache(true)->find();
                    if($ree['game_dm']=='mq_gzfbjkd'||$ree['game_dm']=='mq_gvxjkd'){//监控端
                        Db::name("pay_gfg")->where("pid", $row['pid'])->where("typec_id",$ree['id'])->update(array("lastheart"=>time(),"jkstate"=>1));
                    }else{
                        continue;
                    }
                }
            }
        }
        return json($this->getReturn(1, "心跳正常"));
    }
    
    public function appPush()//V免签监控端推送付款数据接口
    {
        if($dernout = $this->dernout()){
           return $dernout;//域名配置
        }
       
        $sign = xss(input("sign"));
        if (!$sign || $sign == "") {
            return json($this->getReturn(-1, "请输入商户KEY"));
        }
        $type = xss(input("type"));
        if (!$type || $type == "") {
            return json($this->getReturn(-1, "请输入付款类型"));
        }
        $money = xss(input("price"));
        if (!$money || $money == "") {
            return json($this->getReturn(-1, "请输入付款金额"));
        }
        if($type=='1'){
            $type = 'weixin';
            $ros = Db::name("pay_qrcode")->where("game_dm",'mq_gvxjkd')->where("state", 1)->find();
            if (!$ros){
                return json($this->getReturn(-1, "该通道已关闭"));
            }
        }else{
            $type = 'alipay';
            $ros = Db::name("pay_qrcode")->where("game_dm",'mq_gzfbjkd')->where("state", 1)->find();
            if (!$ros){
                return json($this->getReturn(-1, "该通道已关闭"));
            }
        }
        $rows = Db::table("pay_user")->where("active", 0)->cursor();
        foreach ($rows as $row){
            if ($sign == md5(xss(input("type")).xss(input("price")).xss(input("t")).$row["key"])) {
                $row = Db::name("pay_order")->where("mid_dm",$ros['game_dm'])->where("type",$type)->where("price",$money)->where("state", 1)->find();
                if($row){
                    $data = Db::name("pay_user")->where("id", $row['pid'])->find();
                    $eer = Db::name("pay_gfg")->where("mid",$row['mid'])->find();
                    if(url_notify($eer,$row,$data)){
                        return json($this->getReturn(1, $row['order_id'],"回调成功"));
                    }else{
                        return json($this->getReturn(1, $row['order_id'],"回调失败"));
                    }
                }else{
                    return json($this->getReturn(-1, "未检测到正在付款的订单"));
                }
            }
        }
    }
    
    public function pcvxyjk()//PC端云监控微信推送付款数据
    {
        if($dernout = $this->dernout()){
           return $dernout;//域名配置
        }
       
        $ym = xss(input("ym"));
        if (!$ym || $ym == "") {
            return json($this->getReturn(-1, "请输入主域名"));
        }
        $sq = xss(input("sq"));
        if (!$sq || $sq == "") {
            return json($this->getReturn(-1, "请输入设备码"));
        }
        $game_dm = xss(input("game_dm"));
        if (!$game_dm || $game_dm == "") {
            return json($this->getReturn(-1, "请输入通道标识"));
        }
        $type = xss(input("type"));
        if (!$type || $type == "") {
            return json($this->getReturn(-1, "请输入付款类型"));
        }
        $mingcheng = xss(input("mingcheng"));
        if (!$mingcheng || $mingcheng == "") {
            return json($this->getReturn(-1, "请输入店员名称"));
        }
        $money = xss(input("money"));
        if (!$money || $money == "") {
            return json($this->getReturn(-1, "请输入付款金额"));
        }
        if ($ym != getUrlHost(config('url'))) {
            return json($this->getReturn(-1, "信息验证错误"));
        }else
        $cache = tohc('ydqu');
        if ($sq != $cache->get('sq')) {
            return json($this->getReturn(-1, "信息验证错误"));
        }
        $rows = Db::name("pay_qrcode")->where("game_dm",$game_dm)->where("state", 1)->find();
        if (!$rows){
            return json($this->getReturn(-1, "该通道已关闭"));
        }
        $eer = Db::name("pay_gfg")->where("zhanghao",DeleteHtml($mingcheng))->where("jkstate",1)->find();
        $row = Db::name("pay_order")->where("mid_dm",$rows['game_dm'])->where("type",$type)->where("mid",$eer['mid'])->where("price",$money)->where("state", 1)->find();
        $data = Db::name("pay_user")->where("id", $row['pid'])->find();
        if($row){
            if(url_notify($eer,$row,$data)){
                return json($this->getReturn(1, $row['order_id'],"回调成功"));
            }else{
                return json($this->getReturn(1, $row['order_id'],"回调失败"));
            }
        }else{
           return json($this->getReturn(-1, "未检测到正在付款的订单"));
        }
    }
    
    public function ydvxyd()//云端微信推送付款数据
    {
        if($dernout = $this->dernout()){
           return $dernout;//域名配置
        }
       
        $ym = xss(input("ym"));
        if (!$ym || $ym == "") {
            return json($this->getReturn(-1, "请输入主域名"));
        }
        $sq = xss(input("sq"));
        if (!$sq || $sq == "") {
            return json($this->getReturn(-1, "请输入设备码"));
        }
        $game_dm = xss(input("game_dm"));
        if (!$game_dm || $game_dm == "") {
            return json($this->getReturn(-1, "请输入通道标识"));
        }
        $type = xss(input("type"));
        if (!$type || $type == "") {
            return json($this->getReturn(-1, "请输入付款类型"));
        }
        $wxsb = xss(input("wxsb"));
        if (!$wxsb || $wxsb == "") {
            return json($this->getReturn(-1, "请输入微信设备"));
        }
        if ($ym != getUrlHost(config('url'))) {
            return json($this->getReturn(-1, "信息验证错误"));
        }else
        $cache = tohc('ydqu');
        if ($sq != $cache->get('sq')) {
            return json($this->getReturn(-1, "信息验证错误"));
        }
        $rows = Db::name("pay_qrcode")->where("game_dm",$game_dm)->where("state", 1)->find();
        if (!$rows){
            return json($this->getReturn(-1, "该通道已关闭"));
        }
        $api = file_get_contents("php://input");
        $json = json_decode($api,true);
        if($json['Module'] == 'WXSyncMsg'){
            $json = json_decode($json['AddMsgs'],true);
            foreach($json as $json){
                if ($json['MsgType'] == 49){
                    $txt = $json['Content']['String'];
                    if(mb_strpos($txt,'收款到账通知') !== false){
                        if(mb_strpos($txt,'收款方备注') !== false){
                            $pay_id = getSubstr($txt,'收款方备注',"\n");
                        }
                        $money = getSubstr($txt,'收款金额￥',"\n");
                    }else if(mb_strpos($txt,'微信收款商业版') !== false){
                        if(mb_strpos($txt,'微信收款单') !== false){
                            if(mb_strpos($txt,'收款说明') !== false){
                                $pay_id = getSubstr($txt,'收款说明',"\n");
                            }else{
                                $pay_id = getSubstr($txt,'收款门店',"\n");
                            }
                        }else{
                            $pay_id = getSubstr($txt,'收款门店',"\n");
                        }
                        $money = getSubstr($txt,'收款金额¥',"\n");
                    }else if(mb_strpos($txt,'赞赏到账通知') !== false){
                        if(mb_strpos($txt,'来自') !== false){
                            $pay_id = getSubstr($txt,'来自',"\n");
                        }
                        $money = getSubstr($txt,'收款金额￥',"\n");
                    }else{
                        continue;
                    }
                }else{
                    continue;
                }
            }
        }
        $eer = Db::name("pay_gfg")->where("zhanghao",$wxsb)->where("jkstate",1)->find();
        if($eer['ck']=='无数据'){
            $row = Db::name("pay_order")->where("mid_dm",$rows['game_dm'])->where("type",$type)->where("mid",$eer['mid'])->where("pay_id",$pay_id)->where("price",$money)->where("state", 1)->find();
        }else{
            $row = Db::name("pay_order")->where("mid_dm",$rows['game_dm'])->where("type",$type)->where("mid",$eer['mid'])->where("price",$money)->where("state", 1)->find();
        }
        $data = Db::name("pay_user")->where("id", $row['pid'])->find();
        if($row){
            if(url_notify($eer,$row,$data)){
                return json($this->getReturn(1, $row['order_id'],"回调成功"));
            }else{
                return json($this->getReturn(1, $row['order_id'],"回调失败"));
            }
        }else{
           return json($this->getReturn(-1, "未检测到正在付款的订单"));
        }
    }
    
    public function enQrcode($url)//生成二维码
    {
        if($dernout = $this->dernout()){
           return $dernout;//域名配置
        }
       
        $data = Db::name("pay_sz")->find();
        // 自定义二维码配置
        $config = [
            'title'         => true,
            'title_content' => $data['web_name'],
            'logo'          => false,
            'logo_url'      => (xss(input('logo'))==''? './static/user/assets/images/favicon.ico':xss(input('logo'))),
            'logo_size'     => 50,
        ];
        $qr_code = new QrcodeServer($config);
        $content = $qr_code->createServer($url);
        return response($content,250,['Content-Length'=>strlen($content)])->contentType('image/png');
    }
    
    public function shoquan()//更换云端
    {
        $sq = xss(input("sq"));
        if (!$sq || $sq == "") {
            return json($this->getReturn(-1, '请输入设备码'));
        }
        $cache = tohc('ydqu');
        if ($sq != $cache->get('sq')) {
            return json($this->getReturn(-1, '设备码错误'));
        }else{
            $ydwg = xss(input("ydwg"));
            if (!$ydwg || $ydwg == "") {
                return json($this->getReturn(-1, '请输入您要更换的云端网关'));
            }
            $data = array(
                "ds_todaorul" => $ydwg,//云端网关
            );
            $sz = Db::name("pay_sz")->find();
            Db::name("pay_sz")->where("id",$sz['id'])->update($data);
            return json($this->getReturn(1, '云端网关['.$ydwg.']更换成功'));
        }
    }
    
    public function ceshi()//测试
    {
        $post = array(
                'game_dm' => 'zfb_gck',//通道标识
                'type' => 'exec',//传出类型:支付类型:exec||监控类型:cehsald
                'post' => array(//传出数据
                            'uid' => '',//账号UID
                            'ck' => '',//支付宝设备
                        )
            );
        //$cache = tohc('Heart');
        //$ret = curl_post($cache->get('ds_yjkrul'),http_build_query(ydqu($post)));
        //return $ret;    
    }
}