You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

374 lines
16 KiB

<?php
declare (strict_types=1);
namespace app\api\model;
use app\common\model\Field;
use app\Request;
use think\facade\Config;
use think\facade\Db;
use think\Model;
/**
* @mixin think\Model
*/
class Module extends Common
{
protected $table = 'bmz_module';
protected $cms_table = 'bmz_cms';
/**
* 管理员列表数据模型
* @return array|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function index()
{
$this->search_arr = ['module_name','table_name'];
$id = $this->request->param('id', 0);
$_op = $this->field('*');
if($id){
$_op = $this->where('id',$id);
}
//执行通用查询
return parent::parentLists($_op);
}
/**
* 读取后
* @param Model $model
* @return bool|void
*/
// public static function onAfterRead(Model $model)
// {
// if (in_array(request()->action(), [ 'delete', 'status'])) {
// return true;
// }
//
// }
// public static function onBeforeWrite(Model $model)
// {
// if (!in_array(request()->action(), ['add'])) {
// return;
// }
//
// }
public function delModule($id){
if (strpos($id, ',') !== false) {
return -1;
}
// 当有栏目使用该模块时不可删除
// $count = \app\common\model\Cate::where('module_id', $id)->count();
// if ($count) {
// return ['error' => 1, 'msg' => '删除失败,请先删除已使用该模块的栏目'];
// }
$site_id = SITE_ID;
//获取module信息
$m_model = new Module();
//获取模型信息
$module_info = $m_model->find($id)->toArray();
if(!$module_info){
return -1;
}
$table_name = $this->cms_table.'_'.$module_info['table_name'].'_'.$site_id;
// 模块删除的同时删除字段管理中对应的数据
$field_model = new \app\api\model\Field();
$f_ids = $field_model->where('module_id', '=' , $id)->where('site_id', SITE_ID)->select()->toArray();
$f_ids_str = '';
$f_ids_arr = [];
foreach ($f_ids as $item){
$f_ids_arr[] = $item['id'];
}
$f_ids_str = implode(',',$f_ids_arr);
if($f_ids_str){
$field_model->parentDel($f_ids_str);
}
// $field_model->where('module_id', $id)->delete();
$state = $this->parentDel($id);
if($state){
//删除表
$tables = Db::getTables();
//表已存在不能创建该模型 需要修改表名
if(in_array($table_name,$tables)){
Db::execute("DROP TABLE `{$table_name}` ");
}
}
return $state;
}
public function addModule(){
$_param = $this->request->post();
if(!$_param){
return 0;
}
$site_id = SITE_ID;
$table_name = $this->cms_table.'_'.$_param['table_name'].'_'.$site_id;
$tables = Db::getTables();
//表已存在不能创建该模型 需要修改表名
if(in_array($table_name,$tables)){
return 0;
}else{
//添加数据
$module_id = $this->parentAdd($_param);
//创建表结构
if($module_id>0){
$sqlStr = "`id` int(11) unsigned NOT NULL AUTO_INCREMENT, " .
"`common_id` int(11) NOT NULL DEFAULT '0', ";
$sql = "CREATE TABLE `{$table_name}` (
{$sqlStr}
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COMMENT='".$_param['table_comment']."'";
Db::execute($sql);
$data = [
['module_id' => $module_id, 'field' => 'id', 'name' => '编号', 'type' => 'hidden', 'is_list' => '1', 'status' => '1', 'sort' => '1', 'remark' => '自增ID', 'setup' => "array ('default' => '0','extra_attr' => '','extra_class' => '','step' => '1','fieldtype' => 'int','group' => '')",'is_system' => '1'],
['module_id' => $module_id, 'field' => 'site_id', 'name' => '网站编号', 'type' => 'hidden', 'is_list' => '1', 'status' => '1', 'sort' => '1', 'remark' => '', 'setup' => "array ('default' => '1','extra_attr' => '','extra_class' => '','step' => '','fieldtype' => 'int','group' => '')",'is_system' => '1'],
['title' => $module_id, 'field' => 'title', 'name' => '标题', 'type' => 'text', 'is_list' => '1','is_edit'=>'1','is_add'=>'1', 'status' => '1', 'remark' => '', 'setup' => "array ('default' => '','extra_attr' => '','extra_class' => '','step' => '','fieldtype' => 'varchar','group' => '')",'is_system' => '1'],
['module_id' => $module_id, 'field' => 'common_id', 'name' => '主表id', 'type' => 'hidden', 'is_list' => '1', 'status' => '1', 'sort' => '1', 'remark' => '', 'setup' => "array ('default' => '0','extra_attr' => '','extra_class' => '','step' => '','fieldtype' => 'int','group' => '')",'is_system' => '1'],
['module_id' => $module_id, 'field' => 'create_time', 'name' => '添加时间', 'maxlength' => '11', 'type' => 'datetime', 'is_list' => '1', 'search_type' => '=', 'status' => '1', 'sort' => '1', 'remark' => '添加时间', 'setup' => "array ('default' => '0', 'format' => '', 'extra_attr' => '', 'extra_class' => '', 'placeholder' => '', 'fieldtype' => 'int',)",'is_system' => '1'],
['module_id' => $module_id, 'field' => 'update_time', 'name' => '更新时间', 'maxlength' => '11', 'type' => 'datetime', 'is_list' => '1', 'search_type' => '=', 'status' => '1', 'sort' => '1', 'remark' => '更新时间', 'setup' => "array ('default' => '0', 'format' => '', 'extra_attr' => '', 'extra_class' => '', 'placeholder' => '', 'fieldtype' => 'int',)",'is_system' => '1'],
['module_id' => $module_id, 'field' => 'sort', 'name' => '排序', 'required' => '1', 'maxlength' => '8', 'type' => 'number', 'is_add' => '1', 'is_edit' => '1', 'is_list' => '1', 'is_sort' => '1', 'search_type' => '=', 'status' => '1', 'sort' => '1', 'remark' => '', 'setup' => "array ('default' => '50', 'extra_attr' => '', 'extra_class' => '', 'step' => '1', 'fieldtype' => 'int',)",'is_system' => '1'],
['module_id' => $module_id, 'field' => 'status', 'name' => '状态', 'required' => '1', 'maxlength' => '1', 'type' => 'radio', 'data_source' => '1', 'dict_code' => '1', 'is_add' => '1', 'is_edit' => '1', 'is_list' => '1', 'is_search' => '1', 'is_sort' => '0', 'search_type' => '=', 'status' => '1', 'sort' => '1', 'remark' => '', 'setup' => "array ('default' => '1', 'extra_attr' => '', 'extra_class' => '', 'fieldtype' => 'tinyint',)",'is_system' => '1'],
[
'module_id' => $module_id,
'field' => 'cate_id',
'name' => '栏目',
'required' => '1',
'maxlength' => '0',
'type' => 'select',
'data_source' => '2',
'relation_model' => 'Cate',
'relation_field' => 'cate_name',
'is_add' => '1',
'is_edit' => '1',
'is_list' => '1',
'is_search' => '1',
'is_sort' => '0',
'search_type' => '=',
'status' => '1',
'sort' => '1',
'remark' => '栏目',
'setup' => "array ('default' => '0', 'extra_attr' => '', 'extra_class' => '', 'fieldtype' => 'tinyint',)"
,'is_system' => '1'
],
[
'module_id' => $module_id,
'field' => 'hits',
'name' => '点击次数',
'required' => '0',
'maxlength' => '0',
'type' => 'number',
'data_source' => '0',
'relation_model' => '',
'relation_field' => '',
'is_add' => '1',
'is_edit' => '1',
'is_list' => '1',
'is_search' => '0',
'is_sort' => '1',
'search_type' => '=',
'status' => '1',
'sort' => '1',
'remark' => '点击次数',
'setup' => "array ('default' => '0', 'extra_attr' => '', 'extra_class' => '', 'step' => '1', 'fieldtype' => 'int', )"
,'is_system' => '1'
],
[
'module_id' => $module_id,
'field' => 'keywords',
'name' => '关键词',
'required' => '0',
'maxlength' => '255',
'type' => 'text',
'data_source' => '0',
'relation_model' => '',
'relation_field' => '',
'is_add' => '1',
'is_edit' => '1',
'is_list' => '0',
'is_search' => '0',
'is_sort' => '0',
'search_type' => '=',
'status' => '1',
'sort' => '1',
'remark' => '关键词',
'setup' => "array ( 'default' => '', 'extra_attr' => '', 'extra_class' => '', 'placeholder' => '', 'fieldtype' => 'varchar', 'group' => '', )"
,'is_system' => '1'
],
[
'module_id' => $module_id,
'field' => 'description',
'name' => '描述',
'required' => '0',
'maxlength' => '255',
'type' => 'textarea',
'data_source' => '0',
'relation_model' => '',
'relation_field' => '',
'is_add' => '1',
'is_edit' => '1',
'is_list' => '0',
'is_search' => '0',
'is_sort' => '0',
'search_type' => '=',
'status' => '1',
'sort' => '1',
'remark' => '描述',
'setup' => "array ( 'default' => '', 'extra_attr' => '', 'extra_class' => '', 'placeholder' => '', 'fieldtype' => 'varchar', )"
,'is_system' => '1'
],
[
'module_id' => $module_id,
'field' => 'template',
'name' => '模板',
'tips' => '单独设置此条记录的模板,如:article_show.html 或 article_show',
'required' => '0',
'maxlength' => '30',
'type' => 'text',
'data_source' => '0',
'relation_model' => '',
'relation_field' => '',
'is_add' => '1',
'is_edit' => '1',
'is_list' => '0',
'is_search' => '0',
'is_sort' => '0',
'search_type' => '=',
'status' => '1',
'sort' => '1',
'remark' => '模板',
'setup' => "array ( 'default' => '', 'extra_attr' => '', 'extra_class' => '', 'placeholder' => '', 'fieldtype' => 'varchar', 'group' => '', )"
,'is_system' => '1'
],
[
'module_id' => $module_id,
'field' => 'url',
'name' => '跳转地址',
'tips' => '如需直接跳转,请填写完整的网站地址或相对地址',
'required' => '0',
'maxlength' => '255',
'type' => 'text',
'data_source' => '0',
'relation_model' => '',
'relation_field' => '',
'is_add' => '1',
'is_edit' => '1',
'is_list' => '0',
'is_search' => '0',
'is_sort' => '0',
'search_type' => '=',
'status' => '1',
'sort' => '1',
'remark' => '跳转地址',
'setup' => "array ( 'default' => '', 'extra_attr' => '', 'extra_class' => '', 'placeholder' => '', 'fieldtype' => 'varchar', 'group' => '',)"
,'is_system' => '1'
]
];
$fild = new \app\api\model\Field();
$fild->saveAll($data);
return $module_id;
}else{
return $module_id;
}
}
}
/**
* 获取数据列表基类模型
* @param null $_op 模型链式对象
* @param int $ispage 是否分页,默认分页
* @return array|\think\Collection
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function parentLists($_op = null, $ispage = 1,$issite=true)
{
//模型对象为空,设定默认查询对象
if($issite){
if (!is_object($_op) || empty($_op)) {
$_op = $this->where('site_id', SITE_ID); //没有默认where查询 这个一定要 否则获取不到下面的getOptions查询条件值
} else {
$_op->where('site_id', SITE_ID); //强制绑定站点参数
}
}
//公共搜索查询
if (count($this->search_arr) > 0) {
$_param = request()->param(); //搜索参数集合
$_op->where(function ($query) use ($_param) {
foreach ($this->search_arr as $k => $v) {
$search_arr = explode(':', $v);
$search_name = $search_arr[0];
$logic = $search_arr[1] ?? 'AND';
if (array_key_exists($search_name, $_param)) {
if (!empty($_param[$search_name])) {
$query->whereLike($search_name, "%{$_param[$search_name]}%", $logic);
}
}
}
});
}
//执行分页查询
$page = $this->request->param('page', config('page'));
$limit = $this->request->param('limit', config('limit'));
$count = $_op->count();
$this->listsOrder($_op); //统一排序
//无分页判断
$ispage != 1 ?: $_op->page((int)$page, (int)$limit);
$_data = [
'data' => $_op->select()->toArray(),
'page_total' => $count,
];
return $_data;
}
/**
* 数据排序
*/
private function listsOrder(&$_op)
{
if ($_op->getOptions('order')) {
return;
}
$sort = $this->request->get('sort');
$sort = in_array($sort, $this->checkAllowFields()) ? $sort : '';
$order = $this->request->get('order', 'DESC', 'strtoupper') == 'ASC' ? 'ASC' : 'DESC';
$sort_order = $sort . ' ' . $order;
if (empty($sort)) {
$sort_order = in_array('sort', $this->checkAllowFields()) ? 'sort DESC,create_time DESC' : 'create_time DESC';
}
$_op->order($sort_order);
}
}