InspectionApi.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. public function __construct(Request $request = null)
  18. {
  19. $this->request = is_null($request) ? Request::instance() : $request;
  20. $this->_initialize();
  21. }
  22. protected function _initialize()
  23. {
  24. // 跨域检测
  25. check_cors_request();
  26. // IP 检查
  27. check_ip_allowed();
  28. // 过滤请求
  29. $this->request->filter('trim,strip_tags,htmlspecialchars');
  30. $this->auth = InspectionAuth::instance();
  31. // token
  32. $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token')));
  33. // 初始化验货员身份
  34. $this->auth->init($token);
  35. if (!$this->auth->isLogin()) {
  36. $this->error('请先登录', null, 401);
  37. }
  38. $this->application = $this->auth->getApplication();
  39. $this->user = $this->auth->getUser();
  40. // 检查审核状态
  41. if (!$this->application || $this->application->audit_status != 2) {
  42. $this->error('验货员未通过审核', null, 403);
  43. }
  44. // 检查供应商绑定
  45. if (!$this->application->supplier_id) {
  46. $this->error('未绑定供应商', null, 403);
  47. }
  48. // 加载语言包
  49. $controllername = strtolower($this->request->controller());
  50. $lang = $this->request->langset();
  51. $lang = preg_match("/^([a-zA-Z\-_]{2,10})$/i", $lang) ? $lang : 'zh-cn';
  52. Lang::load(ADDON_PATH . 'shop/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
  53. }
  54. protected function success($msg = '', $data = null, $code = 1, $type = null, array $header = [])
  55. {
  56. $this->result($msg, $data, $code, $type, $header);
  57. }
  58. protected function error($msg = '', $data = null, $code = 0, $type = null, array $header = [])
  59. {
  60. $this->result($msg, $data, $code, $type, $header);
  61. }
  62. protected function result($msg, $data = null, $code = 0, $type = null, array $header = [])
  63. {
  64. $result = [
  65. 'code' => $code,
  66. 'msg' => $msg,
  67. 'time' => Request::instance()->server('REQUEST_TIME'),
  68. 'data' => $data,
  69. ];
  70. $type = $type ?: $this->responseType;
  71. if (isset($header['statuscode'])) {
  72. $code = $header['statuscode'];
  73. unset($header['statuscode']);
  74. } else {
  75. $code = $code >= 1000 || $code < 200 ? 200 : $code;
  76. }
  77. $response = Response::create($result, $type, $code)->header($header);
  78. throw new HttpResponseException($response);
  79. }
  80. }