diff --git a/server/addons/cos/library/Auth.php b/server/addons/cos/library/Auth.php index 3aaf5f3..640406b 100644 --- a/server/addons/cos/library/Auth.php +++ b/server/addons/cos/library/Auth.php @@ -27,11 +27,27 @@ class Auth if (in_array($module, $noNeedLogin) || ($module == 'admin' && \app\admin\library\Auth::instance()->id) || ($module != 'admin' && \app\common\library\Auth::instance()->id) + || ($module != 'admin' && \app\common\library\MasterAuth::instance()->id()) || $isModuleLogin) { return true; - } else { - return false; } + + // 若当前尚未初始化会话(如 API 模式),尝试从请求头或参数中解析 Token 校验用户端或师傅端 + if ($module == 'api' || $module != 'admin') { + $token = request()->server('HTTP_TOKEN', request()->header('token', request()->request('token', \think\Cookie::get('token', '')))); + if ($token) { + $userAuth = \app\common\library\Auth::instance(); + if ($userAuth->init($token) && $userAuth->id) { + return true; + } + $masterAuth = \app\common\library\MasterAuth::instance(); + if ($masterAuth->init($token) && $masterAuth->id() > 0) { + return true; + } + } + } + + return false; } } diff --git a/server/application/api/controller/Common.php b/server/application/api/controller/Common.php index 3be0eea..f74e45c 100755 --- a/server/application/api/controller/Common.php +++ b/server/application/api/controller/Common.php @@ -69,7 +69,7 @@ class Common extends Api public function upload() { if (!$this->auth->isLogin()) { - $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', '')); + $token = $this->request->server('HTTP_TOKEN', $this->request->header('token', $this->request->request('token', ''))); $masterAuth = \app\common\library\MasterAuth::instance(); if (!$token || !$masterAuth->init($token)) { $this->error(__('Please login first'), null, 401); diff --git a/server/extend/fast/Random.php b/server/extend/fast/Random.php index 34088b1..936c22a 100755 --- a/server/extend/fast/Random.php +++ b/server/extend/fast/Random.php @@ -97,6 +97,13 @@ class Random */ public static function uuid(): string { - return Uuid::uuid4()->toString(); + try { + $data = random_bytes(16); + $data[6] = chr(ord($data[6]) & 0x0f | 0x40); + $data[8] = chr(ord($data[8]) & 0x3f | 0x80); + return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4)); + } catch (\Throwable $e) { + return Uuid::uuid4()->toString(); + } } }