微信登录接入
微信登录接入
沉淀岁月 发表于1年前
微信登录接入
  • 发表于 1年前
  • 阅读 8
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 十分钟定制你的第一个小程序>>>   

<div id="login_container" style=" display:none; position:absolute; text-align:center; z-index:9999; width:320px; height:400px; background:#fff; "></div>

<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js"></script>

$('[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'));

}

共有 人打赏支持
粉丝 25
博文 228
码字总数 82285
评论 (0)
×
沉淀岁月
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: