1
0
mirror of https://gitee.com/koogua/course-tencent-cloud.git synced 2025-06-27 21:10:24 +08:00

Merge branch 'koogua/dingtalk-notice' into demo

# Conflicts:
#	scheduler.php
This commit is contained in:
koogua 2021-02-23 20:29:26 +08:00
commit 90581c4119
8 changed files with 322 additions and 1 deletions

View File

@ -26,6 +26,7 @@ class CleanLogTask extends Task
$this->cleanOrderLog();
$this->cleanRefundLog();
$this->cleanPointLog();
$this->cleanDingTalkLog();
$this->cleanNoticeLog();
$this->cleanOtherLog();
}
@ -234,6 +235,18 @@ class CleanLogTask extends Task
$this->whitelist[] = $type;
}
/**
* 清理钉钉日志
*/
protected function cleanDingTalkLog()
{
$type = 'dingtalk';
$this->cleanLog($type, 7);
$this->whitelist[] = $type;
}
/**
* 清理通知日志
*/

View File

@ -378,4 +378,30 @@ class SettingController extends Controller
}
}
/**
* @Route("/dingtalk/robot", name="admin.setting.dingtalk_robot")
*/
public function dingtalkRobotAction()
{
$section = 'dingtalk.robot';
$settingService = new SettingService();
if ($this->request->isPost()) {
$data = $this->request->getPost();
$settingService->updatePointSettings($section, $data);
return $this->jsonSuccess(['msg' => '更新配置成功']);
} else {
$robot = $settingService->getSettings($section);
$this->view->pick('setting/dingtalk_robot');
$this->view->setVar('robot', $robot);
}
}
}

View File

@ -6,6 +6,7 @@ use App\Http\Admin\Services\AlipayTest as AlipayTestService;
use App\Http\Admin\Services\Setting as SettingService;
use App\Http\Admin\Services\WxpayTest as WxpayTestService;
use App\Services\Captcha as CaptchaService;
use App\Services\DingTalkNotice as DingTalkNoticeService;
use App\Services\Live as LiveService;
use App\Services\Mail\Test as TestMailService;
use App\Services\MyStorage as StorageService;
@ -250,4 +251,20 @@ class TestController extends Controller
return $this->jsonSuccess(['status' => $status]);
}
/**
* @Post("/dingtalk/robot", name="admin.test.dingtalk_robot")
*/
public function dingTalkRobotAction()
{
$noticeService = new DingTalkNoticeService();
$result = $noticeService->test();
if ($result) {
return $this->jsonSuccess(['msg' => '发送消息成功,请到钉钉确认']);
} else {
return $this->jsonError(['msg' => '发送消息失败,请检查配置']);
}
}
}

View File

@ -800,6 +800,12 @@ class AuthNode extends Service
'type' => 'menu',
'route' => 'admin.setting.wechat_oa',
],
[
'id' => '5-1-15',
'title' => '钉钉机器人',
'type' => 'menu',
'route' => 'admin.setting.dingtalk_robot',
],
[
'id' => '5-1-14',
'title' => '积分设置',

27
app/Library/Benchmark.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace App\Library;
class Benchmark
{
protected $startTime = 0;
protected $endTime = 0;
public function start()
{
$this->startTime = microtime(true);
}
public function stop()
{
$this->endTime = microtime(true);
}
public function getElapsedTime()
{
return $this->endTime - $this->startTime;
}
}

View File

@ -35,6 +35,24 @@ function kg_substr($str, $start, $length, $suffix = '...')
return $str == $result ? $str : $result . $suffix;
}
/**
* 占位替换
*
* @param string $str
* @param array $data
* @return string
*/
function kg_ph_replace($str, $data = [])
{
if (empty($data)) return $str;
foreach ($data as $key => $value) {
$str = str_replace('{' . $key . '}', $value, $str);
}
return $str;
}
/**
* uniqid封装
*

View File

@ -17,7 +17,7 @@ class Task extends Model
* 针对外部用户
*/
const TYPE_NOTICE_ACCOUNT_LOGIN = 11; // 帐号登录通知
const TYPE_NOTICE_LIVE_BEGIN = 12; // 直播开始通知
const TYPE_NOTICE_LIVE_BEGIN = 12; // 直播学员通知
const TYPE_NOTICE_ORDER_FINISH = 13; // 订单完成通知
const TYPE_NOTICE_REFUND_FINISH = 14; // 退款完成通知
const TYPE_NOTICE_CONSULT_REPLY = 15; // 咨询回复通知

View File

@ -0,0 +1,214 @@
<?php
namespace App\Services;
use App\Repos\Account as AccountRepo;
use App\Repos\Course as CourseRepo;
use GuzzleHttp\Client as HttpClient;
use Phalcon\Logger\Adapter\File as FileLogger;
class DingTalkNotice extends Service
{
/**
* @var array
*/
protected $settings;
/**
* @var FileLogger
*/
protected $logger;
public function __construct()
{
$this->settings = $this->getSettings('dingtalk.robot');
$this->logger = $this->getLogger('dingtalk');
}
/**
* 测试消息
*
* @return bool
*/
public function test()
{
$params = [
'msgtype' => 'text',
'text' => ['content' => '我是一条测试消息啦!'],
];
return $this->send($params);
}
/**
* 给技术人员发消息
*
* @param string $content
* @return bool
*/
public function atTechSupport($content)
{
$atMobiles = $this->parseAtMobiles($this->settings['ts_mobiles']);
$atContent = $this->buildAtContent($content, $atMobiles);
$params = [
'msgtype' => 'text',
'text' => ['content' => $atContent],
'at' => ['atMobiles' => $atMobiles],
];
return $this->send($params);
}
/**
* 给客服人员发消息
*
* @param string $content
* @return bool
*/
public function atCustomService($content)
{
$atMobiles = $this->parseAtMobiles($this->settings['cs_mobiles']);
$atContent = $this->buildAtContent($content, $atMobiles);
$params = [
'msgtype' => 'text',
'text' => ['content' => $atContent],
'at' => ['atMobiles' => $atMobiles],
];
return $this->send($params);
}
/**
* 给课程讲师发消息
*
* @param int $courseId
* @param string $content
* @return bool
*/
public function atCourseTeacher($courseId, $content)
{
$courseRepo = new CourseRepo();
$course = $courseRepo->findById($courseId);
$accountRepo = new AccountRepo();
$account = $accountRepo->findById($course->teacher_id);
if (empty($account->phone)) {
return false;
}
$atMobiles = $account->phone;
$atContent = $this->buildAtContent($content, $atMobiles);
$params = [
'msgtype' => 'text',
'text' => ['content' => $atContent],
'at' => ['atMobiles' => $atMobiles],
];
return $this->send($params);
}
/**
* 发送消息
*
* @param array $params
* @return bool
*/
public function send($params)
{
if (isset($params['msgtype'])) {
$params['msgtype'] = 'text';
}
$webHook = "https://oapi.dingtalk.com/robot/send?access_token=%s&timestamp=%s&sign=%s";
$appSecret = $this->settings['app_secret'];
$appToken = $this->settings['app_token'];
$timestamp = time() * 1000;
$data = sprintf("%s\n%s", $timestamp, $appSecret);
$sign = urlencode(base64_encode(hash_hmac('sha256', $data, $appSecret, true)));
$postUrl = sprintf($webHook, $appToken, $timestamp, $sign);
try {
$client = new HttpClient();
$response = $client->post($postUrl, ['json' => $params]);
$content = $response->getBody()->getContents();
$content = json_decode($content, true);
$this->logger->debug('Send Message Request ' . kg_json_encode($params));
$this->logger->debug('Send Message Response ' . kg_json_encode($content));
$result = $content['errcode'] == 0;
if ($result == false) {
$this->logger->error('Send Message Failed ' . kg_json_encode($content));
}
} catch (\Exception $e) {
$this->logger->error('Send Message Exception ' . kg_json_encode([
'file' => $e->getFile(),
'line' => $e->getLine(),
'message' => $e->getMessage(),
]));
$result = false;
}
return $result;
}
/**
* @param string $mobiles
* @return array
*/
protected function parseAtMobiles($mobiles)
{
if (empty($mobiles)) {
return [];
}
$mobiles = explode(',', $mobiles);
return array_map(function ($mobile) {
return trim($mobile);
}, $mobiles);
}
/**
* @param string $content
* @param array $mobiles
* @return string
*/
protected function buildAtContent($content, $mobiles = [])
{
if (count($mobiles) == 0) {
return $content;
}
$result = '';
foreach ($mobiles as $mobile) {
$result .= sprintf('@%s ', $mobile);
}
$result .= $content;
return $result;
}
}