ecshop微信登录php代码,ecshop第三方登陆之微信登录

if(isset($_REQUEST['code'])&&isset($_REQUEST['state'])&&$action == 'weixin'){

include_once(ROOT_PATH . 'includes/website/jntoo.php');

$code = $_GET['code'];

$state = $_GET['state'];

//xxxx修改成自己的appID和AppSecret$appid = 'XXXXX';

$appsecret = 'XXXXXXX';

if (empty($code))

show_message('授权失败', '返回首页', '', 'wrong');

$token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$appsecret.'&code='.$code.'&grant_type=authorization_code';

$token = json_decode(file_get_contents($token_url));

if (isset($token->errcode))

{

show_message($token->errmsg, '返回首页', '', 'wrong');

}

$access_token_url = 'https://api.weixin.qq.com/sns/oauth2/refresh_token?appid='.$appid.'&grant_type=refresh_token&refresh_token='.$token->refresh_token;

$access_token = json_decode(file_get_contents($access_token_url));

if (isset($access_token->errcode))

{

show_message($access_token->errmsg, '返回首页', '', 'wrong');

}

$user_info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token->access_token.'&openid='.$access_token->openid.'&lang=zh_CN';

$user_info = json_decode(file_get_contents($user_info_url));

if (isset($user_info->errcode)) {

show_message($user_info->errmsg, '返回首页', '', 'wrong');

}

setcookie('user_info',$user_info);

$info = $user_info;

$type='weixin';

$info_user_id = $type .'_'.$info->openid; //  加个标识!!!防止 其他的标识 一样  // 以后的ID 标识 将以这种形式 辨认

$info->nickname= str_replace("'" , "" ,$info->nickname);

$sql = 'SELECT user_name,password,aite_id FROM '.$ecs->table('users').' WHERE aite_id = \''.$info_user_id.'\' OR aite_id=\''.$info->openid.'\'';

$count = $db->getRow($sql);

$login_name = $info->nickname;

if(!$count)   // 没有当前数据

{

if($user->check_user($info->nickname))  // 重名处理

{

$info->nickname = $info->nickname.'_'.$type.(rand()*1000);

}

$login_name = $info->nickname;

$user_pass = $user->compile_password(array('password'=>$info->openid));

$sql = 'INSERT INTO '.$ecs->table('users').'(user_name , password, aite_id , sex , reg_time , user_rank , is_validated) VALUES '.

"('$info->nickname' , '$user_pass' , '$info_user_id' , '$info->sex' , '".gmtime()."' , '0' , '1')" ;

$db->query($sql);

}

else

{

$login_name = $count['user_name'];

$sql = '';

if($count['aite_id'] == $info->openid)

{

$sql = 'UPDATE '.$ecs->table('users')." SET aite_id = '$info_user_id' WHERE aite_id = '$count[aite_id]'";

$db->query($sql);

}

}

$user->set_session($login_name);

$user->set_cookie($login_name);

update_user_info();

$redirect_url =  "http://".$_SERVER["HTTP_HOST"].str_replace("user.php", "index.php", $_SERVER["REQUEST_URI"]);

header('Location: '.$redirect_url);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值