2024-09-19 11:32:39 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace service\modules\v1\controllers;
|
|
|
|
|
|
|
|
|
|
use common\core\BaseServiceController;
|
2024-12-19 19:20:27 +08:00
|
|
|
use common\models\oldDb\ReplayTemplate;
|
|
|
|
|
use common\models\oldDb\ReplayTemplateGroup;
|
2024-09-19 11:32:39 +08:00
|
|
|
use yii\base\Exception;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @doc-module 快捷功能
|
|
|
|
|
*/
|
|
|
|
|
class QuickController extends BaseServiceController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* @doc-name 快捷回复-模版列表
|
|
|
|
|
* @doc-param keyword string 关键字 / optional
|
|
|
|
|
* @doc-return string group_name 分组名
|
|
|
|
|
* @doc-return string group_name 分组名
|
|
|
|
|
* @doc-return mixed @Templates{id-int-id,group_id-int-分组Id,content-string-内容,sort-int-排序} 回复模板
|
|
|
|
|
*/
|
|
|
|
|
public function actionTemplateLists()
|
|
|
|
|
{
|
|
|
|
|
if (ReplayTemplateGroup::find()->where(['su_id'=> \Yii::$app->user->identity->getId()])->count() == 0) {
|
|
|
|
|
$info = new ReplayTemplateGroup();
|
|
|
|
|
$info->su_id = \Yii::$app->user->identity->getId();
|
|
|
|
|
$info->group_name = '默认分组';
|
|
|
|
|
$info->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$keyword = \Yii::$app->request->post('keyword');
|
|
|
|
|
$query = ReplayTemplateGroup::find()
|
|
|
|
|
->alias('group')
|
|
|
|
|
->select(['group.id', 'group.group_name', 'group.sort'])
|
|
|
|
|
->with([
|
|
|
|
|
'templates' => function ($query) {
|
|
|
|
|
$query
|
|
|
|
|
->select(['yii_replay_template.id', 'yii_replay_template.group_id', 'yii_replay_template.content', 'yii_replay_template.sort'])
|
|
|
|
|
->where(['yii_replay_template.su_id' => \Yii::$app->user->identity->getId()]);
|
|
|
|
|
}
|
|
|
|
|
])
|
|
|
|
|
->where([
|
|
|
|
|
'group.su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
])
|
|
|
|
|
->orderBy(['sort' => SORT_ASC]);
|
|
|
|
|
if ($keyword) {
|
|
|
|
|
$query->joinWith([
|
|
|
|
|
'templates' => function ($query) use ($keyword) {
|
|
|
|
|
$query->andFilterWhere(['like', 'content', $keyword]);
|
|
|
|
|
}
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
return $query->asArray()->all();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @doc-name 快捷回复-模版新增/修改
|
|
|
|
|
* @doc-desc 如果新增id则不填
|
|
|
|
|
* @doc-param string content 模板内容
|
|
|
|
|
* @doc-param int group_id 分组ID
|
|
|
|
|
*/
|
|
|
|
|
public function actionTemplateSave()
|
|
|
|
|
{
|
|
|
|
|
$post=\Yii::$app->request->post();
|
|
|
|
|
$this->requestValidate($post,[
|
|
|
|
|
[['content','group_id'],'required']
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($post['id']) {
|
|
|
|
|
$info = ReplayTemplate::find()
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
'id' => $post['id'],
|
|
|
|
|
'group_id' => $post['group_id'],
|
|
|
|
|
])
|
|
|
|
|
->one();
|
|
|
|
|
if (!$info) {
|
|
|
|
|
throw new Exception('模版不存在');
|
|
|
|
|
}
|
|
|
|
|
if (
|
|
|
|
|
ReplayTemplate::find()
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
'group_id' => $post['group_id'],
|
|
|
|
|
'content' => $post['content'],
|
|
|
|
|
])
|
|
|
|
|
->andWhere(['<>', 'id', $post['id']])
|
|
|
|
|
->exists()
|
|
|
|
|
) {
|
|
|
|
|
throw new Exception('模版重复');
|
|
|
|
|
}
|
|
|
|
|
$info->content = $post['content'];
|
|
|
|
|
$info->saveOrFail();
|
|
|
|
|
return ['修改成功'];
|
|
|
|
|
} else {
|
|
|
|
|
if (
|
|
|
|
|
ReplayTemplate::find()
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
'content' => $post['content'],
|
|
|
|
|
])
|
|
|
|
|
->exists()
|
|
|
|
|
)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception('模版重复');
|
|
|
|
|
}
|
|
|
|
|
$info = new ReplayTemplate();
|
|
|
|
|
$info->su_id = \Yii::$app->user->identity->getId();
|
|
|
|
|
$info->group_id = $post['group_id'];
|
|
|
|
|
$info->content = $post['content'];
|
|
|
|
|
$info->saveOrFail();
|
|
|
|
|
return ['新增成功'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @doc-name 快捷回复-模版删除
|
|
|
|
|
* @doc-param int id 模板ID
|
|
|
|
|
* @doc-param int group_id 分组ID
|
|
|
|
|
*/
|
|
|
|
|
public function actionTemplateDelete()
|
|
|
|
|
{
|
|
|
|
|
$id = \Yii::$app->request->post('id');
|
|
|
|
|
$group_id = \Yii::$app->request->post('group_id');
|
|
|
|
|
if (!$id || !$group_id) {
|
|
|
|
|
throw new Exception('请选择一个模版');
|
|
|
|
|
}
|
|
|
|
|
$info = ReplayTemplate::find()
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
'id' => $id,
|
|
|
|
|
'group_id' => $group_id,
|
|
|
|
|
])
|
|
|
|
|
->one();
|
|
|
|
|
if (!$info) {
|
|
|
|
|
throw new Exception('模版不存在');
|
|
|
|
|
}
|
|
|
|
|
$info->delete();
|
|
|
|
|
return ['删除成功'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @doc-name 快捷回复-模板排序更改
|
|
|
|
|
* @doc-param array data 分组[{id:1,sort:0}]
|
|
|
|
|
* @doc-param int group_id 分组ID
|
|
|
|
|
*/
|
|
|
|
|
public function actionTemplateSort()
|
|
|
|
|
{
|
|
|
|
|
$post = \Yii::$app->request->post();
|
|
|
|
|
|
|
|
|
|
$this->requestValidate($post,[
|
|
|
|
|
[['group_id','data'],'required']
|
|
|
|
|
]);
|
|
|
|
|
foreach ($post['data'] as $value) {
|
|
|
|
|
$info = ReplayTemplate::find()
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
'id' => $value['id'],
|
|
|
|
|
'group_id' => $post['group_id'],
|
|
|
|
|
])
|
|
|
|
|
->one();
|
|
|
|
|
$info->sort = $value['sort'];
|
|
|
|
|
$info->save();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @doc-name 快捷回复-分组列表
|
|
|
|
|
* @doc-author hyl
|
|
|
|
|
* @doc-return int id 分组id
|
|
|
|
|
* @doc-return string group_name 名称
|
|
|
|
|
* @doc-return int sort 排序
|
|
|
|
|
*/
|
|
|
|
|
public function actionGroupLists()
|
|
|
|
|
{
|
|
|
|
|
if (ReplayTemplateGroup::find()->where(['su_id' => \Yii::$app->user->identity->getId(),])->count() == 0)
|
|
|
|
|
{
|
|
|
|
|
$info = new ReplayTemplateGroup();
|
|
|
|
|
$info->su_id = \Yii::$app->user->identity->getId();
|
|
|
|
|
$info->group_name = '默认分组';
|
|
|
|
|
$info->save();
|
|
|
|
|
}
|
|
|
|
|
$lists = ReplayTemplateGroup::find()
|
|
|
|
|
->select(['id', 'group_name', 'sort'])
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
])
|
|
|
|
|
->orderBy(['sort' => SORT_ASC])
|
|
|
|
|
->all();
|
|
|
|
|
return $lists;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @doc-name 快捷回复-分组新增/修改
|
|
|
|
|
* @doc-desc 分组id如果新增则不传
|
|
|
|
|
* @doc-param int id 分组id / optional
|
|
|
|
|
* @doc-param string group_name 分组名
|
|
|
|
|
*/
|
|
|
|
|
public function actionGroupSave()
|
|
|
|
|
{
|
|
|
|
|
$post = \Yii::$app->request->post();
|
|
|
|
|
|
|
|
|
|
$this->requestValidate($post,[
|
|
|
|
|
['group_name','required']
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
if ($post['id']) {
|
|
|
|
|
$info = ReplayTemplateGroup::find()
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
'id' => $post['id'],
|
|
|
|
|
])->one();
|
|
|
|
|
|
|
|
|
|
if (!$info) throw new Exception('分组名不存在');
|
|
|
|
|
|
|
|
|
|
if ($post['group_name'] == '默认分组') throw new Exception('默认分组名不可修改');
|
|
|
|
|
|
|
|
|
|
if (
|
|
|
|
|
ReplayTemplateGroup::find()
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
'group_name' => $post['group_name'],
|
|
|
|
|
])
|
|
|
|
|
->andWhere(['<>', 'id', $post['id']])
|
|
|
|
|
->exists()
|
|
|
|
|
) throw new Exception('分组名重复');
|
|
|
|
|
|
|
|
|
|
$info->group_name = $post['group_name'];
|
|
|
|
|
$info->saveOrFail();
|
|
|
|
|
return ['修改成功'];
|
|
|
|
|
} else {
|
|
|
|
|
if (
|
|
|
|
|
ReplayTemplateGroup::find()
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
'group_name' => $post['group_name'],
|
|
|
|
|
])
|
|
|
|
|
->exists()
|
|
|
|
|
) {
|
|
|
|
|
throw new Exception('分组名重复');
|
|
|
|
|
}
|
|
|
|
|
$info = new ReplayTemplateGroup();
|
|
|
|
|
$info->su_id = \Yii::$app->user->identity->getId();
|
|
|
|
|
$info->group_name = $post['group_name'];
|
|
|
|
|
$info->saveOrFail();
|
|
|
|
|
return ['新增成功'];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @doc-name 快捷回复-分组删除
|
|
|
|
|
* @doc-param id int 分组id
|
|
|
|
|
*/
|
|
|
|
|
public function actionGroupDelete()
|
|
|
|
|
{
|
|
|
|
|
$id = \Yii::$app->request->post('id');
|
|
|
|
|
if (!$id) {
|
|
|
|
|
throw new Exception('请选择一个分组');
|
|
|
|
|
}
|
|
|
|
|
$info = ReplayTemplateGroup::find()
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
'id' => $id,
|
|
|
|
|
])
|
|
|
|
|
->one();
|
|
|
|
|
if (!$info) {
|
|
|
|
|
throw new Exception('分组名不存在');
|
|
|
|
|
}
|
|
|
|
|
$info->delete();
|
|
|
|
|
return ['删除成功'];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @doc-name 快捷回复-分组排序更改
|
|
|
|
|
* @doc-param array data 分组[{id:1,sort:0}]
|
|
|
|
|
*/
|
|
|
|
|
public function actionGroupSort()
|
|
|
|
|
{
|
|
|
|
|
$data = \Yii::$app->request->post('data');
|
|
|
|
|
if (!$data) throw new \yii\db\Exception('data不能为空');
|
|
|
|
|
|
|
|
|
|
foreach ($data as $value) {
|
|
|
|
|
$info = ReplayTemplateGroup::find()
|
|
|
|
|
->where([
|
|
|
|
|
'su_id' => \Yii::$app->user->identity->getId(),
|
|
|
|
|
'id' => $value['id'],
|
|
|
|
|
])
|
|
|
|
|
->one();
|
|
|
|
|
$info->sort = $value['sort'];
|
|
|
|
|
$info->save();
|
|
|
|
|
}
|
|
|
|
|
return ['更新成功'];
|
|
|
|
|
}
|
|
|
|
|
}
|