简单写下过程
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);
}
}