/**
  * 验证字段是否符合注解数据参数约定条件,如果不能满足条件,返回错误结束该请求。
  *
  * @param string $key 传入的域的值
  * @param int $value
  * @param array $params 基于类模板读取的注解数组。
  */
 public function validator($key, $value, $params)
 {
     //是否必须
     if (isset($params[IntValidator::$_REQUIRED]) && $params[IntValidator::$_REQUIRED] == true) {
         if (!is_numeric($value)) {
             ResponseApi::sendErrorAndEnd($key . "=" . $value . "是必填项)", Message::$_ERROR_FORMAT);
         }
     }
     if ($value != null) {
         //类型检查
         if (!is_numeric($value)) {
             ResponseApi::sendErrorAndEnd($key . "=" . $value . "是必须是int型)", Message::$_ERROR_FORMAT);
         }
         $cvalue = intval($value);
         if (isset($params[IntValidator::$_MIN])) {
             $min = $params[IntValidator::$_MIN];
             if ($min && $cvalue < $min) {
                 ResponseApi::sendErrorAndEnd($key . "=" . $value . "的最小值是" . $min . ")", Message::$_ERROR_FORMAT);
             }
         }
         if (isset($params[IntValidator::$_MAX])) {
             $max = $params[IntValidator::$_MAX];
             if ($max && $cvalue > $max) {
                 ResponseApi::sendErrorAndEnd($key . "=" . $value . "的最大值是" . $max . ")", Message::$_ERROR_FORMAT);
             }
         }
     }
 }
 /**
  * 验证字段是否符合注解数据参数约定条件,如果不能满足条件,返回错误结束该请求。
  *
  * @param $key string 传入的域的值
  * @param $value int
  * @param array $params  基于类模板读取的注解数组。
  */
 public function validator($key, $value, $params)
 {
     if (isset($params[StringValidator::$_REQUIRED]) && $params[StringValidator::$_REQUIRED] == true) {
         if (is_null($value)) {
             ResponseApi::sendErrorAndEnd($key . "=" . $value . "是必填项)", Message::$_ERROR_FORMAT);
         }
     }
     if ($value) {
         if (!is_string($value)) {
             ResponseApi::sendErrorAndEnd($key . "=" . $value . "必须是string型)", Message::$_ERROR_FORMAT);
         }
         if (isset($params[StringValidator::$_MIN_LEN])) {
             $minlen = $params[StringValidator::$_MIN_LEN];
             if ($minlen && strlen($value) < $minlen) {
                 ResponseApi::sendErrorAndEnd($key . "=" . $value . "最小长度是" . $minlen . ")", Message::$_ERROR_FORMAT);
             }
         }
         if (isset($params[StringValidator::$_MAX_LEN])) {
             $maxlen = $params[StringValidator::$_MAX_LEN];
             if ($maxlen && strlen($value) > $maxlen) {
                 ResponseApi::sendErrorAndEnd($key . "=" . $value . "最大长度是" . $maxlen . ")", Message::$_ERROR_FORMAT);
             }
         }
     }
 }
 /**
  * 验证字段是否符合注解数据参数约定条件,如果不能满足条件,返回错误结束该请求。
  *
  * @param string $key 传入的域的值
  * @param int $value
  * @param array $params 基于类模板读取的注解数组。
  */
 public function validator($key, $value, $params)
 {
     //是否必须检测
     if (isset($params[DoubleValidator::$_REQUIRED]) && $params[DoubleValidator::$_REQUIRED] == true) {
         if (!is_numeric($value)) {
             ResponseApi::sendErrorAndEnd($key . "=" . $value . "是必填项)", Message::$_ERROR_FORMAT);
         }
     }
     if ($value != null) {
         //类型检查
         if (!is_numeric($value)) {
             ResponseApi::sendErrorAndEnd($key . "=" . $value . "必须是double型)", Message::$_ERROR_FORMAT);
         }
         $dvalue = doubleval($value);
         if (isset($params[DoubleValidator::$_MIN])) {
             $min_d = $params[DoubleValidator::$_MIN];
             if (isset($min_d)) {
                 if ($min_d && $dvalue < $min_d) {
                     ResponseApi::sendErrorAndEnd($key . "=" . $value . "是最小值是" . $min_d . ")", Message::$_ERROR_FORMAT);
                 }
             }
         }
         if (isset($params[DoubleValidator::$_MAX])) {
             $max_d = $params[DoubleValidator::$_MAX];
             if (isset($max_d)) {
                 if ($max_d && $dvalue > $max_d) {
                     ResponseApi::sendErrorAndEnd($key . "=" . $value . "是最大值是" . $max_d . ")", Message::$_ERROR_FORMAT);
                 }
             }
         }
     }
 }
 /**
  * 验证字段是否符合注解数据参数约定条件,如果不能满足条件,返回错误结束该请求。
  * @param string $key 传入的域的值
  * @param object $value
  * @param array $params 基于类模板读取的注解数组
  * @throws
  */
 public function validator($key, $value, $params)
 {
     if (isset($params[EnumValidator::$_REQUIRED]) && $params[EnumValidator::$_REQUIRED] == true) {
         if ($value == null) {
             ResponseApi::sendErrorAndEnd($key . "=" . $value . "是必填项", Message::$_ERROR_FORMAT);
         }
     }
     if (isset($params[EnumValidator::$_REQUIRED]) && !is_array($params[EnumValidator::$_SCOPE])) {
         throw new ValidatorNotFoundException("类枚举注解格式错误,应该定义为数组,例如[1,2,3]");
     }
     if ($value && isset($params[EnumValidator::$_SCOPE])) {
         $b = 0;
         $arr = $params[EnumValidator::$_SCOPE];
         $c = count($arr);
         $message = "";
         for ($i = 0; $i < $c; $i++) {
             $message = $message . $arr[$i] . ",";
             if ($params[EnumValidator::$_SCOPE][$i] == $value) {
                 $b = 1;
                 break;
             }
         }
         if ($b == 0) {
             ResponseApi::sendErrorAndEnd($key . "=" . $value . ",范围必须在[" . $message . "]", Message::$_ERROR_FORMAT);
         }
     }
 }
 /**
  * 校验拦截器,用以传入参数的校验。
  * @param \Phalcon\Events\Event $event
  * @param \Phalcon\Mvc\Dispatcher $dispatcher
  * @param array $params
  * @return mixed
  */
 public function action($event, $dispatcher, $params)
 {
     //$module = $dispatcher->getModuleName();
     //$controller = $dispatcher->getControllerName();
     $action = $dispatcher->getActionName();
     if ($action == 'auth') {
         return;
     }
     $auth = $dispatcher->getDI()->get(AppConstant::DI_SERVICE_SESSION)->get(AppConstant::HTTP_USER_AUTH);
     if (!$auth) {
         ResponseApi::sendErrorAndEnd(null, Message::$_ERROR_UNLOGIN);
     }
 }
Beispiel #6
0
 /**
  * 将后返回数据包裹成统一格式。
  * @param int $code
  * @param null $message
  */
 public static function sendError($code = -10, $message = '')
 {
     if (is_object($message) && method_exists($message, 'getMessage')) {
         //错误的时候,取出错误提示语
         $message = $message->getMessage();
     }
     $result = array();
     $result['body'] = "";
     $result['code'] = $code;
     if ($message == null) {
         $result['message'] = Messages::get($code);
     } else {
         $result['message'] = $message;
     }
     ResponseApi::sendResult($result);
 }
 /**
  * 抛出异常
  * @param Event $event
  * @param Dispatcher $dispatcher
  */
 public function beforeException(Event $event, Dispatcher $dispatcher, $exception)
 {
     ResponseApi::sendError($exception->getCode(), $exception->getMessage());
 }