* Set default msgtype as markdown for dingding.

This commit is contained in:
liugang
2017-10-30 14:26:51 +08:00
parent 4b141a137f
commit 3076736e9e
11 changed files with 133 additions and 109 deletions
+5 -5
View File
@@ -27,9 +27,9 @@
</tr>
</thead>
<tbody>
<?php foreach($entries as $entry):?>
<?php foreach($entries as $id => $entry):?>
<tr>
<td class='text-center'><?php echo $entry->id;?></td>
<td class='text-center'><?php echo $id;?></td>
<td title='<?php echo $entry->name;?>'><?php echo $entry->name;?></td>
<td><?php echo $entry->code;?></td>
<td title='<?php echo $entry->key;?>'><?php echo $entry->key;?></td>
@@ -37,11 +37,11 @@
<td title='<?php echo $entry->desc;?>'><?php echo $entry->desc;?></td>
<td class='text-right'>
<?php
common::printIcon('entry', 'log', "entryID=$entry->id", '', 'list', 'file-text-o');
common::printIcon('entry', 'edit', "entryID=$entry->id", '', 'list');
common::printIcon('entry', 'log', "entryID=$id", '', 'list', 'file-text-o');
common::printIcon('entry', 'edit', "entryID=$id", '', 'list');
if(common::hasPriv('entry', 'delete'))
{
$deleteURL = $this->createLink('entry', 'delete', "entryID=$entry->id&confirm=yes");
$deleteURL = $this->createLink('entry', 'delete', "entryID=$id&confirm=yes");
echo html::a("javascript:ajaxDelete(\"$deleteURL\",\"entryList\",confirmDelete)", '<i class="icon-remove"></i>', '', "title='{$lang->entry->delete}' class='btn-icon'");
}
?>
+1 -2
View File
@@ -15,8 +15,7 @@
<ul class='nav'>
<?php
echo "<li id='entry' class='active'>"; common::printLink('entry', 'browse', '', $lang->entry->common); echo '</li>';
echo "<li id='webhook'>"; common::printLink('webhook', 'browse', 'type=default', $lang->entry->webhook); echo '</li>';
echo "<li id='dingding'>"; common::printLink('webhook', 'browse', 'type=dingding', $lang->entry->dingding); echo '</li>';
echo "<li id='webhook'>"; common::printLink('webhook', 'browse', '', $lang->entry->webhook); echo '</li>';
?>
</ul>
<div class='actions'>
+7 -11
View File
@@ -14,7 +14,6 @@ class webhook extends control
/**
* Browse webhooks.
*
* @param string $type
* @param string $orderBy
* @param int $recTotal
* @param int $recPerPage
@@ -22,14 +21,13 @@ class webhook extends control
* @access public
* @return void
*/
public function browse($type = '', $orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
public function browse($orderBy = 'id_desc', $recTotal = 0, $recPerPage = 20, $pageID = 1)
{
$this->app->loadClass('pager', $static = true);
$pager = new pager($recTotal, $recPerPage, $pageID);
$this->view->title = $this->lang->webhook->api . $this->lang->colon . $this->lang->webhook->list;
$this->view->webhooks = $this->webhook->getList($type, $orderBy, $pager);
$this->view->type = $type;
$this->view->webhooks = $this->webhook->getList($orderBy, $pager);
$this->view->orderBy = $orderBy;
$this->view->pager = $pager;
$this->display();
@@ -38,17 +36,16 @@ class webhook extends control
/**
* Create a webhook.
*
* @param string $type
* @access public
* @return void
*/
public function create($type = '')
public function create()
{
if($_POST)
{
$this->webhook->create($type);
$this->webhook->create();
if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError()));
$this->send(array('result' => 'success', 'message' => $this->lang->webhook->saveSuccess, 'locate' => inlink('browse', "type=$type")));
$this->send(array('result' => 'success', 'message' => $this->lang->webhook->saveSuccess, 'locate' => inlink('browse')));
}
$this->app->loadLang('action');
@@ -57,7 +54,6 @@ class webhook extends control
$this->view->projects = $this->loadModel('project')->getPairs();
$this->view->objectTypes = $this->webhook->getObjectTypes();
$this->view->objectActions = $this->webhook->getObjectActions();
$this->view->type = $type;
$this->display();
}
@@ -75,7 +71,7 @@ class webhook extends control
{
$this->webhook->update($id);
if(dao::isError()) $this->send(array('result' => 'fail', 'message' => dao::getError()));
$this->send(array('result' => 'success', 'message' => $this->lang->webhook->saveSuccess, 'locate' => inlink('browse', "type=$webhook->type")));
$this->send(array('result' => 'success', 'message' => $this->lang->webhook->saveSuccess, 'locate' => inlink('browse')));
}
$this->app->loadLang('action');
@@ -136,7 +132,7 @@ class webhook extends control
*/
public function asyncSend()
{
$webhooks = $this->webhook->getList($type = '', $orderBy = 'id_desc', $pager = null, $decode = false);
$webhooks = $this->webhook->getList($orderBy = 'id_desc', $pager = null, $decode = false);
if(empty($webhooks))
{
echo "NO WEBHOOK EXIST.\n";
+9
View File
@@ -1,5 +1,14 @@
$(function()
{
$('#type').change(function()
{
var type = $(this).val();
$('#contentTypeTR').toggle(type != 'dingding');
$('#sendTypeTR').toggle(type != 'dingding');
$('#paramsTR').toggle(type != 'bearychat' && type != 'dingding');
$('#urlNote').html(urlNote[type]);
});
$('.objectType').click(function()
{
if($(this).prop('checked'))
+11 -5
View File
@@ -3,7 +3,6 @@ $lang->webhook->common = 'webhook';
$lang->webhook->list = '钩子列表';
$lang->webhook->api = '接口';
$lang->webhook->entry = '应用';
$lang->webhook->dingding = '钉钉';
$lang->webhook->log = '日志';
$lang->webhook->assigned = '指派给';
@@ -31,6 +30,10 @@ $lang->webhook->date = '发送时间';
$lang->webhook->data = '数据';
$lang->webhook->result = '结果';
$lang->webhook->typeList['bearychat'] = '倍洽';
$lang->webhook->typeList['dingding'] = '钉钉';
$lang->webhook->typeList['default'] = '默认';
$lang->webhook->sendTypeList['sync'] = '同步';
$lang->webhook->sendTypeList['async'] = '异步';
@@ -50,7 +53,10 @@ $lang->webhook->confirmDelete = '您确认要删除该webhook吗?';
$lang->webhook->trimWords = '了';
$lang->webhook->note = new stdClass();
$lang->webhook->note->async = '异步需要打开计划任务';
$lang->webhook->note->dingding = '请在钉钉中添加一个自定义机器人,并将其webhook填写到此处。';
$lang->webhook->note->product = '此项为空时所有产品的动作都会触发钩子,否则只有关联产品的动作才会触发。';
$lang->webhook->note->project = '此项为空时所有项目的动作都会触发钩子,否则只有关联项目的动作才会触发。';
$lang->webhook->note->async = '异步需要打开计划任务';
$lang->webhook->note->product = '此项为空时所有产品的动作都会触发钩子,否则只有关联产品的动作才会触发。';
$lang->webhook->note->project = '此项为空时所有项目的动作都会触发钩子,否则只有关联项目的动作才会触发。';
$lang->webhook->note->typeList['bearychat'] = '请在倍洽中添加一个禅道机器人,并将其webhook填写到此处。';
$lang->webhook->note->typeList['dingding'] = '请在钉钉中添加一个自定义机器人,并将其webhook填写到此处。';
$lang->webhook->note->typeList['default'] = '从第三方系统获取webhook并填写到此处。';
+78 -64
View File
@@ -28,18 +28,16 @@ class webhookModel extends model
/**
* Get webhook list.
*
* @param string $type
* @param string $orderBy
* @param object $pager
* @param bool $decode
* @access public
* @return array
*/
public function getList($type = '', $orderBy = 'id_desc', $pager = null, $decode = true)
public function getList($orderBy = 'id_desc', $pager = null, $decode = true)
{
$webhooks = $this->dao->select('*')->from(TABLE_WEBHOOK)
->where('deleted')->eq('0')
->beginIF($type)->andWhere('type')->eq($type)->fi()
->orderBy($orderBy)
->page($pager)
->fetchAll('id');
@@ -142,14 +140,12 @@ class webhookModel extends model
/**
* Create a webhook.
*
* @param string $type
* @access public
* @return bool
*/
public function create($type)
public function create()
{
$webhook = fixer::input('post')
->add('type', $type)
->add('createdBy', $this->app->user->account)
->add('createdDate', helper::now())
->join('products', ',')
@@ -241,6 +237,7 @@ class webhookModel extends model
*/
public function buildData($objectType, $objectID, $actionType, $actionID, $webhook)
{
/* Validate data. */
if(!isset($this->lang->action->label)) $this->loadModel('action');
if(!isset($this->lang->action->label->$actionType)) return false;
if(empty($this->config->objectTables[$objectType])) return false;
@@ -262,69 +259,43 @@ class webhookModel extends model
$object = $this->dao->select('*')->from($this->config->objectTables[$objectType])->where('id')->eq($objectID)->fetch();
$field = $this->config->action->objectNameFields[$objectType];
$text = $object->$field;
$host = common::getSysURL();
$viewLink = $this->getViewLink($objectType, $objectID);
$title = $this->app->user->realname . $this->lang->action->label->$actionType . $this->lang->action->objectTypes[$objectType];
$text = $title . ' ' . "[#{$objectID}::{$object->$field}](" . $host . $viewLink . ")";
$mobile = '';
$email = '';
if($actionType == 'assigned')
{
$assignedTo = $object->assignedTo;
foreach($users as $user)
{
if($user->account == $object->assignedTo)
{
$assignedTo = $user->realname;
$mobile = $user->mobile;
$email = $user->email;
break;
}
}
$text .= ' ' . $this->lang->webhook->assigned . ' ' . $assignedTo;
}
$action->text = $text;
$data = new stdclass();
if($webhook->type == 'dingding')
{
$title = $this->app->user->realname . $this->lang->action->label->$actionType . $this->lang->action->objectTypes[$objectType];
$mobile = '';
if($actionType == 'assigned')
{
$assignedTo = $object->assignedTo;
foreach($users as $user)
{
if($user->account == $object->assignedTo)
{
$assignedTo = $user->realname;
$mobile = $user->mobile;
break;
}
}
if($mobile) $title .= ' ' . "[#{$objectID}::{$title}](" . $host . $viewLink . ")";
$title .= ' ' . $this->lang->webhook->assigned . ' ' . $assignedTo;
}
if($mobile)
{
$at = new stdclass();
$at->atMobiles = array($mobile);
$at->isAtAll = false;
$data->msgtype = 'text';
$data->text = array('content' => $title);
$data->at = $at;
}
else
{
$link = new stdclass();
$link->text = "[#{$objectID}::{$text}]";
$link->title = $title;
$link->picUrl = '';
$link->messageUrl = $host . $viewLink;
$data->msgtype = 'link';
$data->link = $link;
}
return helper::jsonEncode($data);
$data = $this->getDingdingData($title, $text, $mobile);
}
foreach(explode(',', $webhook->params) as $param)
elseif($webhook->type == 'bearychat')
{
if($param == 'text')
{
$data->text = $this->app->user->realname . $this->lang->action->label->$actionType . $this->lang->action->objectTypes[$objectType] . ' ' . "[#{$objectID}::{$text}](" . $host . $viewLink . ")";
if($actionType == 'assigned') $data->text .= ' ' . $this->lang->webhook->assigned . ' ' . zget($users, $object->assignedTo);
}
else
{
$data->$param = $action->$param;
}
$data = $this->getBearychatData($text, $mobile, $email, $objectType, $objectID);
}
else
{
$data = new stdclass();
foreach(explode(',', $webhook->params) as $param) $data->$param = $action->$param;
}
$data = $this->getFiles($data, $objectType, $objectID);
return helper::jsonEncode($data);
}
@@ -353,16 +324,58 @@ class webhookModel extends model
}
/**
* Get files.
* Get hook data for dingding.
*
* @param object $data
* @param string $title
* @param string $text
* @param string $mobile
* @access public
* @return object
*/
public function getDingdingData($title, $text, $mobile)
{
$markdown = new stdclass();
$markdown->title = $title;
$markdown->text = $text;
$data = new stdclass();
if($mobile)
{
$at = new stdclass();
$at->atMobiles = array($mobile);
$at->isAtAll = false;
$data->msgtype = 'text';
$data->text = array('content' => $text);
$data->at = $at;
}
else
{
$data->msgtype = 'markdown';
$data->markdown = $markdown;
}
return $data;
}
/**
* Get hook data for bearychat.
*
* @param string $text
* @param string $mobile
* @param string $email
* @param string $objectType
* @param int $objectID
* @access public
* @return object
*/
public function getFiles($data, $objectType, $objectID)
public function getBearychatData($text, $mobile, $email, $objectType, $objectID)
{
$data = new stdclass();
$data->text = $text;
$data->markdown = 'true';
$data->user = $mobile ? $mobile : ($email ? $email : $this->app->user->account);
if(!empty($_FILES['files']['name'][0]))
{
$this->loadModel('file');
@@ -378,6 +391,7 @@ class webhookModel extends model
}
}
}
return $data;
}
+5 -5
View File
@@ -12,23 +12,23 @@
?>
<?php include 'header.html.php';?>
<?php js::set('confirmDelete', $lang->webhook->confirmDelete);?>
<?php js::set('type', $type);?>
<form id='ajaxForm' method='post'>
<table id='webhookList' class='table table-condensed table-hover table-striped table-fixed'>
<thead>
<tr>
<?php $vars = "type=$type&orderBy=%s&recTotal={$pager->recTotal}&recPerPage={$pager->recPerPage}&pageID={$pager->pageID}";?>
<?php $vars = "orderBy=%s&recTotal={$pager->recTotal}&recPerPage={$pager->recPerPage}&pageID={$pager->pageID}";?>
<th class='w-60px'><?php common::printOrderLink('id', $orderBy, $vars, $lang->webhook->id);?></th>
<th class='w-100px'><?php common::printOrderLink('type', $orderBy, $vars, $lang->webhook->type);?></th>
<th><?php common::printOrderLink('name', $orderBy, $vars, $lang->webhook->name);?></th>
<th><?php common::printOrderLink('url', $orderBy, $vars, $lang->webhook->url);?></th>
<th class='w-70px'><?php echo $lang->actions;?></th>
</tr>
</thead>
<tbody>
<?php $index = 1;?>
<?php foreach($webhooks as $id => $webhook):?>
<tr>
<td class='text-center'><?php echo $index++;?></td>
<td class='text-center'><?php echo $id;?></td>
<td><?php echo zget($lang->webhook->typeList, $webhook->type);?></td>
<td class='text' title='<?php echo $webhook->name;?>'><?php echo $webhook->name;?></td>
<td class='text' title='<?php echo $webhook->url;?>'><?php echo $webhook->url;?></td>
<td class='text-right'>
@@ -47,7 +47,7 @@
</tbody>
<tfoot>
<tr>
<td colspan='4'><?php $pager->show();?></td>
<td colspan='5'><?php $pager->show();?></td>
</tr>
</tfoot>
</table>
+12 -10
View File
@@ -12,6 +12,7 @@
?>
<?php include '../../common/view/header.html.php';?>
<?php include '../../common/view/form.html.php';?>
<?php js::set('urlNote', $lang->webhook->note->typeList);?>
<div class='container mw-1400px'>
<div id="titlebar">
<div class="heading">
@@ -22,27 +23,30 @@
<form id='webhookForm' method='post' class='ajaxForm'>
<table class='table table-form'>
<tr>
<th class='w-80px'><?php echo $lang->webhook->name;?></th>
<td class='w-p50'><?php echo html::input('name', '', "class='form-control'");?></td>
<th class='w-80px'><?php echo $lang->webhook->type;?></th>
<td class='w-p50'><?php echo html::select('type', $lang->webhook->typeList, 'default', "class='form-control'");?></td>
<td></td>
</tr>
<tr>
<th><?php echo $lang->webhook->name;?></th>
<td><?php echo html::input('name', '', "class='form-control'");?></td>
<td></td>
</tr>
<tr>
<th><?php echo $lang->webhook->url;?></th>
<td><?php echo html::input('url', '', "class='form-control'");?></td>
<td><?php if($type == 'dingding') echo $lang->webhook->note->dingding;?></td>
<td id='urlNote'><?php echo $lang->webhook->note->typeList['default'];?></td>
</tr>
<?php if($type != 'dingding'):?>
<tr>
<tr id='contentTypeTR'>
<th><?php echo $lang->webhook->contentType;?></th>
<td><?php echo html::select('contentType', $config->webhook->contentTypes, '', "class='form-control'");?></td>
<td></td>
</tr>
<tr>
<tr id='sendTypeTR'>
<th><?php echo $lang->webhook->sendType;?></th>
<td><?php echo html::select('sendType', $lang->webhook->sendTypeList, '', "class='form-control'");?></td>
<td><?php echo $lang->webhook->note->async;?></td>
</tr>
<?php endif;?>
<tr>
<th><?php echo $lang->webhook->product;?></th>
<td><?php echo html::select('products[]', $products, '', "class='form-control chosen' multiple");?></td>
@@ -53,12 +57,10 @@
<td><?php echo html::select('projects[]', $projects, '', "class='form-control chosen' multiple");?></td>
<td><?php echo $lang->webhook->note->project;?></td>
</tr>
<?php if($type != 'dingding'):?>
<tr>
<tr id='paramsTR'>
<th><?php echo $lang->webhook->params;?></th>
<td class='labelWidth' colspan='2'><?php echo html::checkbox('params', $lang->webhook->paramsList, 'text');?></td>
</tr>
<?php endif;?>
<tr>
<th><?php echo $lang->webhook->action;?></th>
<td colspan='2'>
+2 -2
View File
@@ -29,7 +29,7 @@
<tr>
<th><?php echo $lang->webhook->url;?></th>
<td><?php echo html::input('url', $webhook->url, "class='form-control'");?></td>
<td><?php if($webhook->type == 'dingding') echo $lang->webhook->note->dingding;?></td>
<td><?php echo zget($lang->webhook->note->typeList, $webhook->type);?></td>
</tr>
<?php if($webhook->type != 'dingding'):?>
<tr>
@@ -53,7 +53,7 @@
<td><?php echo html::select('projects[]', $projects, $webhook->projects, "class='form-control chosen' multiple");?></td>
<td><?php echo $lang->webhook->note->project;?></td>
</tr>
<?php if($webhook->type != 'dingding'):?>
<?php if(strpos(',bearychat,dingding,', ",$webhook->type,") === false):?>
<tr>
<th><?php echo $lang->webhook->params;?></th>
<td class='labelWidth' colspan='2'><?php echo html::checkbox('params', $lang->webhook->paramsList, $webhook->params);?></td>
+2 -4
View File
@@ -15,16 +15,14 @@
<ul class='nav'>
<?php
echo "<li id='entry'>"; common::printLink('entry', 'browse', '', $lang->webhook->entry); echo '</li>';
echo "<li id='webhook'>"; common::printLink('webhook', 'browse', 'type=default', $lang->webhook->common); echo '</li>';
echo "<li id='dingding'>"; common::printLink('webhook', 'browse', 'type=dingding', $lang->webhook->dingding); echo '</li>';
echo "<li id='webhook' class='active'>"; common::printLink('webhook', 'browse', '', $lang->webhook->common); echo '</li>';
?>
</ul>
<div class='actions'>
<div class='btn-group'>
<div class='btn-group' id='createActionMenu'>
<?php common::printIcon('webhook', 'create', "type=$type", '', 'button', '', '', 'btn-primary');?>
<?php common::printIcon('webhook', 'create', '', '', 'button', '', '', 'btn-primary');?>
</div>
</div>
</div>
</div>
<script>$('.nav li a[href*=<?php echo $type;?>]').parent().addClass('active');</script>
+1 -1
View File
@@ -13,7 +13,7 @@
<?php include '../../common/view/header.html.php';?>
<div id="titlebar">
<div class="heading">
<strong><?php echo html::a(inlink('browse', "type={$webhook->type}"), $webhook->type == 'dingding' ? $lang->webhook->dingding : $lang->webhook->common);?></strong>
<strong><?php echo html::a(inlink('browse'), $lang->webhook->common);?></strong>
<small class="text-muted"> <?php echo $webhook->name;?> </small>
<small class="text-muted"> <?php echo $lang->webhook->log;?> <i class="icon-file-text-o"></i></small>
</div>