<?php

namespace app\api\controller\tencentim;

use app\common\controller\Api;
use app\common\helper\HttpHelper;
use think\Db;
use think\Request;

header('Content-type:text/html; Charset=utf-8');
date_default_timezone_set('PRC');

class Base extends Api
{

    protected $noNeedLogin = ['*'];

    public $tim_url;
    protected $tim_sdkappid;
    protected $tim_key;
    protected $tim_userId;
    protected $userSig;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->tim_url = config("site.tim_url");//推送域名URL
        $this->tim_sdkappid = config("site.tim_sdkappid");//应用sdkappid
        $this->tim_key = config("site.tim_key");//应用密钥
        $this->tim_userId = config("site.tim_userId");//管理员账号

        Vendor("tencent.TLSSigAPIv2");
        $this->api = new \TLSSigAPIv2($this->tim_sdkappid, $this->tim_key);
        $where = [
            "sdkappid" => $this->tim_sdkappid,
            "userId" => $this->tim_userId,
        ];
        $tim_sign_info = Db::name("tencent_tim")->where($where)->find();
        if (1>1) {
            //存过sign信息并且未失效
            $this->userSig = $tim_sign_info['userSig'];
        } else {
            //已失效或者未存过
            $expire = 86400;//存180天
            $nowtm = time();
            $this->userSig = $this->api->genUserSig($this->tim_userId, $expire);

            if ($this->userSig) {
                if (!empty($tim_sign_info)) {
                    //更新
                    $upt_data = [
                        "expire" => $expire,
                        "failuretime" => $expire + $nowtm,
                        "updatetime" => $nowtm
                    ];
                    Db::name("tencent_tim")->where("id", $tim_sign_info['id'])->update($upt_data);
                } else {
                    //插入
                    $insert_data = [
                        "sdkappid" => $this->tim_sdkappid,
                        "userId" => $this->tim_userId,
                        "userSig" => $this->userSig,
                        "expire" => $expire,
                        "failuretime" => $expire + $nowtm,
                        "createtime" => $nowtm
                    ];
                    Db::name("tencent_tim")->insert($insert_data);
                }
            }

        }
    }

    public function send($url, $data)
    {
        $url = $this->tim_url . $url
            . "?usersig=" . $this->userSig
            . "&identifier=" . $this->tim_userId
            . "&sdkappid=" . $this->tim_sdkappid
            . "&random=" . mt_rand(0, 4294967295)
            . "&contenttype=json";
//        file_put_contents("kevin_head.log", $url . PHP_EOL, FILE_APPEND);
//        file_put_contents("kevin_head.log", json_encode($data, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
//        $res = $this->curlPost($url,json_encode($data,JSON_UNESCAPED_UNICODE));
        $res = HttpHelper::curl($url, "POST", $data);
        return $res;
    }

    function curlPost($Url, $data)
    {
        $ch = curl_init($Url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//$data JSON类型字符串
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($data)));
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

}