| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?phpnamespace app\common\controller;use app\common\ErrorCode;use think\exception\HttpResponseException;use think\exception\ValidateException;use think\facade\Log;use think\Response;/** * 控制器基类 * Class Base * @package app\common\controller */class Base{    protected $debug=false;//是否启用调试模式    protected $checkSignOpen = false;//是否校验签名,true校验,false不校验    //=== begin 处理跨域====    /**     * 构造函数,处理跨域     * Base constructor     */    public function __construct()    {        if ($this->catchOptions()) {            die("this is option");        }        //启用调试模式    }    protected function catchOptions()    {        $request = request();        return $request->method() == "OPTIONS";    }    //===end 处理跨域====    /**     * 成功返回内容     * @param $data     * @param string $msg     * @param int $code     */    public function success($data,$msg="",$code=0)    {        if($msg==""){            $msg=ErrorCode::getError($code);        }        $response = returnFormat($code, $msg, $data);        Log::record(json_encode($response, JSON_UNESCAPED_UNICODE), "response");        throw new HttpResponseException(Response::create($response, "json"));    }    /**     * 失败返回内容     * @param int $code     * @param null $data     */    public function error($code = 9999,$msg="", $data = null)    {        if($msg==""){            $msg=ErrorCode::getError($code);        }        if ($data === null) {            $data = new \stdClass();        }        $response = returnFormat($code, $msg, $data);        Log::record(json_encode($response, JSON_UNESCAPED_UNICODE), "response");        throw new HttpResponseException(Response::create($response, "json"));    }    /**     * 自动校验     * @param $class     * @param $param     */    protected function autoValid($class, $param)    {        try {            validate($class)->check($param);        } catch (ValidateException $e) {            // 验证失败 输出错误信息            return returnFormat(999,$e->getError());            //$this->error($e->getError());        }        return returnFormat(0);    }    /**     * 接口签名校验     */    protected function checkApiSign()    {        $param = request()->param();        $validRes= $this->autoValid([            "_timestamp" => "require",            "_sign" => "require",        ], $param);        if($validRes['code']!==0){            return $validRes;        }        $effectMinute=2;//有效时间,分钟数        if (!($param["_timestamp"] >= time() - $effectMinute * 60 && $param["_timestamp"] <= time() + $effectMinute * 60)) {            return returnFormat(9004);        }        $sign = $param["_sign"];        unset($param["_sign"]);//删除原签名字段,用来重新生成签名        ksort($param);        $secret = config("common.api_sign_secret");        $sign2 = md5(json_encode($param, JSON_UNESCAPED_UNICODE) . $secret);        if ($sign !== $sign2) {            return returnFormat(9002," $secret 原签名:$sign 新签名:$sign2 ");        }        return returnFormat(0);    }}
 |