优化访问接口报错Server internal error
This commit is contained in:
@@ -45,16 +45,20 @@ class BaseController extends OpenController
|
||||
*/
|
||||
protected function init(): void
|
||||
{
|
||||
// 登录模式赋值
|
||||
// 登录模式赋值(仅当 check_admin 有效时赋值,避免登录接口等未带 token 时访问 null 导致报错)
|
||||
$isLogin = request()->header('check_login', false);
|
||||
if ($isLogin) {
|
||||
$result = request()->header('check_admin');
|
||||
$this->adminId = $result['id'];
|
||||
$this->adminName = $result['username'];
|
||||
$this->adminInfo = UserInfoCache::getUserInfo($result['id']);
|
||||
$result = request()->header('check_admin');
|
||||
if ($isLogin && $result !== null && (is_array($result) || is_object($result))) {
|
||||
$arr = is_array($result) ? $result : (array) $result;
|
||||
$adminId = $arr['id'] ?? null;
|
||||
if ($adminId !== null) {
|
||||
$this->adminId = (int) $adminId;
|
||||
$this->adminName = $arr['username'] ?? '';
|
||||
$this->adminInfo = UserInfoCache::getUserInfo($adminId);
|
||||
|
||||
// 用户数据传递给逻辑层
|
||||
$this->logic && $this->logic->init($this->adminInfo);
|
||||
// 用户数据传递给逻辑层
|
||||
$this->logic && $this->logic->init($this->adminInfo);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -106,18 +106,21 @@ class BaseModel extends Model implements ModelInterface
|
||||
*/
|
||||
public static function onBeforeInsert($model): void
|
||||
{
|
||||
$createTime = $model->createTime ?? 'create_time';
|
||||
if ($createTime && !$model->getData($createTime)) {
|
||||
$model->set($createTime, date('Y-m-d H:i:s'));
|
||||
try {
|
||||
$createTime = $model->createTime ?? 'create_time';
|
||||
if ($createTime && !$model->getData($createTime)) {
|
||||
$model->set($createTime, date('Y-m-d H:i:s'));
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
if (function_exists('getCurrentInfo')) {
|
||||
$info = getCurrentInfo();
|
||||
if (!empty($info['id'])) {
|
||||
try {
|
||||
try {
|
||||
if (function_exists('getCurrentInfo')) {
|
||||
$info = getCurrentInfo();
|
||||
if (!empty($info['id'])) {
|
||||
$model->setAttr('created_by', $info['id']);
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
}
|
||||
|
||||
@@ -128,20 +131,23 @@ class BaseModel extends Model implements ModelInterface
|
||||
*/
|
||||
public static function onBeforeWrite($model): void
|
||||
{
|
||||
if ($model->isExists()) {
|
||||
$updateTime = $model->updateTime ?? 'update_time';
|
||||
if ($updateTime) {
|
||||
$model->set($updateTime, date('Y-m-d H:i:s'));
|
||||
}
|
||||
}
|
||||
if (function_exists('getCurrentInfo')) {
|
||||
$info = getCurrentInfo();
|
||||
if (!empty($info['id'])) {
|
||||
try {
|
||||
$model->setAttr('updated_by', $info['id']);
|
||||
} catch (\Throwable $e) {
|
||||
try {
|
||||
if ($model->isExists()) {
|
||||
$updateTime = $model->updateTime ?? 'update_time';
|
||||
if ($updateTime) {
|
||||
$model->set($updateTime, date('Y-m-d H:i:s'));
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
try {
|
||||
if (function_exists('getCurrentInfo')) {
|
||||
$info = getCurrentInfo();
|
||||
if (!empty($info['id'])) {
|
||||
$model->setAttr('updated_by', $info['id']);
|
||||
}
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user