config->base.php
<?php
return [
'DIR_ROOT' => dirname(__FILE__) . '/',
'OAPI_HOST' => 'https://oapi.dingtalk.com',
'CORPID' => 'ding5c49********dc0d35c2f4657eb6378f',
'SECRET' => 'pm7eJPbO8X********SkIt8IVcX7EKkHXZ5_28nqKe_we1j-aowauPDLkGl7dlTT',
'AGENTID' => '16*****28',
'ENCODING_AES_KEY' => '1*****6',
'TOKEN' => 'VazMJWWiPbq********qtVBaKW3lvm5c7tB9aODCrI8',
];
controller->Index.php
<?php
namespace app\index\controller;
use think\facade\Config;
use think\Controller;
use think\facade\Request;
class Index extends Controller {
public function index() {
$config = Config::pull('base');
$corpid = $config['CORPID'];
$corpsecret = $config['SECRET'];
//return $corpid.$corpsecret.$href;
$urltoken = "https://oapi.dingtalk.com/gettoken?corpid=" . $corpid . "&corpsecret=" . $corpsecret;
$access_token_arr = json_decode(curl_http_request($urltoken), true);
$access_token = $access_token_arr["access_token"];
//$urlticket = "https://oapi.dingtalk.com/get_jsapi_ticket?access_token=" . $access_token;
//$ticket_arr = json_decode(curl_http_request($urlticket), true);
//$ticket = $ticket_arr["ticket"];
//$nonceStr = $config['ENCODING_AES_KEY'];
//$timeStamp = time();
//$agentId = $config['AGENTID'];
//$href = get_current_page_url();
// $url = urldecode($href);
// $corpAccessToken = $access_token;
//$signature = sign($ticket, $nonceStr, $timeStamp, $url);
$this->assign('access_token', $access_token);
$this->assign('corpid', $corpid);
return $this->fetch();
}
public function login() {
$urluserid = "https://oapi.dingtalk.com/user/getuserinfo?access_token=" . Request::param('acctoken') . "&code=" . Request::param('username');
$userid_arr = json_decode(curl_http_request($urluserid), true);
$userid = $userid_arr["userid"];
$urluserinfo = "https://oapi.dingtalk.com/user/get?access_token=" . Request::param('acctoken') . "&userid=" . $userid;
$userinfo = json_decode(curl_http_request($urluserinfo), true);
$this->assign('userinfo', $userinfo['mobile']);
return $this->fetch();
}
}
view->index->index.html
<!DOCTYPE html>
<html>
<head>
<title>我的工资条</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{load href="/static/css/bootstrap.css" /}
{load href="/static/js/jquery.min.js" /}
{load href="/static/js/bootstrap.min.js" /}
{load href="/static/js/dingtalk.js" /}
</head>
<body>
<div class="container">
<div class="col-sm-12 text-center">用户信息验证中,请稍后……</div>
<form action="{:url('login')}" id="ddform" method="post">
<input type="hidden" value="" id="username" name="username" />
<input type="hidden" value="{$access_token}" id="acctoken" name="acctoken" />
</form>
</div>
</body>
<script type="text/javascript">
dd.ready(function () {
dd.runtime.permission.requestAuthCode({
corpId: '{$corpid}',
onSuccess: function (result) {
$("#username").val(result.code);
$("#ddform").submit();
},
onFail: function (err) {
document.write('验证失败!' + err);
}
});
});
</script>
</html>
view->index->login.html
<!DOCTYPE html>
<html>
<head>
<title>我的工资条</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="container">
<div class="col-sm-12 text-center">我的电话是:{$userinfo}</div>
</div>
</body>
</html>
需要引入的文件:http://g.alicdn.com/dingding/open-develop/1.6.9/dingtalk.js