<?php

namespace app\model;

use think\facade\Db;
use think\facade\Cache;
use think\facade\Log;


class TronSdk 
{
    
    private $domain;
    private $username;

    public function __construct()
    {
        $this->domain = "http://tron.konline.top:888/";
        $this->username= "kaihekaiyuan";
         $this->token= "a782a1f489d32687fd9f67f42f35e9e1";
    }
    //setwebhook
    public function setwebhook2($webhookurl, $trcadd){
         $func= $this->domain."custom/webhook";
        // $webhookurl='http://' . env('proxyDomain', 'false') . '/customer/PayApi/pay?type='.$mode;
         $data=['username'=>$this->username,'address'=>$trcadd,"url"=>$webhookurl];
        $sing= md5($this->username.$trcadd.$webhookurl.$this->token);
        $data['sign']=$sing;
         return json_decode(curl_post($func,http_build_query($data)),true);
    }
    public function setwebhook($mode, $trcadd) //模块 订阅地址
    {   $func= $this->domain."custom/webhook";
         $webhookurl='http://' . env('proxyDomain', 'false') . '/customer/PayApi/pay?type='.$mode;
         $data=['username'=>$this->username,'address'=>$trcadd,"url"=>$webhookurl];
        $sing= md5($this->username.$trcadd.$webhookurl.$this->token);
        $data['sign']=$sing;
         return json_decode(curl_post($func,http_build_query($data)),true);

    }
    //delete_webhook
    public function delete_webhook($address){ //删除订阅
        $func= $this->domain."custom/delete_webhook";
        
         $data=['username'=>$this->username,'address'=>$address];
        $sing= md5($this->username.$address.$this->token);
        $data['sign']=$sing;
         return json_decode(curl_post($func,http_build_query($data)),true); 
    }
    public function up_webhook($address,$weburl){ //更新订阅
      $func= $this->domain."custom/up_webhook";
        
         $data=['username'=>$this->username,'url'=>$weburl,'address'=>$address];
        $sing= md5($this->username.$address.$weburl.$this->token);
        $data['sign']=$sing;
         return json_decode(curl_post($func,http_build_query($data)),true);  
    }
    
    public function queryhook($address,$cache=null) //查询订阅
    {   $func= $this->domain."custom/query";
        
         $data=['username'=>$this->username,'address'=>$address];
        $sing= md5($this->username.$address.$this->token);
        $data['sign']=$sing;
        if(empty($cache)){
          $cache= cache::get("custom/query".$address); 
          if(!empty($cache)){
             // var_dump("99999");
             return  $cache;
          }else{
              $cache=json_decode(curl_post($func,http_build_query($data)),true);
              cache::set("custom/query".$address,$cache,1600);
            return $cache;  
          }
        }else{
           $cache=json_decode(curl_post($func,http_build_query($data)),true);
              cache::set("custom/query".$address,$cache,1600);
            return $cache;   
        }
        
    }
    //查询地址是否合规
   public function checkaddress($address){
      $url="https://apilist.tronscanapi.com/api/multiple/chain/query?address=".$address;// 
     $data=json_decode(curl_get($url),true);
     $publicTag=isset($data['publicTag'])?$data['publicTag']:"";
      //var_dump($data);
     if(empty($publicTag)){
        return true; 
     }else{
         return false;
     }
    
    }
}