1.前端:
微信扫码登录
2.二维码页面
微信扫码登录
3.后端:
#微信登录
public function wxlogin()
{
$config = cmf_get_plugin_config('WxLogin');
$this->assign('config',$config);
return $this->fetch();
}
public function wxredirect(){
import('wxlogin.wxlogin');
$Wechat = new \Wxlogin();
$token = $Wechat->get_access_token($_GET['code']); //确认授权后会,根据返回的code获取token
session('token',$token); //保存授权信息
$user_info = $Wechat->get_user_info($token['access_token'],$token['openid']); //获取用户信息
$user = Db::name('user')->where(['wx_open_id'=>$user_info['openid']])->find();
if ($user) {
session('user',$user);
Db::name('user')->where('id',cmf_get_current_user_id())->update(['last_login_time'=>time(),'last_login_ip'=>get_client_ip(0, true)]);
Db::name('user_login_log')->insert(['user_id'=>cmf_get_current_user_id(),'ip'=>get_client_ip(),'create_time'=>time(),'type'=>2]);
$this->redirect('portal/index/index');
}else{
$data = [];
$data['wx_open_id'] = $user_info['openid']; // 微信openid
$data['login_type'] = 2; // 用户帐号类型 2为微信
$data['user_type'] = 2;
$data['create_time'] = time();
$data['user_nickname'] = $user_info['nickname']; // 用户昵称
$data['sex']= $user_info['sex']; // 性别
$data['avatar'] = $user_info['headimgurl']; // 用户头像
$result = Db::name('user')
->insertGetId($data);
if(is_numeric($result)){
$user = Db::name('user')->where('id',$result)->find();
session('user',$user);
Db::name('user')->where('id',cmf_get_current_user_id())->update(['last_login_time'=>time(),'last_login_ip'=>get_client_ip(0, true)]);
Db::name('user_login_log')->insert(['user_id'=>cmf_get_current_user_id(),'ip'=>get_client_ip(),'create_time'=>time(),'type'=>2]);
$this->redirect('portal/index/index');
}
}
}
4.配置参数:例如:array(
"appid"=> "123456",
"app_secret"=> "123456",
"scope"=> "snsapi_login",
"redirect_uri"=> "http://www.weixin/user/login/wxredirect"
)
5.第三方类 放在simplewind/extend/wxlogin/wxlogin.php
app_id = $config['appid'];
$this->app_secret = $config['app_secret'];
}
/**
* # +========================================================================
* # | - @name 获取微信授权链接
* # | - @author cq
* # | - @copyright zmtek 2018-11-07
* # +------------------------------------------------------------------------
* # | - 1.获取微信授权链接
* # +========================================================================
*/
public function get_authorize_url($redirect_uri = '', $state = ''){
$redirect_uri = urlencode($redirect_uri);
return "https://open.weixin.qq.com/connect/qrconnect?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect";
}
/**
* # +========================================================================
* # | - @name 获取授权token
* # | - @author cq
* # | - @copyright zmtek 2018-11-07
* # +------------------------------------------------------------------------
* # | - 1.通过get_authorize_url获取到的code
* # +========================================================================
*/
public function get_access_token($code = ''){
$token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code";
$token_data = $this->http($token_url);
if($token_data[0] == 200){
return json_decode($token_data[1], TRUE);
}
return FALSE;
}
/**
* # +========================================================================
* # | - @name 获取授权后的微信用户信息
* # | - @author cq
* # | - @copyright zmtek 2018-11-07
* # +------------------------------------------------------------------------
* # | - 1.获取授权后的微信用户信息
* # +========================================================================
*/
public function get_user_info($access_token = '', $open_id = ''){
if($access_token && $open_id){
$info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN";
$info_data = $this->http($info_url);
if($info_data[0] == 200){
return json_decode($info_data[1], TRUE);
}
}
return FALSE;
}
/**
* # +========================================================================
* # | - @name 验证授权
* # | - @author cq
* # | - @copyright zmtek 2018-11-07
* # +------------------------------------------------------------------------
* # | - 1.验证授权
* # +========================================================================
*/
public function check_access_token($access_token = '', $open_id = ''){
if($access_token && $open_id){
$info_url = "https://api.weixin.qq.com/sns/auth?access_token={$access_token}&openid={$open_id}&lang=zh_CN";
$info_data = $this->http($info_url);
if($info_data[0] == 200){
return json_decode($info_data[1], TRUE);
}
}
return FALSE;
}
/**
* # +========================================================================
* # | - @name 请求
* # | - @author cq
* # | - @copyright zmtek 2018-11-07
* # +------------------------------------------------------------------------
* # | - 1.请求
* # +========================================================================
*/
public function http($url, $method='POST', $postfields = null, $headers = array()){
$ci = curl_init();
curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ci, CURLOPT_TIMEOUT, 30);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);
switch ($method) {
case 'POST':
curl_setopt($ci, CURLOPT_POST, true);
if (!empty($postfields)) {
curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
$this->postdata = $postfields;
}
break;
}
curl_setopt($ci, CURLOPT_URL, $url);
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
$response = curl_exec($ci);
$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);
curl_close($ci);
return array($http_code, $response);
}
}
?>