...
|
...
|
@@ -10,6 +10,7 @@ |
|
|
// +----------------------------------------------------------------------
|
|
|
namespace cmf\controller;
|
|
|
|
|
|
use app\portal\controller\LoginController;
|
|
|
use think\Db;
|
|
|
use app\admin\model\ThemeModel;
|
|
|
use think\View;
|
...
|
...
|
@@ -20,6 +21,8 @@ use think\Session; |
|
|
|
|
|
class HomeBaseController extends BaseController
|
|
|
{
|
|
|
private $appkey2 = 'wx0bd7bc2aa0f332d6';//微信公众号appkey
|
|
|
private $appsecret2 = 'b62e49f48f48de7b7fff2ea0af3939de';//微信公众号appsecret
|
|
|
|
|
|
protected function initialize()
|
|
|
{
|
...
|
...
|
@@ -37,6 +40,7 @@ class HomeBaseController extends BaseController |
|
|
$this->is_like();
|
|
|
$this->getShareCount();
|
|
|
$keyword = $this->request->param('keyword');
|
|
|
$this->configShareWx();
|
|
|
$this->assign('keyword',$keyword);
|
|
|
}
|
|
|
|
...
|
...
|
@@ -395,4 +399,85 @@ 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.'×tamp='.$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 |
...
|
...
|
|