C層,操控數據庫,並處理頁面數據展示。
M層,純粹的操作自己所對應的數據庫。
Service層,可以通用的處理一些邏輯計算,也可以將復雜的數據表處理整合到一起,也可以將復雜的業務邏輯整合到一起。
創建了一個CommonService
<?php
/**
* Created by PhpStorm.
* User: jiqing
* Date: 18-12-21
* Time: 下午8:49
*/
namespace app\common\service;
// 服務層,介於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 app\common\service;
// 服務層,介於C層與M層之間
/** 根據上面的分析,Service夾在C層和M層中間,從邏輯上大致划分為3大類:
### model側的Service:也就是封裝每個model與業務相關的通用數據接口,比如:查詢訂單。(我認為:訪問遠程服務獲取數據也應該歸屬於這一類Service)
### 中間的Service:封裝通用的業務邏輯,比如:計算訂單折扣(會用到1中的Service)。
### controller側的Service:基於1、2中的Service進一步封裝對外接口的用戶業務邏輯。
**/
use app\common\model\UserAuditLogModel;
use app\common\model\UserModel;
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層很有意思。