编程语言
首页 > 编程语言> > php生成微信jssdk wx.config参数,调用js接口

php生成微信jssdk wx.config参数,调用js接口

作者:互联网

服务端请求微信生成config相关参数

客户端ajax请求服务器获取参数

config.php

<?php $appid = 'wx8e66f8993d'; $appsecret = '42190267910b765d37'; index.php url为当前调用jssdk的完整网址,前端url编码 <?php include 'config.php'; include 'JSSDK.php'; $jssdk = new JSSDK($appid, $appsecret); $url = $_GET['url']; $signPackage = $jssdk->getSignPackage($url); $config = array( 'debug' => true, 'appId' => $signPackage['appId'], 'timestamp' => $signPackage['timestamp'], 'nonceStr' => $signPackage['nonceStr'], 'signature' => $signPackage['signature'], 'jsApiList' => array( 'checkJsApi', 'updateTimelineShareData', 'hideOptionMenu', 'updateAppMessageShareData', 'hideMenuItems', 'showMenuItems' ) ); echo json_encode(['code' => 0,'data' => $config ,'msg' => 'ok']); exit; JSSDK.php <?php /** * @auther: xxf * Date: 2019/11/8 * Time: 15:59 */ class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appSecret) { $this->appId = $appId; $this->appSecret = $appSecret; } public function getSignPackage($url) { $jsapiTicket = $this->getJsApiTicket(); $timestamp = time(); $nonceStr = $this->createNonceStr(); $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; $signature = sha1($string); $signPackage = array( "appId" => $this->appId, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; } private function createNonceStr($length = 16) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; $str = ""; for ($i = 0; $i < $length; $i++) { $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1); } return $str; } private function getJsApiTicket() { $file = 'jsapi_ticket.json'; if(!file_exists($file)){ file_put_contents($file, ''); } $data = json_decode(file_get_contents($file)); if (empty($data) || $data->expire_time < time()) { $accessToken = $this->getAccessToken(); $url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken"; $res = json_decode($this->httpGet($url)); $ticket = $res->ticket; if ($ticket) { $data = new stdClass(); $data->expire_time = time() + 4000; $data->jsapi_ticket = $ticket; file_put_contents($file, json_encode($data)); } } else { $ticket = $data->jsapi_ticket; } return $ticket; } private function getAccessToken() { $file = 'access_token.json'; if(!file_exists($file)){ file_put_contents($file, ''); } $data = json_decode(file_get_contents($file)); if (empty($data) || $data->expire_time < time()) { $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret"; $res = json_decode($this->httpGet($url)); $access_token = $res->access_token; if ($access_token) { $data = new stdClass(); $data->expire_time = time() + 4000; $data->access_token = $access_token; file_put_contents($file, json_encode($data)); } } else { $access_token = $data->access_token; } return $access_token; } private function httpGet($url) { return file_get_contents($url); } } 客户端调用示例 $.ajax({ type: "GET", url: "/jssdk/?url=" + encodeURI(location.href), dataType: "json", data: {}, success: function (res) { console.log(res); wx.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature,// 必填,签名 jsApiList: res.data.jsApiList// 必填,需要使用的JS接口列表 }); }, }) ———————————————— 版权声明:本文为CSDN博主「flysnownet」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/flysnownet/article/details/103009217

标签:jssdk,url,微信,js,token,file,res,ticket,data
来源: https://blog.csdn.net/kunga0814/article/details/118417089