239 lines
7.3 KiB
PHP
239 lines
7.3 KiB
PHP
<?php
|
|
|
|
namespace app\common\model;
|
|
|
|
use think\Model;
|
|
|
|
/**
|
|
* 基础扩展模型
|
|
*/
|
|
class BaseModel extends Model
|
|
{
|
|
|
|
|
|
use \traits\ModelTrait;
|
|
// use \traits\ErrorTrait;
|
|
|
|
public $timeKey = 'createtime';
|
|
public static $staticTimeKey = 'createtime';
|
|
|
|
//允许修改的字段
|
|
public $no_auth_fields = [];
|
|
|
|
//更新数据是否需要触发审核开关
|
|
|
|
public $have_auth = false;
|
|
|
|
|
|
|
|
|
|
public function checkAssemblyParameters($get=[],$exclude = []){
|
|
//得到所有get参数
|
|
$get = $get ?: request()->get();
|
|
//得到当前model所有字段
|
|
|
|
|
|
$fields = $this->getTableFields();
|
|
|
|
// $commonFields = (new Field())->getCommonFields();
|
|
// var_dump($commonFields);
|
|
$fieldLists = $fields;
|
|
// foreach ($commonFields as $commonField) {
|
|
// if (!in_array($commonField['column_name'], $fields)) {
|
|
// $fieldLists[] = $commonField;
|
|
// }
|
|
// }
|
|
$q_fields = [];
|
|
|
|
foreach ($get as $kay=>$getField) {
|
|
if (in_array($kay, $fieldLists) && !in_array($kay, $exclude)) {
|
|
$q_fields[$kay] = $getField;
|
|
}
|
|
}
|
|
|
|
return $q_fields;
|
|
|
|
//将q_fields塞入模板中
|
|
// foreach ($q_fields as $k=>$v) {
|
|
// //渲染站点配置
|
|
// $this->assign('q_'.$k, $v);
|
|
// }
|
|
|
|
// foreach ($this->qFields as $k) {
|
|
// //渲染站点配置
|
|
// if(!isset($q_fields[$k]))$this->assign('q_'.$k, "");
|
|
// }
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function no_auth_fields_check($params,$row){
|
|
|
|
foreach ($params as $k=>$v){
|
|
|
|
//说明数值有变动
|
|
//$params[$k] 去掉两端空格
|
|
$params[$k] = trim($v);
|
|
|
|
if($row[$k]!=$params[$k]){
|
|
//当修改参数不在允许修改的字段中
|
|
if(!in_array($k,$this->no_auth_fields)){
|
|
|
|
$this->have_auth = true;break;
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
return $this->have_auth;
|
|
|
|
}
|
|
|
|
|
|
/**得到基础条件
|
|
* @param $status
|
|
* @param null $model
|
|
* @param string $alisa
|
|
*/
|
|
public static function getBaseWhere($whereData = [], $model = null, $alisa = '',$with = false)
|
|
{
|
|
if (!$model) {
|
|
$model = new static;
|
|
if ($alisa&&!$with) $model = $model->alias($alisa);
|
|
}
|
|
if ($alisa) $alisa = $alisa . '.';
|
|
//$model = $model->where($alisa . 'status', '1');
|
|
$tableFields = (new static)->getTableFields();
|
|
foreach ($tableFields as $fields)
|
|
{
|
|
if (isset($whereData[$fields]) && $whereData[$fields]){
|
|
if(is_array($whereData[$fields])){
|
|
$model = $model->where("{$alisa}{$fields}", $whereData[$fields][0], $whereData[$fields][1]);
|
|
}else{
|
|
$model = $model->where("{$alisa}{$fields}", '=', $whereData[$fields]);
|
|
}
|
|
|
|
}
|
|
}
|
|
return $model;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 基础列表
|
|
*/
|
|
public function getBaseList($whereData = [], $page = 0, $limit = 0, $sort = '',$field =[],$where=[],$toArray = true)
|
|
{
|
|
$alisa = $this->getWithAlisaName();
|
|
if($field){
|
|
//如果是一维数组
|
|
if(is_array($field)&&count($field) == count($field,1)) $field = ['base'=>$field];
|
|
//如果是字符串
|
|
if(is_string($field)) $field = ['base'=>explode(',',$field)];
|
|
}
|
|
$this->withTable = array_keys($field);
|
|
$base_i = array_search("base",$this->withTable);
|
|
if($base_i!==false)unset($this->withTable[$base_i]);
|
|
if(!$this->withTable)$alisa = '';
|
|
$alisa_name = '';
|
|
if($alisa)$alisa_name = "{$alisa}.";
|
|
if(!$sort)$sort = "{$alisa_name}id asc";
|
|
$self = static::getBaseWhere($whereData, null, $alisa,true);
|
|
if($this->withTable)$self = $self->with($this->withTable);
|
|
if($page&&$limit)$self = $self->orderRaw($sort)->where($where)->page($page, $limit);
|
|
$list = $self->select();
|
|
foreach ($list as $row) {
|
|
|
|
if(isset($field['base'])&&$field['base']!=['*']){
|
|
$row->visible($field['base']);
|
|
}else{
|
|
$getTableFields = $this->getTableFields();
|
|
if(!empty($this->hidden) && is_array($this->hidden)){
|
|
$getTableFields = array_diff($getTableFields,$this->hidden);
|
|
}
|
|
$row->visible($getTableFields);
|
|
}
|
|
foreach ($this->withTable as $withName) {
|
|
if(isset($field[$withName])&&$field[$withName]!=['*']){
|
|
$row->visible([$withName]);
|
|
$row->getRelation($withName)->visible($field[$withName]);
|
|
}elseif(isset($field[$withName])&&$field[$withName]==['*']){
|
|
$row->visible([$withName]);
|
|
}
|
|
}
|
|
}
|
|
if($toArray)$list = collection($list)->toArray();
|
|
$countSelf = static::getBaseWhere($whereData, null, $alisa,true);
|
|
if($this->withTable)$countSelf = $countSelf->with($this->withTable);
|
|
$count = $countSelf->where($where)->count();
|
|
return compact('list', 'count','page','limit');
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* 时间段搜索器
|
|
* @param Model $query
|
|
* @param $value
|
|
*/
|
|
public function scopeTime($query, $value)
|
|
{
|
|
$timeKey = $this->timeKey;
|
|
if(static::$staticTimeKey)$timeKey =static::$staticTimeKey;
|
|
|
|
if(is_array($value)){
|
|
$timeKey = $value[0];
|
|
$value = $value[1];
|
|
}
|
|
|
|
|
|
switch ($value) {
|
|
case 'today':
|
|
case 'week':
|
|
case 'month':
|
|
case 'year':
|
|
case 'yesterday':
|
|
case 'last year':
|
|
case 'last week':
|
|
case 'last month':
|
|
$query->whereTime($timeKey, $value);
|
|
break;
|
|
case 'quarter':
|
|
list($startTime, $endTime) = static::getMonth();
|
|
$query->whereTime($timeKey, 'between', [$startTime, $endTime]);
|
|
break;
|
|
case 'lately7':
|
|
$query->whereTime($timeKey, 'between', [strtotime("-7 day"), time()]);
|
|
break;
|
|
case 'lately30':
|
|
$query->whereTime($timeKey, 'between', [strtotime("-30 day"), time()]);
|
|
break;
|
|
default:
|
|
if (strstr($value, '---') !== false||strstr($value, '-') !== false) {
|
|
if(strstr($value, '---') !== false){
|
|
[$startTime, $endTime] = explode('---', $value);
|
|
}elseif (strstr($value, '-') !== false){
|
|
[$startTime, $endTime] = explode('-', $value);
|
|
}
|
|
$startTime = trim($startTime);
|
|
$endTime = trim($endTime);
|
|
if ($startTime && $endTime) {
|
|
$query->whereTime($timeKey, 'between', [strtotime($startTime), $startTime == $endTime ? strtotime($endTime) + 86400 : strtotime($endTime)]);
|
|
} else if (!$startTime && $endTime) {
|
|
$query->whereTime($timeKey, '<', strtotime($endTime) + 86400);
|
|
} else if ($startTime && !$endTime) {
|
|
$query->whereTime($timeKey, '>=', strtotime($startTime));
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
|
|
|
|
}
|