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

调式分享

... ... @@ -35,4 +35,3 @@
</ul>
</div>
<!--微信分享-->
<include file="public@share" />
... ...
... ... @@ -21,8 +21,7 @@ use think\Session;
class HomeBaseController extends BaseController
{
private $appkey2 = 'wx0bd7bc2aa0f332d6';//微信公众号appkey
private $appsecret2 = 'b62e49f48f48de7b7fff2ea0af3939de';//微信公众号appsecret
protected function initialize()
{
// 监听home_init
... ... @@ -38,7 +37,6 @@ class HomeBaseController extends BaseController
$this->is_collection();
$this->is_like();
$this->getShareCount();
$this->wxShare();
$keyword = $this->request->param('keyword');
$this->assign('keyword',$keyword);
}
... ... @@ -398,76 +396,4 @@ hello;
$this->assign('share',$share);
}
//获取微信分享配置信息
public function wxShare($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;
}
//获取access_token
public function getWxAccessToken(){
$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);
$token = $json_arr['access_token'];
return $token;
}
//获取微信分享签名
public function getSignature(){
if(isset($_SESSION['ticket_expire_time']) && $_SESSION['ticket_expire_time'] > time() && $_SESSION['ticket']){
$ticket = $_SESSION['ticket'];
}else{
$token = $this->getWxAccessToken();
$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'];
$_SESSION['ticket'] = $ticket;
$_SESSION['ticket_expire_time'] = time()+7000;
}
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;
}
}
\ No newline at end of file
... ...