1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\server;
-
-
- use think\Exception;
- use think\facade\Cache;
-
-
- class TokenServer
- {
-
-
- public static function generateToken($tokenSalt)
- {
- $randChar = getRandChar(32);
- $timestamp = $_SERVER['REQUEST_TIME_FLOAT'];
- return md5($randChar . $timestamp . $tokenSalt);
- }
-
-
-
- public static function getCurrentTokenVar($key, $isWhole=false)
- {
- $token = request()->header('token');
- $vars = Cache::get($token);
-
- if (empty($token) || !$token || !$vars) {
-
- throw new Exception('Token已过期或无效Token');
-
- } else {
-
- if (!is_array($vars)) {
- $vars = json_decode($vars, true);
- }
-
- if (array_key_exists($key, $vars)) {
-
- Cache::set($token, json_encode($vars, JSON_UNESCAPED_UNICODE), 7200);
-
- return $isWhole ? $vars : $vars[$key];
- } else {
- throw new Exception('尝试获取的Token变量并不存在');
- }
- }
- }
- }
|