微信扫码登录注册实现

2018-06-22 05:34:39来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

扫码登录是当下最流行也是最简便的登录方式。这里我用微信公众号来演示扫码登录的流程。

需要信息:

1.公众号的AppId 和公众号密钥AppSecret

2.公众号获取access_toke

public function getToken(){

$access_token = S("gzh_access_token");
if(!$access_token){
$appid = C('AppId');
$secret = C('AppSecret);
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$token = json_decode(curl_request($url,"get"),1);

S("gzh_access_token",$token["access_token"],7100);
$access_token = S("gzh_access_token");
}

return $access_token;
}

3.获取临时二维码

public function getcode($access_token,$str){

//查询字符串是否唯一
$model = D("merchant");
$res = $model->where("str='".$str."'")->find();

$str = $this->parameter();

if($res){
$this->getcode($access_token);
}else{
//生成临时带参数二维码
$url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$access_token;
$data["expire_seconds"] = 7200;
$data["action_name"] = "QR_STR_SCENE";
$data["action_info"]["scene"]["scene_str"] = $str;

$ticket = json_decode(curl_request($url,"post",json_encode($data)),1);

$qrcoderul = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$ticket["ticket"];
$qrcode = curl_request($qrcoderul,"get");

return $qrcode;
}
}

//二维码参数
public function parameter(){
$str = $_SESSION["str"];
if(strlen($str)<5){
//生成随机字符串
$str = md5(rand(0,999999)."qiao.16166.org");
$_SESSION["str"] = $str;
}

return $str;
}

 前端代码:

<body style="margin: 0px;padding: 0px">
<div style="width: 100%;height: 100vh;background-image: url('https://public.16166.org/Skin/items/qbl/qbl.jpg');background-repeat: no-repeat;background-size: 100% 100%;overflow: hidden">
<div style="background-color: white;margin-left: auto;margin-right: auto;width: 300px;height:auto;;margin-top:13vh;padding-top: 25px;padding-bottom: 25px;border-radius: 6px">
<p style="margin: 0px;text-align: center"><strong>微信扫码登陆</strong></p>
<div style="width: 300px;text-align: center;margin-top: 10px;margin-bottom: 10px">
{$img}
</div>
<p id="strstate" style="margin: 0px;text-align: center;font-size: smaller">使用微信扫一扫登录或注册</p>
</div>
<input type="hidden" id="str" value="{$str}" />
</div>

<script type="text/javascript" >
$(function (){
checklogin();
});

function checklogin(){
var url = "{:U('Sjmanager/Login/checklogin')}";
var str = $("#str").val();
var postData = "str="+str;
$.post(url,postData,function(jsons){
console.log(jsons);
if(jsons.state== "login"){
$("#strstate").html("登录成功,请稍后...");
setTimeout(function(){
window.location = "{:U('Merchant/index/index')}";
},3000);
}

if(jsons.state=="register"){
$("#strstate").html("扫码成功,请稍后...");
setTimeout(function(){
window.location = "{:U('Sjmanager/Login/register')}";
},3000);
}

if(jsons.state=="no"){
$("#strstate").html("扫码成功");
setTimeout(function(){
alert("您的信息审核未通过,请联系管理员");
},3000);
}

else{
setTimeout(function(){
checklogin();
},10000);
}
},'json');
}
</script>
</body>

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:WordPress百度熊掌号页面改造(纯代码实现)

下一篇:最简单安装laravel