1. 云栖社区>
  2. PHP教程>
  3. 正文

微信登录接入

作者:用户 来源:互联网 时间:2017-12-01 09:10:01

登录接入

微信登录接入 - 摘要: 本文讲的是微信登录接入, " rel="nofollow">http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"> $('[btn="signWithWechat"]').clic


" rel="nofollow">http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js">


$('[btn="signWithWechat"]').click(function(){


$('#login_container').css({top:200,left:($(document).width()- 320) /2}).show();


$('#login_container').click(function(){$(this).hide()});


var obj = new WxLogin({


id:"login_container",


appid: "xxxxxx",


scope: "snsapi_login",


redirect_uri: "<?php echo 'http://xxxxxxx.com' . url('default::public/wechat');?>",


state: "",


style: "",


href: ""


});


});

public function actionWechat()


{


$code = $this->_context->code;

if(empty($code))


{


return '';


}

$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=aaaa&secret=ccccc&code={$code}&grant_type=authorization_code";


$result = Helper_Common::chttpRequest($url,array());

$data = json_decode($result);


$url = "https://api.weixin.qq.com/sns/auth?access_token=".$data->access_token."&openid=" . $data->openid;

$result = Helper_Common::chttpRequest($url,array());


$ldata = json_decode($result);


if($ldata->errcode > 0)


{


return $ldata->errmsg;


}

$open_id = $data->openid;


//查询是否存在此用户


$user = User::find('wechat_id=?',$open_id)->query();


if(!$user->id())


{


//获取个人信息


$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$data->access_token."&openid=" . $data->openid;


$result = Helper_Common::chttpRequest($url,array());


$udata = json_decode($result);


$register_ip = Helper_Utils::getClientIP();

$username = 'w_' . $udata->nickname;


//需要创建用户


$user_data = array


(


'username' => $username,


'real_name' => $udata->nickname,


'last_login' => CURRENT_TIMESTAMP,


'register_ip' => $register_ip,


'last_login_ip' => $register_ip,


'email' => CURRENT_TIMESTAMP . '@aaaaa.com',


'password' => mt_rand(100000,999999),


'wechat_id' => $open_id,


'is_other' => 1,


);

$user = new User($user_data);


$user->status = User::STATUS_NORMAL;


$role = Role::find('rolename=?','UNCHECKED')->query();


if(!$role->id())


{


throw new QException('未找到相关会员组。');


}


$user->group_id = 1;


$user->roles = $role['rid'];


$user->save();

}


$userinfo = $user->aclData();


//设置为登录状态


$this->_indexchangeCurrentUser($userinfo);

return $this->_redirect('http://'.$_SERVER['HTTP_HOST'].url('user/index'));


}



以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索登录 接入 ,以便于您获取更多的相关知识。