Files
p_ysk/app/chat/controller/CommonPhraseController.php
2025-11-18 13:38:05 +08:00

75 lines
2.1 KiB
PHP

<?php
namespace app\chat\controller;
use app\chat\model\ChatCommonPhrase;
use app\common\controller\ApiController;
use support\Request;
use support\Response;
use support\think\Db;
class CommonPhraseController extends ApiController
{
/**
* 获取常用语列表
*/
public function index(Request $request): Response
{
return $this->success(Db::name('chat_common_phrase')->where('user_id', $this->uid)
->order('sort', 'desc')
->field(['id', 'content', 'sort', 'created_time'])
->select());
}
/**
* 添加常用语
*/
public function store(Request $request): Response
{
$content = $request->post('content', '');
$sort = $request->post('sort', 0);
if (empty($content) || mb_strlen($content) > 500) {
return $this->error('常用语内容不能为空且长度≤500字');
}
// 有重复的内容不给添加
$msg = Db::name('chat_common_phrase')->where(['user_id' => $this->uid, 'content' => $content])->find();
if($msg) {
return $this->error('此内容已经存在,不能重复添加');
}
$res = Db::name('chat_common_phrase')->insert([
'user_id' => $this->uid,
'content' => $content,
'sort' => $sort,
'created_time' => d(),
]);
if($res) {
return $this->success();
}else {
return $this->error();
}
}
/**
* 删除常用语
*/
public function destroy(Request $request): Response
{
$id = $request->post('id');
$phrase = Db::name('chat_common_phrase')->where([
'user_id' => $this->uid,
'id' => $id,
])->find();
if (!$phrase) {
return $this->error('常用语不存在');
}
$phrase_del = Db::name('chat_common_phrase')->where([
'user_id' => $this->uid,
'id' => $id,
])->delete();
if($phrase_del) {
return $this->success();
}else {
return $this->error();
}
}
}