微信 JSSDK 獲取config

class Jsjdk extends Base
{
    private $appId='';
    private $appSecret='';

    public function getSignPackage() {
        $url = $this->data['url'];//前端url
        $jsapiTicket = $this->getJsApiTicket();
        // $url = "http://jin.cisdom.com.cn/furnish.html";
        
        $timestamp = time();
        $nonceStr = $this->createNonceStr();
        // 這裏參數的順序要按照 key 值 ASCII 碼升序排序
        $string = "jsapi_ticket=".$jsapiTicket."&noncestr=".$nonceStr."&timestamp=".$timestamp."&url=".$url;
        $signature = sha1($string);
        $signPackage = array(
            "appId"     => $this->appId,
            "timestamp" => $timestamp,
            "nonceStr"  => $nonceStr,
            "signature" => $signature,
            "url"       => $url,
            "rawString" => $string
        );
        _ajaxReturn(200,'操作成功',$signPackage);
    }

    private function createNonceStr($length = 32) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    private function getJsApiTicket() {
       $data = $this->getCacheTicket();
       if ($data) {
            $ticket = $data['value'];
       } else {
           $accessToken = $this->getAccessToken();
            $url = "http://api.weixin.qq.com/cgi-bin/ticket/getticket?type=1&access_token=".$accessToken;
            $res = $this->httpGet($url);
            $ticket = $res['ticket'];
            if ($ticket) {
                //將微信ticket添加到數據庫
                $data['name'] = "jsapi_ticket";
                $data['expire_time'] = time() + 7000;
                $data['value'] = $ticket;
                $this->setCacheTicket($data);
            }
       }
       return $ticket;
    }

    /**
     * 獲取access_token
     * @return [type] [description]
     * 查看數據庫有沒有符合的access_token,有則返回access_token,沒有則獲取並返回
     */
    private function getAccessToken() {
        
        $data = $this->getCacheToken();
        if ($data) {
            $access_token = $data['value'];
        } else {
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appId."&secret=".$this->appSecret;
            $res = $this->httpGet($url);

            $access_token = $res['access_token'];
            if ($access_token) {
                //將微信access_token添加到數據庫
                $data['name'] = "access_token";
                $data['expire_time'] = time() + 7000;
                $data['value'] = $access_token;
                $this->setCacheToken($data);
            }
        }
        return $access_token;
    }

    private function httpGet($url) {
        $ch = curl_init();
        //設置選項,包括URL
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);//這個是重點。
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);// https請求 不驗證證書和hosts
        curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
        //執行並獲取HTML文檔內容
        $res = curl_exec($ch);
        //釋放curl句柄
        curl_close($ch);
        $arr = json_decode($res,true);
        return $arr;
    }


    //獲取access_token
    private function getCacheToken()
    {
        $time = time();
        $where['name'] = ['=','access_token'];
        $where['expire_time'] = ['>',$time];
        $result = model('wx')->WxFind($where);
        return $result;
    }

    //添加access_token
    private function setCacheToken($token)
    {
        $res = model('wx')->WxAdd($token);
        return $res;
    }

    //獲取jsapi_ticket
    private function getCacheTicket()
    {
        $time = time();
        $where['name'] = ['=','jsapi_ticket'];
        $where['expire_time'] = ['>',$time];
        $result = model('wx')->WxFind($where);
        return $result;
    }

    //添加jsapi_ticket
    private function setCacheTicket($ticket)
    {
        $res = model('wx')->WxAdd($ticket);
        return $res;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章