Files
CompanyProject/芳林公司项目/www.zhiyuanhelp.com/application/api/service/Token.php
2024-09-27 01:32:49 +08:00

65 lines
1.5 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: Administrator
* Date: 2020/4/13
*/
namespace app\api\service;
use think\Exception;
use think\facade\Cache;
use think\facade\Request;
class Token
{
public static function generateToken(){
$randChar = getRandChar(32);
$timestamp = $_SERVER['REQUEST_TIME_FLOAT'];
$tokenSalt = config('secure.token_salt');
return md5($randChar . $timestamp . $tokenSalt);
}
public static function getCurrentTokenVar($key){
$token = Request::header('token');
// $vars = Cache::get($token);
$vars = Cache::store('redis')->get($token);
if(!$vars){
throw new TokenException();
}else{
if(!is_array($vars))
{
$vars = json_decode($vars, true);
return $vars;
}
if (array_key_exists($key, $vars)) {
return $vars[$key];
}
else{
throw new Exception('尝试获取的Token变量并不存在');
}
}
}
public static function getCurrentUid(){
$uid = self::getCurrentTokenVar('uid');
return $uid;
}
public static function isValidOperate($checkedUID){
if(!$checkedUID){
throw new Exception('检测UID时必须传入一个被检测的UID');
}
$currentOperateUID = self::getCurrentUid();
if($currentOperateUID == $checkedUID){
return true;
}
return false;
}
}