Base.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\library\Auth;
  5. use think\Config;
  6. use think\Lang;
  7. class Base extends Api
  8. {
  9. protected $noNeedLogin = [];
  10. protected $noNeedRight = ['*'];
  11. //设置返回的会员字段
  12. protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'score', 'level', 'bio', 'balance', 'money', 'gender'];
  13. public function _initialize()
  14. {
  15. if (isset($_SERVER['HTTP_ORIGIN'])) {
  16. header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
  17. }
  18. //跨域检测
  19. check_cors_request();
  20. if (!isset($_COOKIE['PHPSESSID'])) {
  21. Config::set('session.id', $this->request->server("HTTP_SID"));
  22. }
  23. parent::_initialize();
  24. $config = get_addon_config('shop');
  25. Config::set('shop', $config);
  26. Config::set('default_return_type', 'json');
  27. Auth::instance()->setAllowFields($this->allowFields);
  28. //判断站点状态
  29. if (isset($config['openedsite']) && !in_array('uniapp', explode(',', $config['openedsite']))) {
  30. $this->error('站点已关闭');
  31. }
  32. //这里手动载入语言包
  33. Lang::load(ROOT_PATH . '/addons/shop/lang/zh-cn.php');
  34. Lang::load(APP_PATH . '/index/lang/zh-cn/user.php');
  35. //加载当前控制器的语言包
  36. $controllername = strtolower($this->request->controller());
  37. $lang = $this->request->langset();
  38. $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
  39. Lang::load(ADDON_PATH . 'shop/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
  40. }
  41. }