fix(gateway): 优化接口异常处理

- 在 query 和 submit 方法中添加 try-catch 块,捕获异常并记录日志
- 新增 writelog 方法用于统一日志记录
- 异常发生时返回错误码 -100 和异常信息
This commit is contained in:
mzeros 2024-12-07 12:24:34 +08:00
parent 44fd4d4af5
commit d02041d686

View File

@ -3,6 +3,7 @@
namespace app\gateway\controller\v4;
use app\channel\service\CardService;
use dever\Log;
/**
* 接口处理
@ -17,34 +18,47 @@ class Handle extends Core
# 查询接口 一般用于查询数据,同步执行
public function query(): void
{
$settingStatus = sysconf('settingStatus');
if ($settingStatus == 1) {
$this->no(-1000);
try{
$settingStatus = sysconf('settingStatus');
if ($settingStatus == 1) {
$this->no(-1000);
}
$this->checkOpenTime(sysconf('settingOpenTime'));
if (isset($this->merchant['opentime']) && $this->merchant['opentime']) {
$this->checkOpenTime($this->merchant['opentime']);
}
$data = $this->channel($this->mid, $this->product, false);
$this->yes($data);
} catch (\Exception $e){
$this->writelog($e->getMessage(), 'error');
$this->no(-100,$e->getMessage());
}
$this->checkOpenTime(sysconf('settingOpenTime'));
if (isset($this->merchant['opentime']) && $this->merchant['opentime']) {
$this->checkOpenTime($this->merchant['opentime']);
}
$data = $this->channel($this->mid, $this->product, false);
$this->yes($data);
}
# 提交接口 一般用于提交数据,异步执行
public function submit(): void
{
$settingStatus = sysconf('settingStatus');
if ($settingStatus == 1) {
$this->no(-1000);
}
$this->checkOpenTime(sysconf('settingOpenTime'));
if (isset($this->merchant['opentime']) && $this->merchant['opentime']) {
$this->checkOpenTime($this->merchant['opentime']);
try{
$settingStatus = sysconf('settingStatus');
if ($settingStatus == 1) {
$this->no(-1000);
}
$this->checkOpenTime(sysconf('settingOpenTime'));
if (isset($this->merchant['opentime']) && $this->merchant['opentime']) {
$this->checkOpenTime($this->merchant['opentime']);
}
$data = $this->channel($this->mid, $this->product, true);
$this->yes($data);
} catch (\Exception $e){
$this->writelog($e->getMessage(), 'error');
$this->no(-100,$e->getMessage());
}
$data = $this->channel($this->mid, $this->product, true);
$this->yes($data);
}
# 提交接口 一般用于提交数据,异步执行
@ -305,4 +319,14 @@ class Handle extends Core
return $v;
}
protected function writelog($data,$t = '')
{
if($t){
Log::write('gateway', 'Handle_'.$t, $data);
}else{
Log::write('gateway', 'Handle', $data);
}
}
}