tp5微信自定义分享标题,图片(备忘)

浏览:1574 发布日期:2018/04/11 分类:技术分享 关键字: thinkphp5 微信分享
1、将access_token.json、jsapi_ticket.json文件放在api的同级文件下

=======access_token.json 内容=======

{"access_token":"","expire_time":0}

=======access_token.json 结束=======


=======jsapi_ticket.json 内容=======

{"jsapi_ticket":"","expire_time":0}

=======jsapi_ticket.json 结束=======



2、在控制器中引入JSSDK.php文件(这里附上我的JSSDK.php文件代码)

//=========开始=============

<?php

namespace app\home\controller;


class JSSDK
{
private $appId;
private $appSecret;

public function __construct($appId, $appSecret)
{
$this->appId = $appId;
$this->appSecret = $appSecret;
}

public function getSignPackage()
{
$jsapiTicket = $this->getJsApiTicket();
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = $this->createNonceStr();

// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url";

$signature = sha1($string);

$signPackage = array(
"appId" => $this->appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}

private 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;
}

private function getJsApiTicket()
{
// jsapi_ticket 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents("jsapi_ticket.json"));
if ($data->expire_time < time()) {
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
$ticket = $res->ticket; //服务器,在本地会报错的,必须上传到服务器
// $ticket = $res['ticket']; //本地
if ($ticket) {
$data->expire_time = time() + 7000;
$data->jsapi_ticket = $ticket;
$fp = fopen("jsapi_ticket.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$ticket = $data->jsapi_ticket;
}
return $ticket;
}

private function getAccessToken()
{
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode(file_get_contents("access_token.json"));
if ($data->expire_time < time()) {
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecret}";
$res = json_decode($this->httpGet($url));
$access_token = $res->access_token; //服务器,在本地会报错的,必须上传到服务器
// $access_token = $res['access_token']; //本地
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$fp = fopen("access_token.json", "w");
fwrite($fp, json_encode($data));
fclose($fp);
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}

private function httpGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_URL, $url);

$res = curl_exec($curl);
curl_close($curl);

return $res;
}
}

//=========结束=============



3、在需要分享的页面加入代码

//==============3.1、引入js================
<script type="text/javascript" src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>

//==============3.2、写js代码================
<script>
wx.config({
debug: true //调试模式
appId: '{$signPackage.appId}',
timestamp: '{$signPackage.timestamp}',//生成签名的时间戳
nonceStr: '{$signPackage.nonceStr}',//生成签名的随机串
signature: '{$signPackage.signature}',
jsApiList: ['onMenuShareAppMessage','onMenuShareTimeline']// 所有要调用的 API 都要加到这个列表中
});
wx.ready(function() {
wx.onMenuShareAppMessage({ //分享给朋友
title: '123', // 分享标题,可在控制器端传递
desc: '123',//分享的描述,可在控制器端传递
link: window.location.href, // 分享链接,可在控制器端传递
imgUrl: '', // 分享图标 ,可在控制器端传递
success: function() {
// 用户确认分享后执行的回调函数
},
cancel: function() {
// 用户取消分享后执行的回调函数
}
});
wx.onMenuShareTimeline({ //分享到朋友圈
title: '123',
desc: '123',
link: window.location.href, // 分享链接
imgUrl: '', // 分享图标
success: function() {
// 用户确认分享后执行的回调函数
},
cancel: function() {
// 用户取消分享后执行的回调函数
}
});
});
</script>

//==============3.3、PHP代码================
$jssdk = new JSSDK(你的appid,密钥);
$signPackage = $jssdk->GetSignPackage();
$this->assign('signPackage',$signPackage);


(PS:不知道为什么附件传不上去 *-*)
最佳答案
评论( 相关
后面还有条评论,点击查看>>