1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- <?php
- namespace app\utils;
- use think\Request;
- class CheckSignUtil{
-
- const hitList = [
- 'api/gift/givegift_typing',
- 'api/usercenter/chat_once',
- 'api/usercenter/voice_onemin',
- 'api/usercenter/videochat_onemin',
- ];
-
- const deadTime = 150;
-
- public static function check($path,$token,$sign): array
- {
- if (!in_array($path,self::hitList)){
- return [true,'success'];
- }
- if (empty($sign)){
- return [false,'签名缺失!'];
- }
-
- $rsa = new RsaUtil();
- $sign = $rsa->privateDecrypt($sign);
- if (!$sign || !$sign = json_decode($sign,true)){
- return [false,'签名错误!'];
- }
- if (empty($sign['token']) || empty($sign['timestamp']) || empty($sign['timezone'])){
- return [false,'签名参数错误!'];
- }
- if ($token != $sign['token']){
- return [false,'签名参数校验错误!'];
- }
- if ($token != $sign['token']){
- return [false,'签名参数校验错误!'];
- }
-
- $now = new \DateTime(null, new \DateTimeZone((string)$sign['timezone']));
- $time = (int)($now->format('U'));
-
- if ($sign['timestamp'] <= ($time - self::deadTime)){
- return [false,'签名过期,请求已过期!'];
- }
-
- $key = md5($sign['token'] . '_' .((string)$sign['timestamp']));
-
- if (!RedisUtil::getInstance('check_sign_lock',$key)->tryTimes(self::deadTime,1)) {
- return [false,'点的太快啦!'];
- }
- return [true,'success'];
- }
- }
|