Base.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace app\api\controller\inspection;
  3. use app\common\controller\Api;
  4. use app\common\library\Auth;
  5. use think\Config;
  6. use think\Lang;
  7. use app\common\Service\InspectionService;
  8. use app\common\model\inspection\InspectionApplication;
  9. class Base extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. protected $application = null;
  14. //设置返回的会员字段
  15. protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'score', 'level', 'bio', 'balance', 'money', 'gender'];
  16. public function _initialize()
  17. {
  18. if (isset($_SERVER['HTTP_ORIGIN'])) {
  19. header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
  20. }
  21. //跨域检测
  22. check_cors_request();
  23. if (!isset($_COOKIE['PHPSESSID'])) {
  24. Config::set('session.id', $this->request->server("HTTP_SID"));
  25. }
  26. parent::_initialize();
  27. Auth::instance()->setAllowFields($this->allowFields);
  28. // 查询是否是审核员
  29. $user = Auth::instance()->getUser();
  30. $isInspection = null;
  31. if ($user) {
  32. $isInspection = InspectionService::getUserApplication($user->id);
  33. if (!$isInspection) {
  34. $this->error('您不是审核员');
  35. }
  36. // 验证是否 通过
  37. if ($isInspection->audit_status !== InspectionApplication::AUDIT_STATUS_PASSED) {
  38. $this->error('您的验货员申请未通过');
  39. }
  40. }
  41. $this->application = $isInspection;
  42. //这里手动载入语言包
  43. Lang::load(ROOT_PATH . '/addons/shop/lang/zh-cn.php');
  44. Lang::load(APP_PATH . '/index/lang/zh-cn/user.php');
  45. //加载当前控制器的语言包
  46. $controllername = strtolower($this->request->controller());
  47. $lang = $this->request->langset();
  48. $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
  49. Lang::load(ADDON_PATH . 'shop/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
  50. }
  51. }