zzwy2/app/controller/manager/Category.php

347 lines
14 KiB
PHP
Raw Normal View History

2022-10-08 09:31:39 +00:00
<?php
namespace app\controller\manager;
use app\model\{Category as MCategory, Model as MCModel, Log, SpecialRoute};
use app\validate\Category as VCategory;
use think\facade\Db;
use think\Exception;
use think\exception\ValidateException;
use think\facade\Cache;
use app\service\Tool;
class Category extends Base
{
//栏目列表
public function index()
{
$auth = session('auth');
$authCates = array_filter(explode(',', $auth['cates']));
if ($auth['groupId'] == 1) {
$cates = MCategory::getList();
} else {
$cates = MCategory::getList(true, $authCates);
}
$items = MCategory::getCates($cates);
$powerAdd = false;
$ruleNames = Cache::get('rule_names_'.$auth['groupId']);
if (is_array($ruleNames) && in_array('category/add', $ruleNames, true)) {
$powerAdd = true;
}
$this->data['items'] = $items;
$this->data['power_add'] = $powerAdd;
return $this->view();
}
//批量删除
public function batchDel()
{
if($this->request->isPost()){
$ids = input('post.ids/a');
if(is_array($ids)) {
$idsArr = $ids;
} else {
$idsArr = array_filter(explode(',', $ids));
}
if(count($idsArr) == 0) {
return $this->json(1, '无效请求,参数错误!');
}
if (MCategory::hasChildren($idsArr)) {
return $this->json(2, '需删除的栏目下存在下级栏目,不可删除。请检查核实后再操作!');
}
$categories = MCategory::getListByIds($idsArr);
if(!empty($categories)){
$hasIds = [];
foreach($categories as $cate){
$hasIds[] = $cate['id'];
}
MCategory::destroy($hasIds);
Cache::delete("categoryNames");
SpecialRoute::deleteByTypeIds($hasIds ,SpecialRoute::type_archives_category);
Log::write('category', 'betchDel', '批量删除了栏目涉及到的ID为' . implode(',', $hasIds));
return $this->json();
}
return $this->json(3, '删除失败!栏目不存在,请刷新页面后再试!');
}
return $this->json(1, '非法请求!');
}
/**
* 删除
*/
public function del()
{
if($this->request->isPost()){
$id = input('post.id/d');
if(is_numeric($id) && $id > 0) {
if(MCategory::hasChildren($id)){
return $this->json(4, '此栏目有下级栏目,不可删除。请检查核实后再操作!');
}
$cate = MCategory::getById($id);
if(!empty($cate)){
MCategory::destroy($id);
Cache::delete("categoryNames");
SpecialRoute::deleteByTypeIds([$id] ,SpecialRoute::type_archives_category);
Log::write('category', 'del', '删除栏目ID' . $id . ',标题:' . $cate['title']);
return $this->json();
}
return $this->json(3, '删除失败!栏目不存在,请刷新页面后再试!');
} else {
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 = MCategory::getById($id);
if(empty($item)) {
return $this->json(3, '无此栏目!');
}
if($sort == 'up'){
$where = "parent_id='{$item['parent_id']}' and sort < {$item['sort']}";
$order = "sort desc";
}else{
$where = "parent_id='{$item['parent_id']}' and sort > {$item['sort']}";
$order = "sort asc";
}
$forSortItems = MCategory::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 MCategory();
$model->saveAll($updateData);
$sortStr = $sort == 'up' ? '上移' : '下调';
Log::write('category', 'sort', "栏目排序ID{$id} ,标题:{$item['title']}{$sortStr}{$num}");
return $this->json();
}
}
return $this->json(4, '无须调整排序!');
}
return $this->json(1, '非法请求!');
}
/**
* 更新栏目数据
*/
public function edit()
{
if($this->request->isPost()){
$item = input('post.item/a');
$id = input('post.id');
$img = input('post.img');
$img_mobile = input('post.img_mobile');
2022-11-05 15:35:58 +00:00
$bg_img = input('post.img_bg');
$bg_img_mobile = input('post.img_bg_mobile');
2022-10-08 09:31:39 +00:00
$icon = input('post.imgicon');
if (count($item) > 0 && (is_numeric($id) === true && $id > 0)) {
Db::startTrans();
try {
validate(VCategory::class)->scene("edit")->check($item);
$item['src'] = empty($img) ? '' : $img;
$item['src_mobile'] = empty($img_mobile) ? '' : $img_mobile;
2022-11-05 15:35:58 +00:00
$item['bg'] = empty($bg_img) ? '' : $bg_img;
$item['bg_mobile'] = empty($bg_img_mobile) ? '' : $bg_img_mobile;
2022-10-08 09:31:39 +00:00
$item['icon_img'] = empty($icon) ? '' : $icon;
// 只允许文章类栏目可以设置汇总查看
if ($item['model_id'] != MCModel::MODEL_ARTICLE) {
$item['parent_show'] = 0;
}
if (isset($item['category_key']) && strlen($item['category_key']) > 0) {
$hadCate = MCategory::findByCategoryKey($item['category_key']);
if (!empty($hadCate) && $hadCate['id'] != $id) {
throw new ValidateException('栏目标识已存在');
}
}
MCategory::updateById($id, $item);
//处理特殊路由
if (array_key_exists("route", $item) && !empty($item['route'])) {
$specialRoute = SpecialRoute::findByTypeRelaTioneId($id,SpecialRoute::type_archives_category);
if(empty($specialRoute)){
$specialRouteData = [
"route" =>$item["route"]??'',
"type" =>SpecialRoute::type_archives_category,
"relation_id" =>$id,
];
SpecialRoute::create($specialRouteData);
}else{
$specialRoute->save(["route"=>$item["route"]??'']);
}
}else{
SpecialRoute::deleteByTypeIds($id,SpecialRoute::type_archives_category);
}
Db::commit();
Cache::delete("categoryNames");
Log::write('category', 'edit', "栏目编辑ID{$id} ,标题:{$item['title']}");
return $this->json();
} catch (ValidateException $e) {
Db::rollback();
return $this->json(2, $e->getError());
}catch (\Exception $e) {
return $this->json(2, $e->getMessage());
}
}
return $this->json(1,'传入参数错误,请核对之后再操作!');
} else {
$id = input('param.id');
if (is_numeric($id) === true && $id > 0) {
$item = MCategory::getById($id);
if(empty($item)){
return $this->json(1,'参数错误,无此栏目!');
}
$this->data['item'] = $item;
$this->data['parent'] = MCategory::getById($item['parent_id']);
$this->data['models'] = MCModel::getList();
$this->data['iconImgSize'] = '64px * 64px';
return $this->view();
}
return $this->json(1,'参数错误,请核对之后再操作!');
}
}
/**
* 栏目添加
*/
public function add()
{
if($this->request->isPost()){
$item = input('post.item/a');
$img = input('post.img');
$img_mobile = input('post.img_mobile');
2022-11-05 15:35:58 +00:00
$bg_img = input('post.img_bg');
$bg_img_mobile = input('post.img_bg_mobile');
2022-10-08 09:31:39 +00:00
$icon = input('post.imgicon');
if (is_array($item) === true && count($item) > 0) {
if (!empty($img)) {
$item['src'] = $img;
2022-11-05 15:35:58 +00:00
}
if (!empty($img_mobile)) {
2022-10-08 09:31:39 +00:00
$item['src_mobile'] = $img_mobile;
}
2022-11-05 15:35:58 +00:00
if (!empty($bg_img)) {
$item['bg'] = $bg_img;
}
if (!empty($bg_img_mobile)) {
$item['bg_mobile'] = $bg_img_mobile;
}
2022-10-08 09:31:39 +00:00
if(!empty($icon)){
$item['icon_img'] = $icon;
}
Db::starttrans();
try {
validate(VCategory::class)->check($item);
if(!isset($item['number']) || $item['number'] <= 0){
$item['number'] = 20;
}
if($item['parent_id'] == 0){
$item['path'] = ',0,';
}else{
$parent = MCategory::getById($item['parent_id']);
if(empty($parent)){
$item['path'] = ',0,';
}else{
$item['path'] = $parent['path'] . $parent['id'] . ',';
}
}
// 只允许文章类栏目可以设置汇总查看
if ($item['model_id'] != MCModel::MODEL_ARTICLE) {
$item['parent_show'] = 0;
}
if (isset($item['category_key']) && strlen($item['category_key']) > 0) {
$hadCate = MCategory::findByCategoryKey($item['category_key']);
if (!empty($hadCate)) {
throw new ValidateException('栏目标识已存在');
}
}
$category = MCategory::create($item);
// 更新session
$member = \app\model\Member::getById(session('auth')['userId']);
$newCates = $member['cates'].','.$category['id'];
(new \app\model\Member())->where('id', $member['id'])->save([
'cates' => $newCates
]);
$auth = session('auth');
$auth['cates'] = $newCates;
session('auth', $auth);
//处理特殊路由
if (array_key_exists("route", $item) && !empty($item['route'])) {
$specialRouteData = [
"route" =>$item["route"]??'',
"type" =>SpecialRoute::type_archives_category,
"relation_id" =>$category['id'],
];
SpecialRoute::create($specialRouteData);
}
Cache::delete("categoryNames");
Db::commit();
Log::write('category', 'add', "栏目新增ID{$category->id} ,标题:{$item['title']}");
return $this->json();
} catch (ValidateException $e) {
return $this->json(2,$e->getError());
} catch (\Exception $e) {
return $this->json(2,$e->getMessage());
}
}
return $this->json(1, '传入值错误,请核对之后再操作!');
} else {
$parentId = input('param.id')??input('param.parent_id');
if(empty($parentId)){
$parent = [];
}else{
$parent = MCategory::getById($parentId);
}
$this->data['parent'] = $parent;
$this->data['models'] = MCModel::getList();
$this->data['iconImgSize'] = '64px * 64px';
return $this->view();
}
}
}