shop-php/crmeb/utils/DingTalk.php

79 lines
2.2 KiB
PHP
Raw Normal View History

2023-05-27 11:14:59 +08:00
<?php
namespace crmeb\utils;
class DingTalk
{
public static $url = 'https://oapi.dingtalk.com/robot/send?access_token=%s';
/**
* @param $url
* @param $text
* @param $at array
* @return mixed
*/
public static function send($text, $url = null, array $at = [])
{
$tokenArray = [
'production' => '64f863c5f4415e715c8b9c925b6d58837553335e42059b6fba579f0a301287e9',
'test' => 'f3d3bb7cf3c46073c6f657c021113d74fabd59742d33af5d8601ec8c38f18310',
];
$env = env('APP_ENV', 'test');
// if ($env == 'test') {
// return true;
// }
$token = $tokenArray[$env];
if(empty($url)){
$url = self::$url;
}
$url = sprintf($url, $token);
$data = array (
'msgtype' => 'text',
'text' => [
'content' => $text
],
'at' => [
'atMobiles' => 'all' == $at ? [] : $at,
'isAtAll' => 'all' == $at,
],
);
$data_string = json_encode($data);
$curl = new Curl();
$result = $curl->postJson($url, $data_string);
if ($result) {
return true;
}
return false;
}
/**
* 发送钉钉异常告警
* @param \Throwable $exception
* @param $msg
* @param null $request
* @return bool|mixed
*/
public static function exception(\Throwable $exception, $msg, $request = null)
{
$message = [
"业务异常: {$msg}",
'code' . $exception->getCode(),
'message' . $exception->getMessage(),
'file' . $exception->getFile(),
'line' . $exception->getLine(),
];
if ($request !== null) {
$message[] = 'method' . $request->method();
$message[] = 'route' . $request->rule()->getRule();
$message[] = 'params' . json_encode($request->rule()->getVars());
}
if (is_callable([$exception, 'errors'])) {
$message[] = 'errors:' . json_encode($exception->errors(), JSON_UNESCAPED_UNICODE);
}
$message = implode(PHP_EOL, $message);
return self::send($message);
}
}