qingjian/app/controller/manager/Hall.php

168 lines
5.0 KiB
PHP

<?php
namespace app\controller\manager;
use app\model\{Hall as MHall,HallLayout};
use app\validate\Hall as VHall;
use think\exception\ValidateException;
/**
* 大厅管理
*/
class Hall extends Base
{
//列表
public function index()
{
$keyword = input('param.keyword');
$list = MHall::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 = MHall::getById($id);
if(!empty($item)){
MHall::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 = MHall::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 = MHall::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 MHall();
$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');
$img = input('post.img');
$imgs = input('post.imgs/a');
if(!empty($img)){
$item['img'] = $img;
}
if(!empty($imgs)){
$item['imgs'] = json_encode($imgs);
}
$id = input('post.id/d');
$gift = MHall::getById($id);
if (empty($gift)) {
return $this->json(1, '该信息不存在!');
}
try {
validate(VHall::class)->check($item);
$item['update_time'] = time();
MHall::updateById($id, $item);
return $this->json();
} catch (ValidateException $e) {
return $this->json(2, $e->getError());
}
}else{
$id = input('param.id');
$gift = MHall::getById($id);
$this->data['item'] = $gift;
$this->data['hall_layout'] = HallLayout::getAll();
return $this->view();
}
}
//添加
public function add()
{
if($this->request->isPost()){
$item = input('post.item/a');
$img = input('post.img');
$imgs = input('post.imgs/a');
if(!empty($img)){
$item['img'] = $img;
}
if(!empty($imgs)){
$item['imgs'] = $imgs;
}
try {
validate(VHall::class)->check($item);
MHall::create($item);
return $this->json();
} catch (ValidateException $e) {
return $this->json(2, $e->getError());
}
}else{
$this->data['hall_layout'] = HallLayout::getAll();
return $this->view();
}
}
}