Enterprise wechat realizes code scanning login

Time:2021-6-1

1: Get the parameters needed for code scanning login: appid, secret, AgentID

Log in to wechat: https://work.weixin.qq.com/

Code scanning login document: https://work.weixin.qq.com/api/doc/90000/90135/90988

1: Get appid

Click my enterprise to see the enterprise ID information, which is appid

Enterprise wechat realizes code scanning login

2: Get secret and AgentID

(1) : click application management – click create application

Enterprise wechat realizes code scanning login

(2) : after the application is created, we can see the secret and AgentID in the application

Enterprise wechat realizes code scanning login

2: Upload enterprise wechat QR code

(1) : introduce enterprise wechat JS

<script src="http://rescdn.qqmail.com/node/ww/wwopenmng/js/sso/wwLogin-1.0.0.js"></script> 

(2):html

<div id="wx_login"></div>  

(3) : scan code JS

window.WwLogin({  
    "id" : "wx_login",  
    "appid" : "XXX",//appid  
    "agentid" : "XXX",//agentid  
    "redirect_ uri"  :" XXX ", // callback address. Note that the callback address needs URLEncode  
    "state"  : " "XXXX", // used to keep the request and callback status, and bring it back to the enterprise after authorization request. This parameter can be used to prevent CSRF attack, and is not required  
    "href"  : " "// user defined style link, enterprises can override the default style according to the actual needs, parameters are not required  
});

According to the above, the QR code of enterprise wechat can be displayed

Enterprise wechat realizes code scanning login

3: Scan code callback receiving

After the enterprise wechat scan code is displayed, the mobile phone uses the enterprise wechat to scan the code for confirmation. At this time, our callback address will receive the callback information of the enterprise wechat. The callback information contains code parameters. We can obtain some basic information of the code scanning user according to the code. Here, I use PHP to achieve:

$state = $_GET['state'];  
//Judge whether the state value of the displayed scan code is the same as the state value obtained by the callback  
if ($state == 'XXX') {  
//Access is obtained according to appid and secret_ token  
$appid = 'XXX';  
    $secret = 'XXX';  
    $url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid='.$appid.'&corpsecret='.$secret;  
    $token = $this->https_request($url);  
    $accessToken = $token['access_token'];  
    //According to code and access_ Token to get member information  
    $code = $_GET['code'];  
    $url = 'https://qyapi.weixin.qq.com/cgi-bin/user/getuserinfo?access_token='. $accessToken .'&code=' . $code;  
    $usersinfo = $this->https_request($url);  
    //Judge whether the acquisition of member information is successful  
    if ($usersinfo['errcode'] == 0) {  
    if (isset($usersinfo['UserId'])) {  
    //Enterprise users  
    //We can scan the code to login according to the user information, and the specific code is not described  
    } else {  
    //Non enterprise users  
    }  
    } else {  
    Echo 'validation error';  
    }  
} else {  
Echo 'validation error';  
}

The code above uses HTTPS_ The method is as follows:

/**  
 * curl  
 */  
public function https_request($url){  
    $curl=curl_init();  
    curl_setopt($curl, CURLOPT_URL, $url);  
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);  
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);  
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
    $data=json_decode(curl_exec($curl), true);  
    // $data=curl_exec($curl);  
    curl_close($curl);  
    return $data;  
}  

According to the above, we can achieve the scan code login function