}
效果
基础模型类
<?php namespace Root\Model; use Think\Model; /** * 模型基础类 */ class BaseModel extends Model { /** * 解密加密数组 * @param type $data 提交数组 * @param type $flag 过滤数组非数据库字段 开关 * @return type */ public function fields_data_decode($data = array(), $flag = false) { if (!C('MODEL_FIELD_FLAG')) { return $data; } if (empty($data)) { $data = I('post.'); } elseif (is_object($data)) { $data = get_object_vars($data); } $fields = $this->getDbFields(); $table_name = $this->getTableName(); // 检查字段映射 $encode_method = function_exists(C('MODEL_FIELD_EMCODE')) ? C('MODEL_FIELD_EMCODE') : 'md5'; //加密方法 $encode_method_key = C('MODEL_FIELD_EMCODE_KEY') . $table_name; $field_name_pre = C('MODEL_FIELD_NAME_PRE'); if (!empty($fields)) { foreach ($fields as $key => $val) { $key_field = $field_name_pre . call_user_func($encode_method, $val . $encode_method_key); if (isset($data[$key_field])) { $data[$val] = $data[$key_field]; unset($data[$key_field]); } } if ($flag) { foreach ($data as $k => $v) { if (!in_array($k, $fields)) { unset($data[$k]); } } } } return $data; } public function fields_name_encode() { $fields = $this->getDbFields(); $table_name = $this->getTableName(); // 检查字段映射 $encode_method = function_exists(C('MODEL_FIELD_EMCODE')) ? C('MODEL_FIELD_EMCODE') : 'md5'; //加密方法 $encode_method_key = C('MODEL_FIELD_EMCODE_KEY') . $table_name; $field_name_pre = C('MODEL_FIELD_NAME_PRE'); if (!empty($fields)) { foreach ($fields as $key => $val) { if (C('MODEL_FIELD_FLAG')) { $fields[$val] = $field_name_pre . call_user_func($encode_method, $val . $encode_method_key); } else { $fields[$val] = $val; } unset($fields[$key]); } } return $fields; } public function create_decode($data = '', $type = '') { // 如果没有传值默认取POST数据 if (empty($data)) { $data = I('post.'); } elseif (is_object($data)) { $data = get_object_vars($data); } $data = $this->fields_data_decode($data); return $this->create($data, $type); } }
表单 对数据库字段自动加密解密表单提交(THINKPHP3.2),布布扣,bubuko.com
表单 对数据库字段自动加密解密表单提交(THINKPHP3.2)
原文:http://blog.csdn.net/lhb_blog/article/details/37056023