# 服务层基类源码
>[danger] 在服务层的析构函数中 我对任何的操作进行了入库处理
~~~
/**
* Created by PhpStorm.
* User: Mikkle
* QQ:776329498
* Date: 2017/11/18
* Time: 16:22
*/
namespace app\base\service\base;
use app\base\service\center\OptionsCenter;
use app\base\service\center\RandNumCenter;
use think\Db;
use think\Request;
abstract class ServiceBase
{
protected $model;
protected $error;
protected $optionNum;
protected $timeString;
protected $className;
protected $functionName;
protected $args;
public function __construct()
{
$this->className =get_called_class();
$this->optionNum = RandNumCenter::createOperateSerialNumber();
$this->timeString = RandNumCenter::getTimeString();
$this->_initialize();
}
abstract public function _initialize();
protected function getInfoArray($map=[]){
if (!isset($map["status"])){
$map["status"] = 1 ;
}
$result = $this->model->where($map)->find();
if ($result){
return $result->toArray();
}else{
return [];
}
}
protected function getInfoObject($map=[]){
if (!isset($map["status"])){
$map["status"] = 1 ;
}
return $this->model->where($map)->find();
}
public function addError($error){
$this->error = is_string($error) ? $error : json_encode($error);
}
protected function getError(){
return $this->error;
}
public function __destruct()
{
$operateData = [
"number" => $this->optionNum,
"class" => $this->className,
"function" => $this->functionName,
"args" => is_string($this->args) ? $this->args : json_encode($this->args),
"error" => $this->error ? $this->error : null,
"ip" => Request::instance()->ip(),
"time" => $this->timeString,
];
Db::table(OptionsCenter::$logServiceOperate)->insert($operateData);
}
}
~~~
### 操作数据库结构
~~~
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for mk_log_service_operate
-- ----------------------------
DROP TABLE IF EXISTS `mk_log_service_operate`;
CREATE TABLE `mk_log_service_operate` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`number` varchar(30) DEFAULT NULL,
`class` varchar(100) DEFAULT NULL,
`function` varchar(50) DEFAULT NULL,
`error` varchar(100) DEFAULT NULL,
`ip` varchar(20) DEFAULT NULL,
`args` varchar(1000) DEFAULT NULL,
`time` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
~~~