qingjian/app/controller/manager/Store.php

179 lines
6.0 KiB
PHP
Raw Permalink Normal View History

2021-08-06 18:50:55 +08:00
<?php
namespace app\controller\manager;
use app\model\{Article as MArticle ,Store as MStore, Category, System, Log};
use app\validate\Store as VStore;
use think\exception\ValidateException;
/**
* 内容管理 - 文章管理
*/
class Store extends Base
{
public function index()
{
$keyword = input('param.keyword');
$param = input('param.param/a', []);
$list = MStore::getList(20, $keyword);
$this->data['list'] = $list;
$this->data['keyword'] = $keyword;
$this->data['param'] = $param;
return $this->view();
}
//删除
public function del()
{
if ($this->request->isPost()) {
$id = input('post.id/d');
if (is_numeric($id) && $id > 0) {
$item = MArticle::getById($id);
if (!empty($item)) {
MArticle::destroy($id);
Log::write('article', 'del', '删除文章ID' . $id . ',标题:' . $item['title']);
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 = MStore::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 = MStore::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 MStore();
$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');
$imgimgs = input('post.imgimgs');
$id = input('post.id/d');
$article = MStore::getById($id);
if (empty($article)) {
return $this->json(1, '该门店不存在!');
}
if (!empty($img)) {
$item['src'] = $img;
}
if (!empty($imgimgs)) {
$item['imgs'] = json_encode($imgimgs);
}
try {
MStore::updateById($id, $item);
return $this->json();
} catch (ValidateException $e) {
return $this->json(2, $e->getError());
}
} else {
$id = input('param.id');
$article = MStore::getById($id);
$category = Category::getById(MArticle::storeId);
if ($category['img_width'] && $category['img_height']) {
$imgSize = $category['img_width'] . '像素 X ' . $category['img_height'] . '像素';
} else {
$imgSize = System::getArticleImageSize();
}
$this->data['item'] = $article;
$this->data['category'] = $category;
$this->data['imgSize'] = $imgSize;
return $this->view();
}
}
//添加
public function add()
{
if ($this->request->isPost()) {
$item = input('post.item/a');
$img = input('post.img');
$imgimgs = input('post.imgimgs');
if (!empty($img)) {
$item['src'] = $img;
}
if (!empty($imgimgs)) {
$item['imgs'] = json_encode($imgimgs);
}
try {
validate(VStore::class)->check($item);
MStore::create($item);
return $this->json();
} catch (ValidateException $e) {
return $this->json(2, $e->getError());
}
} else {
$categoryId = input('param.category_id');
$category = Category::getById($categoryId);
if (count($category) > 0 && $category['img_width'] && $category['img_height']) {
$imgSize = $category['img_width'] . '像素 X ' . $category['img_height'] . '像素';
} else {
$imgSize = System::getArticleImageSize();
}
$this->data['category'] = $category;
$this->data['imgSize'] = $imgSize;
return $this->view();
}
}
}