cms first commit

This commit is contained in:
JaguarJack
2021-05-22 11:02:45 +08:00
parent 0f24c9c580
commit f81eaf40af
77 changed files with 4990 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
use catchAdmin\cms\model\events\ArticlesEvent;
class Articles extends BaseModel
{
use ArticlesEvent;
// 表名
public $name = 'cms_articles';
// 数据库字段映射
public $field = array(
'id',
// 文章标题
'title',
// 分类ID
'category_id',
'cover', // 封面
// 多图集合
'images',
// 标签集合
'tags',
// 自定义URL
'url',
// 内容
'content',
// 关键字
'keywords',
// 描述
'description',
// 浏览量
'pv',
// 喜欢
'likes',
// 评论数
'comments',
// 1 置顶 2 非置顶
'is_top',
// 1 推荐 2 不推荐
'is_recommend',
// 1 展示 2 隐藏
'status',
'weight', // 权重
// 1 允许 2 不允许
'is_can_comment',
// 创建人ID
'creator_id',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
const TOP = 1; // 置顶
const UN_TOP = 2; // 不置顶
const RECOMMEND = 1; // 推荐
const UN_RECOMMEND = 2; // 不推荐
const CAN_COMMENT = 1; // 评论允许
const UN_CAN_COMMENT = 2; // 评论不允许
/**
* 文章标签
*
* @time 2021年05月17日
* @return \think\model\relation\BelongsToMany
*/
public function tags(): \think\model\relation\BelongsToMany
{
return $this->belongsToMany(Tags::class, 'cms_article_relate_tags',
'tag_id', 'article_id'
);
}
}

View File

@@ -0,0 +1,40 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
class Banners extends BaseModel
{
// 表名
public $name = 'cms_banners';
// 数据库字段映射
public $field = array(
'id',
// banner 标题
'title',
// banner 图片
'banner_img',
// 默认 0 代表首页展示
'category_id',
// 链接地址
'link_to',
// 创建人ID
'creator_id',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
}

View File

@@ -0,0 +1,27 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
use catcher\base\CatchModel as Model;
use catcher\Utils;
class BaseModel extends Model
{
public function __construct($data = [])
{
parent::__construct($data);
$this->field = $this->getTableFields(Utils::tableWithPrefix($this->name));
}
}

View File

@@ -0,0 +1,103 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
use catchAdmin\cms\model\events\CategoryEvent;
use catchAdmin\cms\model\scopes\CategoryScope;
class Category extends BaseModel
{
use CategoryEvent, CategoryScope;
// 表名
public $name = 'cms_category';
// 数据库字段映射
public $field = array(
'id',
// 分类名称
'name',
// 父级ID
'parent_id',
// seo标题
'title',
// seo关键词
'keywords',
// 描述
'description',
// 自定义 URL
'url',
// 1 显示 2 隐藏
'status',
// 是否可以投稿
'is_can_contribute',
// 是否可以评论
'is_can_comment',
// 是否是单页面 1 是 2 否
'type',
// 权重
'weight',
// 1 是 2 否
'is_link_out',
// 链接外部地址
'link_to',
// 创建人
'creator_id',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
const LIST_TYPE = 1; // 列表
const PAGE_TYPE = 2; // 单页
const COVER_TYPE = 3; // 封面
const CAN_COMMENT = 1; // 可以评论
const CAN_NOT_COMMENT = 2; // 不可以评论
const CAN_CONTRIBUTE = 1; // 可以投稿
const CAN_NOT_CONTRIBUTE = 2; // 不可以投稿
/**
* 列表
*
* @time 2021年03月03日
* @return mixed
*/
public function getList()
{
return $this->quickSearch()
->field(['*'])
->catchOrder()
->articlesCount()
->select()->toTree();
}
/**
* 是否存在下级
*
* @time 2021年03月03日
* @param int $id
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array|\think\Model|null
*/
public function hasNextLevel($id = 0)
{
return $this->where('parent_id', $id ? :$this->getKey())->find();
}
}

View File

@@ -0,0 +1,46 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
class Comments extends BaseModel
{
// 表名
public $name = 'cms_comments';
// 数据库字段映射
public $field = array(
'id',
// 文章ID
'article_id',
// 内容
'content',
// 父ID
'parent_id',
// 评论者ID
'user_id',
// ip 地址
'ip',
// agent
'user_agent',
// 1 展示 2 隐藏
'status',
// 创建人ID
'creator_id',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
}

View File

@@ -0,0 +1,42 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
class FormData extends BaseModel
{
// 表名
public $name = 'cms_form_data';
// 数据库字段映射
public $field = array(
'id',
// 表单ID
'form_id',
// json字段
'data',
// 用户ID
'user_id',
// ip 地址
'ip',
// agent
'user_agent',
// 创建人ID
'creator_id',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
}

View File

@@ -0,0 +1,50 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
class FormFields extends BaseModel
{
// 表名
public $name = 'cms_form_fields';
// 数据库字段映射
public $field = array(
'id',
// form id
'form_id',
// 字段 label
'label',
// 表单字段name
'name',
// 默认值
'default_value',
// 类型
'type',
// 验证规则
'rule',
// 字段长度
'length',
// 验证失败信息
'failed_message',
// 1 展示 2 隐藏
'status',
// 创建人ID
'creator_id',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
}

41
catch/cms/model/Forms.php Normal file
View File

@@ -0,0 +1,41 @@
<?php
namespace catchAdmin\cms\model;
class Forms extends BaseModel
{
// 表名
public $name = 'cms_forms';
// 数据库字段映射
public $field = array(
'id',
// 表单名称
'name',
// 表单别名
'alias',
// 表单提交的 URL
'submit_url',
// 表单标题
'title',
// 关键词
'keywords',
// 描述
'description',
// 成功提示信息
'success_message',
// 失败提示信息
'failed_message',
// 成功后跳转
'success_link_to',
// 1 需要 2 不需要
'is_login_to_submit',
// 创建人ID
'creator_id',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
}

View File

@@ -0,0 +1,104 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
use catcher\exceptions\FailedException;
class ModelAuxiliaryTable extends BaseModel
{
// 表名
public $name = 'cms_model_auxiliary_table';
// 数据库字段映射
public $field = array(
'id',
// 模型ID
'model_id',
// 副表名称
'table_name',
// 默认使用
'used',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
const USED = 1;
const NOT_USE = 2;
/**
* 获取默认使用的副表
*
* @time 2021年03月08日
* @param $modelId
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array|\think\Model|null
*/
public static function getDefaultUsed(int $modelId)
{
return self::where('model_id', $modelId)
->where('used', self::USED)
->find();
}
/**
* 默认使用
*
* @time 2021年03月08日
* @param int $id
* @return mixed
*/
public function used(int $id)
{
$t = $this->findBy($id);
$t->used = self::USED;
if ($t->save()) {
self::where('id', '<>', $id)
->where('model_id', $t->model_id)
->update([
'used' => self::NOT_USE,
]);
return $t;
}
throw new FailedException('启用失败');
}
/**
* 获取使用
*
* @time 2021年03月08日
* @param $modelId
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return array|\think\Model|null
*/
public function getUsed($modelId)
{
return $this->where('model_id', $modelId)
->where('used', self::USED)
->find();
}
}

View File

@@ -0,0 +1,112 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
use catchAdmin\cms\model\events\ModelFieldsEvent;
use catchAdmin\cms\support\Helper;
use catcher\Utils;
class ModelFields extends BaseModel
{
use ModelFieldsEvent;
// 表名
public $name = 'cms_model_fields';
// 数据库字段映射
public $field = array(
'id',
// 字段中文名称
'title',
// 表单字段名称
'name',
// 类型
'type',
// 长度
'length',
// 默认值
'default_value',
'is_index', // 是否是索引
'is_unique', // 是否是唯一
'options', // 列表
'rules', // 验证规则
'pattern', // 字段正则
// 模型ID
'model_id',
// 展示在列表 1 是 2 否
'use_at_list',
// 展示在详情 1 是 2 否
'use_at_detail',
// 用作是否搜索 1 是 2 否
'use_at_search',
// 创建人ID
'creator_id',
'sort',
'status',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
const IS_UNIQUE = 1;
const NOT_UNIQUE = 2;
const IS_INDEX = 1;
const NOT_INDEX = 2;
/**
* 获取模型的动态字段
*
* @time 2021年03月08日
* @param $modelId
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return \think\Collection
*/
public function getFieldsByModelId($modelId): \think\Collection
{
return $this->withoutField([
'created_at', 'deleted_at', 'updated_at',
])->where('model_id', $modelId)->select();
}
/**
* 获取规则
*
* @time 2021年03月07日
* @param $value
* @return string[]
*/
public function getRulesAttr($value): array
{
return Utils::stringToArrayBy($value);
}
/**
* 获取选项
*
* @time 2021年03月07日
* @param $value
* @return mixed
*/
public function getOptionsAttr($value)
{
// return Helper::getOptions($value);
return $value;
}
}

View File

@@ -0,0 +1,62 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
use catchAdmin\cms\model\events\ModelsEvent;
class Models extends BaseModel
{
use ModelsEvent;
// 表名
public $name = 'cms_models';
// 数据库字段映射
public $field = array(
'id',
// 模型名称
'name',
// 模型别名
'alias',
// 模型关联的表名,数据来源
'table_name',
// 模型描述
'description',
// 列表字段
'used_at_list',
// 搜索字段
'used_at_search',
// 详情字段
'used_at_detail',
// 创建人ID
'creator_id',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
/**
* 模型字段
*
* @time 2021年05月11日
* @return \think\model\relation\HasMany
*/
public function fields(): \think\model\relation\HasMany
{
return $this->hasMany(ModelFields::class, 'model_id', 'id');
}
}

View File

@@ -0,0 +1,56 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
class SiteLinks extends BaseModel
{
// 表名
public $name = 'cms_site_links';
// 数据库字段映射
public $field = array(
'id',
// 友情链接标题
'title',
// 跳转地址
'link_to',
// 权重
'weight',
// 1 展示 2 隐藏
'is_show',
// 网站图标
'icon',
// 创建人ID
'creator_id',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
/**
* 标题搜索
*
* @time 2021年05月09日
* @param $query
* @param $value
* @param $data
* @return mixed
*/
public function searchTitleAttr($query, $value, $data)
{
return $query->whereLike('title', $value);
}
}

46
catch/cms/model/Tags.php Normal file
View File

@@ -0,0 +1,46 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
class Tags extends BaseModel
{
// 表名
public $name = 'cms_tags';
// 数据库字段映射
public $field = array(
'id',
// 标签名称
'name',
// seo 标签
'title',
// 关键字
'keywords',
// 描述
'description',
// 创建人ID
'creator_id',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
public function searchNameAttr($query, $value)
{
$query->whereLike('name', $value);
}
}

42
catch/cms/model/Users.php Normal file
View File

@@ -0,0 +1,42 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model;
class Users extends BaseModel
{
// 表名
public $name = 'cms_users';
// 数据库字段映射
public $field = array(
'id',
// 用户名
'username',
// 邮箱
'email',
// 手机号
'mobile',
// 头像
'avatar',
// 1 正常 2 禁用
'status',
// 密码
'password',
// 创建时间
'created_at',
// 更新时间
'updated_at',
// 软删除
'deleted_at',
);
}

View File

@@ -0,0 +1,58 @@
<?php
namespace catchAdmin\cms\model\events;
use catchAdmin\cms\model\Articles;
use catchAdmin\cms\model\Tags;
use catcher\exceptions\FailedException;
use catcher\Utils;
trait ArticlesEvent
{
/**
* 插入前
*
* @time 2021年05月21日
* @param Articles $model
* @return void
*/
public static function onBeforeInsert(Articles $model)
{
$model->category_id = $model->category_id[0];
$model->images = implode(',', $model->images);
$model->tags = implode(',', $model->tags);
}
/**
* 插入后
*
* @time 2021年05月21日
* @param Articles $model
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @return void
*/
public static function onAfterInsert(Articles $model)
{
$tags = Utils::stringToArrayBy($model->tags);
$tagModel = new Tags;
$existTags = $tagModel->whereIn('name', $tags)->select()->toArray();
$existTagsName = array_column($existTags, 'name');
$tagsIds = [];
foreach ($tags as $tag) {
if (! in_array($tag, $existTagsName)) {
$tagsIds[] = $tagModel->createBy([
'name' => $tag
]);
}
}
$model->tags()->attach(array_merge(array_column($existTags, 'id'), $tagsIds));
}
}

View File

@@ -0,0 +1,83 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model\events;
use catcher\exceptions\FailedException;
use catcher\Utils;
trait CategoryEvent
{
/**
* 插入前
*
* @time 2021年03月03日
* @param \think\Model $category
* @return void
*/
public static function onBeforeInsert($category): void
{
$category->data(self::changeData($category->getData()));
}
/**
* 更新前
*
* @time 2021年03月03日
* @param \think\Model $category
* @return mixed|void
*/
public static function onBeforeUpdate($category)
{
// $category->data(self::changeData($category->getData()));
}
/**
* 删除前
*
* @time 2021年03月03日
* @param $category
* @return void
*/
public static function onBeforeDelete($category)
{
if ($category->hasNextLevel()) {
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;*/
}
}

View File

@@ -0,0 +1,167 @@
<?php
namespace catchAdmin\cms\model\events;
use catchAdmin\cms\exceptions\ColumnException;
use catchAdmin\cms\model\Models;
use catchAdmin\cms\support\Table;
use catchAdmin\cms\support\TableColumn;
use catchAdmin\cms\tables\Model;
use catcher\exceptions\FailedException;
trait ModelFieldsEvent
{
protected static $modelTableName = null;
/**
* 插入前
*
* @time 2021年03月08日
* @param $modelFields
* @return void
*/
public static function onBeforeInsert($modelFields)
{
$tableName = self::getModelTableName($modelFields->getData('model_id'));
if ($tableName && Table::hasColumn($tableName, $modelFields->getData('name'))) {
throw new ColumnException(sprintf('Column [%s] already exist in Table [%s]', $modelFields->getData('name'), $tableName));
}
$modelFields->data(self::changeData($modelFields->getData()));
}
/**
* 更新后
*
* @time 2021年03月08日
* @param $modelFields
* @return void
*/
public static function onBeforeUpdate($modelFields)
{
$modelFields->data(self::changeData($modelFields->getData(), true));
}
/**
* 插入之后
*
* @time 2021年03月08日
* @param $modelFields
* @return void
*/
public static function onAfterInsert($modelFields)
{
if ($modelFields->getKey()) {
try {
$tableName = self::getModelTableName($modelFields->getData('model_id'));
if ($tableName) {
Table::addColumn($tableName, (new TableColumn($modelFields->getData()))->get());
}
self::addIndexForField($tableName, $modelFields->getData('name'), $modelFields->getData('is_index'));
} catch (\Exception $e) {
$modelFields->delete();
throw new FailedException($e->getMessage());
}
}
}
/**
* 更新之后
*
* @time 2021年04月30日
* @param \think\Model $modelFields
* @return void
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\db\exception\DataNotFoundException
*/
public static function onAfterUpdate(\think\Model $modelFields)
{
$field = $modelFields->find($modelFields->getWhere());
self::addIndexForField(self::getModelTableName($field['model_id']), $field['name'], $field['is_index']);
}
/**
* 添加索引
*
* @time 2021年04月30日
* @param $table
* @param string $column
* @param $isIndex
* @return void
*/
protected static function addIndexForField($table, string $column, $isIndex)
{
if (! Table::isIndex($table, $column) && $isIndex == self::IS_INDEX) {
Table::addIndex($table, $column);
}
if (Table::isIndex($table, $column) && $isIndex == self::NOT_INDEX) {
Table::dropIndex($table, $column);
}
}
/**
* 删除字段
*
* @time 2021年03月08日
* @param $modelFields
* @return void
*/
public static function onAfterDelete($modelFields)
{
$tableName = self::getModelTableName($modelFields->getData('model_id'));
$columnName = $modelFields->getData('name');
if (Table::hasColumn($tableName, $columnName)) {
Table::dropColumn($tableName, $columnName);
}
}
/**
* 校验
*
* @time 2021年03月08日
* @param $data
* @param bool $update
* @return mixed
*/
protected static function changeData($data, $update = false)
{
if (isset($data['type']) && !in_array($data['type'], ['string', 'int'])) {
$data['length'] = 0;
}
// 更新不会校验
if (!$update) {
if (Table::hasColumn(self::getModelTableName($data['model_id']), $data['name'])) {
throw new ColumnException(sprintf('Column [%s] already exist in Table [%s]', $data['name'], self::getModelTableName($data['model_id'])));
}
}
return $data;
}
/**
* 获取模型关联的表
*
* @time 2021年03月08日
* @param $modelId
* @return mixed|null
*/
protected static function getModelTableName($modelId)
{
if (self::$modelTableName) {
return self::$modelTableName;
}
self::$modelTableName = Models::where('id', $modelId)->value('table_name');
return self::$modelTableName;
}
}

View File

@@ -0,0 +1,52 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model\events;
use catchAdmin\cms\support\Table;
use catcher\exceptions\FailedException;
trait ModelsEvent
{
/**
* 插入前
*
* @time 2021年03月03日
* @param \think\Model $model
* @return void
*/
public static function onBeforeInsert($model): void
{
if (!Table::exist($model->getData('table_name'))) {
throw new FailedException('模型关联的表【' .$model->getData('table_name'). '】不存在');
}
}
/**
* 更新前
*
* @time 2021年03月03日
* @param \think\Model $model
* @return void
*/
public static function onBeforeUpdate($model): void
{
$data = $model->getData();
$tableName = $data['table_name'] ?? null;
if ($tableName && !Table::exist($model->getData('table_name'))) {
throw new FailedException('模型关联的表【' .$model->getData('table_name'). '】不存在');
}
}
}

View File

@@ -0,0 +1,36 @@
<?php
// +----------------------------------------------------------------------
// | Catch-CMS Design On 2020
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\cms\model\scopes;
use catchAdmin\cms\model\Articles;
use think\facade\Db;
trait CategoryScope
{
/**
* 文章数量
*
* @time 2020年06月17日
* @param $query
* @return mixed
*/
public function scopeArticlesCount($query)
{
return $query->addSelectSub(function () {
$article = app(Articles::class);
return $article->field(Db::raw('count(*)'))
->whereColumn($this->aliasField('id'), $article->aliasField('category_id'));
}, 'articles');
}
}