229 lines
6.1 KiB
PHP
Executable File
229 lines
6.1 KiB
PHP
Executable File
<?php
|
|
|
|
namespace app\admin\controller;
|
|
|
|
use think\Db;
|
|
use think\Request;
|
|
|
|
/**
|
|
* 快捷回复管理控制器
|
|
* Class ChatQuickReply
|
|
* @package app\admin\controller
|
|
*/
|
|
class ChatQuickReply extends Common
|
|
{
|
|
/**
|
|
* 快捷回复列表
|
|
*/
|
|
public function index()
|
|
{
|
|
$get = Request::instance()->get();
|
|
$this->assign('get', $get);
|
|
|
|
$category = Request::instance()->get('category');
|
|
$status = Request::instance()->get('status');
|
|
|
|
$where = [];
|
|
if ($category) {
|
|
$where['category'] = $category;
|
|
}
|
|
if ($status !== null && $status !== '') {
|
|
$where['status'] = (int)$status;
|
|
}
|
|
|
|
$list = Db::name('chat_quick_reply')
|
|
->where($where)
|
|
->order('sort asc, id desc')
|
|
->paginate(15, false, ['query' => $get]);
|
|
|
|
// 获取分类列表
|
|
$categories = Db::name('chat_quick_reply')
|
|
->whereNotNull('category')
|
|
->where('category', '<>', '')
|
|
->group('category')
|
|
->column('category');
|
|
|
|
$this->assign('list', $list);
|
|
$this->assign('categories', $categories);
|
|
|
|
return $this->fetch();
|
|
}
|
|
|
|
/**
|
|
* 添加快捷回复
|
|
*/
|
|
public function add()
|
|
{
|
|
if (Request::instance()->isPost()) {
|
|
$data = Request::instance()->post();
|
|
|
|
// 验证
|
|
if (empty($data['title'])) {
|
|
$this->error('标题不能为空');
|
|
}
|
|
if (empty($data['content'])) {
|
|
$this->error('内容不能为空');
|
|
}
|
|
|
|
$insertData = [
|
|
'category' => $data['category'] ?? null,
|
|
'title' => $data['title'],
|
|
'content' => $data['content'],
|
|
'sort' => (int)($data['sort'] ?? 0),
|
|
'status' => (int)($data['status'] ?? 1),
|
|
'create_time' => time(),
|
|
'update_time' => time(),
|
|
];
|
|
|
|
$result = Db::name('chat_quick_reply')->insert($insertData);
|
|
|
|
if ($result) {
|
|
insertAdminLog('添加快捷回复', '标题: ' . $data['title']);
|
|
$this->success('添加成功', '/chat_quick_reply/index');
|
|
} else {
|
|
$this->error('添加失败');
|
|
}
|
|
}
|
|
|
|
// 获取分类列表
|
|
$categories = Db::name('chat_quick_reply')
|
|
->whereNotNull('category')
|
|
->where('category', '<>', '')
|
|
->group('category')
|
|
->column('category');
|
|
|
|
$this->assign('categories', $categories);
|
|
|
|
return $this->fetch();
|
|
}
|
|
|
|
/**
|
|
* 编辑快捷回复
|
|
*/
|
|
public function edit()
|
|
{
|
|
$id = Request::instance()->param('id');
|
|
|
|
if (Request::instance()->isPost()) {
|
|
$data = Request::instance()->post();
|
|
|
|
// 验证
|
|
if (empty($data['title'])) {
|
|
$this->error('标题不能为空');
|
|
}
|
|
if (empty($data['content'])) {
|
|
$this->error('内容不能为空');
|
|
}
|
|
|
|
$updateData = [
|
|
'category' => $data['category'] ?? null,
|
|
'title' => $data['title'],
|
|
'content' => $data['content'],
|
|
'sort' => (int)($data['sort'] ?? 0),
|
|
'status' => (int)($data['status'] ?? 1),
|
|
'update_time' => time(),
|
|
];
|
|
|
|
$result = Db::name('chat_quick_reply')->where('id', $id)->update($updateData);
|
|
|
|
if ($result !== false) {
|
|
insertAdminLog('编辑快捷回复', 'ID: ' . $id);
|
|
$this->success('修改成功', '/chat_quick_reply/index');
|
|
} else {
|
|
$this->error('修改失败');
|
|
}
|
|
}
|
|
|
|
$info = Db::name('chat_quick_reply')->where('id', $id)->find();
|
|
if (!$info) {
|
|
$this->error('记录不存在');
|
|
}
|
|
|
|
// 获取分类列表
|
|
$categories = Db::name('chat_quick_reply')
|
|
->whereNotNull('category')
|
|
->where('category', '<>', '')
|
|
->group('category')
|
|
->column('category');
|
|
|
|
$this->assign('info', $info);
|
|
$this->assign('categories', $categories);
|
|
|
|
return $this->fetch('add');
|
|
}
|
|
|
|
/**
|
|
* 删除快捷回复
|
|
*/
|
|
public function del()
|
|
{
|
|
$id = Request::instance()->param('id');
|
|
|
|
if (!$id) {
|
|
$this->error('参数错误');
|
|
}
|
|
|
|
$info = Db::name('chat_quick_reply')->where('id', $id)->find();
|
|
if (!$info) {
|
|
$this->error('记录不存在');
|
|
}
|
|
|
|
$result = Db::name('chat_quick_reply')->where('id', $id)->delete();
|
|
|
|
if ($result) {
|
|
insertAdminLog('删除快捷回复', 'ID: ' . $id . ', 标题: ' . $info['title']);
|
|
$this->success('删除成功');
|
|
} else {
|
|
$this->error('删除失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 修改状态
|
|
*/
|
|
public function status()
|
|
{
|
|
$id = Request::instance()->post('id');
|
|
$status = Request::instance()->post('status');
|
|
|
|
if (!$id) {
|
|
return json(['code' => 1, 'msg' => '参数错误']);
|
|
}
|
|
|
|
$result = Db::name('chat_quick_reply')->where('id', $id)->update([
|
|
'status' => (int)$status,
|
|
'update_time' => time(),
|
|
]);
|
|
|
|
if ($result !== false) {
|
|
return json(['code' => 0, 'msg' => '操作成功']);
|
|
} else {
|
|
return json(['code' => 1, 'msg' => '操作失败']);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 修改排序
|
|
*/
|
|
public function sort()
|
|
{
|
|
$id = Request::instance()->post('id');
|
|
$sort = Request::instance()->post('sort');
|
|
|
|
if (!$id) {
|
|
return json(['code' => 1, 'msg' => '参数错误']);
|
|
}
|
|
|
|
$result = Db::name('chat_quick_reply')->where('id', $id)->update([
|
|
'sort' => (int)$sort,
|
|
'update_time' => time(),
|
|
]);
|
|
|
|
if ($result !== false) {
|
|
return json(['code' => 0, 'msg' => '操作成功']);
|
|
} else {
|
|
return json(['code' => 1, 'msg' => '操作失败']);
|
|
}
|
|
}
|
|
}
|