In the article "Achieving WeChat Third-Party Authorized Scan Code Login", Tiandi briefly described how to implement WeChat third-party authorized login to the Ecshop system and published the core code, which mainly modified the user's login determination, that is, the file user.php
- /***************************/
- /* Wechat login/
- /* by tiandi 2014.12.6 /
- /***************************/
- if (defined('WEBSITE' ) || defined('GETINFO'))
- {
- global $_LANG;
- $_LANG['help']['APP_KEY'] = 'AppID applied on WeChat Developer Platform';
- $_LANG['help'] ['APP_SECRET'] = 'AppSecret applied on WeChat Developer Platform';
-
- $_LANG['APP_KEY'] = 'AppID';
- $_LANG['APP_SECRET'] = 'AppSecret';
-
- $i = isset ($web) ? count($web) : 0;
- // Class name
- $web[$i]['name'] = 'wechat';
- // File name, excluding suffix
- $web[$i ]['type'] = 'wechat';
-
- $web[$i]['className'] = 'wechat';
-
- // Author information
- $web[$i]['author'] = 'tiandi ';
-
- // Author QQ
- $web[$i]['qq'] = '';
-
- // Author's email
- $web[$i]['email'] = '';
-
- // Application URL
- $web[$i]['website'] = 'http://open.weixin.qq.com';
-
- // Version number
- $web[$i]['version'] = '1.0 ';
-
- // Update date
- $web[$i]['date'] = '2014-12-6';
-
- // Configuration information
- $web[$i]['config'] = array(
- array('type'=>'text' , 'name'=>'APP_KEY', 'value'=>''),
- array('type'=>'text' , 'name' = > 'APP_SECRET' , 'value' => ''),
- );
- }
-
-
- if (!defined('WEBSITE'))
- {
- include_once(dirname(__FILE__).'/oath2.class. php');
- class website extends oath2
- {
- function website()
- {
- $this->app_key = APP_KEY;
- $this->app_secret = APP_SECRET;
-
- $this->scope = 'snsapi_login' ;
- //by tiandi authorizeURL is used when opening WeChat login with PHP, and authorizeURL is not used when calling JS.
- $this->authorizeURL = 'https://open.weixin.qq.com/connect/qrconnect';
-
- $this->tokenURL = 'https://api.weixin.qq.com/sns/ oauth2/access_token';
- $this->refreshtokenURL = 'https://api.weixin.qq.com/sns/oauth2/refresh_token';
- $this->userURL = 'https://api.weixin. qq.com/sns/userinfo';
- $this->meth = 'GET';
- }
-
- function Code2Token($code)
- {
- $params = 'appid='.$this->app_key.' &secret='.$this->app_secret.'&code='.$code.
- '&grant_type=authorization_code';
- $tokenurl = $this->tokenURL."?". $params;
- $token = $this ->http($tokenurl, 'GET');
- $token = json_decode($token , true);
- return $token;
- }
-
- function GetRefreshToken($token)
- {
- $params = 'appid=' .$this->app_key.'&grant_type=refresh_token&refresh_token='.$token;
- $tokenurl = $this->refreshtokenURL."?". $params;
- $token = $this->http($tokenurl, 'GET');
- $token = json_decode($token , true);
- return $token;
- }
-
- function Getinfo($token,$openid)
- {
- $params = 'access_token='.$token.' &openid='.$openid;
- $userurl = $this->userURL."?". $params;
- $userinfo = $this->http($userurl, 'GET');
- return json_decode($userinfo , true);
- }
- }
- }
Copy code
|