C层,操控数据库,并处理页面数据展示。
M层,纯粹的操作自己所对应的数据库。
Service层,可以通用的处理一些逻辑计算,也可以将复杂的数据表处理整合到一起,也可以将复杂的业务逻辑整合到一起。
创建了一个CommonService
<?php
/**
* Created by PhpStorm.
* User: jiqing
* Date: 18-12-21
* Time: 下午8:49
*/
namespace appcommonservice;
// 服务层,介于C层与M层之间
/** 根据上面的分析,Service夹在C层和M层中间,从逻辑上大致划分为3大类:
### model侧的Service:也就是封装每个model与业务相关的通用数据接口,比如:查询订单。(我认为:访问远程服务获取数据也应该归属于这一类Service)
### 中间的Service:封装通用的业务逻辑,比如:计算订单折扣(会用到1中的Service)。
### controller侧的Service:基于1、2中的Service进一步封装对外接口的用户业务逻辑。
**/
class CommonService
{
protected $out_data;
// 构造函数
public function __construct()
{
$this->out_data = ['errno'=>0,'errdesc'=>''];
}
public function set_err($errno,$errdesc) {
$this->out_data['errno'] = $errno;
$this->out_data['errdesc'] = $errdesc;
}
public function set_data($data) {
$this->out_data['data'] = $data;
}
}
主要用于输出数据的设置。
用户层的服务,
<?php
/**
* Created by PhpStorm.
* User: jiqing
* Date: 18-12-21
* Time: 下午8:49
*/
namespace appcommonservice;
// 服务层,介于C层与M层之间
/** 根据上面的分析,Service夹在C层和M层中间,从逻辑上大致划分为3大类:
### model侧的Service:也就是封装每个model与业务相关的通用数据接口,比如:查询订单。(我认为:访问远程服务获取数据也应该归属于这一类Service)
### 中间的Service:封装通用的业务逻辑,比如:计算订单折扣(会用到1中的Service)。
### controller侧的Service:基于1、2中的Service进一步封装对外接口的用户业务逻辑。
**/
use appcommonmodelUserAuditLogModel;
use appcommonmodelUserModel;
class UserService extends CommonService
{
public function audit_user($id,$is_pass,$reason) {
if (!$id || !$is_pass) {
$this->set_err('10001','参数缺失');
return $this->out_data;
}
// 处理审核
$user = new UserModel();
$user_info = $user->where('id',$id)->find();
if (!$user_info) {
$this->set_err('10002','用户不存在');
return $this->out_data;
}
if ($is_pass == 1) { // 通过
$edit_data = [
'status' => UserModel::USER_STATUS_PASS,
'audit_time' => time()
];
} else {
$edit_data = [
'status' => UserModel::USER_STATUS_NOT_PASS,
'audit_time' => time()
];
}
$user->startTrans();
$err_count = 0;
$res = $user->save($edit_data,['id'=>$id]);
if (!$res) {
$err_count++;
}
if ($user_info['type'] == UserModel::USER_TYPE_PERSON) {
$apply_info = [
'type' => $user_info['type'],
'telphone' => $user_info['telphone'],
'realname' => $user_info['realname'],
'idcard' => $user_info['idcard'],
'work_unit' => $user_info['work_unit'],
'work_position' => $user_info['work_position'],
'is_party' => $user_info['is_party'],
'is_volunteer' => $user_info['is_volunteer'],
];
} else {
$apply_info = [
'type' => $user_info['type'],
'telphone' => $user_info['telphone'],
'realname' => $user_info['realname'],
'company_name' => $user_info['company_name'],
'legal_name' => $user_info['legal_name'],
'company_address' => $user_info['company_address'],
];
}
$apply_info = json_encode($apply_info,JSON_UNESCAPED_UNICODE);
// 写入日志
$log_data = [
'uid'=>$user_info['id'],
'is_pass'=>$is_pass,
'reason' =>$reason,
'add_time' => time(),
'apply_info' => $apply_info
];
$user_audit_log = new UserAuditLogModel();
$add_res = $user_audit_log->save($log_data);
if (!$add_res) {
$err_count++;
}
if ($err_count > 0) {
$user->rollback();
$this->set_err(10099,'操作失败,请重试');
return $this->out_data;
} else {
$user->commit();
$this->set_err(0,'操作成功');
return $this->out_data;
}
}
}
里面操作了两个数据表,并使用事务。同时能够通过out_data将错误信息进行反馈到C层。
C层就简单多了。
// 审核用户
public function audit_user() {
$id = $_POST['id'];
$is_pass = $_POST['is_pass'];
$reason = input('post.reason/s','无');
if (!$id) {
$this->json->setErr(10001,'缺少参数');
$this->json->Send();
}
if (!$is_pass) {
$this->json->setErr(10002,'缺少参数');
$this->json->Send();
}
$user_service = new UserService();
$res = $user_service->audit_user($id,$is_pass,$reason);
if ($res['errno'] == 0) {
$this->json->setErr(0,'操作成功');
$this->json->Send();
} else {
$this->json->setErr($res['errno'],$res['errdesc']);
$this->json->Send();
}
}
经过Service的处理,C层和M层之间多了一个中间层。它不仅仅可以处理数据库的数据,它还可以处理各种验证之类的事情。计算之类的事情。
Service层很有意思。