WeChat scan code login registration implementation, WeChat scan login Registration

Source: Internet
Author: User

Scan the QR code to register and scan the logon register.

Scan-code logon is the most popular and convenient logon method. Here I use the public number to demonstrate the QR code logon process.

Required information:

1. AppId and AppSecret of the Public Account

2. Get access_toke with the Public Account

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. Get the temporary QR code

Public function getcode ($ access_token, $ str ){

// Query whether the string is unique
$ Model = D ("merchant ");
$ Res = $ model-> where ("str = '". $ str. "'")-> find ();

$ Str = $ this-> parameter ();

If ($ res ){
$ This-> getcode ($ access_token );
} Else {
// Generate a temporary QR code with Parameters
$ 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;
}
}

// QR code Parameters
Public function parameter (){
$ Str = $ _ SESSION ["str"];
If (strlen ($ str) <5 ){
// Generate a random string
$ Str = md5 (rand (0,999999). "qiao.16166.org ");
$ _ SESSION ["str"] = $ str;
}

Return $ str;
}

Front-end code:

<Body style = "margin: 0px; padding: 0px">
<Div style = "width: 100%; height: 100vl; 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: 13vl; padding-top: 25px; padding-bottom: 25px; border-radius: 6px ">
<P style = "margin: 0px; text-align: center"> <strong> scan the QR code for Logon </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"> use a scan to log on or register </p>
</Div>
<Input type = "hidden" id = "str" value = "{$ str}"/>
</Div>

<Script type = "text/javascript">
$ (Function (){
Checklogin ();
});

Function checklogin (){
Var url = "{: U ('sjmanager/Login/checklogin ')}";
Varstr = $ ("# str"). val ();
VarpostData = "str =" + str;
$. Post (url, postData, function (jsons ){
Console. log (jsons );
If (jsons. state = "login "){
$ ("# Strstate" Login .html ("Login successful, please wait ...");
SetTimeout (function (){
Window. location = "{: U ('Merchant/index/Index ')}";
},3000 );
}

If (jsons. state = "register "){
$ ("# Strstate" ).html ("Scan successful, please wait ...");
SetTimeout (function (){
Window. location = "{: U ('sjmanager/Login/register ')}";
},3000 );
}

If (jsons. state = "no "){
$ ("# Strstate" ).html ("Scan successful ");
SetTimeout (function (){
Alert ("your information has not been approved. Please contact the Administrator ");
},3000 );
}

Else {
SetTimeout (function (){
Checklogin ();
},10000 );
}
}, 'Json ');
}
</Script>
</Body>

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.