作者 jinglong
1 个管道 的构建 通过 耗费 1 秒

调式微信分享

... ... @@ -520,4 +520,85 @@ class LoginController extends HomeBaseController
->find();
return $info;
}
//获取微信分享配置信息
public function configShareWx($url=''){
$jsapiTicket = $this->getSignature();
// 注意 URL 一定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
if($url === '') {
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
}
$timestamp = time();
$nonceStr = $this->createNonceStr();
$string = 'jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'&timestamp='.$timestamp.'&url='.$url;
$signature = sha1($string);
$data = [
"appId" => $this->appkey2,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
];
$this->apiResponse(1, '成功',$data);
}
//获取微信分享签名随机字符串
public 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;
}
//获取微信分享签名
public function getSignature(){
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2;
$res = $this->http_get($url);
$json_arr = json_decode($res,true);
if(isset($json_arr['errcode'])&&!empty($json_arr['errcode'])){
//用户取消登录
$this->apiResponse(0, '获取配置失败');
}
$token = $json_arr['access_token'];
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';
$res = $this->http_get($url);
$json_arr = json_decode($res,true);
$ticket = $json_arr['ticket'];
return $ticket;
}
//curl get请求
public function http_get($url){
$curl = curl_init();//启动一个CURL会话
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false);//不开启header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); //执行操作
curl_close($curl);
return $result;
}
//curl post请求
public function http_post($url,$data){
$curl = curl_init();//启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false); // 开启header
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); //执行操作
curl_close($curl);
return $result;
}
}
... ...
... ... @@ -34,14 +34,38 @@
</li>
</ul>
</div>
<script src="__TMPL__/public/assets/js/jquery-2.1.0.js"></script>
<script src="http://res2.wx.qq.com/open/js/jweixin-1.4.0.js"></script>
<script>
$(function () {
var appid = '';
var timestamp = '';
var nonceStr = '';
var signature = '';
$.ajax({
type: 'POST',
url: '/portal/login/configShareWx',
data: {
},
dataType: 'json',
async: false,
success: function(data) {
if (data.code == 1) {
appid = data.appid;
timestamp = data.timestamp;
nonceStr = data.nonceStr;
signature = data.signature;
} else {
console.log(data);
}
}
});
wx.config({
debug: false,
appId: "{$data.appid}",
timestamp: "{$data.timestamp}",
nonceStr: "{$data.nonceStr}",
signature: "{$data.signature}",
appId: appid,
timestamp: timestamp,
nonceStr: nonceStr,
signature: signature,
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
... ... @@ -52,4 +76,6 @@
// config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中。
});
});
</script>
... ...
... ... @@ -21,9 +21,6 @@ use think\Session;
class HomeBaseController extends BaseController
{
private $appkey2 = 'wx0bd7bc2aa0f332d6';//微信公众号appkey
private $appsecret2 = 'b62e49f48f48de7b7fff2ea0af3939de';//微信公众号appsecret
protected function initialize()
{
// 监听home_init
... ... @@ -40,7 +37,6 @@ class HomeBaseController extends BaseController
$this->is_like();
$this->getShareCount();
$keyword = $this->request->param('keyword');
$this->configShareWx();
$this->assign('keyword',$keyword);
}
... ... @@ -399,85 +395,4 @@ hello;
$this->assign('share',$share);
}
//获取微信分享配置信息
public function configShareWx($url=''){
$jsapiTicket = $this->getSignature();
// 注意 URL 一定要动态获取,不能 hardcode.
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
if($url === '') {
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
}
$timestamp = time();
$nonceStr = $this->createNonceStr();
$string = 'jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'&timestamp='.$timestamp.'&url='.$url;
$signature = sha1($string);
$data = [
"appId" => $this->appkey2,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
];
$this->assign('data',$data);
}
//获取微信分享签名随机字符串
public 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;
}
//获取微信分享签名
public function getSignature(){
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2;
$res = $this->http_get($url);
$json_arr = json_decode($res,true);
if(!isset($json_arr['access_token'])&&empty($json_arr['access_token'])){
//用户取消登录
$this->redirect('/portal/login/thirdLogin');
}
$token = $json_arr['access_token'];
$url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';
$res = $this->http_get($url);
$json_arr = json_decode($res,true);
$ticket = $json_arr['ticket'];
return $ticket;
}
//curl get请求
public function http_get($url){
$curl = curl_init();//启动一个CURL会话
curl_setopt($curl, CURLOPT_URL,$url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false);//不开启header
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); //执行操作
curl_close($curl);
return $result;
}
//curl post请求
public function http_post($url,$data){
$curl = curl_init();//启动一个CURL会话
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_POST, true); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data)); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, false); // 开启header
//curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);//请求头部
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
$result = curl_exec($curl); //执行操作
curl_close($curl);
return $result;
}
}
\ No newline at end of file
... ...