PHP接入钉钉扫码登录

class Ding
{/*** @var string*/protected $AgentId='xxxx';/*** @var string*/protected $AppKey='xxxx';/*** @var string*/protected $AppSecret='xxxx';/*** 钉钉跳转* @author   Xun* @version 1.0.0* @date  2021-11-26*/public function Notify(){$data=input();$data=$this->GetUserId($data['code']);//出问题了if($data['code']==0){echo $data['errmsg'];return;}//获取userid$userid=$data['data']['userid'];//找到用户$system_admin=Db::name('system_admin')->where('userid',$userid)->find();if(!$system_admin){echo '您当前非系统用户,请联系管理员!';return;}//写入session,登录跳转首页Session::set('adminId', $system_admin['id']);Session::set('adminInfo', $system_admin);Session::save();$this->redirect("/admin/index");}/*** 钉钉二维码登录* @author   Xun* @version 1.0.0* @date  2021-11-26*/public function QrCode(){$url="https://oapi.dingtalk.com/connect/qrconnect?appid=".$this->AppKey."&response_type=code&scope=snsapi_login&state=STATE&redirect_uri=http://xxxx/Notify";$this->redirect($url);}/*** 获取用户id* @author   Xun* @version 1.0.0* @date  2021-11-26*/public function GetUserId($code){$timestamp=$this->GetMillisecond();$signature=$this->GetSignature($timestamp);$url='https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey='.urlencode($this->AppKey).'×tamp='.urlencode($timestamp).'&signature='.urlencode($signature);$post=['tmp_auth_code'=>$code,];$res=$this->PostCurlRequest($url, json_encode($post));$res=json_decode($res,1);if($res['errcode']!=0)return ['code'=>0,'msg'=>$res['errmsg']];$unionid=$res['user_info']['unionid'];$url='https://oapi.dingtalk.com/topapi/user/getbyunionid?access_token='.$this->getAccessToken();$post=['unionid'=>$unionid];$res=$this->PostCurlRequest($url, json_encode($post));$res=json_decode($res,1);if($res['errcode']!=0)return ['code'=>0,'msg'=>$res['errmsg']];return ['code'=>1,'msg'=>'ok','data'=>$res['result']];}/*** 获取毫秒时间戳* @author   Xun* @version 1.0.0* @date  2021-11-29*/private function GetMillisecond(){list($s1, $s2) = explode(' ', microtime());return (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);}/*** 加密* @author   Xun* @version 1.0.0* @date  2021-11-29*/public function GetSignature($timestamp){$s = hash_hmac('sha256', $timestamp, $this->AppSecret, true);return base64_encode($s);}

}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部