chat_add
This commit is contained in:
67
app/chat/controller/CommonPhraseController.php
Normal file
67
app/chat/controller/CommonPhraseController.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?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
|
||||
{
|
||||
$uid = $request->get('uid');
|
||||
return $this->success(Db::name('chat_common_phrase')->where('user_id', $uid)
|
||||
->order('sort', 'desc')
|
||||
->field(['id', 'content', 'sort', 'created_time'])
|
||||
->select());
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加常用语
|
||||
*/
|
||||
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();
|
||||
if($msg) {
|
||||
return $this->error('此内容已经存在,不能重复添加');
|
||||
}
|
||||
$res = Db::name('chat_common_phrase')->insert([
|
||||
'user_id' => $uid,
|
||||
'content' => $content,
|
||||
'sort' => $sort,
|
||||
'created_time' => d(),
|
||||
]);
|
||||
if($res) {
|
||||
return $this->success();
|
||||
}else {
|
||||
return $this->error();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除常用语
|
||||
*/
|
||||
public function destroy(Request $request, int $id): Response
|
||||
{
|
||||
$uid = $request->uid;
|
||||
$phrase = ChatCommonPhrase::where(['id' => $id, 'user_id' => $uid])->find();
|
||||
if (!$phrase) {
|
||||
return json(['code' => 404, 'msg' => '常用语不存在']);
|
||||
}
|
||||
|
||||
$phrase->delete();
|
||||
return json(['code' => 200, 'msg' => '删除成功']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user