pull/1/head
parent
8c41c84c32
commit
861aa9519d
@ -0,0 +1,58 @@ |
||||
<?php |
||||
|
||||
namespace app\common\model; |
||||
|
||||
use cores\BaseModel; |
||||
use think\db\exception\DataNotFoundException; |
||||
use think\db\exception\DbException; |
||||
use think\db\exception\ModelNotFoundException; |
||||
use think\model\relation\HasOne; |
||||
|
||||
class ServerCategory extends BaseModel |
||||
{ |
||||
// 定义表名 |
||||
protected $name = 'server_category'; |
||||
|
||||
// 定义主键 |
||||
protected $pk = 'category_id'; |
||||
|
||||
/** |
||||
* 分类图片 |
||||
* @return HasOne |
||||
*/ |
||||
public function image(): HasOne |
||||
{ |
||||
return $this->hasOne('UploadFile', 'file_id', 'image_id'); |
||||
} |
||||
|
||||
/** |
||||
* @notes:分类详情 |
||||
* @param $where |
||||
* @param array $with |
||||
* @return ServerCategory|array|null |
||||
* @author: wanghousheng |
||||
*/ |
||||
public static function detail($where, array $with = []) |
||||
{ |
||||
return static::get($where, $with); |
||||
} |
||||
|
||||
/** |
||||
* @notes:获取全部记录 |
||||
* @param array $where |
||||
* @return array |
||||
* @throws DataNotFoundException |
||||
* @throws DbException |
||||
* @throws ModelNotFoundException |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function getList(array $where = []): array |
||||
{ |
||||
$where = $this->setQueryDefaultValue($where); |
||||
return $this->with(['image']) |
||||
->where($where) |
||||
->order(['sort', 'create_time']) |
||||
->select() |
||||
->toArray(); |
||||
} |
||||
} |
@ -0,0 +1,83 @@ |
||||
<?php |
||||
|
||||
namespace app\store\controller; |
||||
|
||||
use app\store\model\ServerCategory; |
||||
use think\db\exception\DataNotFoundException; |
||||
use think\db\exception\DbException; |
||||
use think\db\exception\ModelNotFoundException; |
||||
use think\response\Json; |
||||
|
||||
class Server extends Controller |
||||
{ |
||||
/** |
||||
* @notes:分类列表 10269 |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function categoryList(): Json |
||||
{ |
||||
$name = $this->request->post('name'); |
||||
$where = []; |
||||
if (!empty($name)) { |
||||
$where[] = ['name', 'like', `%$name%`]; |
||||
} |
||||
$model = new ServerCategory(); |
||||
try { |
||||
$list = $model->getList($where); |
||||
} catch (DataNotFoundException|ModelNotFoundException|DbException $e) { |
||||
return $this->renderError($e->getMessage() ?: '接口异常'); |
||||
} |
||||
return $this->renderSuccess(compact('list')); |
||||
} |
||||
|
||||
/** |
||||
* @notes:添加分类 |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function addCategory(): Json |
||||
{ |
||||
$name = $this->request->post('name'); |
||||
if (!$name) { |
||||
return $this->renderError('名称不能为空'); |
||||
} |
||||
$image_id = intval($this->request->post('image_id')); |
||||
if (!$image_id) { |
||||
return $this->renderError('图片不能为空'); |
||||
} |
||||
$status = intval($this->request->post('status')); |
||||
$sort = intval($this->request->post('sort')); |
||||
$model = new ServerCategory(); |
||||
if ($model->add(compact('image_id', 'name', 'status', 'sort'))) { |
||||
return $this->renderSuccess('添加成功'); |
||||
} |
||||
return $this->renderError($model->getError() ?: '添加失败'); |
||||
} |
||||
|
||||
/** |
||||
* @notes:编辑分类 |
||||
* @param int $categoryId |
||||
* @return Json |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function editCategory(int $categoryId) |
||||
{ |
||||
$name = $this->request->post('name'); |
||||
if (!$name) { |
||||
return $this->renderError('名称不能为空'); |
||||
} |
||||
$image_id = intval($this->request->post('image_id')); |
||||
if (!$image_id) { |
||||
return $this->renderError('图片不能为空'); |
||||
} |
||||
$status = intval($this->request->post('status')); |
||||
$sort = intval($this->request->post('sort')); |
||||
$category_id = $categoryId; |
||||
$model = new ServerCategory(); |
||||
if ($model->edit(compact('image_id', 'name', 'status', 'sort', 'category_id'))) { |
||||
return $this->renderSuccess('编辑成功'); |
||||
} |
||||
return $this->renderError($model->getError() ?: '编辑失败'); |
||||
} |
||||
} |
@ -0,0 +1,47 @@ |
||||
<?php |
||||
|
||||
namespace app\store\model; |
||||
|
||||
use app\common\model\ServerCategory as ServerCategoryModel; |
||||
|
||||
class ServerCategory extends ServerCategoryModel |
||||
{ |
||||
/** |
||||
* @notes:新增 |
||||
* @param $data |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function add($data): bool |
||||
{ |
||||
$data['store_id'] = self::$storeId; |
||||
return $this->save($data); |
||||
} |
||||
|
||||
/** |
||||
* @notes:编辑 |
||||
* @param $data |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function edit($data): bool |
||||
{ |
||||
// 是否删除图片 |
||||
!isset($data['image_id']) && $data['image_id'] = 0; |
||||
return $this->save($data) !== false; |
||||
} |
||||
|
||||
/** |
||||
* @notes:删除 |
||||
* @return bool |
||||
* @author: wanghousheng |
||||
*/ |
||||
public function remove(): bool |
||||
{ |
||||
if (!static::detail(['category_id' => $this['category_id']])) { |
||||
$this->error = '记录不存在'; |
||||
return false; |
||||
} |
||||
return $this->delete(); |
||||
} |
||||
} |
Loading…
Reference in new issue