256 lines
		
	
	
		
			8.3 KiB
		
	
	
	
		
			PHP
		
	
	
		
		
			
		
	
	
			256 lines
		
	
	
		
			8.3 KiB
		
	
	
	
		
			PHP
		
	
	
| 
								 | 
							
								<?php
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								namespace app\controller\manager;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								use app\repository\CmsRepository;
							 | 
						|||
| 
								 | 
							
								use app\model\Log;
							 | 
						|||
| 
								 | 
							
								use app\model\ArchivesCategory as ArchivesCategoryModel;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								use app\validate\MenuValidate;
							 | 
						|||
| 
								 | 
							
								use Exception;
							 | 
						|||
| 
								 | 
							
								use think\facade\Cache;
							 | 
						|||
| 
								 | 
							
								use think\facade\Db;
							 | 
						|||
| 
								 | 
							
								use think\db\exception\DataNotFoundException;
							 | 
						|||
| 
								 | 
							
								use think\db\exception\DbException;
							 | 
						|||
| 
								 | 
							
								use think\db\exception\ModelNotFoundException;
							 | 
						|||
| 
								 | 
							
								use think\exception\ValidateException;
							 | 
						|||
| 
								 | 
							
								use think\response\Json;
							 | 
						|||
| 
								 | 
							
								use think\response\View;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								/**
							 | 
						|||
| 
								 | 
							
								 * 栏目管理
							 | 
						|||
| 
								 | 
							
								 *
							 | 
						|||
| 
								 | 
							
								 * Class Menu
							 | 
						|||
| 
								 | 
							
								 * @package app\controller\manager
							 | 
						|||
| 
								 | 
							
								 */
							 | 
						|||
| 
								 | 
							
								class ArchivesCategory extends Base
							 | 
						|||
| 
								 | 
							
								{
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * 删除
							 | 
						|||
| 
								 | 
							
								     *
							 | 
						|||
| 
								 | 
							
								     * @return Json
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    public function del(): Json
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        Cache::delete("categoryNames");//删除缓存
							 | 
						|||
| 
								 | 
							
								        if ($this->request->isPost()) {
							 | 
						|||
| 
								 | 
							
								            $ids = input('post.ids/a', []);
							 | 
						|||
| 
								 | 
							
								            if (empty($ids)) {
							 | 
						|||
| 
								 | 
							
								                $ids[] = input('post.id/d');
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            if (ArchivesCategoryModel::hasChildrenByIds($ids)) {
							 | 
						|||
| 
								 | 
							
								                return $this->json(4002, '待删除数据存在子数据');
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            if (ArchivesCategoryModel::hasContentByIds($ids)) {
							 | 
						|||
| 
								 | 
							
								                return $this->json(4002, '待删除数据存在内容文章');
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            ArchivesCategoryModel::deleteByIds($ids);
							 | 
						|||
| 
								 | 
							
								//            Log::write(get_class().'Del', 'del', '涉及到的ID为:'.implode(',', $ids));
							 | 
						|||
| 
								 | 
							
								            return $this->json();
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        return $this->json(4001, '非法请求!');
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * 编辑
							 | 
						|||
| 
								 | 
							
								     *
							 | 
						|||
| 
								 | 
							
								     * @return Json|View
							 | 
						|||
| 
								 | 
							
								     * @throws DataNotFoundException
							 | 
						|||
| 
								 | 
							
								     * @throws DbException
							 | 
						|||
| 
								 | 
							
								     * @throws ModelNotFoundException
							 | 
						|||
| 
								 | 
							
								     * @throws Exception
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    public function edit()
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        Cache::delete("categoryNames");//删除缓存
							 | 
						|||
| 
								 | 
							
								        $id = input('id/d', 0);
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								        if (!$info = ArchivesCategoryModel::findById($id)) {
							 | 
						|||
| 
								 | 
							
								            return $this->json(4001, '记录不存在');
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								        if ($this->request->isPost()) {
							 | 
						|||
| 
								 | 
							
								            $item     = input('post.');
							 | 
						|||
| 
								 | 
							
								            $validate = $this->validateByApi($item, [
							 | 
						|||
| 
								 | 
							
								                'pid|父级分类'       => 'require|number',
							 | 
						|||
| 
								 | 
							
								                'model_id|所属模型'  => 'require|number|gt:0',
							 | 
						|||
| 
								 | 
							
								                'title|标题'       => 'require|max:100',
							 | 
						|||
| 
								 | 
							
								                'name|标识'        => 'unique:archives_category,name,'.$info['id'] ?? 0,
							 | 
						|||
| 
								 | 
							
								                'description|描述' => 'max:255',
							 | 
						|||
| 
								 | 
							
								            ], ['model_id' => '所属模型必需选择']);
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            if ($validate !== true) {
							 | 
						|||
| 
								 | 
							
								                return $validate;
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            Db::startTrans();
							 | 
						|||
| 
								 | 
							
								            try {
							 | 
						|||
| 
								 | 
							
								                $oldPath      = $info['path'] ?? '';
							 | 
						|||
| 
								 | 
							
								                $item['path'] = ArchivesCategoryModel::getPath($item['pid']);
							 | 
						|||
| 
								 | 
							
								                $info->save($item);
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								                //刷新所有路径
							 | 
						|||
| 
								 | 
							
								                $oldPath = $oldPath.','.$id;
							 | 
						|||
| 
								 | 
							
								                $newPath = $item['path'].','.$id;
							 | 
						|||
| 
								 | 
							
								                if ($oldPath != $newPath) {
							 | 
						|||
| 
								 | 
							
								                    ArchivesCategoryModel::refreshPath();
							 | 
						|||
| 
								 | 
							
								                }
							 | 
						|||
| 
								 | 
							
								                Db::commit();
							 | 
						|||
| 
								 | 
							
								                return $this->json();
							 | 
						|||
| 
								 | 
							
								            } catch (ValidateException $e) {
							 | 
						|||
| 
								 | 
							
								                Db::rollback();
							 | 
						|||
| 
								 | 
							
								                return $this->json(4001, $e->getError());
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								        $disabled                = ArchivesCategoryModel::getAllChildrenIds($id);
							 | 
						|||
| 
								 | 
							
								        $disabled[]              = $id;
							 | 
						|||
| 
								 | 
							
								        $this->data['jsonList']  = $this->categoryJson([$info['pid']], $disabled);
							 | 
						|||
| 
								 | 
							
								        $this->data['modelList'] = $this->modelJson([$info['model_id']], []);
							 | 
						|||
| 
								 | 
							
								        $this->data['item']      = $info;
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								        return $this->view();
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * 单个字段编辑
							 | 
						|||
| 
								 | 
							
								     *
							 | 
						|||
| 
								 | 
							
								     * @return Json
							 | 
						|||
| 
								 | 
							
								     * @throws DataNotFoundException
							 | 
						|||
| 
								 | 
							
								     * @throws DbException
							 | 
						|||
| 
								 | 
							
								     * @throws ModelNotFoundException
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    public function modify(): Json
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        Cache::delete("categoryNames");//删除缓存
							 | 
						|||
| 
								 | 
							
								        if ($this->request->isPost()) {
							 | 
						|||
| 
								 | 
							
								            $item     = input('post.');
							 | 
						|||
| 
								 | 
							
								            $validate = new MenuValidate();
							 | 
						|||
| 
								 | 
							
								            if (!$validate->scene('menu_modify')->check($item)) {
							 | 
						|||
| 
								 | 
							
								                return $this->json(4002, $validate->getError());
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            if (!$info = ArchivesCategoryModel::findById($item['id'])) {
							 | 
						|||
| 
								 | 
							
								                return $this->json(4001, '记录不存在');
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            $update = [$item['field'] => $item['value']];
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            try {
							 | 
						|||
| 
								 | 
							
								                $info->save($update);
							 | 
						|||
| 
								 | 
							
								                return $this->json();
							 | 
						|||
| 
								 | 
							
								            } catch (ValidateException $e) {
							 | 
						|||
| 
								 | 
							
								                return $this->json(4001, $e->getError());
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        return $this->json(4000, '非法请求');
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * 添加
							 | 
						|||
| 
								 | 
							
								     *
							 | 
						|||
| 
								 | 
							
								     * @return Json|View
							 | 
						|||
| 
								 | 
							
								     * @throws Exception
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    public function add()
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        Cache::delete("categoryNames");//删除缓存
							 | 
						|||
| 
								 | 
							
								        if ($this->request->isPost()) {
							 | 
						|||
| 
								 | 
							
								            $item     = input('post.');
							 | 
						|||
| 
								 | 
							
								            $validate = $this->validateByApi($item, [
							 | 
						|||
| 
								 | 
							
								                'pid|父级分类'       => 'require|number',
							 | 
						|||
| 
								 | 
							
								                'model_id|所属模型'  => 'require|number|gt:0',
							 | 
						|||
| 
								 | 
							
								                'title|标题'       => 'require|max:100',
							 | 
						|||
| 
								 | 
							
								                'name|标识'        => 'require|unique:archives_category',
							 | 
						|||
| 
								 | 
							
								                'description|描述' => 'max:255',
							 | 
						|||
| 
								 | 
							
								            ], ['model_id' => '所属模型必需选择']);
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								            if ($validate !== true) {
							 | 
						|||
| 
								 | 
							
								                return $validate;
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								            try {
							 | 
						|||
| 
								 | 
							
								                $item['path'] = ArchivesCategoryModel::getPath($item['pid']);
							 | 
						|||
| 
								 | 
							
								                ArchivesCategoryModel::create($item);
							 | 
						|||
| 
								 | 
							
								                return $this->json();
							 | 
						|||
| 
								 | 
							
								            } catch (ValidateException $e) {
							 | 
						|||
| 
								 | 
							
								                return $this->json(4001, $e->getError());
							 | 
						|||
| 
								 | 
							
								            }
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								        $this->data['jsonList']  = $this->categoryJson();
							 | 
						|||
| 
								 | 
							
								        $this->data['modelList'] = $this->modelJson();
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								        return $this->view();
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * 列表
							 | 
						|||
| 
								 | 
							
								     *
							 | 
						|||
| 
								 | 
							
								     * @return Json|View
							 | 
						|||
| 
								 | 
							
								     * @throws DataNotFoundException
							 | 
						|||
| 
								 | 
							
								     * @throws DbException
							 | 
						|||
| 
								 | 
							
								     * @throws ModelNotFoundException
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    public function index()
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        if ($this->request->isPost()) {
							 | 
						|||
| 
								 | 
							
								            $menus = ArchivesCategoryModel::getList();
							 | 
						|||
| 
								 | 
							
								            $res   = [
							 | 
						|||
| 
								 | 
							
								                'code'  => 0,
							 | 
						|||
| 
								 | 
							
								                'msg'   => 'success',
							 | 
						|||
| 
								 | 
							
								                'count' => $menus->count(),
							 | 
						|||
| 
								 | 
							
								                'data'  => $menus->toArray(),
							 | 
						|||
| 
								 | 
							
								            ];
							 | 
						|||
| 
								 | 
							
								            return json($res);
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        return $this->view();
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * @param  array  $selected
							 | 
						|||
| 
								 | 
							
								     * @param  array  $disabled
							 | 
						|||
| 
								 | 
							
								     * @return false|string
							 | 
						|||
| 
								 | 
							
								     * @throws DataNotFoundException
							 | 
						|||
| 
								 | 
							
								     * @throws DbException
							 | 
						|||
| 
								 | 
							
								     * @throws ModelNotFoundException
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    private function categoryJson(array $selected = [], array $disabled = [])
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        $categoryList[] = ['title' => '顶级分类', 'id' => 0, 'disabled' => false, 'selected' => in_array(0, $selected)];
							 | 
						|||
| 
								 | 
							
								        $menus          = ArchivesCategoryModel::getList();
							 | 
						|||
| 
								 | 
							
								        $menus          = $menus->toArray();
							 | 
						|||
| 
								 | 
							
								        foreach ($menus as $k => $m) {
							 | 
						|||
| 
								 | 
							
								            $menus[$k]['selected'] = in_array($m['id'], $selected);
							 | 
						|||
| 
								 | 
							
								            $menus[$k]['disabled'] = in_array($m['id'], $disabled);
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        $menus        = CmsRepository::getInstance()->buildMenuChild(0, $menus);
							 | 
						|||
| 
								 | 
							
								        $categoryList = array_merge($categoryList, CmsRepository::getInstance()->handleSelectedList($menus));
							 | 
						|||
| 
								 | 
							
								        return json_encode($categoryList, JSON_UNESCAPED_UNICODE);
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								
							 | 
						|||
| 
								 | 
							
								    /**
							 | 
						|||
| 
								 | 
							
								     * @param  array  $selected
							 | 
						|||
| 
								 | 
							
								     * @param  array  $disabled
							 | 
						|||
| 
								 | 
							
								     * @return false|string
							 | 
						|||
| 
								 | 
							
								     * @throws DataNotFoundException
							 | 
						|||
| 
								 | 
							
								     * @throws DbException
							 | 
						|||
| 
								 | 
							
								     * @throws ModelNotFoundException
							 | 
						|||
| 
								 | 
							
								     */
							 | 
						|||
| 
								 | 
							
								    private function modelJson(array $selected = [], array $disabled = [])
							 | 
						|||
| 
								 | 
							
								    {
							 | 
						|||
| 
								 | 
							
								        $categoryList[] = ['title' => '全部', 'id' => 0, 'disabled' => false, 'selected' => in_array(0, $selected)];
							 | 
						|||
| 
								 | 
							
								        $menus          = \app\model\ArchivesModel::field('id,0 as pid,title,name,sort,true as open')
							 | 
						|||
| 
								 | 
							
								            ->order('sort', 'desc')
							 | 
						|||
| 
								 | 
							
								            ->order('id', 'asc')
							 | 
						|||
| 
								 | 
							
								            ->select();;
							 | 
						|||
| 
								 | 
							
								        $menus = $menus->toArray();
							 | 
						|||
| 
								 | 
							
								        foreach ($menus as $k => $m) {
							 | 
						|||
| 
								 | 
							
								            $menus[$k]['selected'] = in_array($m['id'], $selected);
							 | 
						|||
| 
								 | 
							
								            $menus[$k]['disabled'] = in_array($m['id'], $disabled);
							 | 
						|||
| 
								 | 
							
								        }
							 | 
						|||
| 
								 | 
							
								        $menus        = CmsRepository::getInstance()->buildMenuChild(0, $menus);
							 | 
						|||
| 
								 | 
							
								        $categoryList = array_merge($categoryList, CmsRepository::getInstance()->handleSelectedList($menus));
							 | 
						|||
| 
								 | 
							
								        return json_encode($categoryList, JSON_UNESCAPED_UNICODE);
							 | 
						|||
| 
								 | 
							
								    }
							 | 
						|||
| 
								 | 
							
								}
							 |