qingjian/app/controller/manager/HallLayout.php

154 lines
4.8 KiB
PHP

<?php
namespace app\controller\manager;
use app\model\{HallLayout as MHallLayout};
use app\validate\HallLayout as VHallLayout;
use think\exception\ValidateException;
/**
* 大厅布局管理
*/
class HallLayout extends Base
{
//列表
public function index()
{
$keyword = input('param.keyword');
$list = MHallLayout::getList( 20, $keyword);
$this->data['list'] = $list;
$this->data['keyword'] = $keyword;
return $this->view();
}
//删除
public function del()
{
if ($this->request->isPost()) {
$id = input('post.id/d');
if(is_numeric($id) && $id > 0) {
$item = MHallLayout::getById($id);
if(!empty($item)){
MHallLayout::destroy($id);
return $this->json();
}
return $this->json(3,'待删除信息不存在');
}
return $this->json(2, '参数错误,请核对之后再操作!');
}
return $this->json(1, '非法请求!');
}
//排序
public function sort()
{
if($this->request->isPost()){
$id = input('post.id/d');
$sort = input('post.sort');
$num = input('post.num/d', 1);
if($num <= 0){
$num = 1;
}
if(!in_array($sort, ['up', 'down'], true)){
return $this->json(2, '参数错误');
}
$item = MHallLayout::getById($id);
if(empty($item)){
return $this->json(3, '该文章信息不存在');
}
if($sort == 'up'){
$where = " sort > {$item['sort']} ";
$order = "sort asc";
}else{
$where = " sort < {$item['sort']} ";
$order = "sort desc";
}
$forSortItems = MHallLayout::getListByWhereAndOrder($where, $order, $num);
if(!empty($forSortItems)){
$updateData = [];
$forSortCount = count($forSortItems);
for($i = 0; $i < $forSortCount; $i++){
if($i == 0){
$updateData[] = [
'id' => $forSortItems[$i]['id'],
'sort' => $item['sort']
];
}else{
$updateData[] = [
'id' => $forSortItems[$i]['id'],
'sort' => $forSortItems[$i - 1]['sort']
];
}
}
$updateData[] = [
'id' => $item['id'],
'sort' => $forSortItems[$i - 1]['sort']
];
if(!empty($updateData)){
$model = new MHallLayout();
$model->saveAll($updateData);
return $this->json();
}
}
return $this->json(4, '无须调整排序!');
}
return $this->json(1, '非法请求!');
}
//编辑
public function edit()
{
if($this->request->isPost()){
$item = input('post.item/a');
$imglayout = input('post.img');
if(!empty($imglayout)){
$item['layout_img'] = $imglayout;
}
$id = input('post.id/d');
$gift = MHallLayout::getById($id);
if (empty($gift)) {
return $this->json(1, '该信息不存在!');
}
try {
validate(VHallLayout::class)->check($item);
$item['update_time'] = time();
MHallLayout::updateById($id, $item);
return $this->json();
} catch (ValidateException $e) {
return $this->json(2, $e->getError());
}
}else{
$id = input('param.id');
$gift = MHallLayout::getById($id);
$this->data['item'] = $gift;
return $this->view();
}
}
//添加
public function add()
{
if($this->request->isPost()){
$item = input('post.item/a');
$imglayout = input('post.img');
if(!empty($imglayout)){
$item['layout_img'] = $imglayout;
}
try {
validate(VHallLayout::class)->check($item);
MHallLayout::create($item);
return $this->json();
} catch (ValidateException $e) {
return $this->json(2, $e->getError());
}
}else{
return $this->view();
}
}
}