服务类 php源码,服务层基类源码 · ThinkPHP5高阶实战教程 --诠释为API开发而生 · 看云...

# 服务层基类源码

>[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;

~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值