优化tenant模式
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\admin\controller\Cms\Article;
|
||||
|
||||
use app\admin\BaseController;
|
||||
use think\exception\ValidateException;
|
||||
use think\facade\Db;
|
||||
use think\facade\Session;
|
||||
use think\response\Json;
|
||||
use think\db\exception\DbException;
|
||||
use app\model\Cms\ArticlesCategory;
|
||||
|
||||
class ArticleCategoryController extends BaseController
|
||||
{
|
||||
|
||||
/**
|
||||
* 获取文章分类列表接口
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getAllArticleCategories(string $keyword)
|
||||
{
|
||||
try {
|
||||
//判断keyword是否存在
|
||||
$keyword = $keyword ?? '';
|
||||
|
||||
// 获取文章分类列表
|
||||
$categories = ArticlesCategory::where('delete_time', null)
|
||||
->where('tid', $this->getTenantId())
|
||||
->where('status', 1)
|
||||
->where('name', 'like', '%' . $keyword . '%')
|
||||
->order('sort', 'desc')
|
||||
->order('id', 'asc')
|
||||
->select()
|
||||
->toArray(); // 转换为数组
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => $categories
|
||||
]);
|
||||
} catch (DbException $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => 'fail:' . $e->getMessage(),
|
||||
'data' => $e->getTraceAsString()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文章分类列表接口
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function getArticleCategories()
|
||||
{
|
||||
try {
|
||||
// 获取文章分类列表
|
||||
$categories = ArticlesCategory::where('delete_time', null)
|
||||
->where('tid', $this->getTenantId())
|
||||
->where('status', 1)
|
||||
->field('id,cid,name,image,sort')
|
||||
->order('sort', 'desc')
|
||||
->order('id', 'asc')
|
||||
->select()
|
||||
->toArray(); // 转换为数组
|
||||
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => 'success',
|
||||
'data' => $categories
|
||||
]);
|
||||
} catch (DbException $e) {
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => 'fail:' . $e->getMessage(),
|
||||
'data' => $e->getTraceAsString()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增文章分类
|
||||
* @return Json
|
||||
*/
|
||||
public function createCategory(): Json
|
||||
{
|
||||
try {
|
||||
$data = request()->only(['cid','name','image','desc','sort','status']);
|
||||
// 基础校验
|
||||
if (empty($data['name'])) {
|
||||
throw new ValidateException('分类名称不能为空');
|
||||
}
|
||||
$data['cid'] = isset($data['cid']) ? (int)$data['cid'] : 0;
|
||||
$data['sort'] = isset($data['sort']) ? (int)$data['sort'] : 0;
|
||||
$data['status']= isset($data['status']) ? (int)$data['status'] : 1;
|
||||
$data['tid'] = $this->getTenantId();
|
||||
$data['create_time'] = date('Y-m-d H:i:s');
|
||||
$data['update_time'] = $data['create_time'];
|
||||
|
||||
$id = ArticlesCategory::insertGetId($data);
|
||||
return json(['code'=>200,'msg'=>'success','data'=>['id'=>$id]]);
|
||||
} catch (ValidateException $ve) {
|
||||
// 记录失败日志
|
||||
$this->logFail('文章分类管理', '新增文章分类', $ve->getMessage());
|
||||
return json(['code'=>422,'msg'=>$ve->getMessage(),'data'=>null]);
|
||||
} catch (\Exception $e) {
|
||||
// 记录失败日志
|
||||
$this->logFail('文章分类管理', '新增文章分类', $e->getMessage());
|
||||
return json(['code'=>500,'msg'=>'fail:'.$e->getMessage(),'data'=>$e->getTraceAsString()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文章分类
|
||||
* @param int $id
|
||||
* @return Json
|
||||
*/
|
||||
public function editCategory(int $id): Json
|
||||
{
|
||||
try {
|
||||
$data = request()->only(['cid','name','image','desc','sort','status']);
|
||||
if (empty($data)) {
|
||||
return json(['code'=>400,'msg'=>'无更新数据','data'=>null]);
|
||||
}
|
||||
$data['update_time'] = date('Y-m-d H:i:s');
|
||||
$affected = ArticlesCategory::where('id',$id)->where('tid', $this->getTenantId())->update($data);
|
||||
if ($affected) {
|
||||
// 记录成功日志
|
||||
$this->logSuccess('文章分类管理', '更新文章分类', ['id' => $id]);
|
||||
return json(['code'=>200,'msg'=>'success','data'=>null]);
|
||||
}
|
||||
return json(['code'=>404,'msg'=>'not found','data'=>null]);
|
||||
} catch (\Exception $e) {
|
||||
// 记录失败日志
|
||||
$this->logFail('文章分类管理', '更新文章分类', $e->getMessage());
|
||||
return json(['code'=>500,'msg'=>'fail:'.$e->getMessage(),'data'=>$e->getTraceAsString()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除文章分类(软删除)
|
||||
* @param int $id
|
||||
* @return Json
|
||||
*/
|
||||
public function deleteCategory(int $id): Json
|
||||
{
|
||||
try {
|
||||
$affected = ArticlesCategory::where('id', $id)
|
||||
->where('tid', $this->getTenantId())
|
||||
->update(['delete_time' => date('Y-m-d H:i:s')]);
|
||||
|
||||
if ($affected) {
|
||||
// 记录成功日志
|
||||
$this->logSuccess('文章分类管理', '删除文章分类', ['id' => $id]);
|
||||
return json(['code' => 200, 'msg' => 'success', 'data' => null]);
|
||||
}
|
||||
return json(['code' => 404, 'msg' => 'not found', 'data' => null]);
|
||||
} catch (\Exception $e) {
|
||||
// 记录失败日志
|
||||
$this->logFail('文章分类管理', '删除文章分类', $e->getMessage());
|
||||
return json([
|
||||
'code' => 500,
|
||||
'msg' => 'fail:' . $e->getMessage(),
|
||||
'data' => $e->getTraceAsString()
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新分类状态
|
||||
* @param int $id
|
||||
* @return Json
|
||||
*/
|
||||
public function updateCategoryStatus(int $id): Json
|
||||
{
|
||||
try {
|
||||
$data = request()->only(['status']);
|
||||
if (empty($data)) {
|
||||
return json(['code'=>400,'msg'=>'无更新数据','data'=>null]);
|
||||
}
|
||||
$data['status'] = isset($data['status']) ? (int)$data['status'] : 1;
|
||||
$data['update_time'] = date('Y-m-d H:i:s');
|
||||
$affected = ArticlesCategory::where('id',$id)->where('tid', $this->getTenantId())->update($data);
|
||||
if ($affected) {
|
||||
// 记录成功日志
|
||||
$this->logSuccess('文章分类管理', '更新分类状态', ['id' => $id]);
|
||||
return json(['code'=>200,'msg'=>'success','data'=>null]);
|
||||
}
|
||||
return json(['code'=>404,'msg'=>'not found','data'=>null]);
|
||||
} catch (\Exception $e) {
|
||||
// 记录失败日志
|
||||
$this->logFail('文章分类管理', '更新分类状态', $e->getMessage());
|
||||
return json(['code'=>500,'msg'=>'fail:'.$e->getMessage(),'data'=>$e->getTraceAsString()]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user