<?php namespace Apicp\Controller\NewsClass; use Com\PackageValidate; use Common\Service\ArticleService; use Common\Service\ClassService; class SaveController extends \Apicp\Controller\AbstractController { /** * Save * @author tangxingguo * @desc 保存分类 * @param Int parent_id 上级分类ID(二级分类保存时必填) * @param Int class_id 分类ID * @param String class_name:true 分类标题 * @param String description 分类描述 * @return array */ public function Index_post() { // 验证规则 $rules = [ 'parent_id' => 'integer', 'class_id' => 'integer', 'class_name' => 'require|max:20', 'description' => 'max:120', 'is_open' => 'in:1,2', ]; // 验证数据 $validate = new PackageValidate($rules, [], array_keys($rules)); $postData = $validate->postData; // 分类信息初始化 $classServ = new ClassService(); $classInfo = [ 'class_name' => $postData['class_name'], 'description' => isset($postData['description']) ? $postData['description'] : '', 'is_open' => $postData['is_open'] ]; // 唯一分类名检查 if (!$classServ->uniqueName($classInfo['class_name'], $postData['class_id'])) { E('_ERR_CLASS_NAME_REPEAT'); } // 二级分类操作 if (isset($postData['parent_id'])) { if (!isset($postData['parent_id']) || empty($postData['parent_id'])) { E('_ERR_CLASS_PARENT_ID_EMPTY'); } $classInfo['parent_id'] = $postData['parent_id']; // 分类名改变,修改article内分类名称 if (isset($postData['class_id'])) { $class = $classServ->get($postData['class_id']); if ($class && $class['class_name'] != $postData['class_name']) { $articleServ = new ArticleService(); $articleServ->update_by_conds( [ 'class_id' => $postData['class_id']], [ 'class_name' => $postData['class_name'] ] ); } } } else { // 一级分类 默认开启 $classInfo['is_open'] = 2; } if (empty($postData['class_id'])) { // 保存 $postData['class_id'] = $classServ->insert($classInfo); } else { // 修改 $classServ->update($postData['class_id'], $classInfo); } } }