feat: 添加问答分页/详情/新增/更新/设置排序值/删除接口
This commit is contained in:
154
app/admin/controller/v1/Faq.php
Normal file
154
app/admin/controller/v1/Faq.php
Normal file
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\controller\v1;
|
||||
|
||||
use app\admin\model\v1\FaqModel;
|
||||
use app\admin\validate\v1\FaqValidate;
|
||||
|
||||
/**
|
||||
* 常见问题控制器
|
||||
*/
|
||||
class Faq
|
||||
{
|
||||
// 分页
|
||||
public function index()
|
||||
{
|
||||
$param = request()->param([
|
||||
'question',
|
||||
'created_at',
|
||||
'page/d' => 1,
|
||||
'size/d' => 10
|
||||
]);
|
||||
|
||||
$faqs = FaqModel::field([
|
||||
'id',
|
||||
'image',
|
||||
'question',
|
||||
'recommend',
|
||||
'sort',
|
||||
'created_at'
|
||||
])
|
||||
->withSearch(['question', 'created_at'], [
|
||||
'question' => $param['question'] ?? null,
|
||||
'created_at' => !empty($param['created_at']) ? explode(',', $param['created_at']) : null
|
||||
])
|
||||
->language(request()->lang_id)
|
||||
->order(['sort' => 'asc', 'id' => 'desc'])
|
||||
->paginate([
|
||||
'list_rows' => $param['size'],
|
||||
'page' => $param['page'],
|
||||
]);
|
||||
|
||||
return success('获取成功', $faqs);
|
||||
}
|
||||
|
||||
// 详情
|
||||
public function read()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
|
||||
$faq = FaqModel::withoutField([
|
||||
'language_id',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
'deleted_at',
|
||||
])
|
||||
->bypk($id)
|
||||
->find();
|
||||
if (empty($faq)) {
|
||||
return error('数据不存在');
|
||||
}
|
||||
|
||||
return success('获取成功', $faq);
|
||||
}
|
||||
|
||||
// 新增
|
||||
public function save()
|
||||
{
|
||||
$post = request()->post([
|
||||
'question',
|
||||
'image',
|
||||
'recommend',
|
||||
'sort',
|
||||
'answer'
|
||||
]);
|
||||
$data = array_merge($post, ['language_id' => request()->lang_id]);
|
||||
|
||||
$validate = new FaqValidate;
|
||||
if (!$validate->scene('add')->check($data)) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$faq = FaqModel::create($data);
|
||||
if ($faq->isEmpty()) {
|
||||
return error('操作失败');
|
||||
}
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
// 修改
|
||||
public function update()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
$put = request()->put([
|
||||
'question',
|
||||
'image',
|
||||
'recommend',
|
||||
'sort',
|
||||
'answer'
|
||||
]);
|
||||
|
||||
$validate = new FaqValidate;
|
||||
if (!$validate->scene('edit')->check(array_merge($put, ['id' => $id]))) {
|
||||
return error($validate->getError());
|
||||
}
|
||||
|
||||
$faq = FaqModel::bypk($id)->find();
|
||||
if (empty($faq)) {
|
||||
return error('请确认操作对象是否存在');
|
||||
}
|
||||
|
||||
if (!$faq->save($put)) {
|
||||
return error('操作失败');
|
||||
}
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
// 设置排序值
|
||||
public function sort()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
$sort = request()->post('sort');
|
||||
|
||||
$faq = FaqModel::bypk($id)->find();
|
||||
if (empty($faq)) {
|
||||
return error('请确认操作对象是否存在');
|
||||
}
|
||||
|
||||
if ($faq->sort != $sort) {
|
||||
$faq->sort = $sort;
|
||||
if (!$faq->save()) {
|
||||
return error('操作失败');
|
||||
}
|
||||
}
|
||||
return success('操作成功');
|
||||
}
|
||||
|
||||
// 删除
|
||||
public function delete()
|
||||
{
|
||||
$id = request()->param('id');
|
||||
|
||||
$faq = FaqModel::bypk($id)->find();
|
||||
if (empty($faq)) {
|
||||
return error('请确认操作对象是否存在');
|
||||
}
|
||||
|
||||
if (!$faq->delete()) {
|
||||
return error('操作失败');
|
||||
}
|
||||
|
||||
return success('操作成功');
|
||||
}
|
||||
}
|
||||
48
app/admin/model/v1/FaqModel.php
Normal file
48
app/admin/model/v1/FaqModel.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\model\v1;
|
||||
|
||||
use app\common\model\FaqBaseModel;
|
||||
use think\model\concern\SoftDelete;
|
||||
|
||||
/**
|
||||
* @mixin \think\Model
|
||||
*/
|
||||
class FaqModel extends FaqBaseModel
|
||||
{
|
||||
// 启用软删除
|
||||
use SoftDelete;
|
||||
// 软删除字段
|
||||
protected $deleteTime = 'deleted_at';
|
||||
|
||||
// 根据语言查询
|
||||
public function scopeLanguage($query, $value)
|
||||
{
|
||||
$query->where('language_id', '=', $value);
|
||||
}
|
||||
|
||||
// 根据问题搜索
|
||||
public function searchQuestionAttr($query, $value, $data)
|
||||
{
|
||||
if (is_null($value)) {
|
||||
return;
|
||||
}
|
||||
$query->where('question', 'like', "%$value%");
|
||||
}
|
||||
|
||||
// 根据添加时间搜索
|
||||
public function searchCreatedAtAttr($query, $value, $data)
|
||||
{
|
||||
if (is_null($value)) {
|
||||
return;
|
||||
}
|
||||
if (is_array($value)) {
|
||||
if (count($value) > 1) {
|
||||
$query->whereBetweenTime('created_at', $value[0], $value[1]);
|
||||
} else {
|
||||
$query->whereTime('created_at', '>=', $value[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -498,6 +498,27 @@ Route::group('v1', function () {
|
||||
});
|
||||
});
|
||||
|
||||
// 问答管理
|
||||
Route::group('faq', function() {
|
||||
// 问答分页
|
||||
Route::get('index', 'Faq/index');
|
||||
|
||||
// 问答详情
|
||||
Route::get('read/:id', 'Faq/read');
|
||||
|
||||
// 问答新增
|
||||
Route::post('save', 'Faq/save');
|
||||
|
||||
// 问答更新
|
||||
Route::put('update/:id', 'Faq/update');
|
||||
|
||||
// 设置问答排序值
|
||||
Route::post('sort/:id', 'Faq/sort');
|
||||
|
||||
// 问答删除
|
||||
Route::delete('delete/:id', 'Faq/delete');
|
||||
});
|
||||
|
||||
// 配置项列表
|
||||
Route::group('config', function() {
|
||||
// 配置分组
|
||||
|
||||
57
app/admin/validate/v1/FaqValidate.php
Normal file
57
app/admin/validate/v1/FaqValidate.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\validate\v1;
|
||||
|
||||
use think\Validate;
|
||||
|
||||
class FaqValidate extends Validate
|
||||
{
|
||||
/**
|
||||
* 定义验证规则
|
||||
* 格式:'字段名' => ['规则1','规则2'...]
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $rule = [
|
||||
'id' => 'require|integer',
|
||||
'language_id' => 'require|integer',
|
||||
'image' => 'max:125',
|
||||
'question' => 'require|max:255',
|
||||
'recommend' => 'integer|in:0,1',
|
||||
'sort' => 'integer'
|
||||
|
||||
];
|
||||
|
||||
/**
|
||||
* 定义错误信息
|
||||
* 格式:'字段名.规则名' => '错误信息'
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $message = [
|
||||
'id.require' => 'ID不能为空',
|
||||
'id.integer' => 'ID必须是整数',
|
||||
'language_id.require' => '语言ID不能为空',
|
||||
'language_id.integer' => '语言ID必须是整数',
|
||||
'language_id.mustOmit' => '语言ID不能修改',
|
||||
'image.max' => '图片不能超过125个字符',
|
||||
'question.require' => '问答标题不能为空',
|
||||
'question.max' => '问答标题不能超过255个字符',
|
||||
'recommend.integer' => '推荐值必须是整数',
|
||||
'recommend.in' => '推荐值只能是0或1',
|
||||
'sort.integer' => '排序值必须是整数'
|
||||
];
|
||||
|
||||
// 新增场景
|
||||
public function sceneAdd()
|
||||
{
|
||||
return $this->remove('id', 'require|integer');
|
||||
}
|
||||
|
||||
// 更新场景
|
||||
public function sceneEdit()
|
||||
{
|
||||
return $this->remove('language_id', 'require|integer')->append('language_id', 'mustOmit');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user