12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\library\Auth;
- use think\Config;
- use think\Lang;
- class Base extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = ['*'];
- //设置返回的会员字段
- protected $allowFields = ['id', 'username', 'nickname', 'mobile', 'avatar', 'score', 'level', 'bio', 'balance', 'money', 'gender'];
- protected $platformKey;
- public function _initialize()
- {
- if (isset($_SERVER['HTTP_ORIGIN'])) {
- header('Access-Control-Expose-Headers: __token__');//跨域让客户端获取到
- }
- //跨域检测
- check_cors_request();
- if (!isset($_COOKIE['PHPSESSID'])) {
- Config::set('session.id', $this->request->server("HTTP_SID"));
- }
- parent::_initialize();
- Config::set('default_return_type', 'json');
- Auth::instance()->setAllowFields($this->allowFields);
- $platform = $this->request->header('platform');
- // 需要转换 前段穿 DouyinMiniProgram 后端配置是 douyin_mini_program
- $platformKey = $this->convertPlatformName($platform);
- $this->platformKey = $platformKey;
- $platformConfig = shop_config('shop.platform.' . $platformKey);
- if (empty($platformConfig['status']) || !$platformConfig['status']) {
- $this->error('暂不支持该平台,请前往商城配置启用对应平台');
- }
- //判断站点状态
- // if (isset($config['openedsite']) && !in_array('uniapp', explode(',', $config['openedsite']))) {
- // $this->error('站点已关闭');
- // }
- //这里手动载入语言包
- // Lang::load(ROOT_PATH . '/addons/shop/lang/zh-cn.php');
- // Lang::load(APP_PATH . '/index/lang/zh-cn/user.php');
- // //加载当前控制器的语言包
- // $controllername = strtolower($this->request->controller());
- // $lang = $this->request->langset();
- // $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn';
- // Lang::load(ADDON_PATH . 'shop/lang/' . $lang . '/' . str_replace('.', '/', $controllername) . '.php');
- }
- /**
- * 转换平台名称格式
- * 将前端驼峰命名转换为后端下划线格式
- *
- * @param string $platform
- * @return string
- */
- protected function convertPlatformName($platform)
- {
- // 将驼峰命名转换为下划线格式
- return strtolower(preg_replace('/([A-Z])/', '_$1', lcfirst($platform)));
- }
- }
|