微信登陸接入

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

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

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

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

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

var obj = new WxLogin({app

  id:"login_container", ide

  appid: "xxxxxx", this

  scope: "snsapi_login", url

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

  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'));

}

相關文章
相關標籤/搜索