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.

80 lines
1.7 KiB

<?php
namespace app\entrust\controller;
use app\BaseController;
use app\service\SignService;
use think\exception\HttpResponseException;
use think\facade\Log;
use think\facade\Request;
use think\Validate;
class Base extends BaseController
{
/**
* 无需检测的方法.
*
* @var array
*/
protected $noNeedCheck = ['apiDoc'];
protected $log = null;
protected $token = null;
public function __construct()
{
parent::__construct();
$this->log = new Log();
if(!$this->match($this->noNeedCheck)){
$this->checkSign();
}
}
/**
* 检测当前控制器和方法是否匹配传递的数组.
*
* @param array $arr 需要验证权限的数组
* @return bool
*/
public function match($arr = [])
{
$request = Request::instance();
$arr = is_array($arr) ? $arr : explode(',', $arr);
if (! $arr) {
return false;
}
$arr = array_map('strtolower', $arr);
// 是否存在
if (in_array(strtolower($request->action()), $arr) || in_array('*', $arr)) {
return true;
}
// 没找到匹配
return false;
}
/**
* 校验签名.
*
* @return bool
*/
public function checkSign()
{
$signService = new SignService();
// 获取请求数据
$post = $this->request->post();
// 获取签名以及key
$sign = isset($_SERVER['HTTP_SIGN']) ? $_SERVER['HTTP_SIGN'] : '';
if (!$sign){
throw new HttpResponseException(_error('缺少sign', 400));
}
$signService->checkSign($post, $sign);
}
}