作者 赵彬吉

update

... ... @@ -18,4 +18,5 @@ final class Common extends Enum
//端
const A='a';
const B='b';
const C='c';
}
... ...
<?php
namespace App\Exceptions;
use App\Enums\Common\Code;
use Exception;
use Throwable;
/**
* @notes: C端接口统一错误格式
* Class CsideGlobalException
* @package App\Exceptions
*/
class CsideGlobalException extends Exception
{
public function __construct($code = 0, $message = "", Throwable $previous = null)
{
$this->code = $code;
$this->message = $message;
if (empty($this->message)) {
$this->message = Code::fromValue($code)->description;
}
}
}
... ...
... ... @@ -72,6 +72,10 @@ class Handler extends ExceptionHandler
elseif($exception instanceof BsideGlobalException) {
LogUtils::error("BsideGlobalException", [], $exceptionMessage);
}
//C端错误
elseif($exception instanceof CsideGlobalException) {
LogUtils::error("CsideGlobalException", [], $exceptionMessage);
}
//验证错误(非手动抛出)
elseif ($exception instanceof ValidationException) {
LogUtils::error("参数验证失败", [], $exceptionMessage);
... ... @@ -110,6 +114,8 @@ class Handler extends ExceptionHandler
$code = $exception->getCode();
}elseif ($exception instanceof BsideGlobalException) {
$code = $exception->getCode();
}elseif ($exception instanceof CsideGlobalException) {
$code = $exception->getCode();
} elseif ($exception instanceof ValidationException) {
$code = Code::USER_PARAMS_ERROE();
$message = $code->description = Arr::first(Arr::first($exception->errors()));
... ...
<?php
namespace App\Http\Controllers\Cside;
use App\Enums\Common\Code;
use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Session;
class BaseController extends Controller
{
protected $param = [];//所有请求参数
protected $token = ''; //token
protected $request = [];//助手函数
protected $project = [];//当前登录用户详情
/**
* 获取所有参数
*/
public function __construct(Request $request)
{
$this->request = $request;
$this->param = $this->request->all();
$this->token = $this->request->header('token');
if(!empty($this->token) && !empty(Cache::get($this->token))){
$info = Cache::get($this->token);
$this->user = $info;
$this->uid = $info['id'];
}
}
/**
* 成功返回
* @param array $data
* @param string $code
* @param bool $objectData
* @return JsonResponse
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
function success(array $data = [], string $code = Code::SUCCESS, bool $objectData = false): JsonResponse
{
if ($objectData) {
$data = (object)$data;
}
$code = Code::fromValue($code);
$response = [
'code' => $code->value,
'data' => $data,
'msg' => $code->description,
];
return response()->json($response,200);
}
}
... ...
<?php
namespace App\Http\Controllers\Cside;
use App\Http\Logic\Cside\InquiryLogic;
use App\Http\Requests\Cside\InquiryRequest;
/**
* 精准询盘
* Class InquiryController
* @package App\Http\Controllers\Bside
* @author zbj
* @date 2023/5/4
*/
class InquiryController extends BaseController
{
public function save(InquiryRequest $request, InquiryLogic $logic)
{
$data = $logic->save($this->param);
return $this->success($data);
}
}
... ...
... ... @@ -2,13 +2,11 @@
namespace App\Http\Logic\Bside;
use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Exceptions\BsideGlobalException;
use App\Helper\Common;
use App\Http\Controllers\ImageController;
use App\Http\Logic\Logic;
use App\Models\Image as ImageModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
/**
... ... @@ -25,6 +23,8 @@ class BaseLogic extends Logic
protected $user;
protected $side = Common::B;
public function __construct()
{
$this->request = request();
... ...
<?php
namespace App\Http\Logic\Cside;
use App\Enums\Common\Common;
use App\Exceptions\BsideGlobalException;
use App\Http\Logic\Logic;
/**
* @notes: 逻辑层基类 控制器调用 统一返回 统一抛出异常
*/
class BaseLogic extends Logic
{
protected $requestAll;
protected $param;
protected $request;
protected $project;
protected $side = Common::C;
public function __construct()
{
$this->request = request();
$this->requestAll = request()->all();
$this->project = $this->request->get('project');
}
/**
* 列表
* @param array $map
* @param array $sort
* @param array $columns
* @param int $limit
* @return array
* @author zbj
* @date 2023/4/13
*/
public function getList(array $map = [], array $sort = ['id' => 'desc'], array $columns = ['*'], int $limit = 20)
{
$map[] = ['project_id' => $this->project['id']];
return parent::getList($map, $sort, $columns, $limit);
}
/**
* @param $id
* @return mixed
* @author zbj
* @date 2023/4/15
*/
public function getCacheInfo($id)
{
$info = parent::getCacheInfo($id);
if ($info && $info['project_id'] != $this->project['id']) {
$info = null;
}
return $info;
}
/**
* 保存
* @param $param
* @return array
* @throws BsideGlobalException
* @author zbj
* @date 2023/4/13
*/
public function save($param)
{
$param['project_id'] = $this->project['id'];
return parent::save($param);
}
/**
* 批量删除
* @param $ids
* @param array $map
* @return array
* @author zbj
* @date 2023/4/13
*/
public function delete($ids, $map = [])
{
$map[] = ['project_id' => $this->project['id']];
return parent::delete($ids, $map);
}
}
... ...
<?php
namespace App\Http\Logic\Cside;
use App\Helper\Arr;
use App\Models\Inquiry;
/**
* Class InquiryLogic
* @package App\Http\Logic\Bside
* @author zbj
* @date 2023/5/4
*/
class InquiryLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new Inquiry();
}
public function save($param)
{
$param['ip_info'] = Arr::s2a($param['ip_info']);
$param['ip'] = $param['ip_info']['ip'] ?? '';
$param['ip_country'] = $param['ip_info']['country'] ?? '';
return parent::save($param);
}
}
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Http\Logic;
use App\Enums\Common\Code;
use App\Enums\Common\Common;
use App\Exceptions\CsideGlobalException;
use \App\Helper\Common as CommonHelper;
use App\Exceptions\AsideGlobalException;
use App\Exceptions\BsideGlobalException;
... ... @@ -45,6 +46,9 @@ class Logic
if((request()->path()[0]) == Common::B){
throw new BsideGlobalException($code, $message);
}
if((request()->path()[0]) == Common::C){
throw new CsideGlobalException($code, $message);
}
throw new AsideGlobalException($code, $message);
}
... ...
<?php
namespace App\Http\Middleware\Cside;
use App\Enums\Common\Code;
use App\Models\Project\Project;
use App\Services\ProjectServer;
use Closure;
use Illuminate\Http\Request;
class ParamMiddleware
{
protected $param = [];
protected $project = [];
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse) $next
* @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
*/
public function handle(Request $request, Closure $next)
{
$this->param = $request->all();
$domain = $request->header('domain');
if(!isset($domain) || empty($domain)){
return response(['code'=>Code::USER_ERROR,'msg'=>'非法请求']);
}
$project = Project::getProjectByDomain($domain);
if(empty($project)){
return response(['code'=>Code::USER_ERROR,'msg'=>'非法请求']);
}
// 设置数据信息
// $project = ProjectServer::useProject($project['id']);
// if($project){
// return response(['code'=>Code::USER_ERROR,'msg'=>'数据库未配置']);
// }
$request->attributes->add(['project' => $project]);
return $next($request);
}
}
... ...
<?php
namespace App\Http\Requests\Cside;
use Illuminate\Foundation\Http\FormRequest;
/**
* Class InquiryRequest
* @package App\Http\Requests\Cside
* @author zbj
* @date 2023/5/4
*/
class InquiryRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|max:200',
'email' => 'required|email|max:200',
'phone' => 'max:200',
'content' => 'required',
];
}
public function messages()
{
return [];
}
}
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Models\Project;
use App\Helper\Arr;
use App\Models\Base;
use Illuminate\Support\Facades\Cache;
class DeployBuild extends Base
{
... ... @@ -19,4 +20,8 @@ class DeployBuild extends Base
return Arr::setToArr($value);
}
public static function clearCache($row){
$cache_key = 'project_' . $row->original['test_domain'];
Cache::forget($cache_key);
}
}
... ...
... ... @@ -4,6 +4,7 @@ namespace App\Models\Project;
use App\Helper\Arr;
use App\Models\Base;
use Illuminate\Support\Facades\Cache;
class DeployOptimize extends Base
{
... ... @@ -19,4 +20,9 @@ class DeployOptimize extends Base
return Arr::s2a($value);
}
public static function clearCache($row){
$cache_key = 'project_' . $row->original['domain'];
Cache::forget($cache_key);
}
}
... ...
... ... @@ -5,6 +5,7 @@ namespace App\Models\Project;
use App\Helper\Arr;
use App\Models\Base;
use App\Models\Devops\ServerConfig;
use Illuminate\Support\Facades\Cache;
class Project extends Base
{
... ... @@ -144,4 +145,30 @@ class Project extends Base
return Arr::s2a($value);
}
/**
* 根据域名获取项目信息
* @author zbj
* @date 2023/5/5
*/
public static function getProjectByDomain($domain){
$cache_key = 'project_'.$domain;
$data = Cache::get($cache_key);
if(!$data){
//是否测试域名
$project_id = DeployBuild::where('test_domain', $domain)->value('project_id');
//是否正式域名
if(!$project_id){
$project_id = DeployOptimize::where('domain', $domain)->value('project_id');
}
if(!$project_id){
return [];
}
$data = self::find($project_id);
if($data){
Cache::put($cache_key, $data);
}
}
return $data;
}
}
... ...
... ... @@ -60,6 +60,12 @@ return [
'via' => \App\Factory\LogFormatterFactory::class,
'prefix' => 'bside',
],
//自定义B端错误日志
'cside' => [
'driver' => 'custom',
'via' => \App\Factory\LogFormatterFactory::class,
'prefix' => 'cside',
],
'stack' => [
'driver' => 'stack',
... ...
<?php
/**
* C端用户路由文件
*/
use Illuminate\Support\Facades\Route;
//必须登录验证的路由组
Route::middleware([])->group(function () {
//添加询盘信息
Route::post('/inquiry/save', [\App\Http\Controllers\Cside\InquiryController::class, 'save'])->name('inquiry_save');
});
... ...