C層,操控數據庫,並處理頁面數據展現。
M層,純粹的操做本身所對應的數據庫。
Service層,能夠通用的處理一些邏輯計算,也能夠將複雜的數據表處理整合到一塊兒,也能夠將複雜的業務邏輯整合到一塊兒。php
建立了一個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:基於一、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; } }
主要用於輸出數據的設置。json
用戶層的服務,app
<?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:基於一、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層。ide
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層頗有意思。post