From 9e6fba335dbc3c3136140b082f7e2cc0d1077928 Mon Sep 17 00:00:00 2001 From: molong Date: Mon, 26 Jun 2017 15:05:11 +0800 Subject: [PATCH] update --- addons/syslogin/controller/Admin.php | 4 - application/admin/controller/Content.php | 2 +- application/common.php | 9 + application/common/model/BaseModel.php | 239 +++++++++++++++++++++++ application/common/model/Content.php | 225 +-------------------- application/common/model/DiyForm.php | 17 ++ application/common/model/Form.php | 17 ++ application/index/controller/Content.php | 2 +- 8 files changed, 286 insertions(+), 229 deletions(-) create mode 100644 application/common/model/BaseModel.php create mode 100644 application/common/model/DiyForm.php create mode 100644 application/common/model/Form.php diff --git a/addons/syslogin/controller/Admin.php b/addons/syslogin/controller/Admin.php index c57af263..9848f7b2 100644 --- a/addons/syslogin/controller/Admin.php +++ b/addons/syslogin/controller/Admin.php @@ -11,10 +11,6 @@ namespace addons\syslogin\controller; use app\common\controller\Addons; class Admin extends Addons{ - - public function lists(){ - - } public function setting(){ $this->setMeta('第三方登录设置'); diff --git a/application/admin/controller/Content.php b/application/admin/controller/Content.php index 4487dbaa..1d8e6d06 100644 --- a/application/admin/controller/Content.php +++ b/application/admin/controller/Content.php @@ -22,7 +22,7 @@ class Content extends Admin { return $this->error("无此模型!"); } else { $this->modelInfo = $list[$model_id]; - $this->model = new \app\common\model\Content($this->modelInfo['name']); + $this->model = M($this->modelInfo['name']); } $this->assign('model_id', $model_id); diff --git a/application/common.php b/application/common.php index 4a7d1f94..2300b775 100644 --- a/application/common.php +++ b/application/common.php @@ -997,6 +997,15 @@ function send_email($to, $subject, $message) { return $email->send(); } +//实例化模型 +function M($name, $type = 'model'){ + if ($type == 'model') { + return new \app\common\model\Content(strtolower($name)); + }elseif ($type == 'form'){ + return new \app\common\model\DiyForm(strtolower($name)); + } +} + //php获取中文字符拼音首字母 function getFirstCharter($s0) { $fchar = ord($s0{0}); diff --git a/application/common/model/BaseModel.php b/application/common/model/BaseModel.php new file mode 100644 index 00000000..a3f09339 --- /dev/null +++ b/application/common/model/BaseModel.php @@ -0,0 +1,239 @@ + +// +---------------------------------------------------------------------- + +namespace app\common\model; + +use \think\Validate; + +/** + * @title 基础模型 + * 自定义基础模型 + */ +class BaseModel { + + protected $data; + protected $autoWriteTimestamp = true; + protected $auto = array('update_time'); + protected $insert = array('create_time'); + protected $update = array(); + // 创建时间字段 + protected $createTime = 'create_time'; + // 更新时间字段 + protected $updateTime = 'update_time'; + // 时间字段取出后的默认时间格式 + protected $dateFormat; + // 字段类型或者格式转换 + protected $type = []; + + public function __construct($name) { + $this->db = db($name); + } + + public function save($data, $where = array()) { + $this->data = $data; + $rule = $msg = array(); + $attr = db('Attribute')->where('id', $data['model_id'])->select(); + foreach ($attr as $key => $value) { + if ($value['is_must'] == 1) { + $rule[$value['name']] = "require"; + $msg[$value['name'] . '.require'] = $value['title'] . "不能为空!"; + } + } + $validate = new Validate($rule, $msg); + $result = $validate->check($data); + if (!$result) { + $this->error = $validate->getError(); + return false; + } + $this->autoCompleteData($this->auto); + if (!empty($where)) { + $this->autoCompleteData($this->update); + return $this->where($where)->update($this->data); + } else { + $this->autoCompleteData($this->insert); + return $this->insert($this->data); + } + } + + /** + * 返回模型的错误信息 + * @access public + * @return string|array + */ + public function getError() { + return $this->error; + } + + /** + * 数据自动完成 + * @access public + * @param array $auto 要自动更新的字段列表 + * @return void + */ + protected function autoCompleteData($auto = []) { + foreach ($auto as $field => $value) { + if (is_integer($field)) { + $field = $value; + $value = null; + } + + if (!isset($this->data[$field])) { + $default = null; + } else { + $default = $this->data[$field]; + } + + $this->setAttr($field, !is_null($value) ? $value : $default); + } + } + /** + * 修改器 设置数据对象值 + * @access public + * @param string $name 属性名 + * @param mixed $value 属性值 + * @param array $data 数据 + * @return $this + */ + public function setAttr($name, $value, $data = []) { + if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) { + // 自动写入的时间戳字段 + $value = $this->autoWriteTimestamp($name); + } else { + // 检测修改器 + $method = 'set' . Loader::parseName($name, 1) . 'Attr'; + if (method_exists($this, $method)) { + $value = $this->$method($value, array_merge($this->data, $data)); + } elseif (isset($this->type[$name])) { + // 类型转换 + $value = $this->writeTransform($value, $this->type[$name]); + } + } + + // 设置数据对象属性 + $this->data[$name] = $value; + return $this; + } + + /** + * 自动写入时间戳 + * @access public + * @param string $name 时间戳字段 + * @return mixed + */ + protected function autoWriteTimestamp($name) { + if (isset($this->type[$name])) { + $type = $this->type[$name]; + if (strpos($type, ':')) { + list($type, $param) = explode(':', $type, 2); + } + switch ($type) { + case 'datetime': + case 'date': + $format = !empty($param) ? $param : $this->dateFormat; + $value = $this->formatDateTime(time(), $format); + break; + case 'timestamp': + case 'integer': + default: + $value = time(); + break; + } + } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [ + 'datetime', + 'date', + 'timestamp', + ]) + ) { + $value = $this->formatDateTime(time(), $this->dateFormat); + } else { + $value = $this->formatDateTime(time(), $this->dateFormat, true); + } + return $value; + } + + /** + * 时间日期字段格式化处理 + * @access public + * @param mixed $time 时间日期表达式 + * @param mixed $format 日期格式 + * @param bool $timestamp 是否进行时间戳转换 + * @return mixed + */ + protected function formatDateTime($time, $format, $timestamp = false) { + if (false !== strpos($format, '\\')) { + $time = new $format($time); + } elseif (!$timestamp && false !== $format) { + $time = date($format, $time); + } + return $time; + } + + /** + * 数据写入 类型转换 + * @access public + * @param mixed $value 值 + * @param string|array $type 要转换的类型 + * @return mixed + */ + protected function writeTransform($value, $type) { + if (is_null($value)) { + return; + } + + if (is_array($type)) { + list($type, $param) = $type; + } elseif (strpos($type, ':')) { + list($type, $param) = explode(':', $type, 2); + } + switch ($type) { + case 'integer': + $value = (int) $value; + break; + case 'float': + if (empty($param)) { + $value = (float) $value; + } else { + $value = (float) number_format($value, $param, '.', ''); + } + break; + case 'boolean': + $value = (bool) $value; + break; + case 'timestamp': + if (!is_numeric($value)) { + $value = strtotime($value); + } + break; + case 'datetime': + $format = !empty($param) ? $param : $this->dateFormat; + $value = is_numeric($value) ? $value : strtotime($value); + $value = $this->formatDateTime($value, $format); + break; + case 'object': + if (is_object($value)) { + $value = json_encode($value, JSON_FORCE_OBJECT); + } + break; + case 'array': + $value = (array) $value; + case 'json': + $option = !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE; + $value = json_encode($value, $option); + break; + case 'serialize': + $value = serialize($value); + break; + + } + return $value; + } + public function __call($method, $args) { + return call_user_func_array([$this->db, $method], $args); + } +} \ No newline at end of file diff --git a/application/common/model/Content.php b/application/common/model/Content.php index 40fd15a5..a3dbb32e 100644 --- a/application/common/model/Content.php +++ b/application/common/model/Content.php @@ -9,230 +9,9 @@ namespace app\common\model; -use \think\Validate; - /** * 设置模型 */ -class Content { - - protected $data; - protected $autoWriteTimestamp = true; - protected $auto = array('update_time'); - protected $insert = array('create_time'); - protected $update = array(); - // 创建时间字段 - protected $createTime = 'create_time'; - // 更新时间字段 - protected $updateTime = 'update_time'; - // 时间字段取出后的默认时间格式 - protected $dateFormat; - // 字段类型或者格式转换 - protected $type = []; - - public function __construct($name) { - $this->db = db($name); - } - - public function save($data, $where = array()) { - $this->data = $data; - $rule = $msg = array(); - $attr = db('Attribute')->where('id', $data['model_id'])->select(); - foreach ($attr as $key => $value) { - if ($value['is_must'] == 1) { - $rule[$value['name']] = "require"; - $msg[$value['name'] . '.require'] = $value['title'] . "不能为空!"; - } - } - $validate = new Validate($rule, $msg); - $result = $validate->check($data); - if (!$result) { - $this->error = $validate->getError(); - return false; - } - $this->autoCompleteData($this->auto); - if (!empty($where)) { - $this->autoCompleteData($this->update); - return $this->where($where)->update($this->data); - } else { - $this->autoCompleteData($this->insert); - return $this->insert($this->data); - } - } - - /** - * 返回模型的错误信息 - * @access public - * @return string|array - */ - public function getError() { - return $this->error; - } - - /** - * 数据自动完成 - * @access public - * @param array $auto 要自动更新的字段列表 - * @return void - */ - protected function autoCompleteData($auto = []) { - foreach ($auto as $field => $value) { - if (is_integer($field)) { - $field = $value; - $value = null; - } - - if (!isset($this->data[$field])) { - $default = null; - } else { - $default = $this->data[$field]; - } - - $this->setAttr($field, !is_null($value) ? $value : $default); - } - } - /** - * 修改器 设置数据对象值 - * @access public - * @param string $name 属性名 - * @param mixed $value 属性值 - * @param array $data 数据 - * @return $this - */ - public function setAttr($name, $value, $data = []) { - if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) { - // 自动写入的时间戳字段 - $value = $this->autoWriteTimestamp($name); - } else { - // 检测修改器 - $method = 'set' . Loader::parseName($name, 1) . 'Attr'; - if (method_exists($this, $method)) { - $value = $this->$method($value, array_merge($this->data, $data)); - } elseif (isset($this->type[$name])) { - // 类型转换 - $value = $this->writeTransform($value, $this->type[$name]); - } - } - - // 设置数据对象属性 - $this->data[$name] = $value; - return $this; - } - - /** - * 自动写入时间戳 - * @access public - * @param string $name 时间戳字段 - * @return mixed - */ - protected function autoWriteTimestamp($name) { - if (isset($this->type[$name])) { - $type = $this->type[$name]; - if (strpos($type, ':')) { - list($type, $param) = explode(':', $type, 2); - } - switch ($type) { - case 'datetime': - case 'date': - $format = !empty($param) ? $param : $this->dateFormat; - $value = $this->formatDateTime(time(), $format); - break; - case 'timestamp': - case 'integer': - default: - $value = time(); - break; - } - } elseif (is_string($this->autoWriteTimestamp) && in_array(strtolower($this->autoWriteTimestamp), [ - 'datetime', - 'date', - 'timestamp', - ]) - ) { - $value = $this->formatDateTime(time(), $this->dateFormat); - } else { - $value = $this->formatDateTime(time(), $this->dateFormat, true); - } - return $value; - } - - /** - * 时间日期字段格式化处理 - * @access public - * @param mixed $time 时间日期表达式 - * @param mixed $format 日期格式 - * @param bool $timestamp 是否进行时间戳转换 - * @return mixed - */ - protected function formatDateTime($time, $format, $timestamp = false) { - if (false !== strpos($format, '\\')) { - $time = new $format($time); - } elseif (!$timestamp && false !== $format) { - $time = date($format, $time); - } - return $time; - } - - /** - * 数据写入 类型转换 - * @access public - * @param mixed $value 值 - * @param string|array $type 要转换的类型 - * @return mixed - */ - protected function writeTransform($value, $type) { - if (is_null($value)) { - return; - } - - if (is_array($type)) { - list($type, $param) = $type; - } elseif (strpos($type, ':')) { - list($type, $param) = explode(':', $type, 2); - } - switch ($type) { - case 'integer': - $value = (int) $value; - break; - case 'float': - if (empty($param)) { - $value = (float) $value; - } else { - $value = (float) number_format($value, $param, '.', ''); - } - break; - case 'boolean': - $value = (bool) $value; - break; - case 'timestamp': - if (!is_numeric($value)) { - $value = strtotime($value); - } - break; - case 'datetime': - $format = !empty($param) ? $param : $this->dateFormat; - $value = is_numeric($value) ? $value : strtotime($value); - $value = $this->formatDateTime($value, $format); - break; - case 'object': - if (is_object($value)) { - $value = json_encode($value, JSON_FORCE_OBJECT); - } - break; - case 'array': - $value = (array) $value; - case 'json': - $option = !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE; - $value = json_encode($value, $option); - break; - case 'serialize': - $value = serialize($value); - break; - - } - return $value; - } - public function __call($method, $args) { - return call_user_func_array([$this->db, $method], $args); - } +class Content extends BaseModel{ + } \ No newline at end of file diff --git a/application/common/model/DiyForm.php b/application/common/model/DiyForm.php new file mode 100644 index 00000000..4f82c568 --- /dev/null +++ b/application/common/model/DiyForm.php @@ -0,0 +1,17 @@ + +// +---------------------------------------------------------------------- + +namespace app\common\model; + +/** + * 自定义表单模型 + */ +class DiyForm extends BaseModel{ + +} \ No newline at end of file diff --git a/application/common/model/Form.php b/application/common/model/Form.php new file mode 100644 index 00000000..e6858ada --- /dev/null +++ b/application/common/model/Form.php @@ -0,0 +1,17 @@ + +// +---------------------------------------------------------------------- + +namespace app\common\model; + +/** + * 表单 + */ +class Form extends Base{ + +} \ No newline at end of file diff --git a/application/index/controller/Content.php b/application/index/controller/Content.php index 100f80e5..cdbd4f76 100644 --- a/application/index/controller/Content.php +++ b/application/index/controller/Content.php @@ -164,7 +164,7 @@ class Content extends Fornt { return $this->error("无此模型!"); } else { $this->modelInfo = $model_name ? $name_list[$model_name] : $id_list[$model_id]; - $this->model = new \app\common\model\Content($this->modelInfo['name']); + $this->model = new M($this->modelInfo['name']); $this->assign('model_id', $this->modelInfo['id']); $this->assign('model_list', $name_list);