123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace weixin;
- /**
- * @title : 微信自动登录接口插件
- * @desc : 使用微信授权登录系统
- * @Author : Mr.Fu
- * @Date : 2023-04-06 19:06:59
- */
- class Wxauth
- {
- //微信认证URL
- private $Host="https://open.weixin.qq.com/connect/oauth2/authorize?";
- private $Response_type="code"; //private $Scope="snsapi_base";
- private $Scope="snsapi_base";//snsapi_userinfo/snsapi_base
- private $State="1";
- private $Appid="";
- private $Appsecret="";
- private $Redirect_uri="";//回调地址
- private $Auth_url="";
-
- public function __construct()
- {
- //读取微信登录配置
- // $this->Host =$this->Config['wxauth_url'];
- $this->Redirect_uri = urlencode('');
- $this->APPID =sysconfig('wchat.wxmini_user_appid');
- $this->APPSECRET =sysconfig('wchat.wxmini_user_secret');
- $this->Auth_url =$this->Host."appid=".$this->APPID."&redirect_uri=".$this->Redirect_uri."&response_type=".$this->Response_type."&scope=".$this->Scope."&state=".$this->State."#wechat_redirect";
-
- }
- /**
- * @title: 根据CODE获取AccessToken
- * @desc: 描述
- * @param {string} {code} {} {微信用户登录code}
- * @return {*}
- * @author: Mr.Fu
- * @method: POST
- * @Date: 2023-04-06 19:42:22
- */
- public function getAccesstoken(string $code=''){
- $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?';
- $url .= "appid=$this->APPID&secret=$this->APPSECRET&CODE=$code&grant_type=authorization_code";
- $result = file_get_contents($url);
- $result = json_decode($result,true);
- WLog('wxlogin',serialize($result));
- if(empty($result['access_token'])){
- return Result(0,'微信登录失败');
- }else{
- return Result(1,'登录成功',$result);
- }
- }
- /**
- * @title: 返回微信登录请求URL
- * @desc: 描述
- * @return {*}
- * @author: Mr.Fu
- * @method: POST
- * @Date: 2023-04-06 19:43:31
- */
- public function GetLoginUrl()
- {
- return $this->Auth_url;
- }
- /**
- * @title: 立即跳转微信登录
- * @desc: 描述
- * @return {*}
- * @author: Mr.Fu
- * @method: POST
- * @Date: 2023-04-06 19:43:44
- */
- public function Login()
- {
- J($this->Auth_url);
- }
- /**
- * @title: 微信认证后的回调处理
- * @desc: 描述
- * @return {*}
- * @author: Mr.Fu
- * @method: POST
- * @Date: 2023-04-06 19:43:59
- */
- public function Auth()
- {
- $code=input('code');
- if($code==""){
- $this->error('微信登录回调失败',url('index/login'));
- }
- $result = $this->getAccesstoken($code);
- if($result['status']==1){
- $this->error("微信登录失败",url('index/login'));
- }else{
- $access_token = $result['data']['access_token'];
- $openid = $result['data']['openid'];
- }
- $url='https://api.weixin.qq.com/sns/userinfo?access_token=';
- $wxuserinfo=file_get_contents($url.$access_token."&openid=".$openid);
- $wxuserinfo=json_decode($wxuserinfo,true);
- // $userinfo=db('member')->where('wxopenid',$wxuserinfo['openid'])->find();
- //微信用户是否存在
- // if(is_array($userinfo) and $wxuserinfo['openid']!=""){
- // if($this->login_status($userinfo['id'])){
- // $this->Log_Write($userinfo['id'],$userinfo['username'],2,$userinfo['username'].'微信登录成功',0);
- // J('/');
- // }else{
- // $this->Log_Write($userinfo['id'],$userinfo['username'],2,$userinfo['username'].'微信登录失败',1);
- // $this->error('微信登录失败!','/');
- // }
- // }else{
- // //不存在直接注册微信会员
- // $wxusername="WX_".GetRandStr(18);
- // $wxuserpwd=GetRandStr(10);
- // $facepic_data=file_get_contents($wxuserinfo['headimgurl']);
- // $DIR1="/uploads/facepic/".MyDate('Ymd',time())."/";
- // $FILENAME=time().GetRandStr(10).".jpg";
- // $path=ROOT_PATH.$DIR1;
- // if(!file_exists($path)){
- // mkdir($path);
- // }
- // file_put_contents($path.$FILENAME,$facepic_data);
- // $ip=GetIP();
- // $time=time();
- // $safecode=GetRandStr(16);
- // $password=Encrypt($safecode,$wxuserpwd);
- // $data=array(
- // 'group'=>1,
- // 'username'=>$wxusername,
- // 'nickname'=>$wxuserinfo['nickname'],
- // 'password'=>$password,
- // 'safecode'=>$safecode,
- // 'wxopenid'=>$wxuserinfo['openid'],
- // 'facepic'=>$DIR1.$FILENAME,
- // 'balance'=>0,
- // 'integral'=>0,
- // 'type'=>'wx',
- // 'regtime'=>$time,
- // 'regip'=>$ip,
- // 'logintime'=>$time,
- // 'loginip'=>$ip,
- // 'status'=>0
- // );
- // $uid=db('member')->insertGetId($data);
- // if($this->LoginStatus($uid)){
- // $this->Log_Write($uid,$wxusername,2,$wxusername.'微信注册登录成功',0);
- // J('/');
- // }else{
- // $this->Log_Write($uid,$wxusername,2,$wxusername.'微信注册登录失败',1);
- // $this->error('微信注册登录失败!','/');
- // }
- // }
- }
- public function LoginStatus($id)
- {
- // $userinfo=db('member')->where('id',$id)->find();
- // if(is_array($userinfo) and $id>=1){
- // $ip=GetIP();
- // $time=time();
- // //开始登录
- // $login_data=array(
- // 'id'=>$userinfo['id'],
- // 'username'=>$userinfo['username'],
- // 'nickname'=>$userinfo['nickname'],
- // 'logintime'=>$time,
- // 'loginip'=>$ip
- // );
- // $update=array('logintime'=>$time,'loginip'=>$ip);
- // //记录登录时间和IP地址
- // db('member')->where('username',$userinfo['username'])->update($update);
- // //加密管理登录信息数据
- // $ciphertext=AuthCode(serialize($login_data),'ENCODE',$this->Config['cookie_secretkey']);
- // //把密文存储到会话中
- // session('userinfo',$ciphertext);
- // return true;
- // }else{
- // return false;
- // }
- }
- }
- ?>
|