调起微信扫一扫

时间:2020-06-24 19:49:43   收藏:0   阅读:92
<?php
namespace app\service\Jssdk;
/**
* Created by PhpStorm.
* User: lyy
* Date: 2020/6/22
* Time: 14:51
*/



use Redis;

class Jssdk
{


/**
* @var Redis
*/
private $redis;

private $appId;
private $appSecret;
private $cacheKey;
private $url;

public function __construct( $url, $cacheKey = null)
{
$this->setConfig($url, $cacheKey);
}

public function setConfig($url, $cacheKey = ‘‘){
$this->appId = ‘xxxxx‘;
$this->appSecret = ‘xxxxxxxxxxx‘;
$this->url = $url??‘https://xxxxxx‘;
$this->redis = self::redisConfig();
$this->cacheKey = $cacheKey;
}

private static function redisConfig(){
$redis = new Redis();
$redis->connect(‘127.0.0.1‘, 6379);
return $redis;
}

public function getSignPackage()
{
$nonceStr = $this->createNonceStr();
$timestamp = time();
$queryString = sprintf(‘jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s‘, $this->getJsApiTicket(), $nonceStr, $timestamp, $this->url);

return [
‘appId‘ => $this->appId,
‘nonceStr‘ => $nonceStr,
‘timestamp‘ => $timestamp,
‘url‘ => $this->url,
‘signature‘ => sha1($queryString),
‘rawString‘ => $queryString,
];
}

private function createNonceStr($length = 16)
{
$chars = ‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789‘;
$len = strlen($chars);
$str = ‘‘;
for ($i = 0; $i < $length; ++$i) {
$str .= $chars[mt_rand(0, $len - 1)];
}

return $str;
}

private function getJsApiTicket()
{
$ticket = is_null($this->redis) ? null : ($this->redis->get($this->getJsApiTicketKey()) ?: null);

if (is_null($ticket)) {
// $request = $this->messageFactory->createRequest(
// ‘GET‘,
// ‘https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=‘ . $this->getAccessToken()
// );
$url = ‘https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=‘ . $this->getAccessToken();
$ticketData = json_decode(self::httpGet($url), true);

if (is_array($ticketData) && isset($ticketData[‘ticket‘])) {
!is_null($this->redis) && $this->redis->set($this->getJsApiTicketKey(), $ticketData[‘ticket‘], $ticketData[‘expires_in‘] - 200);
} else {
throw new Exception($ticketData[‘errmsg‘]);
}

return $ticketData[‘ticket‘];
} else {
return $ticket;
}
}

private function getJsApiTicketKey()
{
return ‘wx:jsapi_ticket‘;
}

private function getAccessToken()
{
$accessToken = is_null($this->redis) ? null : ($this->redis->get($this->getAccessTokenKey()) ?: null);

if (is_null($accessToken)) {
// $request = $this->messageFactory->createRequest(
// ‘GET‘,
// "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"
// );
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$accessTokenData = json_decode(self::httpGet($url), true);

if (is_array($accessTokenData) && isset($accessTokenData[‘access_token‘])) {
!is_null($this->redis) && $this->redis->set($this->getAccessTokenKey(), $accessTokenData[‘access_token‘], $accessTokenData[‘expires_in‘] - 200);
} else {
throw new Exception($accessTokenData[‘errmsg‘]);
}

return $accessTokenData[‘access_token‘];
} else {
return $accessToken;
}
}

private function getAccessTokenKey()
{
return $this->cacheKey ?? ‘wx:access_token‘;
}

private static function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);

$res = curl_exec($curl);
curl_close($curl);

return $res;
}





}

https://xiaolongxiacs-app-api.2v2.co

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!