作者 赵彬吉

del c_side

... ... @@ -18,5 +18,4 @@ 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,10 +72,6 @@ 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);
... ... @@ -114,9 +110,7 @@ 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) {
}elseif ($exception instanceof ValidationException) {
$code = Code::USER_PARAMS_ERROE();
$message = $code->description = Arr::first(Arr::first($exception->errors()));
} elseif ($exception instanceof NotFoundHttpException || $exception instanceof MethodNotAllowedHttpException) {
... ...
<?php
namespace App\Helper;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
/**
* Class FormGlobalsoApi
* @package App\Helper
* @author zbj
* @date 2023/5/17
*/
class FormGlobalsoApi
{
//接口地址
protected $url = 'https://form.globalso.com';
/**
* 设置询盘通知
* @author zbj
* @date 2023/5/17
*/
public function setInquiry($domain, $emails, $phones)
{
$api_url = $this->url . '/api/external-project/save/dc77a54480b184c4';
$params = [
'token' => md5($domain.$emails.$phones.date("Y-m-d")),
'domain' => $domain,
'email' => $emails,
'phone' => $phones,
];
try {
$res = HttpUtils::get($api_url, $params);
$res = Arr::s2a($res);
} catch (\Exception | GuzzleException $e) {
errorLog('设置询盘通知', $params, $e);
return false;
}
return $res;
}
/**
* 询盘列表
* @author zbj
* @date 2023/5/17
*/
public function getInquiryList($domain, $search = '', $page = 1, $page_size = 20)
{
$api_url = $this->url . '/api/external-interface/6a1bd159b1fd60af';
$params = [
'token' => md5($domain.$search.date("Y-m-d")),
'domain' => $domain,
'limit' => $page_size,
'page' => $page,
'source' => '1,3' //来源类型 新项目用1,3
];
if($search){
$params['name'] = $search;
}
try {
$res = HttpUtils::get($api_url, $params);
$res = Arr::s2a($res);
} catch (\Exception | GuzzleException $e) {
errorLog('询盘列表', $params, $e);
return false;
}
return $res;
}
/**
* 设置询盘信息已读
* @author zbj
* @date 2023/5/17
*/
public function saveInquiryRead($domain, $id)
{
$api_url = $this->url . '/api/external-interface/save/d1483a8e57cb485a';
$params = [
'token' => md5($domain.$id.date("Y-m-d")),
'domain' => $domain,
'id' => $id,
];
try {
$res = HttpUtils::get($api_url, $params);
$res = Arr::s2a($res);
} catch (\Exception | GuzzleException $e) {
errorLog('设置询盘信息已读', $params, $e);
return false;
}
return $res;
}
/**
* 删除询盘信息
* @author zbj
* @date 2023/5/17
*/
public function delInquiry($domain, $ids)
{
$api_url = $this->url . '/api/external-interface/del/c4b11cf6f1508489';
$ids = Arr::arrToSet($ids);
$params = [
'token' => md5($domain.$ids.date("Y-m-d")),
'domain' => $domain,
'id' => $ids,
];
try {
$res = HttpUtils::get($api_url, $params);
$res = Arr::s2a($res);
} catch (\Exception | GuzzleException $e) {
errorLog('删除询盘信息', $params, $e);
return false;
}
return $res;
}
}
... ...
... ... @@ -2,16 +2,12 @@
namespace App\Http\Controllers\Bside;
use App\Exceptions\BsideGlobalException;
use App\Helper\Arr;
use App\Http\Logic\Bside\InquiryLogic;
use App\Http\Requests\Bside\InquiryRequest;
use App\Rules\Ids;
use App\Services\BatchExportService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\ValidationException;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
/**
* 精准询盘
... ... @@ -25,23 +21,18 @@ class InquiryController extends BaseController
public function index(InquiryLogic $logic)
{
$map = [];
if(!empty($this->param['search'])){
$map[] = ['name|email|content', 'like', "%{$this->param['search']}%"];
}
$sort = ['id' => 'desc'];
$data = $logic->getList($map, $sort, ['id', 'name', 'email', 'phone', 'url', 'ip', 'ip_country', 'status', 'created_at']);
$data = $logic->getApiList();
return $this->success($data);
}
public function info(Request $request, InquiryLogic $logic){
$request->validate([
'id'=>'required'
'id' => 'required',
],[
'id.required' => 'ID不能为空'
]);
$data = $logic->getInfo($this->param['id']);
return $this->success(Arr::twoKeepKeys($data, ['id', 'name', 'email', 'phone', 'url', 'ip', 'ip_country', 'status', 'content', 'trans_content', 'created_at']));
return $this->success($data);
}
public function delete(Request $request, InquiryLogic $logic)
... ... @@ -52,8 +43,8 @@ class InquiryController extends BaseController
'ids.required' => 'ID不能为空'
]);
$data = $logic->delete($this->param['ids']);
return $this->success($data);
$logic->delete($this->param['ids']);
return $this->success();
}
/**
... ... @@ -66,22 +57,20 @@ class InquiryController extends BaseController
*/
public function export(InquiryLogic $logic)
{
$sort = ['id' => 'desc'];
//最多到1w条
$data = $logic->getList([], $sort, ['name', 'email', 'phone', 'url', 'ip', 'ip_country', 'content', 'created_at'], 10000);
$data = $logic->getApiList(true);
$data = $data['list'] ?? [];
foreach ($data as &$item){
$item['ip_address'] = "{$item['ip_country']}({$item['ip']})";
$item['ip_address'] = "{$item['country']}({$item['ip']})";
}
$map = [
'created_at' => '询盘发送时间',
'submit_time' => '询盘发送时间',
'name' => '姓名',
'email' => '邮箱',
'phone' => '电话',
'ip_address' => '访问国家/地区(IP)',
'url' => '发送页面',
'content' => '询盘内容',
'refer' => '发送页面',
'message' => '询盘内容',
];
//生成文件,发送到客户端
... ... @@ -94,4 +83,17 @@ class InquiryController extends BaseController
// return Storage::disk('runtime')->download($file); //直接下载
return $this->success(['url' => $fileurl]);
}
/**
* 询盘通知设置
* @author zbj
* @date 2023/5/17
*/
public function set(Request $request, InquiryLogic $logic){
if($request->isMethod('get')){
return $this->success($logic->getSet());
}
$logic->saveSet();
return $this->success();
}
}
... ...
<?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);
}
}
... ... @@ -11,7 +11,6 @@ use App\Http\Middleware\Bside\EnableCrossRequestMiddleware;
use App\Http\Middleware\Bside\ParamMiddleware as BsideParamMiddleware;
use App\Http\Middleware\Aside\LoginAuthMiddleware as AsideLoginAuthMiddleware;
use App\Http\Middleware\Bside\LoginAuthMiddleware as BsideLoginAuthMiddleware;
use App\Http\Middleware\Cside\ParamMiddleware as CsideParamMiddleware;
use App\Http\Middleware\PreventRepeatQuitCallMiddleware;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
... ... @@ -77,11 +76,6 @@ class Kernel extends HttpKernel
//允许跨域请求
EnableCrossRequestMiddleware::class
],
//C端中间件组
'cside'=>[
//参数处理中间件--涉及-参数记录-参数加解密等
CsideParamMiddleware::class,
]
];
/**
... ...
... ... @@ -3,7 +3,10 @@
namespace App\Http\Logic\Bside;
use App\Helper\Arr;
use App\Models\Inquiry;
use App\Helper\FormGlobalsoApi;
use App\Helper\Translate;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\InquirySet;
/**
* Class InquiryLogic
... ... @@ -11,25 +14,119 @@ use App\Models\Inquiry;
* @author zbj
* @date 2023/5/4
*/
class InquiryLogic extends BaseLogic
class InquiryLogic extends BaseLogic
{
protected $form_globalso_api;
public function __construct()
{
parent::__construct();
$this->model = new Inquiry();
$this->form_globalso_api = new FormGlobalsoApi();
}
public function getApiList($export)
{
$page_size = $export ? 1000 : 20;
$search = $this->request['search'] ?: '';
$page = $this->request['page'] ?: 1;
$project = (new ProjectLogic())->getInfo($this->user['project_id']);
$domain = $project['deploy_optimize']['domain'] ?: '';
$list = $this->form_globalso_api->getInquiryList($domain, $search, $page, $page_size);
//处理格式 免得前端又改
$data = [
"list" => [],
"total" => 0,
"page" => $page,
"total_page" => 1,
"size" => $page_size
];
if (!empty($list['status']) && $list['status'] == 200) {
foreach ($list['data']['data'] as $item) {
$data['list'][] = $item;
}
$data['total'] = $list['data']['total'];
$data['total_page'] = $list['data']['last_page'];
}
return $this->success($data);
}
public function getInfo($id)
{
$info = $this->getCacheInfo($id);
if(!$info){
$this->fail('数据不存在或者已经删除');
$project = (new ProjectLogic())->getInfo($this->user['project_id']);
$domain = $project['deploy_optimize']['domain'] ?: '';
//修改状态为已读
if($this->request['read_status']){
$this->form_globalso_api->saveInquiryRead($domain, $id);
}
//翻译
$trans_message = '';
if($this->request['message']){
$trans_message = Translate::tran($this->request['message'], 'zh');
}
return $this->success(['trans_message' => $trans_message]);
}
public function delete($ids, $map = [])
{
$project = (new ProjectLogic())->getInfo($this->user['project_id']);
$domain = $project['deploy_optimize']['domain'] ?: '';
$ids = array_filter(Arr::splitFilterToArray($ids), 'intval');
if(!$ids){
$this->fail('ID不能为空');
}
$this->form_globalso_api->delInquiry($domain, $ids);
return $this->success();
}
/**
* 读取设置
* @return array
* @author zbj
* @date 2023/5/17
*/
public function getSet()
{
$set = InquirySet::where('project_id', $this->user['project_id'])->first();
if ($set) {
return $this->success(Arr::twoKeepKeys($set->toArray(), ['emails', 'phones']));
} else {
return $this->success(['emails' => '', 'phones' => '']);
}
}
/**
* 保存设置
* @author zbj
* @date 2023/5/17
*/
public function saveSet()
{
$project = (new ProjectLogic())->getInfo($this->user['project_id']);
//同步到接口
$domain = parse_url($project['deploy_optimize']['domain'])['host'] ?? ['path'];
$emails = Arr::arrToSet($this->request['emails'], 'trim');
$phones = Arr::arrToSet($this->request['phones'], 'trim');
$res = $this->form_globalso_api->setInquiry($domain, $emails, $phones);
if (!$res) {
$this->fail('保存失败');
}
//标记已读
if($info->status == Inquiry::STATUS_UNREAD){
parent::save(['id' => $info['id'], 'status' => Inquiry::STATUS_READ]);
if ($res['status'] != 200) {
$this->fail($res['message'] ?? '保存失败');
}
return $this->success($info->toArray());
//保存
$set = InquirySet::where('project_id', $this->user['project_id'])->first();
if (!$set) {
$set = new InquirySet();
}
$set->project_id = $this->user['project_id'];
$set->emails = $emails;
$set->phones = $phones;
$set->save();
}
}
... ...
<?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,7 +4,6 @@ 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;
... ... @@ -46,9 +45,6 @@ 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 [];
}
}
<?php
namespace App\Models;
use App\Helper\Arr;
use Illuminate\Database\Eloquent\SoftDeletes;
/**
* Class Inquiry
* @package App\Models
* @author zbj
* @date 2023/5/4
*/
class Inquiry extends Base
{
use SoftDeletes;
//设置关联表名
protected $table = 'gl_inquiry';
const STATUS_UNREAD = 0;
const STATUS_READ = 1;
public function setIpInfoAttribute($value){
$this->attributes['ip_info'] = Arr::a2s($value);
}
public function getIpInfoAttribute($value){
return Arr::s2a($value);
}
}
<?php
namespace App\Models;
/**
* Class InquirySet
* @package App\Models
* @author zbj
* @date 2023/5/17
*/
class InquirySet extends Base
{
//设置关联表名
protected $table = 'gl_inquiry_set';
}
... ...
... ... @@ -40,7 +40,6 @@ class RouteServiceProvider extends ServiceProvider
//预定义两个端的API路由
$this->mapAsideRoute();
$this->mapBsideRoute();
$this->mapCsideRoute();
// 暂时无用
$this->routes(function () {
... ... @@ -75,16 +74,6 @@ class RouteServiceProvider extends ServiceProvider
->namespace($this->namespace . '\Bside')
->group(base_path('routes/bside.php'));
}
/**
*C端API路由
* @return void
*/
protected function mapCsideRoute(){
Route::middleware('cside')
->prefix('c')
->namespace($this->namespace . '\Cside')
->group(base_path('routes/cside.php'));
}
/**
* Configure the rate limiters for the application.
... ...
... ... @@ -60,12 +60,6 @@ return [
'via' => \App\Factory\LogFormatterFactory::class,
'prefix' => 'bside',
],
//自定义B端错误日志
'cside' => [
'driver' => 'custom',
'via' => \App\Factory\LogFormatterFactory::class,
'prefix' => 'cside',
],
'stack' => [
'driver' => 'stack',
... ...
... ... @@ -199,6 +199,7 @@ Route::middleware(['bloginauth'])->group(function () {
//精准询盘
Route::prefix('inquiry')->group(function () {
Route::get('/', [\App\Http\Controllers\Bside\InquiryController::class, 'index'])->name('inquiry');
Route::any('/set', [\App\Http\Controllers\Bside\InquiryController::class, 'set'])->name('inquiry_set');
Route::get('/info', [\App\Http\Controllers\Bside\InquiryController::class, 'info'])->name('inquiry_info');
Route::any('/delete', [\App\Http\Controllers\Bside\InquiryController::class, 'delete'])->name('inquiry_delete');
Route::any('/export', [\App\Http\Controllers\Bside\InquiryController::class, 'export'])->name('inquiry_export');
... ...
<?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');
});