网站第三方微信登录

官方文档:
https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open5874245868505&token=&lang=zh_CN
微信公众号、微信浏览器申请微信登录授权参考
https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp18698457572
1:申请创建应用:获取appid和appsecret
2:通过以下接口请求code参数
方法一:通过php获取code参数
https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
方法二:通过js获取code参数(JS微信登录主要用途:网站希望用户在网站内就能完成登录,无需跳转到微信域下登录后再返回,提升微信登录的流畅性与成功率。网站内嵌二维码微信登录JS实现办法)
步骤1:在页面中先引入如下JS文件(支持https):
http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js
步骤2:在需要使用微信登录的地方实例以下JS对象:
var obj = new WxLogin({
self_redirect:true,
id:"login_container",
appid: "",
scope: "",
redirect_uri: "",
state: "",
style: "",
href: ""
});
PS:1:style字段:设置二维码背景样式
2:href字段:如果第三方觉得微信团队提供的默认样式与自己的页面样式不匹配,可以自己提供样式文件来覆盖默认样式。举个例子,如第三方觉得默认二维码过大,可以提供相关css样式文件,并把链接地址填入href字段
3:通过2获取到的code参数请求以下链接,请求access_token和openid
https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
4:通过access_token和openid获取微信账号信息
https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN


以下是可以直接使用的微信登录代码:
class ControllerAccountWxlogin{
//--微信登录-----
public function index() {
$state = md5(uniqid(rand(), TRUE)); //生成唯一随机串防CSRF攻击
$_SESSION["wx_state"] = $state; //存到SESSION
$callBackUrl = HTTP_SERVER.'index.php?route=account/wxlogin/wxBack'; //回调地址
$callback = urlencode($callBackUrl);
//微信浏览器打开
//https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE&state=STATE#wechat_redirect
//网页微信登录
//https://open.weixin.qq.com/connect/qrconnect?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_login&state=STATE#wechat_redirect
if($this->is_weixin()){ //微信浏览器打开
$wxurl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".OPEN_APPID."&redirect_uri=".$callback."&response_type=code&scope=snsapi_userinfo&state=".$state."#wechat_redirect";
}else{//网页微信登录
$wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid="
.WXOPEN_APPID."&redirect_uri="
.$callback."&response_type=code&scope=snsapi_login&state="
.$state."#wechat_redirect";
}
header("Location: ".$wxurl); //请求code
}
//通过code,请求access_token和openid
//https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
public function wxBack(){
if($_GET['state']!= $_SESSION["wx_state"]){
echo 'sorry,网络请求失败...';
exit();
}
if($this->is_weixin()){ //微信浏览器打开
$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.OPEN_APPID.'&secret='.OPEN_APPSECRET.'&code='.$_GET['code'].'&grant_type=authorization_code';
}else{//网页微信登录
$url='https://api.weixin.qq.com/sns/oauth2/access_token?appid='.WXOPEN_APPID.'&secret='.WXOPEN_APPSECRET.'&code='.$_GET['code'].'&grant_type=authorization_code';
}
$arr = file_get_contents($url);
$arr = json_decode($arr,true);
$this->getUserInfo($arr['access_token'],$arr['openid']);
}
//通过access_token和openid获取微信账号信息
//https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN
public function getUserInfo($access_token,$openid){
$url='https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$openid.'&lang=zh_CN';
$user_info = file_get_contents($url);
$this->dealWithWxLogin($user_info);
}
/**
【网站第三方微信登录】* 根据微信授权用户的信息 进行下一步的梳理
* @param $user_info
*/
public function dealWithWxLogin($user_info){
$user_info = json_decode($user_info,true);
//print_r($user_info); //TODO 数据处理
if (!empty($user_info['nickname']) && !empty($user_info['openid'])) {
//判断数据库是否已有该openid
$query=$this->db->query("select * from ".DB_PREFIX."customer where openid='".$this->db->escape($user_info['openid'])."'");
if($query->num_rows){
$_SESSION['customer_id'] = $query->row['customer_id'];
}else{
$this->db->query("INSERT ".DB_PREFIX."customer SET firstname='".$this->db->escape($user_info['nickname'])."',openid='".$this->db->escape($user_info['openid'])."',status=1,approved=1,date_added=NOW()");
$customer_id = $this->db->getLastId();
$_SESSION['customer_id']=$customer_id;
}
$this->redirect($this->url->link('account/edit', '', 'SSL'));
}
}
//判断当前浏览器是否微信
function is_weixin(){
if ( strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false ) {
return true;
}
return false;
}
}
?>

    推荐阅读