Server : nginx/1.20.1 System : Linux iZ2ze9ojcl78uluczwag69Z 4.18.0-240.22.1.el8_3.x86_64 #1 SMP Thu Apr 8 19:01:30 UTC 2021 x86_64 User : www ( 1000) PHP Version : 7.3.28 Disable Function : passthru,exec,system,chroot,chgrp,chown,shell_exec,popen,proc_open,pcntl_exec,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,imap_open,apache_setenv Directory : /www/wwwroot/0531yanglao.com/app/admin/service/ |
<?php /* * @Description : Token * @Author : https://github.com/skyselang * @Date : 2021-03-09 * @LastEditTime : 2021-03-10 */ namespace app\admin\service; use think\facade\Config; use app\common\cache\UserCache; use Firebase\JWT\JWT; class TokenService { /** * Token生成 * * @param array $user 用户信息 * * @return string */ public static function create($user = []) { $setting = SettingService::setting(); $token = $setting['token']; $key = Config::get('index.token.key'); //密钥 $iss = $token['iss']; //签发者 $iat = time(); //签发时间 $nbf = time(); //生效时间 $exp = time() + $token['exp'] * 3600; //过期时间 $data = [ 'user_id' => $user['user_id'], 'login_time' => $user['login_time'], 'login_ip' => $user['login_ip'], ]; $payload = [ 'iss' => $iss, 'iat' => $iat, 'nbf' => $nbf, 'exp' => $exp, 'data' => $data, ]; $token = JWT::encode($payload, $key); return $token; } /** * Token验证 * * @param string $token token * * @return json */ public static function verify($token) { try { $key = Config::get('index.token.key'); $decode = JWT::decode($token, $key, array('HS256')); } catch (\Exception $e) { exception('登录状态错误', 401); } $user_id = $decode->data->user_id; $user = UserCache::get($user_id); if (empty($user)) { exception('账号登录状态失效', 401); } else { if ($token != $user['user_token']) { exception('账号已在另一处登录', 401); } else { if ($user['is_disable'] == 1) { exception('账号已被禁用', 401); } if ($user['is_delete'] == 1) { exception('账号已被删除', 401); } } } } /** * Token用户id * * @param string $token token * * @return integer */ public static function userId($token) { try { $key = Config::get('index.token.key'); $decode = JWT::decode($token, $key, array('HS256')); } catch (\Exception $e) { exception('用户登录状态已过期', 401); } $user_id = $decode->data->user_id; return $user_id; } }