zoukankan      html  css  js  c++  java
  • 调起微信扫一扫

    <?php
    namespace appserviceJssdk;
    /**
    * 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

  • 相关阅读:
    背水一战 Windows 10 (90)
    背水一战 Windows 10 (89)
    背水一战 Windows 10 (88)
    背水一战 Windows 10 (87)
    背水一战 Windows 10 (86)
    背水一战 Windows 10 (85)
    背水一战 Windows 10 (84)
    背水一战 Windows 10 (83)
    背水一战 Windows 10 (82)
    背水一战 Windows 10 (81)
  • 原文地址:https://www.cnblogs.com/jasonLiu2018/p/13189092.html
Copyright © 2011-2022 走看看