<?php<web88@qq.com> 201808301514
namespace app\common\controller;
use think\Controller;
use org\Strs;
use think\facade\Response;
use think\exception\HttpResponseException;
use lib\ReturnCode;
use lib\Auth;
class Base extends Controller
{
protected $tenant_id = 0;
protected $uid = 0;
protected $noNeedLogin = [];
protected $noNeedRight = [];
protected $auth = null;
protected $model = null;
protected $whereFields = '';
protected $searchFields = 'id';
protected $statusField = false;
protected $relationSearch = false;
protected $dataLimit = false;
protected $dataLimitField = 'admin_id';
protected $dataLimitFieldAutoFill = true;
protected $validate = null;
protected $modelSceneValidate = true;
protected $validateAction = ['add','edit','del','save','editfield'];
protected $multiFields = 'status';
protected $selectpageFields = '*';
protected $defaultSort = null;
protected $importHeadType = 'comment';
protected $with = null;
public function initialize() {
parent::initialize();
}
public function _empty(){
}
protected function whereBuild($searchfields = null, $relationSearch = null){
$searchfields = is_null($searchfields) ? $this->searchFields : $searchfields;
$relationSearch = is_null($relationSearch) ? $this->relationSearch : $relationSearch;
$whereFields = $this->whereFields;
$key = $this->request->param('key',null);
$where = $this->request->param('where',''); $page = $this->request->param('page',1);
$limit = $this->request->param('limit',10);
$sort = $this->request->param("sort", "id");
$order = $this->request->param("order", "DESC");
$wheres=[];
$arr = json_decode($where,true);
if ($where && is_array($arr)){
foreach ($arr as $k=>$v) {
if (empty($v))
continue;
if (is_array($v) && isset($v[0]) && isset($v[1])){
if (isset($v[2])){
$wheres[]=[$v[0],$v[1],$v[2]];
}else{
$wheres[]=[$k,$v[1],$v[0]];
}
}elseif(is_string($v)){
$wheres[]=[$k,'=',$v];
}
}
}
if ($whereFields){
if (is_string($whereFields)){
$whereFields = explode(',',$whereFields);
}
if (is_array($whereFields)){
$params = $this->param($whereFields);
foreach ($whereFields as $v){
if (isset($params[$v]) && $params[$v]){
$wheres[]=[$v,'=',$params[$v]];
}
}
}
}
$search = $this->whereKey($searchfields);
if ($search && is_array($search)){
$wheres=array_merge($wheres,$search);
}
if ($this->statusField){
$wheres=array_merge($wheres,$this->whereStatus());
}
if ($this->defaultSort && empty($this->request->param('sort'))){
$sortOrder = $this->defaultSort;
}else{
$sortOrder=[$sort=>$order];
}
return [$wheres,$sortOrder,$page,$limit];
}
protected function whereKey($field=null){
$key=$this->request->param('key',null);
if (empty($key)) return false;
$field = is_null($field) ? $this->searchFields : $field;
if (!empty($field)){
if (is_numeric($field)) {
$map[]=[$field,'=',$key];
}else{
if(is_array($field)){
$field = implode('|',$field);
}else{
$field = str_replace(',','|',$field);
}
$map[]=[$field,'like',(string)'%'.$key.'%'];
}
return $map;
}
$fields=$this->model->getFieldsType()
$string_field=['varchar','char','text']; $re_field=['create_time','update_time','delete_time','action_ip'] $sfield=$intfield='';
$map=[];
foreach ($fields as $k=>$v){
if (in_array($k, $re_field)) {
continue;
}
$type=explode('(', $v);
$type=$type[0];
if (in_array($type, array('tinyint','bigint'))) {
continue;
}
if (is_numeric($key)) {
$intfield .= $intfield ? '|'.$k:$k;
}else {
if(in_array($type, $string_field)){
$sfield .= $sfield ? '|'.$k:$k;
}
}
}
if ($intfield) {
$map[]=[$intfield,'=',$key];
}elseif ($sfield){
$map[]=[$sfield,'like',(string)'%'.$key.'%'];
}
return $map;
}
protected function whereStatus($field=''){
if ($this->request->param('isstatus')){
return false;
}
$field = $field == true ? $field:($this->statusField && is_string($this->statusField)) ? $this->statusField : 'status';
$status=$this->request->param($field);
if (isset($status) && $status !== '') {
$map[]=[$field,'=',$status];
}else {
$map[]=[$field,'gt', -1];
}
return $map;
}
protected function whereTime(){
$map=[];
$start_field='create_time';
$end_field='create_time';
$start_time='';
$end_time='';
$input=$this->request->get();
foreach ($input as $k=>$v){
if ($start=strstr($k, '-start',true)) {
$sfield = Strs::upperFormat($start);
if (strpos($sfield, '_') !== false) {
$start_field=$sfield;
}
$start_time=$v;
}
if ($end=strstr($k, '-end',true)) {
$efield= Strs::upperFormat($end);
if (strpos($efield, '_') !== false) {
$end_field=$efield;
}
$end_time=$v;
}
}
if (empty($start_time) && empty($end_time)){
return false;
}
if ($start_time && $end_time && preg_match('/\d{4}\-\d{1,2}\-\d{1,2}/', $start_time) && preg_match('/\d{4}\-\d{1,2}\-\d{1,2}/', $end_time)) {
$map[]=[$start_field,'between time',[$start_time,$end_time.' 23:59:59']];
}elseif ($start_time && preg_match('/\d{4}\-\d{1,2}\-\d{1,2}/', $start_time)) {
$map[]=[$start_field,'> time',$start_time];
}elseif ($end_time && preg_match('/\d{4}\-\d{1,2}\-\d{1,2}/', $end_time)) {
$map[]=[$start_field,'<= time',$end_time.' 23:59:59'];
}
return $map;
}
protected function buildParam($array=[])
{
$data=[];
if (is_array($array)&&!empty($array)){
foreach( $array as $item=>$value ){
$data[$item] = $this->request->param($value);
}
}
return $data;
}
protected function param($name='', $type = 'param')
{
if (empty($name)){
$name = $this->model->getTableFields();
}
$data = $this->request->only($name,$type);
if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
$data[$this->dataLimitField] = $this->auth->uid;
}
$data['uid'] = $this->auth->getUid();
$data['tenant_id'] = $this->tenant_id;
return $data;
}
protected function isParam($param='id'){
if (empty($this->request->param($param))){
$this->result([],ReturnCode::EXCEPTION,'参数'.$param.'错误!');
}
return $this->request->param($param);
}
protected function reSuccess($data=1, $code = 0, $msg = '成功', array $header = []){
return $this->result($data, $code, $msg, $type = '', $header);
}
protected function reError($code = 999, $msg = '', array $header = []){
if (empty($msg) && isset(ReturnCode::$returnCode[$code])) {
$msg = ReturnCode::$returnCode[$code];
}
return $this->result([], $code, $msg, $type = '', $header);
}
protected function reTable($data = [],$count=10,$code=0, $msg = '')
{
$result = [
'code' => $code,
'msg' => $msg,
'count' => $count,
'time' => time(),
'data' => $data,
];
$response = Response::create($result, 'json');
throw new HttpResponseException($response);
}
protected function match($arr = [])
{
$arr = is_array($arr) ? $arr : explode(',', $arr);
if (!$arr) {
return false;
}
$arr = array_map('strtolower', $arr);
if (in_array(strtolower($this->request->action()), $arr) || in_array('*', $arr)) {
return true;
}
return false;
}
public function validateAuto($data=[], $validate='', $message = [], $batch = false, $callback = null)
{
$this->validate= $validate ? "app\\common\\validate\\" .$validate:$this->validate;
if (empty($this->validate) && $this->model){
$this->validate = "app\\common\\validate\\" .$this->model->getName();
if (!class_exists($this->validate)){
$controller = $this->request->controller();
if (strpos($controller,'.') === false){
$this->validate = "app\\common\\validate\\" . ucfirst($controller);
}else{
$group=strstr($controller,'.',true);
$name=substr(strstr($controller,'.'),1);
$this->validate = 'app\\common\\validate\\'.strtolower($group).'\\'.ucfirst($group).ucfirst($name);
}
}
}
$params = $data ? $data:$this->request->param();
$scene = $this->request->action();
if (class_exists($this->validate)) {
if ($this->modelSceneValidate){
$v = $this->app->validate($this->validate);
if ($v->hasScene($scene)) {
$result = $this->validate($params, $this->validate . '.' . $scene,$message,$batch,$callback);
if (true !== $result) {
$this->reError(ReturnCode::VALIDATE_ERROR,$result);
}
}
}else{
$result = $this->validate($params, $this->validate,$message,$batch,$callback);
if (true !== $result) {
$this->reError(ReturnCode::VALIDATE_ERROR,$result);
}
}
}
}
}