1、创建微信开放平台账号
进入https://open.weixin.qq.com/注册微信开放平台账号
2、创建应用
导航栏选择“管理中心”,进入下图界面:
选择“网站应用”,按提示创建应用
审核通过后,获取“AppID、AppSecret”,如下图
“AppSecret”要保密好,不要让人知道,若泄漏,请“重置”一下
3、对接程序
官方文档:https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html
方案一是通过按钮进行授权登录,方案二是将微信登录二维码内嵌到自己页面请根据自己的实际情况进行选择,下面来介绍方案二:
3.1 前端操作:
步骤1:在页面中先引入如下JS文件(支持https):
引入js文件“http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js”,代码如下:
<script src="http://res.wx.qq.com/connect/zh_CN/htmledition/js/wxLogin.js" type="text/javascript"></script>
演示如下:
步骤2:在需要使用微信登录的地方实例以下JS对象:
代码如下:
<script type="text/javascript">
var obj = new WxLogin({
self_redirect:false,
id:"login_container",
appid: "",
scope: "snsapi_login",
redirect_uri: "<?php echo urlencode($redirect_uri); ?>",
state: "STATE",
style: "black",
href: ""
});
</script>
演示如下:
最终前端看到的效果如下图:
用微信扫上面的二维码,在服务器端需要接收code值,看服务器的操作。
3.2 服务器代码:
//微信登录-微信版本
public function get_weixin_login_reg()
{
$prfUrl = $this->session->userdata('gloabPreUrl') ? $this->session->userdata('gloabPreUrl') : "/index.php/user.html";
$code = $this->input->get("code", true);//获取code
if (!$code) {
printAjaxError('fail', 'DO NOT ACCESS!');
}
$json = file_get_contents("https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->_wx_app_id}&secret={$this->_wx_app_secret}&code={$code}&grant_type=authorization_code");
$obj = json_decode($json);
if (isset($obj->errmsg)) {
printAjaxError('fail', 'invalid code!');
}
if (!array_key_exists("unionid", $obj)) {
printAjaxError('fail', '开放平台与公众号平台没有配置好');
}
$unionid = $obj->unionid;
$access_token = $obj->access_token;
$openid = $obj->openid;
$userInfo = $this->User_model->getInfo('*', ['wx_unionid' => $unionid]);
//已绑定用户直接登录
if ($userInfo) {
if ($userInfo['display'] == 0) {
printAjaxError('fail', "你的账户被未激活,若有疑问,请联系网站在线客服!");
} else if ($userInfo['display'] == 2) {
printAjaxError('fail', "你的账户被冻结,若有疑问,请联系网站在线客服!");
}
$fields = [
'login_time' => time(),
'ip_address' => $this->_getIp(),
];
$scoreInfo = $this->advdbclass->getScore();
if ($scoreInfo['open_score']) {
$fields['score'] = $userInfo['score'] + $scoreInfo['login_score'];
}
if ($this->User_model->save($fields, ['id' => $userInfo['id']])) {
if ($scoreInfo['open_score']) {
$this->load->model('Score_model', '', true);
$sFields = [
'cause' => '登录成功',
'price' => $scoreInfo['login_score'],
'add_time' => time(),
'user_id' => $userInfo['id'],
'username' => $username,
];
$this->Score_model->save($sFields);
}
$this->_setCookie($userInfo, 86400 * 30);
header("Location: {$prfUrl}");
} else {
printAjaxError('fail', '登录失败!');
}
} else {
$result = file_get_contents("https://api.weixin.qq.com/sns/auth?access_token={$access_token}&openid={$openid}");
$access_token_obj = json_decode($result);
if ($access_token_obj->errcode != 0) {
printAjaxError('fail', $access_token_obj->errmsg);
}
$res_user_info = file_get_contents("https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$openid}");
// $myfile = fopen("./uploads/axzpp1.txt", "w");
// fwrite($myfile, $res_user_info);
// fclose($myfile);
$tmp_user_info = json_decode($res_user_info, true);
if (!array_key_exists("unionid", $tmp_user_info)) {
printAjaxError('fail', '开放平台与公众号平台没有配置好');
}
//登录并注册
$addTime = time();
$fields = [
'user_group_id' => 1,
'username' => '',
'display' => 1,
'login_time' => $addTime,
'ip_address' => $this->_getIp(),
'password' => '',
'email' => '',
'add_time' => $addTime,
'wx_unionid' => $unionid,
'sex' => $tmp_user_info['sex'],
'nickname' => $tmp_user_info['nickname'],
'country' => $tmp_user_info['country'],
'province' => $tmp_user_info['province'],
'city' => $tmp_user_info['city'],
];
$scoreInfo = $this->advdbclass->getScore();
if ($scoreInfo['open_score']) {
$fields['score'] = $scoreInfo['login_score'];
}
$ret = $this->User_model->save($fields);
if ($ret) {
if ($scoreInfo['open_score']) {
$this->load->model('Score_model', '', true);
$sFields = [
'cause' => '注册成功',
'price' => $scoreInfo['login_score'],
'add_time' => time(),
'user_id' => $ret,
'username' => $username,
];
$this->Score_model->save($sFields);
}
$ret = $this->User_model->get(['user.id' => $ret]);
$this->_setCookie($ret, 86400 * 30);
header("Location: /index.php/user.html");
} else {
printAjaxError('fail', '注册失败!');
}
}
}
注:具体根据自己的实际情况操作,如上只是示例