CLTPHP添加微信登录功能
为了大家免踩坑,不是所有订阅号都是支持登录的,服务号都有这个功能,配置公众号就不说了,自行百度
前端ajax访问 域名/home/wechat/checklogin如果已经登录,会json传用户数据过来,没登录传1,如果是1前端处理跳转到域名/home/wechat/login
创建app/home/controller/Wechat.php,记得修改数据库名跟回调地址
<?php namespace app\home\controller; class Wechat extends Common{ public function index(){ //获取数据库的微信配置信息 $info = db('wx_config')->where([ 'key' => 'SHOPWCHAT'])->field('value')->find(); $info['value'] = json_decode($info['value'], true); $appid = $info['value']['appid']; //appid $appsecret = $info['value']['appsecret'];//appsecret $redirect_uri = $info['value']['redirect_uri'];//授权后重定向的回调链接地址,我是直接添加在了公众号管理那,这里可以随意修改 $this->appid = $appid; $this->redirect_uri = $redirect_uri; $code=$_GET['code']; //通过code获取网页授权access_token和openid $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)); $opendid= $token->openid; $access_token = $token->access_token; $data['opendid']= $opendid; //通过opendid获取用户信息 $info_url = 'https://api.weixin.qq.com/sns/userinfo?access_token='.$access_token.'&openid='.$opendid.'&lang=zh_CN '; $info = json_decode(file_get_contents($info_url)); $data['nickname'] = $info->nickname;//用户昵称 $data['image'] = $info->headimgurl;//用户头像,最后一个数值代表正方形头像大小(有0、46、64、96、132数值可选,0代表640*640正方形头像),用户没有头像时该项为空。若用户更换头像,原有头像URL将失效。 $data['sex'] = $info->sex;//用户的性别,值为1时是男性,值为2时是女性,值为0时是未知 $data['province'] = $info->province;//用户个人资料填写的省份 $data['city'] = $info->city;//普通用户个人资料填写的城市 $data['country'] = $info->country;//国家,如中国为zh_CN $data['createtime'] = date('Y-m-d H:i:s'); //通过opendid查数据库是否存在该用户 $opid = db('wxuser')->where('opendid',$data['opendid'])->find(); if ($opid['is_lock']=='1') { echo "您已被管理员禁用,无法登录"; return; } //如果存在,则更新数据用户信息 if(isset($opid['id'])){ $wxuser = db('wxuser'); if ($wxuser->where('id', $opid['id'])->update($data)!==false) { session('user',$opid); cookie('user',$opid, 36000); header("Location: ".$redirect_uri."");//如果更新成功则跳转 }else{ echo "登录失败,请重试"; } }elseif($opendid!=''){//否则插入数据 $id = db('wxuser')->insertGetId($data); $user = db('wxuser')->field('id,nickname')->where("id", $id)->find(); session('user',$user); cookie('user',$opid, 36000); header("Location: ".$redirect_uri."");//如果插入成功则跳转 } } public function login(){ $this->index(); $url='http://'.$_SERVER['SERVER_NAME'].$_SERVER["REQUEST_URI"]; header("Location: https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid."&redirect_uri=".dirname($url)."&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"); } public function checklogin(){ if(cookie('user')){ echo json_encode(cookie('user'),true); }else{ echo "1"; } } }
创建数据库,运行sql语句
SET FOREIGN_KEY_CHECKS=0; DROP TABLE IF EXISTS `clt_wxuser`; CREATE TABLE `clt_wxuser` ( `id` int(11) NOT NULL AUTO_INCREMENT, `nickname` varchar(255) DEFAULT NULL, `image` varchar(255) DEFAULT NULL, `sex` varchar(255) DEFAULT NULL, `province` varchar(255) DEFAULT NULL, `city` varchar(255) DEFAULT NULL, `country` varchar(255) DEFAULT NULL, `phone` varchar(255) DEFAULT NULL, `opendid` varchar(255) DEFAULT NULL, `name` varchar(255) DEFAULT NULL, `createtime` datetime NOT NULL, `is_lock` char(2) NOT NULL DEFAULT '0', PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=39 DEFAULT CHARSET=utf8;
需要添加用户列表的,可以移步到http://wymlw.cn/blog/?p=283
码字很辛苦,转载请注明来自感触life-博客的《CLTPHP添加微信登录功能》
👿 ❗
😎