php使用钉钉登录

简单写下过程
1.下载sdk(新的不会用,用旧的。。。)
https://open.dingtalk.com/document/orgapp-server/sdk-download
php版本
https://open-dev.dingtalk.com/download/openSDK/php?spm=ding_open_doc.document.0.0.6f8f722f4sDhgA

2,开始敲代码

protected $AppKey;
protected $AppSecret;
protected $redirect_uri;

protected $LockPass;

public function __construct()
{


    define('DIR_ROOT', dirname(__FILE__) . '/');
    define("OAPI_HOST", "https://oapi.dingtalk.com");
    //应用id
    $this->AppKey = 'xxxx';
    //应用AppSecret
    $this->AppSecret = 'xxxx';
    $this->http = $_SERVER['SERVER_ADDR'];
    //回调链接
    $this->redirect_uri = "http://$this->http/admin/Dingtalk/callback";

    $this->LockPass = new LockPass();

}


/**
 * 用户登录
 * @return string
 * @throws \Exception
 */
public function index()
{

    require_once "../vendor/dingtalk/TopSdk.php";
    date_default_timezone_set('Asia/Shanghai');

    $token = cache('token');
    if (!empty($token) || is_null($token)) {
        $access_token = '';
        $c = new \DingTalkClient(\DingTalkConstant::$CALL_TYPE_OAPI, \DingTalkConstant::$METHOD_GET, \DingTalkConstant::$FORMAT_JSON);
        $req = new \OapiGettokenRequest;
        $req->setAppkey($this->AppKey);
        $req->setAppsecret($this->AppSecret);
        try {
            $resp = $c->execute($req, $access_token, "https://oapi.dingtalk.com/gettoken");
            if ($resp->errmsg === 'ok') {
                $token = $resp->access_token;

                cache('token', $token, 7000);
            }
        } catch (Exception $e) {
            $this->error('参数有误');
        }

    }


    $url = "https://oapi.dingtalk.com/connect/oauth2/sns_authorize?appid=$this->AppKey&response_type=code&scope=snsapi_auth&state=STATE&redirect_uri=" . urlencode($this->redirect_uri);
    header('Location:' . $url);
    die;
}


//回调地址
public function callback()
{

    $data = input('get.');
    if(empty($data)){
        return  false;
    }
    $code = $data['code'];
    $token = cache('token');
    require_once "../vendor/dingtalk/TopSdk.php";
    ini_set("error_reporting", "E_ALL & ~E_NOTICE");

    $c = new \DingTalkClient(\DingTalkConstant::$CALL_TYPE_OAPI, \DingTalkConstant::$METHOD_POST, \DingTalkConstant::$FORMAT_JSON);
    $req = new \OapiSnsGetuserinfoBycodeRequest;


    $req->setTmpAuthCode($code);
 
        $resp = $c->executeWithAccessKey($req, "https://oapi.dingtalk.com/sns/getuserinfo_bycode", $this->AppKey, $this->AppSecret);
        $res = $resp->user_info;
        $save = [
            'openid' => $res->openid,
            'dingId' => $res->dingId,
            'unionids' => $res->unionid,
            'nickName' => $res->nick,

        ];

        $obj = new SystemDing();
        $find  = $obj->where($save)->find();
        if($find){
            //二次登录获取id直接登录,不存在admin_id重新绑定
            if(!empty($find['admin_id'])){
                $this->authLogin($find['admin_id']);
            }
            $dingId = $this->LockPass::lock_url($find['id']);
            return redirect('/admin/login/index?dingType=true&dingId='.$dingId);
        }else{
            //首次登录授权录入信息
            $Id = $obj->insertGetId($save);
            $dingId = $this->LockPass::lock_url($Id);
            return redirect('/admin/login/index?dingType=true&dingId='.$dingId);
        }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值