Commit 2e695cf2 by 刘春刚

checkSign

parent 54e624d4
...@@ -22,18 +22,24 @@ class Handler extends ExceptionHandler ...@@ -22,18 +22,24 @@ class Handler extends ExceptionHandler
* @var array * @var array
*/ */
protected $dontFlash = [ protected $dontFlash = [
'password', //
'password_confirmation',
]; ];
/** /**
* Report or log an exception. * Report or log an exception.
* *
* This is a great spot to send exceptions to Sentry, Bugsnag, etc.
*
* @param \Exception $exception * @param \Exception $exception
* @return void * @return void
*/ */
public function report(Exception $exception) public function report(Exception $exception)
{ {
//添加sentry异常报警
if (app()->bound('sentry') && $this->shouldReport($exception)) {
app('sentry')->captureException($exception);
}
parent::report($exception); parent::report($exception);
} }
...@@ -46,6 +52,24 @@ class Handler extends ExceptionHandler ...@@ -46,6 +52,24 @@ class Handler extends ExceptionHandler
*/ */
public function render($request, Exception $exception) public function render($request, Exception $exception)
{ {
return parent::render($request, $exception); if($request->is('api/*')){
$response = [];
$error = $this->convertExceptionToResponse($exception);
$response['statusCode'] = $error->getStatusCode();
$response['msg'] = 'something error';
if(config('app.debug')) {
$response['msg'] = empty($exception->getMessage()) ? 'something error' : $exception->getMessage();
if($error->getStatusCode() >= 500) {
if(config('app.debug')) {
$response['trace'] = $exception->getTraceAsString();
$response['code'] = $exception->getCode();
}
}
}
$response['params'] = $request->all();
return response()->json($response, $error->getStatusCode());
}else{
return parent::render($request, $exception);
}
} }
} }
<?php <?php
/**
* Created by PhpStorm.
* User: liqingyuan
* Date: 2018/7/24
* Time: 下午7:22
*/
class Constants class Constants
{ {
/** /**
* 基本常量定义
*/
const SSO_LOGIN_CODE_APP_TYPE = 2;
const CALLBACK_SIGN_KEY_19 = 'offcn!login_zaixiankt@)))user|(( md5'; // 19课堂回放 回调函数私有key
const ENCRYPT_SALT = 'offcn!login_zliswe^@)))user|((@myufs';
const SSO_LOGIN_SIGN_KEY = '9eaf618cca780358d7c98c00dc18e148';
const ZXKT_VERIFY_KEY = '6dNIHASZwFpVOYl1Qib3G54x780hjfUkKzPuBcWToDEmtgqX9vaeCnrRyMsJ2L';
const APPLE_COURSE_PREFIX = 'com.eoffcn.classonline.course';
const MESSAGE_TOKEN='52cZSXuG8UFi8fR4HiE7DV7NhX3UehmsC8y91GBNbxB';
/**
* 基本错误类型 * 基本错误类型
*/ */
const SUCCESS = 0; // 成功 const SUCCESS = 0; // 成功
const FAIL = 1; // 失败 const FAIL = -1; // 失败
const FAIL_CODE = 1001;
const COURSE_PLATFORM_OLD = 1;
const COURSE_PLATFORM_NEW = 2;
...@@ -49,33 +26,7 @@ class Constants ...@@ -49,33 +26,7 @@ class Constants
const ERR_INFO_SET_FAIL = -1005; // 信息设置失败 const ERR_INFO_SET_FAIL = -1005; // 信息设置失败
const ERR_PARAMS_NOT_VALIDATE = -2001; // 参数不合法
const ERR_REQUEST_NOT_VALIDATE = -2002; // 访问不合法
const ERR_REQUEST_TIMEOUT = -2003; // 请求已过期
const ERR_SIGN_INVALIDATE = -2004; // 签名错误
const ERR_RESULT_IS_EMPTY = -3001; // 数据为空
const ERR_JPUSH_REGISTER = -4001; // 推送注册失败
const ERR_JPUSH_LOGOUT = -4002; // 推送登出失败
const ERR_DOWNLOAD_RECORD_FAIL = -5001; // 下载记录存储失败
const ERR_IM_LOGIN = -6001; // 融云登陆失败
const ERR_IM_SEND_FAIL = -6002; // 融云消息发送失败
const ERR_FILE_UPLOAD_FAIL = -7001; // 上传文件失败
const ERR_FILE_IS_EMPTY = -7002; // 上传文件不能为空
const ERR_SELF_HELP_SAVE_FAIL = -8001; // 自助开课失败
const ERR_ClOSE_ORDER_FAIL = -8002; // 关闭订单失败
const ERR_SUBMIT_ADDRESS_FAIL = -8003; // 提交地址失败
const ERR_AUTH_ORDER_FAIL = -8004; // 订单鉴权失败,请重试
const ERR_STUDY_RECORD_FAIL = -9001; //学习记录获取失败
/** /**
* curl code值定义 * curl code值定义
*/ */
...@@ -83,207 +34,40 @@ class Constants ...@@ -83,207 +34,40 @@ class Constants
const CURL_DATA_ERROR = 20002; //返回数据格式错误 const CURL_DATA_ERROR = 20002; //返回数据格式错误
const CURL_EXCEPTION_ERROR = 20003; //curl请求异常code const CURL_EXCEPTION_ERROR = 20003; //curl请求异常code
const ROUT_EXCEPTION_ERROR = 20004; //路由接口请求异常code const ROUT_EXCEPTION_ERROR = 20004; //路由接口请求异常code
const PARAMETER_INPUT_ERROR = 10001; //参数传入失败
//redis缓存key
const USER_COURSE_KEY = 'user_course_qiyi';
//获取学习记录信息 app平台介入
const APP_CLIENT_ID = 9;
const APP_CLIENT_ID_SSO = 7;
const IDENTITY_ZAIZHI = 1;
const IDENTITY_DAIYE = 2;
const IDENTITY_ZAIXIAOSHENG = 3;
const PARAMETER_ERROR = 10001;
const PARAMETER_EXPIRE = 10002;
const STATUS_WAITING_POST_ORDER = 31;//代发货状态的订单
const NEED_ADD_ADDRESS = 1;
/**
* @return array
*/
public static function getIdentityName()
{
return array(
self::IDENTITY_ZAIZHI => '在职',
self::IDENTITY_DAIYE => '待业',
self::IDENTITY_ZAIXIAOSHENG => '在校学生',
);
}
/**
* @return array
*/
public static function getAppointmentStatusName()
{
return array(
0 => '预约',
1 => '撤销预约',
2 => '已预约',
3 => '已约满',
4 => '预约'
);
}
/**
* @return array
*/
public static function getIdentityNames()
{
return array(
'在职' => self::IDENTITY_ZAIZHI,
'待业' => self::IDENTITY_DAIYE,
'在校学生'=> self::IDENTITY_ZAIXIAOSHENG,
);
}
//请求 查询时间片段上安排的直播频道信息 返回的code对应的提示信息
public static $liveChannalCodes =
array(
0 => '成功',
101 => '该时间段尚未预约课程',
102 => '该时间段尚未分组安排',
103 => '该时间段分组信息内容错误',
);
public static $liveDatesCodes = array(
0=>'成功',
101=>'该时段您已有课程预约,如调整内容请先取消预约',
102=>'尝试取消该时间段上没有的预约信息',
103=>'您约过同样的课程,请不要重复预约',
104=>'个人信息不完善',
105=>'课时不足,请购买课时',
106=>'预约课程失败',
109=>'截止取消预约',
110=>'个人信息不完善',
114=>'当前时间按已经截至预约',
122=>'当前时间段预约量已满,请选择其他时间段预约',
);
/** /**
* 错误消息响应 * 错误消息响应
*/ */
private static $_errMsg = [ private static $_message = [
// 基础错误 // 基础错误
self::SUCCESS => ['成功', 'success'], self::SUCCESS => ['成功', 'success'],
self::FAIL => ['失败', 'fail'], self::FAIL => ['失败', 'fail'],
self::PARAMETER_ERROR => ['参数接收失败', 'parameter input error'],
self::ERR_INFO_NOT_EXISTS => ['信息不存在', 'info is not exist'], self::PARAMETER_EXPIRE => ['参数过期', 'parameter expire'],
self::ERR_USER_NOT_EXISTS => ['用户不存在', 'user is not exist'],
self::ERR_EXCEPTION => ['异常错误', 'exception err'],
self::ERR_INFO_SET_FAIL => ['信息设置失败', 'user is not exist'],
self::ERR_USER_LOGIN_TIMEOUT => ['用户登陆超时', 'user login timeout'],
self::ERR_PARAMS_NOT_VALIDATE => ['参数不合法', 'params is not validate'],
self::ERR_REQUEST_NOT_VALIDATE => ['访问不合法', 'request is not validate'],
self::ERR_REQUEST_TIMEOUT => ['请求已过期, 请重试!', 'request is timeout'],
self::ERR_SIGN_INVALIDATE => ['签名校验错误, 请重试!', 'sign is not validate'],
self::ERR_RESULT_IS_EMPTY => ['数据为空', 'result is empty'],
self::ERR_JPUSH_REGISTER => ['极光推送注册失败', 'jpush register fail'],
self::ERR_JPUSH_LOGOUT => ['极光推送登出失败', 'jpush logout fail'],
self::ERR_IM_LOGIN => ['融云登陆失败', 'rong_cloud login fail'],
self::ERR_IM_SEND_FAIL => ['融云消息发送失败', 'rong_cloud send msg fail'],
self::ERR_DOWNLOAD_RECORD_FAIL => ['下载记录存储失败', 'download record fail'],
self::ERR_IM_LOGIN => ['登陆IM失败', 'login rongyun im fial'],
self::ERR_FILE_UPLOAD_FAIL => ['文件上传失败', 'file upload fail'],
self::ERR_FILE_IS_EMPTY => ['上传文件不能为空', 'file upload is empty'],
self::ERR_DOWNLOAD_RECORD_FAIL => ['下载记录存储失败', 'download record fail'],
self::ERR_SELF_HELP_SAVE_FAIL => ['自助开课失败', 'self help save fail'],
self::ERR_ClOSE_ORDER_FAIL => ['关闭订单失败', 'close order fail'],
self::ERR_SUBMIT_ADDRESS_FAIL => ['提交地址失败', 'submit address fail'],
self::ERR_AUTH_ORDER_FAIL => ['订单页面鉴权失败,请重试!', 'order h5 auth fail, try again later!'],
self::ERR_STUDY_RECORD_FAIL => ['学习记录获取失败, 请重试!', 'study record fail']
//其他业务请在下面对照写中英文错误信息 //其他业务请在下面对照写中英文错误信息
]; ];
public static function msg($errno, $lang = '') /**
* @param $code
* @param string $lang
* @return string
*/
public static function message($code, $lang = '')
{ {
$lang = empty($lang) ? config('app.locale'):$lang; $lang = empty($lang) ? config('app.locale'):$lang;
$lang = 'zh_cn' === $lang ? 0 : 1; $lang = 'zh_cn' === $lang ? 0 : 1;
return self::$_errMsg[$errno][$lang] ?? ''; return self::$_message[$code][$lang] ?? '';
} }
//图书邮寄常量定义
//delivery_process 表 status
const STATUS_UNSURE = 0; //备货中
const STATUS_NOT_HAS_DELIVERY= 5; // 暂无图书邮寄信息
const STATUS_PART_POST= 4; //部分发货
const STATUS_WAITING_POST = 1; //备货中
const STATUS_POST = 2; //已发货
const STATUS_EXPORT = 3; //已导出
//平台常量
const PLATFORM_DEFAULT = 0;
const PLATFORM_U_CLASS = 1;
//数据是否有效
const IS_DELETE = 1;//已删除
const NOT_DELETE = 0;//合法数据
//是否有需要继续申请的图书
const HAS_DELIVERY = 1;
const HAS_NOT_DELIVERY = 0;
//是否有旧线订单
const HAS_ORDER = 1;
const HAS_NOT_ORDER = 0;
//是否是默认地址
const IS_DEFAULT = 1;
const IS_NOT_DEFAULT = 0;
const EXTRA_STATUS_PURE = 0; //课程下面的图书完全没有下过单
const EXTRA_STATUS_POST = 1;//课程下面的图书全部被其他课程下过单
const EXTRA_STATUS_PART_POST = 2; //课程下的图书 部分下过单
const NOT_LOGIN = 9999;
//7易接口返回的code码
const NOT_BUY_COURSE = 2001;
const NOT_HAS_BOOK_NUMBER = 3;
const PRODUCT_NAME_SUFFIX = ' (1本)';
} }
<?php
namespace App\Http\Middleware;
use Closure, AliYunLog, Constants;
class CheckSign
{
protected $except = [
];
/**
* Handle an incoming request.
* @param $request
* @param Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
$params = $request->all();
if (isset($params['debug_http']) && $params['debug_http'] == 911){
return $next($request);
}
//参数规范性校验
if(empty($params['t'])){
return Response()->json(['code' => Constants::PARAMETER_ERROR, 'msg' => '参数错误,请检查传入参数的时间!', 'params' => $params]);
}
if(empty($params['sign'])){
return Response()->json(['code' => Constants::PARAMETER_ERROR, 'msg' => '参数错误,请检查传入参数的签名!', 'params' => $params]);
}
//参数合法性校验
$diff = time() - $params['t'];
if($diff > 600){
return Response()->json(['code' => Constants::PARAMETER_ERROR, 'msg' => '参数错误,时间戳已过期!', 'params' => $params]);
}
$secretKey = config('api.sign.check_sign');
if(empty($secretKey)){
return Response()->json(['code' => Constants::PARAMETER_ERROR, 'msg' => '参数错误,后台密钥丢失', 'params' => $params]);
}
$dealSign = md5('secret='.$secretKey.'&t='.$params['t']);
if($dealSign != $params['sign']){
return Response()->json(['code' => Constants::PARAMETER_ERROR, 'msg' => '签名错误', 'params' => $params]);
}
return $next($request);
}
}
<?php
namespace App\Lib;
use Base\Log\Log;
class AliLog
{
protected $appName = '';
protected $logDir = '';
protected $requestPath;
/**
* UserLog constructor.
* @param array $config
* @param $path
*/
public function __construct(array $config, $path)
{
$this->appName = $config['appName'];
$this->logDir = $config['logDir'];
Log::init($this->appName, $this->logDir);
$this->requestPath = $path;
}
/**
* @param $level
* @param int $code
* @param string $msg
* @param string $ext
* @param $userInfo
* @return bool
* @throws \Exception
*/
public function recordLog($level, $code = 0, $msg = '', $userInfo = '', $ext = '')
{
switch ($level) {
case 'debug':
Log::debug($this->requestPath, $userInfo, $code, $msg, $ext);
break;
case 'info':
Log::info($this->requestPath, $userInfo, $code, $msg, $ext);
break;
case 'warn':
Log::warn($this->requestPath, $userInfo, $code, $msg, $ext);
break;
case 'error':
Log::error($this->requestPath, $userInfo, $code, $msg, $ext);
break;
case 'fatal':
Log::fatal($this->requestPath, $userInfo, $code, $msg, $ext);
break;
default:
Log::info($this->requestPath, $userInfo, $code, $msg, $ext);
break;
}
Log::flush();
return true;
}
}
\ No newline at end of file
<?php
namespace App\Lib;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use AliYunLog;
use Constants;
/**
* api接口请求类
* Class HttpService
* @package App\Services
*/
class Http
{
protected $client;
public function __construct()
{
$this->client = new Client;
}
/**
* @param string $url
* @param array $data
* @param array $option
* @param array $header
* @return \Illuminate\Http\JsonResponse|\Psr\Http\Message\StreamInterface
*/
public function get(string $url, array $data = [] ,$option = [], array $header = [])
{
if ($data) {
$queryStr = http_build_query($data);
$url .= (false !== strpos($url, '?') ? '&' : '?') . "{$queryStr}";
}
!empty($header) && $option['headers'] = $header;
try {
(isset($_GET['debug_http']) && $_GET['debug_http'] == 911) && $option['debug'] = true;
$response = $this->client->request('GET', $url ,$option);
$this->debugHttp($option, $response);
} catch (GuzzleException $e) {
$exception = $e->getMessage();
$log = array(
'url' => $url,
'parameter'=> $data,
'exceptionMessage' => $exception
);
SentryLog::addLog(json_encode($log));
AliYunLog::recordLog('error', Constants::CURL_REQUEST_ERROR, Constants::message(Constants::CURL_REQUEST_ERROR). json_encode($log));
return response()->json($exception, 200);
}
return $response->getBody();
}
/**
* @param string $url
* @param array $data
* @param array $option
* @param array $header
* @param string $responseType
* @return \Illuminate\Http\JsonResponse|\Psr\Http\Message\StreamInterface
*/
public function post(string $url, array $data = [], array $option = [], array $header =[], $responseType = 'formType')
{
try {
!empty($header) && $option['headers'] = $header;
(isset($_GET['debug_http']) && $_GET['debug_http'] == 911) && $option['debug'] = true;
if ($responseType == 'json')
{
$option['body'] = json_encode($data);
$response = $this->client->request('POST', $url, $option);
}
else
{
$option['form_params'] = $data;
$response = $this->client->request('POST', $url, $option);
}
$this->debugHttp($option, $response);
}catch (GuzzleException $e){
$exception = $e->getMessage();
$log = array(
'url' => $url,
'parameter'=> $data,
'exceptionMessage' => $exception
);
SentryLog::addLog(json_encode($log));
return response()->json($exception, 200);
}
return $response->getBody();
}
/**
* @param string $url
* @param array $data
* @param array $option
* @param array $header
* @param string $responseType
* @return \Illuminate\Http\JsonResponse|\Psr\Http\Message\StreamInterface
*/
public function put(string $url, array $data = [], array $option = [], array $header =[], $responseType = 'formType')
{
try {
!empty($header) && $option['headers'] = $header;
(isset($_GET['debug_http']) && $_GET['debug_http'] == 911) && $option['debug'] = true;
if ($responseType == 'json')
{
$option['body'] = json_encode($data);
$response = $this->client->request('PUT', $url, $option);
}
else
{
$option['form_params'] = $data;
$response = $this->client->request('PUT', $url, $option);
}
$this->debugHttp($option, $response);
}catch (GuzzleException $e){
$exception = $e->getMessage();
$log = array(
'url' => $url,
'parameter'=> $data,
'exceptionMessage' => $exception
);
SentryLog::addLog(json_encode($log));
return response()->json($exception, 200);
}
return $response->getBody();
}
/**
* http 请求调试
* @param $option
* @param $response
*/
private function debugHttp($option, $response)
{
if (isset($_GET['debug_http']) && $_GET['debug_http'] == 911) {
echo "=====request data======\r\n";
print_r($option);
echo '=====request data end====='."\r\n";
echo '=====respond data====='."\r\n";
$body = $response->getBody();
echo $body . "\r\n";
echo '=====respond data end====='."\r\n";
}
}
}
\ No newline at end of file
<?php
namespace App\Lib;
use Illuminate\Support\Facades\Log;
class SentryLog
{
public static function addLog($message,$level = null){
if(is_array($message)){
$message = json_encode($message);
}
if (app()->bound('sentry')) {
app('sentry')->captureMessage($message);
} else {
Log::channel('single')->info($message);
}
}
}
\ No newline at end of file
...@@ -10,8 +10,11 @@ ...@@ -10,8 +10,11 @@
"require": { "require": {
"php": "^7.1.3", "php": "^7.1.3",
"fideloper/proxy": "^4.0", "fideloper/proxy": "^4.0",
"guzzlehttp/guzzle": "~6.0",
"laravel/framework": "5.8.*", "laravel/framework": "5.8.*",
"laravel/tinker": "^1.0" "laravel/tinker": "^1.0",
"base/log-tail": "*",
"sentry/sentry-laravel": "^1.0"
}, },
"require-dev": { "require-dev": {
"beyondcode/laravel-dump-server": "^1.0", "beyondcode/laravel-dump-server": "^1.0",
......
<?php
return [
'sign' => [
'secret_key' => env('SECRET_KEY',''),// 接口请求的密钥
]
];
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment