6 changed files with 239 additions and 0 deletions
-
120app/controller/TagCateController.php
-
63app/controller/TagController.php
-
9app/model/Tag.php
-
13app/model/TagCate.php
-
17app/service/TagCateService.php
-
17route/app.php
@ -0,0 +1,120 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\controller; |
||||
|
|
||||
|
use app\model\ProcessReason; |
||||
|
use app\model\Resource; |
||||
|
use app\model\Share; |
||||
|
use app\model\Tag; |
||||
|
use app\model\TagCate; |
||||
|
use app\service\ResourceService; |
||||
|
use app\service\ShareService; |
||||
|
use app\service\TagCateService; |
||||
|
use app\validate\ResourceValidate; |
||||
|
use think\facade\Db; |
||||
|
use think\facade\Lang; |
||||
|
use think\exception\ValidateException; |
||||
|
use think\Request; |
||||
|
use think\response\Json; |
||||
|
|
||||
|
class TagCateController |
||||
|
{ |
||||
|
public function index(Request $request) |
||||
|
{ |
||||
|
$tage_cate = TagCate::with('cates')->select(); |
||||
|
return jsonReturn(0, Lang::get('成功'), $tage_cate); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 新建标签组 |
||||
|
* @param Request $request |
||||
|
* @return json |
||||
|
*/ |
||||
|
public function save(Request $request) |
||||
|
{ |
||||
|
try { |
||||
|
//默认是自定义标签组
|
||||
|
if (TagCate::where('name', $request->param('name'))->count() > 0) { |
||||
|
return jsonReturn(-5, '标签组已经存在'); |
||||
|
} |
||||
|
TagCate::create($request->param()); |
||||
|
} catch (\Exception $e) { |
||||
|
return jsonReturn(-5, $e->getMessage()); |
||||
|
} |
||||
|
return jsonReturn(0, Lang::get('新建标签组成功')); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 新建标签 |
||||
|
* @param Request $request |
||||
|
* @return json |
||||
|
*/ |
||||
|
public function saveTag(Request $request) |
||||
|
{ |
||||
|
try { |
||||
|
if (Tag::where('cate_id', $request->param('id'))->where('name', $request->param('name'))->count() > 0) { |
||||
|
throw new \Exception('标签已经存在'); |
||||
|
} |
||||
|
$tag_cate = TagCate::find($request->param('id')); |
||||
|
|
||||
|
$tag = new Tag; |
||||
|
$tag->cate_id = $request->param('id'); |
||||
|
$tag->name = $request->param('name'); |
||||
|
$tag->type = $tag_cate->type; //标签的type 和标签组保持一致?
|
||||
|
$tag->save(); |
||||
|
|
||||
|
return jsonReturn(0, Lang::get('新建标签成功')); |
||||
|
} catch (\Exception $e) { |
||||
|
return jsonReturn(-5, $e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 重命名标签组 |
||||
|
* @param Request $request |
||||
|
* @return json |
||||
|
*/ |
||||
|
public function rename(Request $request) |
||||
|
{ |
||||
|
try { |
||||
|
TagCate::whereIn('id', $request->param('id'))->update(['name' => $request->param('name')]); |
||||
|
return jsonReturn(0, Lang::get('重命名成功')); |
||||
|
} catch (\Exception $e) { |
||||
|
return jsonReturn(-5, $e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 删除标签组 |
||||
|
* @param Request $request |
||||
|
* @return json |
||||
|
*/ |
||||
|
public function delete(Request $request) |
||||
|
{ |
||||
|
Db::startTrans(); |
||||
|
try { |
||||
|
$tag_cate = TagCate::where('id', $request->param('id'))->find(); |
||||
|
if ($tag_cate->type == 1) { |
||||
|
throw new \Exception('系统标签无法删除'); |
||||
|
} |
||||
|
|
||||
|
//删除标签组
|
||||
|
$tag_cate->delete(); |
||||
|
|
||||
|
//标签组下标签移动到默认系统标签
|
||||
|
$default_tag_cate = (new TagCateService())->getDefaultTagCate(); |
||||
|
if (!$default_tag_cate) { |
||||
|
throw new \Exception('没有找到默认系统标签'); |
||||
|
} |
||||
|
Tag::where('cate_id', $request->param('id'))->update(['cate_id' => $default_tag_cate->id]); |
||||
|
Db::commit(); |
||||
|
return jsonReturn(0, Lang::get('删除标签组成功')); |
||||
|
|
||||
|
} catch (\Exception $e) { |
||||
|
Db::rollback(); |
||||
|
return jsonReturn(-5, $e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,63 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\controller; |
||||
|
|
||||
|
use app\model\ProcessReason; |
||||
|
use app\model\Resource; |
||||
|
use app\model\Share; |
||||
|
use app\model\Tag; |
||||
|
use app\model\TagCate; |
||||
|
use app\service\ResourceService; |
||||
|
use app\service\ShareService; |
||||
|
use app\service\TagCateService; |
||||
|
use app\validate\ResourceValidate; |
||||
|
use think\facade\Db; |
||||
|
use think\facade\Lang; |
||||
|
use think\exception\ValidateException; |
||||
|
use think\Request; |
||||
|
use think\response\Json; |
||||
|
|
||||
|
class TagController |
||||
|
{ |
||||
|
/** |
||||
|
* 标签重命名 |
||||
|
* @param Request $request |
||||
|
* @return json |
||||
|
*/ |
||||
|
public function rename(Request $request) |
||||
|
{ |
||||
|
try { |
||||
|
$tag = Tag::find($request->param('id')); |
||||
|
if (Tag::where('cate_id', $tag->cate_id)->where('name', $request->param('name'))->count() > 0) { |
||||
|
throw new \Exception('同标签组标签已经存在'); |
||||
|
} |
||||
|
$tag->name = $request->param('name'); |
||||
|
$tag->save(); |
||||
|
return jsonReturn(0, Lang::get('重命名成功')); |
||||
|
} catch (\Exception $e) { |
||||
|
return jsonReturn(-5, $e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 标签移动 |
||||
|
* @param Request $request |
||||
|
* @return json |
||||
|
*/ |
||||
|
public function moveTo(Request $request) |
||||
|
{ |
||||
|
try { |
||||
|
$tag = Tag::find($request->param('id')); |
||||
|
$tag->cate_id = $request->param('cate_id'); |
||||
|
$tag->save(); |
||||
|
return jsonReturn(0, Lang::get('移动成功')); |
||||
|
} catch (\Exception $e) { |
||||
|
return jsonReturn(-5, $e->getMessage()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public function delete() |
||||
|
{ |
||||
|
//todo 删除标签 从素材上移出?后面再看
|
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\model; |
||||
|
|
||||
|
use app\exception\ModelException; |
||||
|
|
||||
|
class Tag extends Model |
||||
|
{ |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\model; |
||||
|
|
||||
|
use app\exception\ModelException; |
||||
|
|
||||
|
class TagCate extends Model |
||||
|
{ |
||||
|
public function cates() |
||||
|
{ |
||||
|
return $this->hasMany(Tag::class, 'cate_id', 'id'); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,17 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace app\service; |
||||
|
|
||||
|
use app\model\TagCate; |
||||
|
|
||||
|
class TagCateService |
||||
|
{ |
||||
|
/** |
||||
|
* @return mixed |
||||
|
* 获取默认分类 (未分类) |
||||
|
*/ |
||||
|
public function getDefaultTagCate() |
||||
|
{ |
||||
|
return TagCate::where('name', '未分类')->where('type', 1)->find(); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue