InspectionApi.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace app\common\controller;
  3. use app\common\library\InspectionAuth;
  4. use think\Request;
  5. use think\Config;
  6. use think\Lang;
  7. use think\Loader;
  8. use think\exception\HttpResponseException;
  9. use think\Response;
  10. class InspectionApi
  11. {
  12. protected $request;
  13. protected $auth = null;
  14. protected $application = null;
  15. protected $user = null;
  16. protected $responseType = 'json';
  17. protected $noNeedLogin = [];
  18. protected $noNeedRight = [];
  19. public function __construct(Request $request = null)
  20. {
  21. $this->request = is_null($request) ? Request::instance() : $request;
  22. $this->_initialize();
  23. }
  24. protected function _initialize()
  25. {
  26. // 跨域检测
  27. check_cors_request();
  28. // IP 检查
  29. check_ip_allowed();
  30. // 过滤请求
  31. $this->request->filter('trim,strip_tags,htmlspecialchars');
  32. $this->auth = InspectionAuth::instance();
  33. // 检查是否需要登录
  34. $action = $this->request->action();
  35. if (!$this->auth->match($this->noNeedLogin)) {
  36. // token获取
  37. $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('inspection_token')));
  38. // 初始化验货员身份
  39. if (!$this->auth->init($token)) {
  40. $this->error($this->auth->getError() ?: '请先登录', null, 401);
  41. }
  42. if (!$this->auth->isLogin()) {
  43. $this->error('请先登录', null, 401);
  44. }
  45. $this->application = $this->auth->getApplication();
  46. $this->user = $this->auth->getUser();
  47. // 检查审核状态
  48. if (!$this->application || $this->application->audit_status != 2) {
  49. $this->error('验货员未通过审核', null, 403);
  50. }
  51. // 检查启用状态
  52. if (!$this->application || $this->application->status != 1) {
  53. $this->error('验货员账号已被禁用', null, 403);
  54. }
  55. // 检查供应商绑定
  56. if (!$this->application->supplier_id) {
  57. $this->error('未绑定供应商', null, 403);
  58. }
  59. // 检查权限
  60. if (!$this->auth->match($this->noNeedRight)) {
  61. // 这里可以添加具体的权限检查逻辑
  62. // 暂时允许所有已登录的验货员访问
  63. }
  64. }
  65. // 加载语言包
  66. $controllername = strtolower($this->request->controller());
  67. $lang = $this->request->langset();
  68. $lang = preg_match("/^([a-zA-Z\-_]{2,10})$/i", $lang) ? $lang : 'zh-cn';
  69. Lang::load(ADDON_PATH . 'shop/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
  70. }
  71. /**
  72. * 获取验货员信息
  73. * @return array
  74. */
  75. protected function getInspectorInfo()
  76. {
  77. if (!$this->auth || !$this->auth->isLogin()) {
  78. return null;
  79. }
  80. return $this->auth->getInspectorInfo();
  81. }
  82. /**
  83. * 检查验货员权限
  84. * @param string $path
  85. * @param string $module
  86. * @return bool
  87. */
  88. protected function checkAuth($path = null, $module = null)
  89. {
  90. return $this->auth->check($path, $module);
  91. }
  92. protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
  93. {
  94. $this->result($msg, $data, $code, $type, $header);
  95. }
  96. protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
  97. {
  98. $this->result($msg, $data, $code, $type, $header);
  99. }
  100. protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
  101. {
  102. $result = [
  103. 'code' => $code,
  104. 'msg' => $msg,
  105. 'time' => Request::instance()->server('REQUEST_TIME'),
  106. 'data' => $data,
  107. ];
  108. // 添加验货员信息到响应中(如果已登录)
  109. if ($this->auth && $this->auth->isLogin()) {
  110. $result['inspector'] = $this->getInspectorInfo();
  111. }
  112. $type = $type ?: $this->responseType;
  113. if (isset($header['statuscode'])) {
  114. $code = $header['statuscode'];
  115. unset($header['statuscode']);
  116. } else {
  117. $code = $code >= 1000 || $code < 200 ? 200 : $code;
  118. }
  119. $response = Response::create($result, $type, $code)->header($header);
  120. throw new HttpResponseException($response);
  121. }
  122. }