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