<?php
/**
 * 
 */
class Tgapi
{
    protected $api_account;
    protected $sign_key;
    protected $api_url;

    public function __construct()
    {
        $this->api_url = "http://tggood.com/";
        $this->api_account = "15929899804440";
        $this->sign_key = "86e2a2a50beca3253d34bd5c7aa2b687";
    }

    /**
     * [generateCode 生成验签code]
     * @param        Array                    $data [提交数据]
     * @return      [string]                         [加密验签code]
     */
    private function generateCode(Array $data)
    {
        ksort($data);
        $str = '';
        foreach($data as $v){
            $str .= $v;
        }
        $str .= $this->sign_key;
        return strtoupper(md5($str));
    }

    private function sendRequest($url,$post_data=array()){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $contents = curl_exec($ch);
        curl_close($ch);
        print_r($contents);exit;
        return json_decode ($contents, TRUE);
    }

    /**
     * [register 注册会员]
     * @param        [string]                   $username  [会员用户名]
     * @param        string                   $plat_type [平台类型]
     * @return      [Array]                              []
     */
    public function register($username,$plat_type="ag")
    {
        $data = [
            'username' => $username,
            'api_account' => $this->api_account,
            'plat_type' => $plat_type,
        ];
        $data['code'] = $this->generateCode($data);
        $res = $this->sendRequest($this->api_url."/api/register",$data);
        return $res;
    }

    /**
     * [login 登录获取游戏地址]
     * @Author      W
     * @DateTime  2020-09-16T09:54:01+0800
     * @param        [string]                   $username      [会员用户名]
     * @param        string                   $plat_type     [平台类型]
     * @param        string                   $game_type     [游戏类型]
     * @param        integer                  $is_mobile_url [是否手机访问]
     * @param        string                   $game_code     [游戏代码]
     * @return      [Array]                                  
     */
    public function login($username,$plat_type="ag",$game_type="1",$is_mobile_url=0,$game_code="")
    {
        $data = [
            'username' => $username,
            'api_account' => $this->api_account,
            'plat_type' => $plat_type,
            'game_type' => $game_type,
            'is_mobile_url' => $is_mobile_url,
            'game_code' => $game_code,
        ];
        $data['code'] = $this->generateCode($data);
        $res = $this->sendRequest($this->api_url."/api/login",$data);
        return $res;
    }

    /**
     * [userBalance 用户某平台余额]
     * @param        [string]                   $username  [用户名]
     * @param        string                   $plat_type [平台类型]
     * @return      [array]                          
     */
    public function userBalance($username,$plat_type="ag")
    {
        $data = [
            'username' => $username,
            'api_account' => $this->api_account,
            'plat_type' => $plat_type,
        ];
        $data['code'] = $this->generateCode($data);
        $res = $this->sendRequest($this->api_url."/api/user/balance",$data);
        return $res;
    }

    /**
     * [allBalance 查询会员游戏全部平台余额]
     * @param        [string]                   $username [用户名]
     * @return      [Array]                             
     */
    public function allBalance($username)
    {
        $data = [
            'username' => $username,
            'api_account' => $this->api_account,
        ];
        $data['code'] = $this->generateCode($data);
        $res = $this->sendRequest($this->api_url."/api/user/all-balance",$data);
        return $res;
    }

    /**
     * [allRecords 所有游戏记录]
     * @Author      W
     * @DateTime  2020-09-16T11:12:24+0800
     * @param        string                   $username  [用户名]
     * @param        string                   $plat_type [平台类型]
     * @param        string                   $game_type [游戏类型]
     * @param        integer                  $page      [页码]
     * @param        integer                  $limit     [每页数量]
     * @return      [Array]                              
     */
    public function allRecords($username="",$plat_type="",$game_type="",$page=1,$limit=100)
    {
        $data = [
            'username' => $username,
            'plat_type' => $plat_type,
            'game_type' => $game_type,
            'page' => $page,
            'limit' => $limit,
            'api_account' => $this->api_account,
        ];
        $data['code'] = $this->generateCode($data);
        $res = $this->sendRequest($this->api_url."/api/record-all",$data);
        return $res;
    }

    /**
     * [trans 更新免转钱包额度]
     * @param        [string]                   $username           [用户名]
     * @param        [decimal]                   $money              [金额]
     * @param        [string]                   $client_transfer_id [订单id]
     * @return      [Array]                                       
     */
    public function trans($username,$money,$client_transfer_id)
    {
        $data = [
            'username' => $username,
            'money' => $money,
            'client_transfer_id' => $client_transfer_id,
            'api_account' => $this->api_account,
        ];
        $data['code'] = $this->generateCode($data);
        $res = $this->sendRequest($this->api_url."/api/wallet/trans",$data);
        return $res;
    }

    /**
     * [walletBalance 免转钱包余额]
     * @Author      W
     * @DateTime  2020-09-16T13:57:09+0800
     * @param        [string]                   $username [用户名]
     * @return      [Array]                             
     */
    public function walletBalance($username)
    {
        $data = [
            'username' => $username,
            'api_account' => $this->api_account,
        ];
        $data['code'] = $this->generateCode($data);
        $res = $this->sendRequest($this->api_url."/api/wallet/balance",$data);
        return $res;
    }
}
$tg = new Tgapi;
$tg->login('test1');