cms update
This commit is contained in:
@@ -15,6 +15,36 @@ namespace catchAdmin\cms\model;
|
||||
|
||||
use catchAdmin\cms\model\events\ArticlesEvent;
|
||||
|
||||
/**
|
||||
* Class Articles
|
||||
* @package catchAdmin\cms\model
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
*
|
||||
* @property Articles category_id
|
||||
* @property Articles id
|
||||
* @property Articles title
|
||||
* @property Articles cover
|
||||
* @property Articles images
|
||||
* @property Articles tags
|
||||
* @property Articles url
|
||||
* @property Articles content
|
||||
* @property Articles keywords
|
||||
* @property Articles description
|
||||
* @property Articles pv
|
||||
* @property Articles likes
|
||||
* @property Articles comments
|
||||
* @property Articles is_top
|
||||
* @property Articles is_recommend
|
||||
* @property Articles status
|
||||
* @property Articles weight
|
||||
* @property Articles is_can_comment
|
||||
* @property Articles creator_id
|
||||
* @property Articles created_at
|
||||
* @property Articles updated_at
|
||||
* @property Articles deleted_at
|
||||
*
|
||||
*/
|
||||
class Articles extends BaseModel
|
||||
{
|
||||
use ArticlesEvent;
|
||||
@@ -31,8 +61,6 @@ class Articles extends BaseModel
|
||||
'cover', // 封面
|
||||
// 多图集合
|
||||
'images',
|
||||
// 标签集合
|
||||
'tags',
|
||||
// 自定义URL
|
||||
'url',
|
||||
// 内容
|
||||
@@ -75,6 +103,44 @@ class Articles extends BaseModel
|
||||
const CAN_COMMENT = 1; // 评论允许
|
||||
const UN_CAN_COMMENT = 2; // 评论不允许
|
||||
|
||||
/**
|
||||
* 列表
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @return mixed
|
||||
*/
|
||||
public function getList()
|
||||
{
|
||||
// 分页列表
|
||||
return $this->catchSearch()
|
||||
->field($this->aliasField('*'))
|
||||
->catchJoin(Category::class, 'id', 'category_id', ['name as category'])
|
||||
->catchOrder()
|
||||
->creator()
|
||||
->paginate();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param $id
|
||||
* @param array|string[] $field
|
||||
* @param bool $trash
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array|mixed|\think\Model|null
|
||||
*/
|
||||
public function findBy($id, array $field = ['*'], bool $trash = false)
|
||||
{
|
||||
return $this->where('id', $id)
|
||||
->with(['tag'])
|
||||
->field('*')
|
||||
->find();
|
||||
}
|
||||
|
||||
/**
|
||||
* 文章标签
|
||||
@@ -82,10 +148,37 @@ class Articles extends BaseModel
|
||||
* @time 2021年05月17日
|
||||
* @return \think\model\relation\BelongsToMany
|
||||
*/
|
||||
public function tags(): \think\model\relation\BelongsToMany
|
||||
public function tag(): \think\model\relation\BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Tags::class, 'cms_article_relate_tags',
|
||||
'tag_id', 'article_id'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除标签
|
||||
*
|
||||
* @param array $ids
|
||||
* @return int
|
||||
* @author CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
*/
|
||||
public function detachTags(array $ids = []): int
|
||||
{
|
||||
return $this->tag()->detach($ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增标签
|
||||
*
|
||||
* @author CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param array $ids
|
||||
* @throws \think\db\exception\DbException
|
||||
* @return array|\think\model\Pivot
|
||||
*/
|
||||
public function attachTags(array $ids)
|
||||
{
|
||||
return $this->tag()->attach($ids);
|
||||
}
|
||||
}
|
@@ -2,6 +2,7 @@
|
||||
namespace catchAdmin\cms\model\events;
|
||||
|
||||
use catchAdmin\cms\model\Articles;
|
||||
use catchAdmin\cms\model\BaseModel;
|
||||
use catchAdmin\cms\model\Tags;
|
||||
use catcher\exceptions\FailedException;
|
||||
use catcher\Utils;
|
||||
@@ -17,11 +18,7 @@ trait ArticlesEvent
|
||||
*/
|
||||
public static function onBeforeInsert(Articles $model)
|
||||
{
|
||||
$model->category_id = $model->category_id[0];
|
||||
|
||||
$model->images = implode(',', $model->images);
|
||||
|
||||
$model->tags = implode(',', $model->tags);
|
||||
self::beforeChangeData($model);
|
||||
}
|
||||
|
||||
|
||||
@@ -36,23 +33,100 @@ trait ArticlesEvent
|
||||
* @return void
|
||||
*/
|
||||
public static function onAfterInsert(Articles $model)
|
||||
{
|
||||
$model->attachTags(self::getTagsId($model));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param Articles $model
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeUpdate(Articles $model)
|
||||
{
|
||||
self::beforeChangeData($model);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param Articles $model
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return void
|
||||
*/
|
||||
public static function onAfterUpdate(Articles $model)
|
||||
{
|
||||
$data = $model->getData();
|
||||
|
||||
if (isset($data['tags'])) {
|
||||
|
||||
$tagIds = self::getTagsId($model);
|
||||
|
||||
$article = $model->where($model->getWhere())->find();
|
||||
|
||||
$article->detachTags($article->tag()->select()->column('id'));
|
||||
|
||||
$article->attachTags(self::getTagsId($model));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入前
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param $model
|
||||
* @return void
|
||||
*/
|
||||
protected static function beforeChangeData($model)
|
||||
{
|
||||
$data = $model->getData();
|
||||
|
||||
if (isset($data['category_id'])) {
|
||||
$model->category_id = $model->category_id[count($model->category_id) - 1];
|
||||
}
|
||||
|
||||
if (isset($data['images'])) {
|
||||
$model->images = empty($model->images) ? '' : implode(',', $model->images);
|
||||
}
|
||||
|
||||
if (isset($data['tags'])) {
|
||||
$model->tags = empty($model->tags) ? '' : implode(',', $model->tags);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 插入后
|
||||
*
|
||||
* @auth CatchAdmin
|
||||
* @time 2021年05月22日
|
||||
* @param $model
|
||||
* @throws \think\db\exception\DataNotFoundException
|
||||
* @throws \think\db\exception\DbException
|
||||
* @throws \think\db\exception\ModelNotFoundException
|
||||
* @return array
|
||||
*/
|
||||
protected static function getTagsId($model): array
|
||||
{
|
||||
$tags = Utils::stringToArrayBy($model->tags);
|
||||
|
||||
$tagModel = new Tags;
|
||||
$existTags = $tagModel->whereIn('name', $tags)->select()->toArray();
|
||||
$existTagsName = array_column($existTags, 'name');
|
||||
|
||||
$tagsIds = [];
|
||||
$tagIds = [];
|
||||
foreach ($tags as $tag) {
|
||||
if (! in_array($tag, $existTagsName)) {
|
||||
$tagsIds[] = $tagModel->createBy([
|
||||
$tagModel = Tags::where('name', $tag)->findOrEmpty();
|
||||
|
||||
if ($tagModel->isEmpty()) {
|
||||
$tagIds[] = $tagModel->storeBy([
|
||||
'name' => $tag
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$model->tags()->attach(array_merge(array_column($existTags, 'id'), $tagsIds));
|
||||
return array_merge(Tags::whereIn('name', $tags)->column('id'), $tagIds);
|
||||
}
|
||||
}
|
@@ -25,9 +25,9 @@ trait CategoryEvent
|
||||
* @param \think\Model $category
|
||||
* @return void
|
||||
*/
|
||||
public static function onBeforeInsert($category): void
|
||||
public static function onBeforeInsert(\think\Model $category): void
|
||||
{
|
||||
$category->data(self::changeData($category->getData()));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,9 +37,9 @@ trait CategoryEvent
|
||||
* @param \think\Model $category
|
||||
* @return mixed|void
|
||||
*/
|
||||
public static function onBeforeUpdate($category)
|
||||
public static function onBeforeUpdate(\think\Model $category)
|
||||
{
|
||||
// $category->data(self::changeData($category->getData()));
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,29 +55,4 @@ trait CategoryEvent
|
||||
throw new FailedException('存在下级栏目, 无法删除');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新Data
|
||||
*
|
||||
* @time 2021年03月03日
|
||||
* @param $data
|
||||
* @return mixed
|
||||
*/
|
||||
protected static function changeData($data)
|
||||
{
|
||||
/**
|
||||
if (isset($data['parent_id'])) {
|
||||
$parentId = $data['parent_id'];
|
||||
|
||||
if (!is_array($parentId)) {
|
||||
// $parentId = Utils::dealWithFormArrayString($parentId);
|
||||
}
|
||||
|
||||
$parentId = count($parentId) ? array_pop($parentId) : 0;
|
||||
|
||||
$data['parent_id'] = $parentId;
|
||||
}
|
||||
|
||||
return $data;*/
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user