151 lines
4.6 KiB
PHP
151 lines
4.6 KiB
PHP
<?php
|
|
namespace app\controller\manager;
|
|
|
|
use app\model\{Gift as MGift, Category, System, Log};
|
|
use app\validate\Gift as VGift;
|
|
use think\exception\ValidateException;
|
|
|
|
/**
|
|
* 礼物管理
|
|
*/
|
|
class Gift extends Base
|
|
{
|
|
|
|
//列表
|
|
public function index()
|
|
{
|
|
$keyword = input('param.keyword');
|
|
$list = MGift::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 = MGift::getById($id);
|
|
if(!empty($item)){
|
|
MGift::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 = MGift::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 = MGift::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 MGift();
|
|
$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');
|
|
|
|
$id = input('post.id/d');
|
|
$gift = MGift::getById($id);
|
|
if (empty($gift)) {
|
|
return $this->json(1, '该信息不存在!');
|
|
}
|
|
if(!empty($img)){
|
|
$item['img'] = $img;
|
|
}
|
|
try {
|
|
validate(VGift::class)->check($item);
|
|
$item['update_time'] = time();
|
|
MGift::updateById($id, $item);
|
|
return $this->json();
|
|
} catch (ValidateException $e) {
|
|
return $this->json(2, $e->getError());
|
|
}
|
|
}else{
|
|
$id = input('param.id');
|
|
$gift = MGift::getById($id);
|
|
$this->data['item'] = $gift;
|
|
return $this->view();
|
|
}
|
|
}
|
|
|
|
//添加
|
|
public function add()
|
|
{
|
|
if($this->request->isPost()){
|
|
$item = input('post.item/a');
|
|
$img = input('post.img');
|
|
|
|
if(!empty($img)){
|
|
$item['img'] = $img;
|
|
}
|
|
|
|
try {
|
|
validate(VGift::class)->check($item);
|
|
MGift::create($item);
|
|
return $this->json();
|
|
} catch (ValidateException $e) {
|
|
return $this->json(2, $e->getError());
|
|
}
|
|
}else{
|
|
return $this->view();
|
|
}
|
|
}
|
|
}
|