php ecshop 微信插件,ecshop二次开发第三方登陆微信登录

1.去weixin.gif 微信

其中appid=XXXXXX把XXXXXX替换成你的appid,state=XXXXXXXX把XXXXXX替换成你的AppSecret

redirect_uri=http%3A%2F%2Fwww.ecshop.cx%2Fuser.php%3Fact%3Dweixin替换成你的回调地址(注意其中转码的url,不能直接写url)

3.回调程序中写对应的处理函数

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

{

include_once(ROOT_PATH . 'includes/website/ecshopcx.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);

}

ecshop微信登陆返回参数详解:

参数

描述

openid

用户的唯一标识

nickname

用户昵称

sex

用户的性别,值为1时是男性,值为2时是女性,值为0时是未知

province

用户个人资料填写的省份

city

普通用户个人资料填写的城市

country

国家,如中国为CN

headimgurl

用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空

privilege

用户特权信息,json 数组,如微信沃卡用户为(chinaunicom)

unionid

只有在用户将公众号绑定到微信开放平台帐号后,才会出现该字段。详见:获取用户个人信息(UnionID机制)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值