<?php

namespace app\api\controller;

use app\api\model\User;
use app\api\model\ProductAccount;

class Account
{

    public function  __construct ()
    {
        if(empty($_GET['apikey'])){
            die;
        }
    }
    //查询账号信息 ?apikey=1232455165&product=5&account=test1231
    public function queryAccount()
    {
        //获取传递的数据
        $data = $_GET;
        $apikey = $data['apikey'];
        $user_model = new User;
        $user_info = $user_model->getOne(['apikey'=>$apikey]);
        if (empty($user_info)){
            $return_data = [
                'Code' => 0,
                'Message' => 'apikey不符合规范',
            ];
            echo json_encode($return_data);
            die;
        }
        
        $account_model = new ProductAccount;
        $info = $account_model->getOne(['Account'=>$data['account'],"ProductId" => $data['product'],'UserId'=>$user_info['Id']],'ProductName,PackageName,Account,ConnectCount,StartTime,EndTime');
       
        
        $return_data = [
            'Code' => 0,
            'Data' => $info,
            'Message' => '',
        ];
        echo json_encode($return_data);
    }
    //通过接口开测试
    // /api/account/creeatTest?apikey=1232455165&product=5&package=61&account=test1231&pwd=123
    public function createTest()
    {
        //获取传递的数据
        $data = $_GET;
        //请求的url
        $url = 'http://www.juip.com/api/course/v1/productaccount/ApiCreateTestAccount';

        //请求参数
        $params = array(
            "apikey" => $data['apikey'],
            "ProductId" => $data['product'],
            "PackageId" => $data['package'],
            "Account" => $data['account'],
            "Pwd" => $data['pwd'],
        );

        $data_string = json_encode($params);

        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );
        
        $result = curl_exec($ch);

        echo $result;
    }

    //通过接口开正式账号
    // /api/account/createAccount?apikey=1232455165&package=17&account=test123211&pwd=123&connect=1&orderType=1
    public function createAccount()
    {
        //获取传递的数据
        $data = $_GET;
        $url = 'http://www.juip.com/product/ApiCreateOrder';

        $params = array(
            "apikey" => $data['apikey'],
            "PackageId" => $data['package'],
            "Account" => $data['account'],
            "Pwd" => $data['pwd'],
            "ConnectCount" =>$data['connect'],
            "OrderType" => $data['type'],
            "OPayType" => '100',
            "PayChannel" => '50',
            "UseAccountAmount" => '1',
        );
        
        $data_string = json_encode($params);
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );
         
        $result = curl_exec($ch);
        
        echo $result;
    }

    //通过接口更改账号密码
    public function updateAccountPwd()
    {
        //获取传递的数据
        $data = $_GET;
        $url = 'http://www.juip.com/user/ApiUpdateAccountPwd';

        $params = array(
            "apikey" => $data['apikey'],
            "Account" => $data['account'],
            "Pwd" => $data['pwd'],
            "RePwd" => $data['newPwd'],
        );
        
        $data_string = json_encode($params);
        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($data_string))
        );
         
        $result = curl_exec($ch);
        
        echo $result;
    }

    //获取账号在线状态
    public function onLine()
    {
        //获取传递的数据
        $data = $_GET;
        $url = 'http://www.juip.com/api/course/v1/productaccount/OnLine?productId='.$data['product'].'&account='.$data['account'];

        $apikey = $data['apikey'];
        $user_model = new User;
        $user_info = $user_model->getOne(['apikey'=>$apikey]);
        if (empty($user_info)){
            $data = [
                'Code' => 0,
                'Message' => 'apikey不符合规范',
            ];
            echo json_encode($data);
            die;
        }

        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        )
        );
         
        $result = curl_exec($ch);
        
        echo $result;
    }

    //踢线
    public function killOut()
    {
        //获取传递的数据
        $data = $_GET;
        $url = 'http://www.juip.com/api/course/v1/productaccount/KillOut?productId='.$data['product'].'&id='.$data['id'];

        $apikey = $data['apikey'];
        $user_model = new User;
        $user_info = $user_model->getOne(['apikey'=>$apikey]);
        if (empty($user_info)){
            $data = [
                'Code' => 0,
                'Message' => 'apikey不符合规范',
            ];
            echo json_encode($data);
            die;
        }

        
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json'
        )
        );
         
        $result = curl_exec($ch);
        
        echo $result;
    }
}