Base.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. protected $platformKey;
  14. public function _initialize()
  15. {
  16. if (isset($_SERVER['HTTP_ORIGIN'])) {
  17. header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
  18. }
  19. //跨域检测
  20. check_cors_request();
  21. if (!isset($_COOKIE['PHPSESSID'])) {
  22. Config::set('session.id', $this->request->server("HTTP_SID"));
  23. }
  24. parent::_initialize();
  25. Config::set('default_return_type', 'json');
  26. Auth::instance()->setAllowFields($this->allowFields);
  27. $platform = $this->request->header('platform');
  28. // 需要转换 前段穿 DouyinMiniProgram 后端配置是 douyin_mini_program
  29. $platformKey = $this->convertPlatformName($platform);
  30. $this->platformKey = $platformKey;
  31. $platformConfig = shop_config('shop.platform.' . $platformKey);
  32. if (empty($platformConfig['status']) || !$platformConfig['status']) {
  33. $this->error('暂不支持该平台,请前往商城配置启用对应平台');
  34. }
  35. //判断站点状态
  36. // if (isset($config['openedsite']) && !in_array('uniapp', explode(',', $config['openedsite']))) {
  37. // $this->error('站点已关闭');
  38. // }
  39. //这里手动载入语言包
  40. // Lang::load(ROOT_PATH . '/addons/shop/lang/zh-cn.php');
  41. // Lang::load(APP_PATH . '/index/lang/zh-cn/user.php');
  42. // //加载当前控制器的语言包
  43. // $controllername = strtolower($this->request->controller());
  44. // $lang = $this->request->langset();
  45. // $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
  46. // Lang::load(ADDON_PATH . 'shop/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
  47. }
  48. /**
  49. * 转换平台名称格式
  50. * 将前端驼峰命名转换为后端下划线格式
  51. *
  52. * @param string $platform
  53. * @return string
  54. */
  55. protected function convertPlatformName($platform)
  56. {
  57. // 将驼峰命名转换为下划线格式
  58. return strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($platform)));
  59. }
  60. }