前端
引入js
$(function(){
$('.wyh_pro').click(function() {
$(this).hide();
});
$.ajax({
url:'=site_url('api/api_wechat/jsapi_config_params');?>',
type:'post',
data:{url:'=$url;?>'},
dataType:'json',
async:false,
success: function(res) {
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: res.data.appid, // 必填,公众号的唯一标识
timestamp: res.data.timestamp, // 必填,生成签名的时间戳
nonceStr: res.data.noncestr, // 必填,生成签名的随机串
signature: res.data.signature,// 必填,签名
jsApiList: [
'checkJsApi',
'onMenuShareTimeline',
'onMenuShareAppMessage',
'onMenuShareQQ',
'onMenuShareWeibo'
]
});
}
});
wx.ready(function () {
wx.onMenuShareAppMessage({//分享朋友
title: '=$_SESSION['user']['tenant_name']?>',
desc: '=$_SESSION['user']['tenant_name']?>分享',
link: '=site_url('wechat/main/enter/').$tenant_id;?>',
// imgUrl: '=base_url();?>media/wechat/luckdraw/img/logo.png'
});
wx.onMenuShareTimeline({//分享朋友圈
title: '=$_SESSION['user']['tenant_name']?>', //标题
desc: '=$_SESSION['user']['tenant_name']?>分享', //内容
link: '=site_url('wechat/main/enter/').$tenant_id;?>', //页面
// imgUrl: '=base_url();?>media/wechat/luckdraw/img/logo.png'//logo图片
});
});
});
后台
/**
* wechat
* 获取jsapi config参数
*/
public function jsapi_config_params() {
$sign_data = [];
$sign_data['jsapi_ticket'] = $this->get_jsapi_ticket($_SESSION['user']['tenant_id']);//获取jsapi_ticket
$this->return['data']['noncestr'] = $sign_data['noncestr'] = md5(rand(100000,999999));//生成noncestr
$this->return['data']['timestamp'] = $sign_data['timestamp'] = time();//生成timestamp
$this->return['data']['url'] = $sign_data['url'] = $this->input->post('url');//生成url
$this->return['data']['signature'] = $this->get_jsapi_sign($sign_data);//生成signature
$this->return['data']['appid'] = $this->app_id;//这是appid
$this->result();
}
//验证签名
private function get_jsapi_sign($data) {
ksort($data);
$tmpSign = $this->arrToTempStr($data);
$sign = strtolower(sha1($tmpSign));
return $sign;
}
/**
* wechat
* 获取当前jsapi_ticket
*/
public function get_jsapi_ticket($tenant_id) {
//将jsapi_ticket存入数据库$tenant_id为数据库的唯一标识数据库表结构如下
$this->load->model('system_wechat_jsapi_tickets_model','system_wechat_jsapi_tickets');
//查询出jsapi_ticket是否存在
$res = $this->system_wechat_jsapi_tickets->find(['tenant_id' => $tenant_id]);
//不存在就创建一条
if (!$res || !is_array($res)) {
$jsapi_ticket = $this->get_new_jsapi_ticket($tenant_id);//获取一个新的jsapi_ticket
//在数据库增加一条新的数据
$this->system_wechat_jsapi_tickets->create(['jsapi_ticket' => $jsapi_ticket,'time' => date('Y-m-d H:i:s'),'tenant_id' => $tenant_id]);
return $jsapi_ticket;//返回jsapi_ticket
} else if (time() - strtotime($res['time']) > 7150) {//如果生成的jsapi_ticket时间超过7200秒就更改jsapi_ticket
$jsapi_ticket = $this->get_new_jsapi_ticket($tenant_id);
$this->system_wechat_jsapi_tickets->update(['jsapi_ticket' => $jsapi_ticket,'time' => date('Y-m-d H:i:s')],['id' => $res['id']]);
return $jsapi_ticket;//返回jsapi_ticket
} else {
return $res['jsapi_ticket'];//正常直接返回数据库存放的jsapi_ticket
}
}
/**
*重新获取jsapi_ticket
*/
private function get_new_jsapi_ticket($tenant_id) {
$result = file_get_contents('https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=' . $this->get_access_token($tenant_id) . '&type=jsapi');
//get_access_token带着唯一标识获取token
$result = json_decode($result,true);
return $result['ticket'];
}
/**
* 获取当前access_token同获取jsapi_ticket一样数据库表结构为
*/
public function get_access_token($tenant_id) {
$this->load->model('system_wechat_access_token_model','system_wechat_access_token');
$res = $this->system_wechat_access_token->find(['tenant_id' => $tenant_id]);
if (!$res || !is_array($res)) {
$access_token = $this->get_new_access_token();
// 插入access_token
$this->system_wechat_access_token->create(['access_token' => $access_token,'time' => date('Y-m-d H:i:s'),'tenant_id' => $tenant_id]);
return $access_token;
} else if (time() - strtotime($res['time']) > 7150) {
$access_token = $this->get_new_access_token();
// 更新access_token
$this->system_wechat_access_token->update(['access_token' => $access_token,'time' => date('Y-m-d H:i:s')],['id' => $res['id']]);
return $access_token;
} else {
return $res['access_token'];
}
}
/**
* 重新从微信获取access_token
*/
private function get_new_access_token() {
$result = file_get_contents('https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' . $this->app_id . '&secret=' . $this->app_secret);
$result = json_decode($result,true);
return $result['access_token'];
}
/**
* 数组转签名字符串
*/
private function arrToTempStr($arr) {
$str = '';
foreach ($arr as $key => $value) {
$str .= $key . '=' . $value . '&';
}
return substr($str,0,-1);
}