You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.6 KiB

<?php
namespace app\service;
use app\model\Channel;
use think\exception\HttpResponseException;
class SignService extends BaseService
{
public function __construct()
{
parent::__construct();
}
// 验签
public function checkSign(array $post = [], string $sign = ""){
$appkey = "";
$timestamp = 0;
if(isset($post['appkey']) && !empty($post['appkey'])){
$appkey = $post['appkey'];
}else{
throw new HttpResponseException(_error('缺少appkey', 400));
}
if(strlen($appkey) != 16){
throw new HttpResponseException(_error('错误appkey', 400));
}
if(isset($post['timestamp']) && !empty($post['timestamp'])){
if($post['timestamp'] < time()-150 || $post['timestamp'] > time()+150){
throw new HttpResponseException(_error('请求已过期', 400));
}
$timestamp = $post['timestamp'];
unset($post['timestamp']);
}else{
throw new HttpResponseException(_error('缺少timestamp', 400));
}
if (empty($sign)){
throw new HttpResponseException(_error('缺少sign', 400));
}
// 获取token
$channel = Channel::where('appkey', $appkey)->where('status', 1)->where('is_deleted', 0)->find();
if(!$channel){
throw new HttpResponseException(_error('缺少appkey', 400));
}
$str = "appkey={$appkey}&timestamp={$timestamp}&token=".$channel['token'];
if($sign != md5($str)){
throw new HttpResponseException(_error('签名错误', 400));
}
return true;
}
}