|
@@ -4,21 +4,66 @@ namespace app\common\controller;
|
|
|
|
|
|
use app\common\library\InspectionAuth;
|
|
|
use think\Request;
|
|
|
-use think\Config;
|
|
|
use think\Lang;
|
|
|
use think\Loader;
|
|
|
use think\exception\HttpResponseException;
|
|
|
use think\Response;
|
|
|
-
|
|
|
+use think\Config;
|
|
|
+use think\exception\ValidateException;
|
|
|
+use think\Hook;
|
|
|
+use think\Route;
|
|
|
+use think\Validate;
|
|
|
+use app\common\model\inspection\InspectionApplication;
|
|
|
+use app\common\Enum\StatusEnum;
|
|
|
class InspectionApi
|
|
|
{
|
|
|
+ /**
|
|
|
+ * @var Request Request 实例
|
|
|
+ */
|
|
|
protected $request;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var bool 验证失败是否抛出异常
|
|
|
+ */
|
|
|
+ protected $failException = false;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var bool 是否批量验证
|
|
|
+ */
|
|
|
+ protected $batchValidate = false;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @var array 前置操作方法列表
|
|
|
+ */
|
|
|
+ protected $beforeActionList = [];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 无需登录的方法,同时也就不需要鉴权了
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ protected $noNeedLogin = [];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 无需鉴权的方法,但需要登录
|
|
|
+ * @var array
|
|
|
+ */
|
|
|
+ protected $noNeedRight = [];
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 权限Auth
|
|
|
+ * @var Auth
|
|
|
+ */
|
|
|
protected $auth = null;
|
|
|
+
|
|
|
protected $application = null;
|
|
|
protected $user = null;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 默认响应输出类型,支持json/xml
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
protected $responseType = 'json';
|
|
|
- protected $noNeedLogin = [];
|
|
|
- protected $noNeedRight = [];
|
|
|
+
|
|
|
|
|
|
public function __construct(Request $request = null)
|
|
|
{
|
|
@@ -56,18 +101,18 @@ class InspectionApi
|
|
|
$this->user = $this->auth->getUser();
|
|
|
|
|
|
// 检查审核状态
|
|
|
- if (!$this->application || $this->application->audit_status != 2) {
|
|
|
- $this->error('验货员未通过审核', null, 403);
|
|
|
+ if (!$this->application || $this->application->audit_status != InspectionApplication::AUDIT_STATUS_PASSED) {
|
|
|
+ $this->error('验货员未通过审核', null);
|
|
|
}
|
|
|
|
|
|
// 检查启用状态
|
|
|
- if (!$this->application || $this->application->status != 1) {
|
|
|
- $this->error('验货员账号已被禁用', null, 403);
|
|
|
+ if (!$this->application || $this->application->status != StatusEnum::ENABLED) {
|
|
|
+ $this->error('验货员账号已被禁用', null);
|
|
|
}
|
|
|
|
|
|
// 检查供应商绑定
|
|
|
if (!$this->application->supplier_id) {
|
|
|
- $this->error('未绑定供应商', null, 403);
|
|
|
+ $this->error('未绑定供应商', null);
|
|
|
}
|
|
|
|
|
|
// 检查权限
|
|
@@ -83,40 +128,59 @@ class InspectionApi
|
|
|
$lang = preg_match("/^([a-zA-Z\-_]{2,10})$/i", $lang) ? $lang : 'zh-cn';
|
|
|
Lang::load(ADDON_PATH . 'shop/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
|
|
|
}
|
|
|
+
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
- * 获取验货员信息
|
|
|
- * @return array
|
|
|
+ * 加载语言文件
|
|
|
+ * @param string $name
|
|
|
*/
|
|
|
- protected function getInspectorInfo()
|
|
|
+ protected function loadlang($name)
|
|
|
{
|
|
|
- if (!$this->auth || !$this->auth->isLogin()) {
|
|
|
- return null;
|
|
|
- }
|
|
|
- return $this->auth->getInspectorInfo();
|
|
|
+ $name = Loader::parseName($name);
|
|
|
+ $name = preg_match("/^([a-zA-Z0-9_\.\/]+)\$/i", $name) ? $name : 'index';
|
|
|
+ $lang = $this->request->langset();
|
|
|
+ $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
|
|
|
+ Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php');
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 检查验货员权限
|
|
|
- * @param string $path
|
|
|
- * @param string $module
|
|
|
- * @return bool
|
|
|
+ * 操作成功返回的数据
|
|
|
+ * @param string $msg 提示信息
|
|
|
+ * @param mixed $data 要返回的数据
|
|
|
+ * @param int $code 错误码,默认为1
|
|
|
+ * @param string $type 输出类型
|
|
|
+ * @param array $header 发送的 Header 信息
|
|
|
*/
|
|
|
- protected function checkAuth($path = null, $module = null)
|
|
|
- {
|
|
|
- return $this->auth->check($path, $module);
|
|
|
- }
|
|
|
-
|
|
|
protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
|
|
|
{
|
|
|
$this->result($msg, $data, $code, $type, $header);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 操作失败返回的数据
|
|
|
+ * @param string $msg 提示信息
|
|
|
+ * @param mixed $data 要返回的数据
|
|
|
+ * @param int $code 错误码,默认为0
|
|
|
+ * @param string $type 输出类型
|
|
|
+ * @param array $header 发送的 Header 信息
|
|
|
+ */
|
|
|
protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
|
|
|
{
|
|
|
$this->result($msg, $data, $code, $type, $header);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 返回封装后的 API 数据到客户端
|
|
|
+ * @access protected
|
|
|
+ * @param mixed $msg 提示信息
|
|
|
+ * @param mixed $data 要返回的数据
|
|
|
+ * @param int $code 错误码,默认为0
|
|
|
+ * @param string $type 输出类型,支持json/xml/jsonp
|
|
|
+ * @param array $header 发送的 Header 信息
|
|
|
+ * @return void
|
|
|
+ * @throws HttpResponseException
|
|
|
+ */
|
|
|
protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
|
|
|
{
|
|
|
$result = [
|
|
@@ -125,20 +189,127 @@ class InspectionApi
|
|
|
'time' => Request::instance()->server('REQUEST_TIME'),
|
|
|
'data' => $data,
|
|
|
];
|
|
|
-
|
|
|
- // 添加验货员信息到响应中(如果已登录)
|
|
|
- if ($this->auth && $this->auth->isLogin()) {
|
|
|
- $result['inspector'] = $this->getInspectorInfo();
|
|
|
- }
|
|
|
-
|
|
|
- $type = $type ?: $this->responseType;
|
|
|
+ // 如果未设置类型则使用默认类型判断
|
|
|
+ $type = $type ? : $this->responseType;
|
|
|
+
|
|
|
if (isset($header['statuscode'])) {
|
|
|
$code = $header['statuscode'];
|
|
|
unset($header['statuscode']);
|
|
|
} else {
|
|
|
+ //未设置状态码,根据code值判断
|
|
|
$code = $code >= 1000 || $code < 200 ? 200 : $code;
|
|
|
}
|
|
|
$response = Response::create($result, $type, $code)->header($header);
|
|
|
throw new HttpResponseException($response);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 前置操作
|
|
|
+ * @access protected
|
|
|
+ * @param string $method 前置操作方法名
|
|
|
+ * @param array $options 调用参数 ['only'=>[...]] 或者 ['except'=>[...]]
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ protected function beforeAction($method, $options = [])
|
|
|
+ {
|
|
|
+ if (isset($options['only'])) {
|
|
|
+ if (is_string($options['only'])) {
|
|
|
+ $options['only'] = explode(',', $options['only']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!in_array($this->request->action(), $options['only'])) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ } elseif (isset($options['except'])) {
|
|
|
+ if (is_string($options['except'])) {
|
|
|
+ $options['except'] = explode(',', $options['except']);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (in_array($this->request->action(), $options['except'])) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ call_user_func([$this, $method]);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置验证失败后是否抛出异常
|
|
|
+ * @access protected
|
|
|
+ * @param bool $fail 是否抛出异常
|
|
|
+ * @return $this
|
|
|
+ */
|
|
|
+ protected function validateFailException($fail = true)
|
|
|
+ {
|
|
|
+ $this->failException = $fail;
|
|
|
+
|
|
|
+ return $this;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证数据
|
|
|
+ * @access protected
|
|
|
+ * @param array $data 数据
|
|
|
+ * @param string|array $validate 验证器名或者验证规则数组
|
|
|
+ * @param array $message 提示信息
|
|
|
+ * @param bool $batch 是否批量验证
|
|
|
+ * @param mixed $callback 回调方法(闭包)
|
|
|
+ * @return array|string|true
|
|
|
+ * @throws ValidateException
|
|
|
+ */
|
|
|
+ protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
|
|
|
+ {
|
|
|
+ if (is_array($validate)) {
|
|
|
+ $v = Loader::validate();
|
|
|
+ $v->rule($validate);
|
|
|
+ } else {
|
|
|
+ // 支持场景
|
|
|
+ if (strpos($validate, '.')) {
|
|
|
+ list($validate, $scene) = explode('.', $validate);
|
|
|
+ }
|
|
|
+
|
|
|
+ $v = Loader::validate($validate);
|
|
|
+
|
|
|
+ !empty($scene) && $v->scene($scene);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 批量验证
|
|
|
+ if ($batch || $this->batchValidate) {
|
|
|
+ $v->batch(true);
|
|
|
+ }
|
|
|
+ // 设置错误信息
|
|
|
+ if (is_array($message)) {
|
|
|
+ $v->message($message);
|
|
|
+ }
|
|
|
+ // 使用回调验证
|
|
|
+ if ($callback && is_callable($callback)) {
|
|
|
+ call_user_func_array($callback, [$v, &$data]);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!$v->check($data)) {
|
|
|
+ if ($this->failException) {
|
|
|
+ throw new ValidateException($v->getError());
|
|
|
+ }
|
|
|
+
|
|
|
+ return $v->getError();
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 刷新Token
|
|
|
+ */
|
|
|
+ protected function token()
|
|
|
+ {
|
|
|
+ $token = $this->request->param('__token__');
|
|
|
+
|
|
|
+ //验证Token
|
|
|
+ if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
|
|
|
+ $this->error(__('Token verification error'), ['__token__' => $this->request->token()]);
|
|
|
+ }
|
|
|
+
|
|
|
+ //刷新Token
|
|
|
+ $this->request->token();
|
|
|
+ }
|
|
|
}
|