前段时间,公司有个项目,想让用户在登录系统的时候就关注公众号,但是现在让用户强制关注是违规的,只能另寻他法,然后,就找到了“微信扫描带参数二维码事件”,下面分享下
配置微信服务器
首先在你系统中新建一个文件,写入以下内容,确保文件可以被浏览器访问,该文件所在页面不能有任何其他内容输出
,并记录你的文件访问链接,还有比较重要的一点,记录下你设置的token
等会在配置微信服务器时要用
// 微信服务器回调
public function wxcallbackAction()
{
// file_put_contents('checkSignature.log', date('Y-m-d H:i:s') . '===get===' . \json_encode($_GET) . chr(10) . chr(10),FILE_APPEND | LOCK_EX);
if($this->checkSignature()){
echo $_GET["echostr"];
}
exit;
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = 'yourtoken';//注意:这块是你自定义的token,等会在配置微信服务器时要用
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
然后登录微信公众平台后,依次找到基本配置
->微信服务器配置
->修改配置
->启用配置
,配置时,URL是你刚才新建的文件的浏览器访问路径,TOKEN是你刚才新建的文件中自定义的token,设置完成后提交,token验证失败的话不会保存
官方文档:https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1472017492_58YV5 1.4
生成微信带参数的二维码
前端发起(使用js轮询发起请求,生成二维码,扫描后登录等)
<script type="text/javascript">
var t2 ='';
$(function() {
var rand = parseInt(Math.random() * (999999999 - 10000000 + 1) + 10000000); //生成随机数做为二维码的参数 (用来判断扫码的用户)
//生成二维码
$.post("后台用于生成二维码的链接",{str:rand},function(result){
console.log(result);
var obj = JSON.parse(result);
var sign = obj.scene_str;
var src = "https://mp.weixin.qq.com/cgi-bin/showqrcode"+obj.src;
$('#wxewm').attr('src',src);
$('input[name=sign]').val(sign);
});
//轮询扫码后的登录
t2 = window.setInterval("login()",1000);//使用字符串执行方法
//window.clearTimeout(t1);//去掉定时器
setTimeout("end()",30000);
})
//如果超过一定时间不扫码,停止轮询,提示过期
function end(){
$('#wxewm').hide(); //隐藏二维码
window.clearTimeout(t2); //停止轮询
$('#notice').show(); //提示二维码过期
}
//不停发送请求,获取扫码人的信息,如果扫码了获取信息停止轮询
function login(){
var code =$('input[name=sign]').val();
$.post("你将要进行数据处理的页面",{str:code},function(result){
console.log(result);
var obj = JSON.parse(result);
var user_name = obj.user_name;
var pass = obj.pass;
if(pass){
window.clearTimeout(t2);
//跳转到登录后的页面
window.location.href="https://www.baidu.com";
}
});
}
</script>
PHP后端进行数据处理
public function get_access_token() //获取token
{
$appid= '你的公众号APPID';
$secret='你的公众号APPSECRET';
$data = json_decode(file_get_contents("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$appid&secret=$secret")) ;
return $data->access_token;
}
public function build_qrcode() //生成带参数二维码
{
$str = $_POST['str'];
$access_token=$this->get_access_token();
// scene_id 参数
$data = '{
"expire_seconds": 604800,
"action_name": "QR_SCENE",
"action_info": {
"scene": {
"scene_id":'.$str.'
}
}
}';
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$obj = json_decode(httpRequest($url, 'POST', $data));
$ticket=$obj->ticket;
return json_encode(['src'=>"?ticket=$ticket",'scene_str'=>$str]);
}
// 登录数据处理
public function login()
{
//$GLOBALS["HTTP_RAW_POST_DATA"]; //这个貌似在PHP7以后用不了了;
$postStr = file_get_contents("php://input"); //换成这种方式收数据
$postObj = json_decode($this->xmlToArrayOrObject($postStr, true),true);
// 取出openID
$openid = $postObj['FromUserName'];
$EventKey = $postObj['EventKey'];
$user=db('fe_users')->field('id')->where(['openid'=>$openid])->find();
if(!$user['id']){
$access_token=$this->get_access_token();
$wxinfo =json_decode (file_get_contents('https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid), true);
}
// 添加session成功后返回值,在前端进行跳转
return json_encode(['code'=>1,'msg'=>'登录成功','url'=>'http://www.yoururl.com']);
}
/**
* 作用:将xml转为object/array
*/
public function xmlToArrayOrObject($xml,$flag=true)
{
if (!$flag) {
//将XML转为object
$xml_data = simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA);
}else{
//将XML转为array
$xml_data = json_decode(json_encode(simplexml_load_string($xml,'SimpleXMLElement', LIBXML_NOCDATA)), true);
}
return $xml_data;
}
评论 (0)