diff --git a/.example.env b/.example.env index 93e5c287..898b7ea9 100644 --- a/.example.env +++ b/.example.env @@ -47,8 +47,8 @@ REFRESH_TOKEN_LIFETIME = 1209600; # 刷新令牌有效期 RESOURCE_IMAGES_DOMAIN = http://local.orico.com; # 图片资源服务器地址 RESOURCE_VIDEOS_DOMAIN = http://local.orico.com; # 视频资源服务器地址 -# 视图模板规则配置 -[VIEW_TPL] +# 前台视图模板规则配置 +[INDEX_VIEW_TPL] # 视图目录 # query 规则:URL参数 mtpl=1 表示移动端访问 # 例如:http://xxxx.com?mtpl=1 @@ -63,4 +63,20 @@ RULE_QUERY_VALUE = 1 RULE_DOMAIN_SCHEME[] = http RULE_DOMAIN_SCHEME[] = https # domain 规则域名 -RULE_DOMAIN_HOST = mobile.orico.cn \ No newline at end of file +RULE_DOMAIN_HOST = mobile.orico.cn + +# 前台语言检测配置 +[INDEX_LANG_DETECT] +# 是否启用域名检测方式来检测语言 +DOMAIN_DETECT = true +# 域名规则 +# 格式:域名=语言 +# 例如:mobile.orico.cn=zh-cn +DOMAIN_RULE[] = orico.cn=zh-cn +DOMAIN_RULE[] = orico.com.cn=zh-cn +DOMAIN_RULE[] = www.orico.cn=zh-cn +DOMAIN_RULE[] = www.orico.com.cn=zh-cn +DOMAIN_RULE[] = ow.f2b211.com=zh-cn +DOMAIN_RULE[] = orico.cc=en-us +DOMAIN_RULE[] = www.orico.cc=en-us +DOMAIN_RULE[] = ow.us.f2b211.com=en-us diff --git a/app/index/common.php b/app/index/common.php index 63d2a364..2e8ad20e 100644 --- a/app/index/common.php +++ b/app/index/common.php @@ -194,14 +194,14 @@ if (!function_exists('get_platform')) { } else { // 在非移动端环境,根据配置规则判断是否要显示移动端 $view_cfg = $view_cfg = [ - 'rule' => env('VIEW_TPL.RULE', 'query'), + 'rule' => env('INDEX_VIEW_TPL.RULE', 'query'), 'query' => [ - 'name' => env('VIEW_TPL.RULE_QUERY_NAME', 'mtpl'), - 'value' => env('VIEW_TPL.RULE_QUERY_VALUE', '1'), + 'name' => env('INDEX_VIEW_TPL.RULE_QUERY_NAME', 'mtpl'), + 'value' => env('INDEX_VIEW_TPL.RULE_QUERY_VALUE', '1'), ], 'domain' => [ - 'scheme' => env('VIEW_TPL.RULE_DOMAIN_SCHEME', ['http']), - 'host' => env('VIEW_TPL.RULE_DOMAIN_HOST'), + 'scheme' => env('INDEX_VIEW_TPL.RULE_DOMAIN_SCHEME', ['http']), + 'host' => env('INDEX_VIEW_TPL.RULE_DOMAIN_HOST'), ], ]; if ($view_cfg['rule'] == 'query') { diff --git a/app/index/middleware.php b/app/index/middleware.php index bf7bb315..91580e28 100644 --- a/app/index/middleware.php +++ b/app/index/middleware.php @@ -2,7 +2,8 @@ // 这是系统自动生成的middleware定义文件 return [ // 启用多语言支持 - think\middleware\LoadLangPack::class, + // think\middleware\LoadLangPack::class, + app\index\middleware\LoadLangPack::class, // 确认请求来源 app\index\middleware\ConfirmRequestFrom::class, ]; diff --git a/app/index/middleware/LoadLangPack.php b/app/index/middleware/LoadLangPack.php new file mode 100644 index 00000000..098f9e33 --- /dev/null +++ b/app/index/middleware/LoadLangPack.php @@ -0,0 +1,39 @@ +getLangSet($request, env('INDEX_LANG_DETECT.DOMAIN_RULE', [])); + if ($lang != '') { + return $lang; + } + } + + return parent::detect($request); + } + + // 根据请求及规则获取语言 + private function getLangSet(Request $request, array $rules): string + { + $map = []; + foreach ($rules as $v) { + $val = str_replace(',', ',', $v); + $item = explode(',', $v); + foreach ($item as $val) { + $it = explode('=', $val); + $map[$it[0]] = $it[1]; + } + } + + return $map[$request->host()] ?? ''; + } +} diff --git a/composer.json b/composer.json index 9eeef5ba..f1bc0ce0 100644 --- a/composer.json +++ b/composer.json @@ -34,7 +34,8 @@ "topthink/think-cors": "^1.0", "phpoffice/phpspreadsheet": "^3.8", "friendsofsymfony/oauth2-php": "^1.3", - "mobiledetect/mobiledetectlib": "4.8.09" + "mobiledetect/mobiledetectlib": "4.8.09", + "qiniu/php-sdk": "^7.14" }, "require-dev": { "symfony/var-dumper": ">=4.2", diff --git a/extend/uploader/Qiniu.php b/extend/uploader/Qiniu.php new file mode 100644 index 00000000..54551cb3 --- /dev/null +++ b/extend/uploader/Qiniu.php @@ -0,0 +1,157 @@ + 1024 * 1024 * 5, // 默认最大上传5M + 'fileExt' => 'jpeg,jpg,png', // 默认上传文件后缀 + 'fileMime' => 'image/jpeg,image/png,image/gif' // 默认上传文件mime + ]; + + private $dir = true; + private $originalName = false; + private $pathPrefix = ''; + private $fileNamePrefix = 'orico'; + + static public $domain = 'http://opdfile.f2b211.com/'; + + public function __construct($conf = []) + { + if (!empty($conf['bucket'])) { + $this->bucket = $conf['bucket']; + } + if (!empty($conf['accessKey'])) { + $this->accessKey = $conf['accessKey']; + } + if (!empty($conf['secretKey'])) { + $this->secretKey = $conf['secretKey']; + } + if (!empty($conf['pathPrefix'])) { + $this->pathPrefix = trim($conf['pathPrefix'], '/'); + } + } + + /** + * 生成随机字符串 + */ + private function random($length, $type = "string", $convert = "0") + { + $conf = [ + 'number' => '0123456789', + 'string' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', + 'all' => 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789=' + ]; + $string = $conf[$type]; + if (!$string) { + $string = $conf['string']; + } + $strlen = strlen($string) - 1; + $char = ''; + for ($i = 0; $i < $length; $i++) { + $char .= $string[mt_rand(0, $strlen)]; + } + if ($convert > 0) { + $res = strtoupper($char); + } elseif ($convert == 0) { + $res = $char; + } elseif ($convert < 0) { + $res = strtolower($char); + } + return $res; + } + + /** + * 组装文件名 + */ + private function buildFileName() + { + return $this->fileNamePrefix . time() . substr(time(), -5) . substr(microtime(), 2, 3) . $this->random(8); + } + + /** + * 上传验证规则 + */ + public function validate($rule) + { + $this->rule = $rule; + } + + /** + * 上传文件到七牛云 + */ + public function uploadFile($name) + { + // 构建鉴权对象 + $auth = new Auth($this->accessKey, $this->secretKey); + + // 生成上传 Token + $token = $auth->uploadToken($this->bucket); + + // 初始化 UploadManager 对象并进行文件的上传。 + $uploadMgr = new UploadManager(); + + $file = request()->file($name); + + $aspectRatio = []; + if (!empty($this->rule['aspectRatio'])) { + $aspectRatio = $this->rule['aspectRatio']; + unset($this->rule['aspectRatio']); + } + $validate = validate([$name => $this->rule]); + if (!$validate->check([$name => $file])) { + throw new \Exception($validate->getError()); + } + + $fileName = $file->getOriginalName(); // 文件原名 + if (!$this->originalName) { + $fileName = $this->buildFileName() . '.' . $file->extension(); + if (!$this->dir && !empty($this->pathPrefix)) { + $fileName = $this->pathPrefix . '/' . $fileName; + } + } + if ($this->dir) { + $fileName = date('Y') . '/' . date('m') . '/' . date('d') . '/' . $fileName; + if (!empty($this->pathPrefix)) { + $fileName = $this->pathPrefix . '/' . $fileName; + } + } + $filePath = $file->getPathname(); // 临时路径 + if (!empty($aspectRatio)) { // 验证图片宽高 + list($width, $height, $type, $attr) = getimagesize($file); + if ($width != $aspectRatio['width'] || $height != $aspectRatio['height']) { + throw new \Exception('图片宽高不符合'); + } + } + $fileType = $file->getOriginalMime(); + list($ret, $err) = $uploadMgr->putFile($token, $fileName, $filePath, null, $fileType, false); + + if ($err !== null) { + throw new \Exception($err); + } else { + return ['hash' => $ret['hash'], 'filename' => $ret['key'], 'remote_url' => self::$domain . $ret['key']]; + } + } + + + /** + * 上传文件到七牛云 + */ + public function deleteFile($name) + { + // 构建鉴权对象 + $auth = new Auth($this->accessKey, $this->secretKey); + // 初始化 BucketManager 对象并进行文件的删除。 + $bucketManager = new BucketManager($auth); + $ret = $bucketManager->delete($this->bucket, $name); + return $ret; + } +}