|
@@ -21,6 +21,8 @@ use think\Session; |
|
@@ -21,6 +21,8 @@ use think\Session; |
21
|
|
21
|
|
22
|
class HomeBaseController extends BaseController
|
22
|
class HomeBaseController extends BaseController
|
23
|
{
|
23
|
{
|
|
|
24
|
+ private $appkey2 = 'wx0bd7bc2aa0f332d6';//微信公众号appkey
|
|
|
25
|
+ private $appsecret2 = 'b62e49f48f48de7b7fff2ea0af3939de';//微信公众号appsecret
|
24
|
protected function initialize()
|
26
|
protected function initialize()
|
25
|
{
|
27
|
{
|
26
|
// 监听home_init
|
28
|
// 监听home_init
|
|
@@ -36,6 +38,7 @@ class HomeBaseController extends BaseController |
|
@@ -36,6 +38,7 @@ class HomeBaseController extends BaseController |
36
|
$this->is_collection();
|
38
|
$this->is_collection();
|
37
|
$this->is_like();
|
39
|
$this->is_like();
|
38
|
$this->getShareCount();
|
40
|
$this->getShareCount();
|
|
|
41
|
+ $this->wxShare();
|
39
|
$keyword = $this->request->param('keyword');
|
42
|
$keyword = $this->request->param('keyword');
|
40
|
$this->assign('keyword',$keyword);
|
43
|
$this->assign('keyword',$keyword);
|
41
|
}
|
44
|
}
|
|
@@ -395,4 +398,76 @@ hello; |
|
@@ -395,4 +398,76 @@ hello; |
395
|
$this->assign('share',$share);
|
398
|
$this->assign('share',$share);
|
396
|
}
|
399
|
}
|
397
|
|
400
|
|
|
|
401
|
+ //获取微信分享配置信息
|
|
|
402
|
+ public function wxShare($url=''){
|
|
|
403
|
+ $jsapiTicket = $this->getSignature();
|
|
|
404
|
+ // 注意 URL 一定要动态获取,不能 hardcode.
|
|
|
405
|
+ $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
|
|
|
406
|
+ if($url === '') {
|
|
|
407
|
+ $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
|
|
|
408
|
+ }
|
|
|
409
|
+ $timestamp = time();
|
|
|
410
|
+ $nonceStr = $this->createNonceStr();
|
|
|
411
|
+ $string = 'jsapi_ticket='.$jsapiTicket.'&noncestr='.$nonceStr.'×tamp='.$timestamp.'&url='.$url;
|
|
|
412
|
+ $signature = sha1($string);
|
|
|
413
|
+ $data = [
|
|
|
414
|
+ "appId" => $this->appkey2,
|
|
|
415
|
+ "nonceStr" => $nonceStr,
|
|
|
416
|
+ "timestamp" => $timestamp,
|
|
|
417
|
+ "url" => $url,
|
|
|
418
|
+ "signature" => $signature,
|
|
|
419
|
+ "rawString" => $string
|
|
|
420
|
+ ];
|
|
|
421
|
+ $this->assign('data',$data);
|
|
|
422
|
+ }
|
|
|
423
|
+
|
|
|
424
|
+ //获取微信分享签名随机字符串
|
|
|
425
|
+ public function createNonceStr($length = 16) {
|
|
|
426
|
+ $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
|
|
427
|
+ $str = "";
|
|
|
428
|
+ for ($i = 0; $i < $length; $i++) {
|
|
|
429
|
+ $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
|
|
|
430
|
+ }
|
|
|
431
|
+ return $str;
|
|
|
432
|
+ }
|
|
|
433
|
+
|
|
|
434
|
+ //获取access_token
|
|
|
435
|
+ public function getWxAccessToken(){
|
|
|
436
|
+ $url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$this->appkey2.'&secret='.$this->appsecret2;
|
|
|
437
|
+ $res = $this->http_get($url);
|
|
|
438
|
+ $json_arr = json_decode($res,true);
|
|
|
439
|
+ $token = $json_arr['access_token'];
|
|
|
440
|
+ return $token;
|
|
|
441
|
+ }
|
|
|
442
|
+
|
|
|
443
|
+ //获取微信分享签名
|
|
|
444
|
+ public function getSignature(){
|
|
|
445
|
+ if(isset($_SESSION['ticket_expire_time']) && $_SESSION['ticket_expire_time'] > time() && $_SESSION['ticket']){
|
|
|
446
|
+ $ticket = $_SESSION['ticket'];
|
|
|
447
|
+ }else{
|
|
|
448
|
+ $token = $this->getWxAccessToken();
|
|
|
449
|
+ $url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token='.$token.'&type=jsapi';
|
|
|
450
|
+ $res = $this->http_get($url);
|
|
|
451
|
+ $json_arr = json_decode($res,true);
|
|
|
452
|
+ $ticket = $json_arr['ticket'];
|
|
|
453
|
+ $_SESSION['ticket'] = $ticket;
|
|
|
454
|
+ $_SESSION['ticket_expire_time'] = time()+7000;
|
|
|
455
|
+ }
|
|
|
456
|
+ return $ticket;
|
|
|
457
|
+ }
|
|
|
458
|
+
|
|
|
459
|
+ //curl get请求
|
|
|
460
|
+ public function http_get($url){
|
|
|
461
|
+ $curl = curl_init();//启动一个CURL会话
|
|
|
462
|
+ curl_setopt($curl, CURLOPT_URL,$url);
|
|
|
463
|
+ curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 对认证证书来源的检查
|
|
|
464
|
+ curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
|
|
|
465
|
+ curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
|
|
|
466
|
+ curl_setopt($curl, CURLOPT_HEADER, false);//不开启header
|
|
|
467
|
+ curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // 获取的信息以文件流的形式返回
|
|
|
468
|
+ $result = curl_exec($curl); //执行操作
|
|
|
469
|
+ curl_close($curl);
|
|
|
470
|
+ return $result;
|
|
|
471
|
+ }
|
|
|
472
|
+
|
398
|
} |
473
|
} |