调起微信扫一扫

Posted 潇潇六月雨

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调起微信扫一扫相关的知识,希望对你有一定的参考价值。

<?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

以上是关于调起微信扫一扫的主要内容,如果未能解决你的问题,请参考以下文章

Android应用跳转到微信和支付宝扫一扫

asp微信扫一扫代码,用asp写的实现调用微信扫一扫功能

日用品扫一扫微信红包活动

支付宝微信扫同一个二维码也可以付款怎么弄?

autojs脚本一键打开微信扫一扫

H5调用微信扫一扫