You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
yanzong/app/store/controller/Server.php

185 lines
5.2 KiB

<?php
namespace app\store\controller;
use app\store\model\server\Server as ServerModel;
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
{
$data = $this->postForm();
if (!$data) {
return $this->renderError('缺少必要参数');
}
$model = new ServerCategory();
if ($model->add($data)) {
return $this->renderSuccess('添加成功');
}
return $this->renderError($model->getError() ?: '添加失败');
}
/**
* @notes:编辑分类
* @param int $categoryId
* @return Json
* @author: wanghousheng
*/
public function editCategory(int $categoryId): Json
{
$data = $this->postForm();
if (!$data) {
return $this->renderError('缺少必要参数');
}
$model = ServerCategory::detail($categoryId);
if ($model->edit($data)) {
return $this->renderSuccess('编辑成功');
}
return $this->renderError($model->getError() ?: '编辑失败');
}
public function deleteCategory(int $categoryId): Json
{
$model = ServerCategory::detail($categoryId);
if ($model->remove()) {
return $this->renderSuccess('删除成功');
}
return $this->renderError('删除失败');
}
/**
* @notes:服务列表
* @return Json
* @author: wanghousheng
*/
public function serverList(): Json
{
// 获取列表记录
$model = new ServerModel();
$server_name = $this->request->post('server_name');
$category_id = intval($this->request->post('category_id'));
$status = intval($this->request->post('status'));
$where = [];
if ($server_name) {
$where[] = ['server.server_name', 'like', "%$server_name%"];
}
if ($category_id) {
$where[] = ['server.category_id', '=', $category_id];
}
if ($status) {
$where[] = ['server.status', '=', $status];
}
try {
$list = $model->getList($where);
} catch (DbException $e) {
return $this->renderError($e->getMessage());
}
return $this->renderSuccess(compact('list'));
}
public function ServerDetail(int $serverId): Json
{
// 获取商品详情
$model = new ServerModel;
$info = $model->getDetail($serverId);
return $this->renderSuccess(compact('info'));
}
/**
* @notes:添加服务
* @return Json
* @author: wanghousheng
*/
public function addServer(): Json
{
$data = $this->postForm();
if (!$data) {
return $this->renderError('缺少必要参数');
}
$model = new ServerModel();
if ($model->add($data)) {
return $this->renderSuccess('添加成功');
}
return $this->renderError($model->getError() ?: '添加失败');
}
/**
* @notes:编辑服务
* @param int $serverId
* @return Json
* @author: wanghousheng
*/
public function editServer(int $serverId): Json
{
$data = $this->postForm();
if (!$data) {
return $this->renderError('缺少必要参数');
}
$model = ServerModel::detail($serverId);
if ($model->edit($data)) {
return $this->renderSuccess('编辑成功');
}
return $this->renderError($model->getError() ?: '编辑失败');
}
/**
* @notes:删除服务
* @param array $serverId
* @return Json
* @author: wanghousheng
*/
public function deleteServer(array $serverId): Json
{
$model = new ServerModel;
if ($model->remove($serverId)) {
return $this->renderSuccess('删除成功');
}
return $this->renderError('删除失败');
}
/**
* 修改服务状态(上下架)
* @param array $serverIds 商品id集
* @param bool $state 为true表示上架
* @return Json
*/
public function serverStatus(array $serverIds, bool $state): Json
{
$model = new ServerModel;
if (!$model->setStatus($serverIds, $state)) {
return $this->renderError($model->getError() ?: '操作失败');
}
return $this->renderSuccess('操作成功');
}
}