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

调式分享

... ... @@ -20,15 +20,6 @@ use app\portal\model\PortalPostModel;
class LoginController extends HomeBaseController
{
private $limit = 8;//收藏,搜索分页
private $appkey = 737607150;//微博appkey
private $appsecret = 'd80b43a1e74e8ba095590b36a3459480';//微博appsecret
private $redirect_uri = 'http://www.starplanet.cn/portal/login/wb_login';//回调地址
private $appkey1 = 'wx9cfa880272f186bf';//微信开放平台appkey
private $appsecret1 = '11b643393b1e54d6ef5eaa984ba4e545';//微信开放平台appsecret
private $appkey2 = 'wx0bd7bc2aa0f332d6';//微信公众号appkey
private $appsecret2 = 'b62e49f48f48de7b7fff2ea0af3939de';//微信公众号appsecret
//登录页面
public function login(){
... ... @@ -522,92 +513,11 @@ class LoginController extends HomeBaseController
return $info;
}
//获取微信分享配置信息
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;
}
//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;
//微信分享
public function wx_share(){
$article_id = $this->request->param('id');
$postModel = new PortalPostModel();
$postModel->where('id', $article_id)->setInc('post_share_wx', 1);
$this->apiResponse(1, '分享成功!');
}
}
... ...
... ... @@ -152,6 +152,7 @@
</div>
</div>
<!--微信分享-->
<include file="public@share" />
<include file="public@footer" />
</body>
<script src="__TMPL__/public/assets/js/base.js"></script>
... ...
... ... @@ -18,11 +18,11 @@
wx.ready(function () { //需在用户可能点击分享按钮前就先调用
wx.updateAppMessageShareData({
title: title_title, // 分享标题
desc: '', // 分享描述
desc: url, // 分享描述
link: url, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
imgUrl: '', // 分享图标
success: function (e) {
// 设置成功
wx_Share();
}
});
wx.updateTimelineShareData({
... ... @@ -31,6 +31,7 @@
imgUrl: '', // 分享图标
success: function () {
// 设置成功
wx_Share();
}
});
});
... ... @@ -39,4 +40,20 @@
});
});
function wx_Share(){
var article_id = getUrlParam('id');
$.ajax({
type: 'POST',
url: '/portal/login/wx_share',
data: {
'id': article_id,
},
dataType: 'json',
async: false,
success: function(data) {
}
});
}
</script>
\ No newline at end of file
... ...
... ... @@ -35,3 +35,4 @@
</ul>
</div>
<!--微信分享-->
<include file="public@share" />
... ...
... ... @@ -21,6 +21,15 @@ use think\Session;
class HomeBaseController extends BaseController
{
public $appkey = 737607150;//微博appkey
public $appsecret = 'd80b43a1e74e8ba095590b36a3459480';//微博appsecret
public $redirect_uri = 'http://www.starplanet.cn/portal/login/wb_login';//回调地址
public $appkey1 = 'wx9cfa880272f186bf';//微信开放平台appkey
public $appsecret1 = '11b643393b1e54d6ef5eaa984ba4e545';//微信开放平台appsecret
public $appkey2 = 'wx0bd7bc2aa0f332d6';//微信公众号appkey
public $appsecret2 = 'b62e49f48f48de7b7fff2ea0af3939de';//微信公众号appsecret
protected function initialize()
{
... ... @@ -39,6 +48,13 @@ class HomeBaseController extends BaseController
$this->getShareCount();
$keyword = $this->request->param('keyword');
$this->assign('keyword',$keyword);
$cmfDefaultTheme = cmf_get_current_theme();
//域名后面相对路径
$url = $_SERVER['REQUEST_URI'];
if($cmfDefaultTheme == 'simpleboot3_mobile' && strpos($url,'Detail')!==false){
$this->wxShare();
}
}
protected function _initializeView()
... ... @@ -396,4 +412,93 @@ 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;
}
//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
... ...