libs-DingTalk
介绍
libs-DingTalk封装了部门管理2.0【1.获取部门列表】、用户管理2.0【获取企业全员信息等10个接口】、消息通知[发送普通消息和发送工作消息等]、上传文件、H5【获取签名接口】、回调校验类,初步提供简洁的 API 以供方便快速地调用钉钉接口。
环境要求
- PHP 7.4+
使用(获取全员用户信息)
- step1(创建config文件):
* dingtalk.php
return [
/*
|--------------------------------------------------------------------------
| 啃肉
|--------------------------------------------------------------------------
*/
'ken' => [
'corpid' => '',
'agent_id' => '',
'gateway' => 'https://oapi.dingtalk.com',
'app_key' => '',
'app_secret' => '',
'token' => '',
'aes_key' => '',
‘call_back_url’ => '',
],
];
- step2(控制器中调用):
/**
* 获取全员用户信息
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
*/
public function getAllUsers(Request $request)
{
if (!in_array($request->get('type'), [1, 2, 3])) {
return $this->errorResponse("类型字段传参错误");
}
$result = (new UserClient())->getAllUserList($request->get('type'));
return $this->successResponse($result['total']);
}
回调Demo
-
step1 (注册回调事件)
-
step2 (控制器:回调接口)
/**
* 回调接口
*
* @param Request $request
* @return \Illuminate\Http\JsonResponse
* @throws \Exception
*/
public function userCallback(Request $request)
{
$result = $request->all();
$data = json_decode(file_get_contents("php://input"), true);
try {
$encrypt = (new Encryptor(
config('dingtalk.ken.token'),
config('dingtalk.ken.aes_key'),
config('dingtalk.ken.app_key'),
config('dingtalk.ken.corpid')
));
} catch (\Exception $e) {
Log::error($e->getMessage());
}
$response = $encrypt->decrypt($result['signature'], $result['timestamp'], $result['nonce'], $data['encrypt']);
Log::debug('success_result: ' . ($response));
$response = json_decode($response, true);
if (isset($response['CorpId'])) {
Log::debug("insert job!!!");
try {
AsyncCallbackJob::dispatch($response)->onConnection('database');
} catch (\Exception $e) {
Log::error($e->getMessage());
}
}
$encryptData = $encrypt->encrypt('success');
return response()->json($encryptData);
}
文档
钉钉官方 https://developers.dingtalk.com/document/app/server-api-overview
内部使用 https://www.showdoc.com.cn/1506620719527138/7270001921955572
License
MIT