增加钉钉消息推动

Signed-off-by: vilson <545522390@qq.com>
This commit is contained in:
vilson 2019-06-24 21:22:41 +08:00
parent 5c2a0e04fc
commit a6c0b15738
7 changed files with 107 additions and 11 deletions

View File

@ -162,6 +162,14 @@ function isOpenNoticePush()
{
return config('config.notice_push');
}
/**
* 是否开启钉钉消息推送
* @return mixed
*/
function isOpenDingTalkNoticePush()
{
return config('config.dingtalk_push');
}
/**
* 日期格式标准输出

View File

@ -134,6 +134,7 @@ class Member extends CommonModel
'dingtalk_openid' => $openid,
'dingtalk_unionid' => $unionid,
'name' => $userInfo['nick'],
'dingtalk_userid' => isset($userInfo['userId']) ? $userInfo['userId'] : '',
'avatar' => isset($userInfo['avatar']) ? $userInfo['avatar'] : '',
'mobile' => isset($userInfo['mobile']) ? $userInfo['mobile'] : '',
'email' => isset($userInfo['email']) ? $userInfo['email'] : '',

View File

@ -24,6 +24,21 @@ class Oauth extends BasicApi
public function index()
{
$app = new Application(config('dingtalk.'));
$msg = [
'msgtype' => "oa",
'oa' => [
'message_url' => 'http://dingtalk.com',
'head' => ['bgcolor' => 'FFBBBBBB', 'title' => '消息通知'],
'body' => ['title' => '888', 'content' => '666'],
]
];
$params = [
'agent_id'=> '271863764',
'userid_list' => 'manager9168',
'msg'=> json_encode($msg)
];
$res = $app->conversation->sendCorporationMessage($params);
echo json_encode($res);die;
$userId = $app->user->getUseridByUnionid('3CnKFHEE7mX1hayPIHvpCwiEiE');
// echo json_encode($userId);die;
$userId = $userId['userid'];
@ -36,6 +51,7 @@ class Oauth extends BasicApi
$currentMember = getCurrentMember();
if (!$currentMember) {
$app = new Application(config('dingtalk.'));
// $response = $app->oauth->use('app-01')->redirect();
$response = $app->oauth->use('app-01')->withQrConnect()->redirect();
$redirect = $response->getTargetUrl();
if ($redirect) {
@ -77,6 +93,7 @@ class Oauth extends BasicApi
$user['user_info']['avatar'] = $userInfo['avatar'];
$user['user_info']['mobile'] = $userInfo['mobile'];
$user['user_info']['email'] = $userInfo['email'];
$user['user_info']['userId'] = $userId;
}
}
Member::dingtalkLogin($user['user_info']);

View File

@ -11,11 +11,16 @@ namespace app\project\behavior;
use app\common\Model\CommonModel;
use app\common\Model\Member;
use app\common\Model\Notify;
use app\common\Model\ProjectLog;
use app\common\Model\ProjectVersion;
use app\common\Model\TaskMember;
use app\common\Model\TaskStages;
use message\DingTalk;
use service\MessageService;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\facade\Log;
class Task
@ -23,9 +28,9 @@ class Task
/**
* 任务操作钩子
* @param $data
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
* @throws DataNotFoundException
* @throws ModelNotFoundException
* @throws DbException
*/
public function run($data)
{
@ -178,7 +183,7 @@ class Task
$notifyActions = ['done', 'redo', 'assign'];
if (in_array($data['type'], $notifyActions)) {
//todo 短信,消息推送
$notifyModel = new \app\common\Model\Notify();
$notifyModel = new Notify();
$member = Member::where(['code' => $data['memberCode']])->find();
$notifyData['title'] = $member['name'] . ' ' . $remark;
$notifyData['content'] = $task['name'];
@ -186,11 +191,26 @@ class Task
$taskMembers = TaskMember::where(['task_code' => $task['code']])->select()->toArray();
if ($taskMembers) {
$messageService = new MessageService();
$messageDingTalk = new DingTalk();
foreach ($taskMembers as $taskMember) {
if ($taskMember['member_code'] == $data['memberCode']) {
continue;//跳过产生者
}
$member = Member::where(['code' => $taskMember['member_code']])->find();
$result = $notifyModel->add($notifyData['title'], $notifyData['content'], $notifyData['type'], $data['memberCode'], $taskMember['member_code'], $notifyData['action'], json_encode($task), $notifyData['terminal'], $notifyData['avatar']);
if (isOpenDingTalkNoticePush()) {
if ($member['dingtalk_userid']) {
$params = [
'msgtype' => "oa",
'oa' => [
'message_url' => 'http://dingtalk.com',
'head' => ['bgcolor' => 'FFBBBBBB', 'text' => '任务动态'],
'body' => ['title' => $notifyData['title'], 'content' => $notifyData['content']],
]
];
$messageDingTalk->sendCorporationMessage($member['dingtalk_userid'], $params);
}
}
if (isOpenNoticePush()) {
$messageService->sendToUid($taskMember['member_code'], ['content' => $notifyData['content'], 'title' => $notifyData['title'], 'data' => ['organizationCode' => getCurrentOrganizationCode()], 'notify' => $result], $notifyData['action']);
}

View File

@ -1,4 +1,5 @@
<?php
return [
'notice_push' => true, //是否开启websocket消息推送
'notice_push' => false, //是否开启websocket消息推送
'dingtalk_push' => false, //是否开启钉钉消息推送
];

View File

@ -6,21 +6,29 @@ return [
| 【必填】企业 corpId
|-----------------------------------------------------------
*/
'corp_id' => 'ccc',
'corp_id' => 'aa',
/*
|-----------------------------------------------------------
| 【必填】应用 AppKey
|-----------------------------------------------------------
*/
'app_key' => 'bbb',
'app_key' => 'bb',
/*
|-----------------------------------------------------------
| 【必填】应用 AppSecret
|-----------------------------------------------------------
*/
'app_secret' => 'aaa',
'app_secret' => 'cc',
/*
|-----------------------------------------------------------
| 【选填】应用agent_id
|-----------------------------------------------------------
| 如果你用到消息推送,需要配置该项
*/
'agent_id' => 'dd',
/*
|-----------------------------------------------------------
@ -61,10 +69,10 @@ return [
| `redirect` 为回调地址
*/
'app-01' => [
'client_id' => 'aaa',
'client_secret' => 'bbb-Tu2CsUZN-ZlQXWQ',
'client_id' => 'ee',
'client_secret' => 'ff',
'scope' => 'snsapi_login',
'redirect' => 'https://example.com',
'redirect' => 'https://gg.com',
],
/*
|-------------------------------------------

View File

@ -0,0 +1,41 @@
<?php
namespace message;
use EasyDingTalk\Application;
/**
* 消息推送服务
* Class MessageService
* @package service
*/
class DingTalk
{
public $app;
public $agentId;
/**
* MessageService constructor.
* @param $agentId
* @param $userIdList
* @param $msg
*/
public function __construct($agentId = '')
{
$app = new Application(config('dingtalk.'));
$this->app = $app;
$this->agentId = $agentId ? $agentId : config('dingtalk.agent_id');
}
public function sendCorporationMessage($userIdList, $msg, $agentId = '')
{
$params = [
'agent_id' => $agentId ? $agentId : $this->agentId,
'userid_list' => $userIdList,
'msg' => is_array($msg) ? json_encode($msg) : $msg
];
return $this->app->conversation->sendCorporationMessage($params);
}
}