From c39868f646502a86760ebcd5d3871c1c7694493c Mon Sep 17 00:00:00 2001 From: "453530270@qq.com" Date: Mon, 7 Oct 2024 20:40:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E7=99=BB=E5=BD=95=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/controller/Index.php | 2 +- app/api/controller/Passport.php | 2 +- app/api/service/UserService.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/api/controller/Index.php b/app/api/controller/Index.php index b8cbe81..0a25dea 100644 --- a/app/api/controller/Index.php +++ b/app/api/controller/Index.php @@ -16,7 +16,7 @@ class Index extends BaseController // make user password $data = $request->post(); // 用户密码 - $salt = $this->makeSalt(6); + $salt = isset($data['salt'])?$data['salt']:makeSalt(6); $arr['encpass']=password($data['upass']); $arr['salt']=$salt; } else { diff --git a/app/api/controller/Passport.php b/app/api/controller/Passport.php index 4ad337d..204e6dc 100644 --- a/app/api/controller/Passport.php +++ b/app/api/controller/Passport.php @@ -27,7 +27,7 @@ class Passport extends ApiController{ } return $this->renderSuccess([ 'userId' => $userInfo['uid'], - 'token' => $model->getToken() + 'token' => $model->getToken($userInfo['uid']) ], ''); } diff --git a/app/api/service/UserService.php b/app/api/service/UserService.php index 8258026..1a9c08f 100644 --- a/app/api/service/UserService.php +++ b/app/api/service/UserService.php @@ -54,4 +54,33 @@ class UserService { $uid = $model->save($arr); return isset($uid)?true:false; } + + /** + * 获取登录的token + * @param int $userId + * @return string + */ + public function getToken(int $userId): string + { + static $token = ''; + if (empty($token)) { + $token = $this->makeToken($userId); + } + return $token; + } + /** + * 生成用户认证的token + * @param int $userId + * @return string + */ + private function makeToken(int $userId): string + { + // 生成一个不会重复的随机字符串 + $guid = \get_guid_v4(); + // 当前时间戳 (精确到毫秒) + $timeStamp = \microtime(true); + // 自定义一个盐 + $salt = makeSalt(12); + return md5("{$timeStamp}_{$userId}_{$guid}_{$salt}"); + } } \ No newline at end of file