首先我们建两张表,在开发当中我们会遇到两个值access_token和jsapi_ticket,微信要求我们要获取,因为微信的access_token和jsapi_ticket有时间限制,有效期为7200 秒机会过期,为了更好的操作,我们需要建两张表。
w_access_token

w_jsapi_ticket

下来就要开始获取signature值,也是最重要的一步
我们只要把一下函数放在公共函数中就可以了
//获取token
function get_token() {
$info=C('Wx');
$token=$info['token'];
session ( 'token', $token );
return $token;
}
// 获取access_token,自动带缓存功能
function get_access_token($token = '') {
empty ($token) && $token = get_token();
$model = M("access_token");
$map['token'] = $token;
$info = $model->where($map)->find();
if(!$info)
{
$newaccess_token = getNowAccesstoken($token);
}
else
{
$nowtime = time();//现在时间
$time = $nowtime - $info['lasttime'];
$newaccess_token = $info['access_token'];
if($time >= 1800){
$newaccess_token = getNowAccesstoken($token);
if($newaccess_token == 0){//重新再 调用一次
$newaccess_token = getNowAccesstoken($token);
}
}
}
return $newaccess_token;
}
function getNowAccesstoken($token = ''){
$nowtime = time();//现在时间
empty ( $token ) && $token = get_token ();
$info = get_token_appinfo ($token);
if (empty ($info ['appid'] ) || empty ($info['secret'])) {
return 0;
}
$url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $info ['appid'] . '&secret=' . $info ['secret'];
$ch1 = curl_init ();
$timeout = 5;
curl_setopt ( $ch1, CURLOPT_URL, $url );
curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch1, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt ( $ch1, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch1, CURLOPT_SSL_VERIFYHOST, false );
$accesstxt = curl_exec ( $ch1 );
curl_close ( $ch1 );
$tempArr = json_decode ($accesstxt, true);
if (!$tempArr->errmsg) {
$model = M("access_token");
$map['token'] = $token;
//保存新access_token到数据库,更新最后时间
$data = array(
'access_token'=>$tempArr ['access_token'],
'lasttime'=>$nowtime
);
$info=$model->where($map)->find();
if($info)
{
$model->where($map)->save($data);
}
else
{
$data['token'] = $token;
$model->where($map)->add($data);
}
return $tempArr ['access_token'];
}else{
return 0;
}
}
// 获取jsapi_ticket,判断是不过期
function getJsapiTicket($token = '') {
empty ($token) && $token = get_token();
$model = M("jsapi_ticket");
$map['token'] = $token;
$info = $model->where($map)->find();
if(!$info)
{
$new_jsapi_ticket = getNowJsapiTicket($token);
}
else
{
$nowtime = time();//现在时间
$time = $nowtime - $info['lasttime'];
$new_jsapi_ticket = $info['ticket'];
if($time>=1800){
$new_jsapi_ticket = getNowJsapiTicket($token);
if($new_jsapi_ticket == 0){//重新再 调用一次
$new_jsapi_ticket = getNowJsapiTicket($token);
}
}
}
return $new_jsapi_ticket;
}
//获取jsapi_ticket
function getNowJsapiTicket($token='')
{
empty ($token) && $token = get_token();
$access_token=get_access_token();
$url='https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' .$access_token. '&type=jsapi';
$ch1 = curl_init ();
$timeout = 5;
curl_setopt ( $ch1, CURLOPT_URL, $url );
curl_setopt ( $ch1, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch1, CURLOPT_CONNECTTIMEOUT, $timeout );
curl_setopt ( $ch1, CURLOPT_SSL_VERIFYPEER, FALSE );
curl_setopt ( $ch1, CURLOPT_SSL_VERIFYHOST, false );
$accesstxt = curl_exec ( $ch1 );
curl_close ( $ch1 );
$tempArr = json_decode ($accesstxt, true);
$ext=$tempArr['errmsg'];
if ($ext=='ok') {
$model = M("jsapi_ticket");
$map['token'] = $token;
$nowtime=time();
//保存新jsapi_ticket到数据库,更新最后时间
$data = array(
'ticket'=>$tempArr ['ticket'],
'lasttime'=>$nowtime
);
$info=$model->where($map)->find();
if($info)
{
$model->where($map)->save($data);
}
else
{
$data['token'] = $token;
$model->where($map)->add($data);
}
return $tempArr['ticket'];
}
else
{
return 0;
}
}
// 获取公众号的信息
function get_token_appinfo() {
$info=C('Wx');
return $info;
}
//获取signature的值 获取签名值数组
function get_signature()
{
$url='http://'.$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$ticket=getJsapiTicket();
$noncestr=createNonceStr();
$timestamp=time();
$string='jsapi_ticket='.$ticket.'&noncestr='.$noncestr.'×tamp='.$timestamp.'&url='.$url;
$signature = sha1($string);
$signPackage = array(
"appId" =>C('Wx.appid'),
"nonceStr" =>$noncestr,
"timestamp" => $timestamp,
"url" => $url,
"signature" => $signature,
"string" => $string
);
return $signPackage;
}
//随机生成字符串
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;
}下来我们只需要在config加上配置信息//微信参数
'Wx' =>array(
'token'=>'gh_52627xxxxxxxx',//微信公众号原始ID
'appid'=>'wxcd1f5xxxxxxxxxx',//微信AppID(应用ID)
'secret'=>'87d68bd6c533ffa088xxxxxxxxxxx',//AppSecret(应用密钥)
),接下来我们只需要调用就可以,把下面的代码放在需要的地方,最好是放在继承类,也就是所谓的公共类中_initialize的方法中$signPackage = get_signature();//自定义分享方法,获取签名值数组
$this->assign('signPackage',$signPackage);以上只要顺利完成,那我们离成功就很近了了,下来我们就在HTML页面调用,就可以。<script src="http://res.wx.qq.com/open/js/jweixin-1.1.0.js"></script>//这个是也是最重要的一部分,一定要加上。
<script>
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '{$signPackage.appId}', // 必填,公众号的唯一标识
timestamp:'{$signPackage.timestamp}', // 必填,生成签名的时间戳
nonceStr: '{$signPackage.nonceStr}', // 必填,生成签名的随机串
signature: '{$signPackage.signature}',// 必填,签名,见附录1
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo'
] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
});
wx.ready(function () {
wx.checkJsApi({
jsApiList: [
'getNetworkType',
'previewImage',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo'
]// 需要检测的JS接口列表,所有JS接口列表见附录2,
});
var wxData = {
"title": "(还剩{$rows['money']-$rows['ymoney']<0?0:$rows['money']-$rows['ymoney']}元红包){$rows.title}", // 分享标题
"desc": "{$rows['content']|htmlspecialchars_decode}",
"link": "{:U('jishu/show')}?id={$rows.id}", // 分享链接
"imgUrl": '{$rows.vid|vpic}'// 分享图标,
};
var weixin = function (title,link,imgurl,desc){
wx.ready(function () {
wx.onMenuShareTimeline({
title: title,
link: link,
imgUrl: imgurl
});
wx.onMenuShareAppMessage({
title: title,
desc: desc,
link: link,
imgUrl: imgurl
});
wx.onMenuShareQQ({
title: title,
desc: desc,
link: link,
imgUrl: imgurl
});
wx.onMenuShareWeibo({
title: title,
desc: desc,
link: link,
imgUrl: imgurl
});
obj.sound();
});
};
weixin(wxData.title,wxData.link,wxData.imgUrl,wxData.desc);
});
</script>好了,现在大功告成了,效果图如下:
另外附上QQ群:515187709
微信自定义分享功能demo.rar
( 1.03 MB 下载:256 次 )
