1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
| <?php
namespace App\HttpController\Common\Logic;
use EasySwoole\ORM\AbstractModel;
class BaseLogic extends AbstractModel { const MODEL_INSERT = 1; const MODEL_UPDATE = 2; const MODEL_BOTH = 3; const MUST_VALIDATE = 1; const EXISTS_VALIDATE = 0; const VALUE_VALIDATE = 2;
public $data = []; public $old_data = []; public $tmp_data = []; public $request_user = []; public $request_user_id = 0;
public $patchValidate = false;
public $handle_sence; public $scene_id; protected $handle_field_arr = []; protected $handle_logs = []; protected $cur_sence_field; protected $allow_fields; protected $db_allow_fields;
protected $logic_auto = array(); protected $logic_validate = array();
public function init($request){ $this->request_user = $request->getAttribute('request_user'); $this->request_user_id = (int)$this->request_user['id']; return $this; }
public function request_user_id(){ return $this->request_user_id; }
public function getRequestUser(){ return $this->request_user; }
public function getError(){ return $this->error; }
public function getSaveData(){ return $this->data; }
public function getOldData(){ return $this->old_data; }
public function run($post_data,$handle_sence){ $this->data = null; $this->data = $post_data; $this->handle_sence = $handle_sence; $this->scene_id = $handle_sence; $cur_sence_field = $this->handle_field_arr[$handle_sence]; if(trim($cur_sence_field) == "*"){ $this->db_allow_fields = array_keys($this->schemaInfo()->getColumns()); $this->allow_fields = $this->db_allow_fields; }else{ $this->db_allow_fields = array_keys($this->schemaInfo()->getColumns()); $this->allow_fields = array_filter(explode(",",$cur_sence_field)); foreach ($this->data as $key => $value) { if(!in_array($key,$this->allow_fields)){ unset($this->data[$key]); unset($post_data[$key]); } } } if(!$this->_beforeAutoValidation($post_data,$handle_sence)) return false; if(!$this->_autoValidation($post_data,$handle_sence)) return false; if(!$this->_afterAutoValidation($post_data,$handle_sence)) return false; $this->_logic_auto_fill($post_data);
if(count($this->data) == 0){ $this->error = "无修改内容项"; return false; } return true; }
protected function _autoValidation($data, $handle_sence){ if(isset($this->logic_validate)) { if($this->patchValidate) { $this->error = array(); } foreach($this->logic_validate as $key=>$val) { if(empty($val[5]) || ( $val[5]== self::MODEL_BOTH && $handle_sence < 3 ) || $val[5]== $handle_sence ) { if(0==strpos($val[2],'{%') && strpos($val[2],'}')) $val[2] = L(substr($val[2],2,-1)); $val[3] = isset($val[3])?$val[3]:self::EXISTS_VALIDATE; $val[4] = isset($val[4])?$val[4]:'regex'; switch($val[3]) { case self::MUST_VALIDATE: if(false === $this->_validationField($data,$val)) return false; break; case self::VALUE_VALIDATE: if('' != trim($data[$val[0]])) if(false === $this->_validationField($data,$val)) return false; break; default: if(isset($data[$val[0]])) if(false === $this->_validationField($data,$val)) return false; } } } if(!empty($this->error)) return false; } return true; }
protected function _validationField($data,$val) { if($this->patchValidate && isset($this->error[$val[0]])) return ; if(false === $this->_validationFieldItem($data,$val)){ if($this->patchValidate) { $this->error[$val[0]] = $val[2]; }else{ $this->error = $val[2]; return false; } } return ; }
protected function _validationFieldItem($data,$val) { if(!isset($data[$val[0]])){ $this->error = "缺少必要的提交字段:".$val[0]; return; } if(false === $data[$val[0]] ) unset($data[$val[0]]); switch(strtolower(trim($val[4]))) { case 'function': case 'callback': $args = isset($val[6])?(array)$val[6]:array(); if(is_string($val[0]) && strpos($val[0], ',')) $val[0] = explode(',', $val[0]); if(is_array($val[0])){ foreach($val[0] as $field) $_data[$field] = $data[$field]; array_unshift($args, $_data); }else{ array_unshift($args, $data[$val[0]]); } if('function'==$val[4]) { return call_user_func_array($val[1], $args); }else{ return call_user_func_array(array(&$this, $val[1]), $args); } default: return false; } }
protected function _beforeAutoValidation($data, $handle_sence){ $this->tmp_data = null; $this->old_data = null; $this->error = null; return true; }
protected function _afterAutoValidation($data, $handle_sence) { if(defined('IS_DEMO') && IS_DEMO == true){ $this->error = "当前系统为演示demo系统,无法进行增删改"; return false; } return true; }
private function _logic_auto_fill($data){ if(isset($this->logic_auto)) { foreach ($this->logic_auto as $auto){ if($auto[2] == $this->handle_sence){ if(empty($auto[3])) $auto[3] = 'string'; switch(trim($auto[3])) { case 'function': case 'callback': $args = isset($auto[4])?(array)$auto[4]:array(); if(isset($data[$auto[0]])) { array_unshift($args,$data[$auto[0]]); } if('function'==$auto[3]) { if($auto[1] == 'time'){ $data[$auto[0]] = call_user_func_array($auto[1],[]); }else{ $data[$auto[0]] = call_user_func_array($auto[1], $args); } }else{ $data[$auto[0]] = call_user_func_array(array(&$this,$auto[1]), $args); } break; case 'string': default: $data[$auto[0]] = $auto[1]; } if(isset($data[$auto[0]]) && false === $data[$auto[0]] ) unset($data[$auto[0]]);
if(isset($data[$auto[0]])){ if(in_array($auto[0],$this->db_allow_fields)){ $this->data[$auto[0]] = $data[$auto[0]]; } } } } } } }
|