php相关代码
class weixin
{
public function getSignPackage() {
$this->appId = C('wx.AppID');
$this->appSecret = C('wx.AppSecret');
$jsapiTicket = $this->getJsApiTicket();
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
// TODO: 受服务器配置影响: Nginx 转发 Apache 导致无法识别 https 的影响
// 暂时强行修改为 https
$protocol = 'http://';
$url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$timestamp = time();
$nonceStr = random(16);
// 这里参数的顺序要按照 key 值 ASCII 码升序排序
$string = sprintf(
"jsapi_ticket=%s&noncestr=%s×tamp=%s&url=%s",
$jsapiTicket,
$nonceStr,
$timestamp,
$url
);
$signature = sha1($string);
$signPackage = array(
"appId" => $this->appId,
"nonceStr" => $nonceStr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"rawString" => $string
);
return $signPackage;
}
private function getJsApiTicket() {
$jsapiTicket = S('wechat_jsapi_ticket');
if ($jsapiTicket === false) {
$accessToken = $this->getAccessToken();
$url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?type=jsapi&access_token=$accessToken";
$res = json_decode($this->httpGet($url));
if ($res->ticket) {
S('wechat_jsapi_ticket', $res->ticket, 7000); // 有效期为7200秒
return $res->ticket;
}
}
return $jsapiTicket;
}
private function getAccessToken() {
$accessToken = S('wechat_access_token');
if ($accessToken === false) {
$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));
if ($res->access_token) {
S('wechat_access_token', $res->access_token, 7000); // 有效期为7200秒
return $res->access_token;
}
}
return $accessToken;
}
private function httpGet($url) {
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
// 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
// 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
}
js相关代码
先引入:http://res.wx.qq.com/open/js/jweixin-1.2.0.js
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '<{$wxinfo.appId}>', // 必填,公众号的唯一标识
timestamp: '<{$wxinfo.timestamp}>', // 必填,生成签名的时间戳
nonceStr: '<{$wxinfo.nonceStr}>', // 必填,生成签名的随机串
signature: '<{$wxinfo.signature}>',// 必填,签名,见附录1
jsApiList: ['onMenuShareTimeline','onMenuShareAppMessage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
wx.checkJsApi({
jsApiList: [
'onMenuShareTimeline',
'onMenuShareAppMessage'
]
});
wx.onMenuShareTimeline({
title: wx_share_title, // 分享标题
link: wx_share_link, // 分享链接
imgUrl: wx_share_imgUrl, // 分享图标
success: function () {
},
cancel: function () {
}
});
wx.onMenuShareAppMessage({
title: wx_share_title, // 分享标题
desc: wx_share_intro, // 分享描述
link: wx_share_link, // 分享链接
imgUrl: wx_share_imgUrl, // 分享图标
type: '', // 分享类型,music、video或link,不填默认为link
dataUrl: '', // 如果type是music或video,则要提供数据链接,默认为空
success: function () {
// 用户确认分享后执行的回调函数
},
cancel: function () {
// 用户取消分享后执行的回调函数
}
});
});
wx.error(function (res) {
});