<?php


use think\Db;

// 封装 增减金额/金币
// $data[xx]['type'] 0加 1减
function _moneyDb($data){
    if(empty($data['uid'])){
        return;
    }
    $uid=$data['uid'];
    $where=[];
    $time=time();
    $ip=_ip();
    if(isset($data['money'])){
        $name=$data['money'];
        if(!isset($name['type'])){
            $type='+';
        }else{
            $type='-';
        }
        $money=$name['money'];
        $where['money'] = Db::raw('money'.$type.$money);//['exp','money'.$type.$money];
        if($type=='+'){
            $where['money_num'] = Db::raw('money_num+'.$money);['exp','money_num+'.$money];
        }
        $content=$name['content'];
        if(isset($content)){
            $datas=$type=='+' ? '0' : '1';
            Db::name('yaoqing_records')->insert(['uid'=>$uid,'content'=>$content ,'money'=>$money,'type'=>0,'data'=>$datas,'time'=>$time,'ip'=>$ip]);
            \app\lumaapi\service\MsgService::weixinUserMoney($uid, $money, 32, $content,$uid);//用户转账通知变动
        }
    }
    if(isset($data['jinbi']['money'])){
        $name=$data['jinbi'];
        if(!isset($name['type'])){
            $type='+';
        }else{
            $type='-';
        }
        $money=$name['money'];
        $where['jinbi'] = Db::raw('jinbi'.$type.$money);//['exp','jinbi'.$type.$money];
        if($type=='+'){
            $where['jinbi_num'] =Db::raw('jinbi_num+'.$money);//['exp','jinbi_num+'.$money];
        }
        $content=$name['content'];
        if(isset($content)){
            $datas=$type=='+' ? '0' : '1';
            Db::name('yaoqing_records')->insert(['uid'=>$uid,'content'=>$content ,'money'=>$money,'data'=>$datas,'type'=>1,'time'=>$time,'ip'=>$ip]);
            \app\lumaapi\service\MsgService::weixinUserMoney($uid, $money, 35, $content,$uid);//用户转账通知变动
        }
    }
    if(empty($where)){
        return;
    }
    $db=DB::name('yaoqing')->where('uid',$uid)->update($where);
    return $db;
}

// 封装 增减金额/金币
function _get_jinbi($jinbi){
    $jinbi1 = $jinbi;
    if($jinbi){
        $jinbi = explode("-",$jinbi);
        if(count($jinbi)>1){
            $jinbi = rand($jinbi[0],$jinbi[1]);
            $jinbi = $jinbi;
        }else{
            $jinbi = $jinbi1;
        }
    }
    return $jinbi;
}

function _huitie_jiangli($user,$id){
    $yaoqing=config()['yaoqing'];
    $uid=$user['id'];   // 我的id
    $s1=$user['yaoqing']['s1']; // 师傅id
    $s0=$user['yaoqing']['s0']; // 师公id
    if(!$id){
        return ['status'=>-1,'error'=>"帖子不存在"];
    }
    $content='回复帖子 id:'.$id.' 奖励金币';
    $jinbi = _get_jinbi($yaoqing['jinbi_huitie_create']);
    if(!$jinbi){
        return ['status'=>-1,'error'=>"没设置回帖奖励"];
    }

    $limit = $yaoqing['jinbi_huitie_limit'];
    if($limit){
        $count=Db::name('yaoqing_records')->where(['uid'=>$uid,'content'=>['like','%回复帖子 id:%']])->whereTime('time','today')->count();
        if($count >= $limit){
            return ['status'=>-1,'error'=>"已达奖励上限"];
        }
    }

    $ret = array();
    $ret['extend'] = 0;
    $tips_jinbi = $jinbi/1;
    $ret['tips'] = '加'.$tips_jinbi.'金币';
    $ret['title'] = '回复帖子';
    $db=_moneyDb([
        'uid'=>$uid,
        'jinbi'=>[
            'money'=>$jinbi,
            'content'=>$content
        ]
    ]);
    if($db){
        $jinbi_s1 = _get_jinbi($yaoqing['jinbi_tiezi_create_s1']);
        $tips_jinbi_s1 = $jinbi_s1/1;
        $content_s1='徒弟回复帖子 id:'.$id.'奖励金币';
        $jinbi_s0 = _get_jinbi($yaoqing['jinbi_tiezi_create_s0']);
        $tips_jinbi_s0 = $jinbi_s0/1;
        $content_s0='徒孙回复帖子 id:'.$id.'奖励金币';
        if($jinbi_s1 || $jinbi_s0){
            if($s1){_moneyDb(['uid'=>$s1,'jinbi'=>['money'=>$jinbi_s1,'content'=>$content_s1]]);}
            if($s0){_moneyDb(['uid'=>$s0,'jinbi'=>['money'=>$jinbi_s0,'content'=>$content_s0]]);}
        }
        /*师傅 师公奖励*/

        $ret['status'] = 1;
        $ret['jinbi'] = $jinbi;
        return $ret;
    }else{
        return ['status'=>-1,'error'=>"保存数据失败"];
    }
}

// 模拟浏览器
 function get($url,$title='pc'){
    if($title=="mobile"){
        $header = "User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Mobile Safari/537.36";
    }else{
        $header = "user-agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.26 Safari/537.36 Core/1.63.6776.400 QQBrowser/10.3.2601.400";
    }
    $cookie = "tt_webid=6638120247277716999; __utma=24953151.217114383.1545557810.1545557810.1545557810.1; __utmc=24953151; __utmz=24953151.1545557810.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); tt_webid=6638120247277716999; UM_distinctid=167da6cb8cb592-0b4d3995c4fb8f-335e4979-144000-167da6cb8ccc32; csrftoken=d029be4d10f8c12c9a4cbbe6fabe0b68; __lnkrntdmcvrd=-1; CNZZDATA1259612802=214011780-1545552526-%7C1545557926; __tasessionId=1tflcj2i01545561045052";
    $headers = array($header);
    // $proxy = "119.127.17.162";
    // $proxyport = "3128";
    $arrContextOptions=array(
        "ssl"=>array(
            "verify_peer"=>false,
            "verify_peer_name"=>false,
        ),
    );
    // return file_get_contents($url,false, stream_context_create($arrContextOptions));
    // exit();
    $url = $url;//以百度为例
    $data = array();
    $curl = curl_init();//初始化一个curl会话；
    curl_setopt($curl,CURLOPT_URL,$url);//指定访问的url
    curl_setopt($curl,CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl,CURLOPT_COOKIE,$cookie);
    // curl_setopt($curl,CURLOPT_PROXY,$proxy);
    // curl_setopt($curl,CURLOPT_PROXYPORT,$proxyport);
    curl_setopt($curl,CURLOPT_POST,$data);//post请求的参数
    curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl,CURLOPT_ENCODING, 'gzip');
    curl_setopt($curl,CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);//获取的信息以文件流的方式返回
    $data = curl_exec($curl);//执行curl;
    curl_close($curl); // 关闭CURL会话
    return $data;
}

