This commit is contained in:
2025-11-18 13:38:05 +08:00
parent a3e5568f93
commit f9062837ab
7 changed files with 286 additions and 116 deletions

View File

@@ -14,8 +14,7 @@ class CommonPhraseController extends ApiController
*/
public function index(Request $request): Response
{
$uid = $request->get('uid');
return $this->success(Db::name('chat_common_phrase')->where('user_id', $uid)
return $this->success(Db::name('chat_common_phrase')->where('user_id', $this->uid)
->order('sort', 'desc')
->field(['id', 'content', 'sort', 'created_time'])
->select());
@@ -26,19 +25,18 @@ class CommonPhraseController extends ApiController
*/
public function store(Request $request): Response
{
$uid = $request->post('uid');
$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' => $uid, 'content' => $content])->find();
$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' => $uid,
'user_id' => $this->uid,
'content' => $content,
'sort' => $sort,
'created_time' => d(),
@@ -53,15 +51,24 @@ class CommonPhraseController extends ApiController
/**
* 删除常用语
*/
public function destroy(Request $request, int $id): Response
public function destroy(Request $request): Response
{
$uid = $request->uid;
$phrase = ChatCommonPhrase::where(['id' => $id, 'user_id' => $uid])->find();
$id = $request->post('id');
$phrase = Db::name('chat_common_phrase')->where([
'user_id' => $this->uid,
'id' => $id,
])->find();
if (!$phrase) {
return json(['code' => 404, 'msg' => '常用语不存在']);
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();
}
$phrase->delete();
return json(['code' => 200, 'msg' => '删除成功']);
}
}