diff --git a/extend/service/FileService.php b/extend/service/FileService.php index 4d2a7df72..3d8922457 100644 --- a/extend/service/FileService.php +++ b/extend/service/FileService.php @@ -15,11 +15,11 @@ namespace service; use Exception; +use think\Log; +use think\Config; use Qiniu\Auth; use Qiniu\Storage\BucketManager; use Qiniu\Storage\UploadManager; -use think\Config; -use think\Log; /** * 系统文件服务 @@ -31,21 +31,20 @@ use think\Log; class FileService { /** - * 获取文件MINE信息 - * @param string $exts + * 根据文件后缀获取文件MINE + * @param array $exts 文件后缀 + * @param array $mine 文件后缀MINE信息 * @return string */ - static public function getFileMine($exts) { - $_exts = is_string($exts) ? explode(',', $exts) : $exts; - $_mines = []; + public static function getFileMine($exts, $mine = []) { $mines = Config::get('mines'); - foreach ($_exts as $_e) { + foreach (is_string($exts) ? explode(',', $exts) : $exts as $_e) { if (isset($mines[strtolower($_e)])) { $_exinfo = $mines[strtolower($_e)]; - $_mines[] = is_array($_exinfo) ? join(',', $_exinfo) : $_exinfo; + $mine[] = is_array($_exinfo) ? join(',', $_exinfo) : $_exinfo; } } - return join(',', $_mines); + return join(',', $mine); } /** @@ -54,7 +53,7 @@ class FileService { * @param string|null $storage * @return bool|string */ - static public function getFileUrl($filename, $storage = null) { + public static function getFileUrl($filename, $storage = null) { if (self::hasFile($filename, $storage) === false) { return false; } @@ -112,7 +111,7 @@ class FileService { * 获取服务器URL前缀 * @return string */ - static public function getBaseUriLocal() { + public static function getBaseUriLocal() { $request = request(); $base = $request->root(); $root = strpos($base, '.') ? ltrim(dirname($base), DS) : $base; @@ -126,7 +125,7 @@ class FileService { * 获取七牛云URL前缀 * @return string */ - static public function getBaseUriQiniu() { + public static function getBaseUriQiniu() { return (sysconf('storage_qiniu_is_https') ? 'https' : 'http') . '://' . sysconf('storage_qiniu_domain') . '/'; } @@ -136,7 +135,7 @@ class FileService { * @param string|null $storage * @return bool */ - static public function hasFile($filename, $storage = null) { + public static function hasFile($filename, $storage = null) { switch (empty($storage) ? sysconf('storage_type') : $storage) { case 'local': return file_exists(ROOT_PATH . 'static/upload/' . $filename); @@ -155,7 +154,7 @@ class FileService { * @param string|null $storage * @return string|null */ - static public function readFile($filename, $storage = null) { + public static function readFile($filename, $storage = null) { switch (empty($storage) ? sysconf('storage_type') : $storage) { case 'local': $filepath = ROOT_PATH . 'static/upload/' . $filename; @@ -177,7 +176,7 @@ class FileService { * @param string|null $file_storage * @return array|false */ - static public function save($filename, $bodycontent, $file_storage = null) { + public static function save($filename, $bodycontent, $file_storage = null) { $type = empty($file_storage) ? sysconf('storage_type') : $file_storage; if (!method_exists(__CLASS__, $type)) { Log::error("保存存储失败,调用{$type}存储引擎不存在!"); @@ -192,9 +191,9 @@ class FileService { * @param string $bodycontent * @return string */ - static public function local($filename, $bodycontent) { - $filepath = ROOT_PATH . 'static/upload/' . $filename; + public static function local($filename, $bodycontent) { try { + $filepath = ROOT_PATH . 'static/upload/' . $filename; !file_exists(dirname($filepath)) && mkdir(dirname($filepath), '0755', true); if (file_put_contents($filepath, $bodycontent)) { return [ @@ -216,7 +215,7 @@ class FileService { * @param string $bodycontent * @return string */ - static public function qiniu($filename, $bodycontent) { + public static function qiniu($filename, $bodycontent) { $auth = new Auth(sysconf('storage_qiniu_access_key'), sysconf('storage_qiniu_secret_key')); $token = $auth->uploadToken(sysconf('storage_qiniu_bucket')); $uploadMgr = new UploadManager(); diff --git a/extend/service/HttpService.php b/extend/service/HttpService.php index e555e830f..d29350282 100644 --- a/extend/service/HttpService.php +++ b/extend/service/HttpService.php @@ -14,6 +14,7 @@ namespace service; +use CURLFile; use think\Config; /** @@ -35,25 +36,19 @@ class HttpService { */ public static function get($url, $data = array(), $second = 30, $header = []) { if (!empty($data)) { - $url .= (stripos($url, '?') === FALSE ? '?' : '&'); + $url .= (stripos($url, '?') === false ? '?' : '&'); $url .= (is_array($data) ? http_build_query($data) : $data); } $curl = curl_init(); curl_setopt($curl, CURLOPT_TIMEOUT, $second); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); - if (!empty($header)) { - curl_setopt($curl, CURLOPT_HTTPHEADER, $header); - } + !empty($header) && curl_setopt($curl, CURLOPT_HTTPHEADER, $header); self::_setSsl($curl, $url); $content = curl_exec($curl); $status = curl_getinfo($curl); curl_close($curl); - if (intval($status["http_code"]) == 200) { - return $content; - } else { - return false; - } + return (intval($status["http_code"]) === 200) ? $content : false; } /** @@ -69,22 +64,16 @@ class HttpService { $curl = curl_init(); curl_setopt($curl, CURLOPT_TIMEOUT, $second); curl_setopt($curl, CURLOPT_URL, $url); - curl_setopt($curl, CURLOPT_HEADER, FALSE); - curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); - curl_setopt($curl, CURLOPT_POST, TRUE); + curl_setopt($curl, CURLOPT_HEADER, false); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); - if (!empty($header)) { - curl_setopt($curl, CURLOPT_HTTPHEADER, $header); - } + !empty($header) && curl_setopt($curl, CURLOPT_HTTPHEADER, $header); self::_setSsl($curl, $url); $content = curl_exec($curl); $status = curl_getinfo($curl); curl_close($curl); - if (intval($status["http_code"]) == 200) { - return $content; - } else { - return false; - } + return (intval($status["http_code"]) === 200) ? $content : false; } /** @@ -94,8 +83,8 @@ class HttpService { */ private static function _setSsl(&$curl, $url) { if (stripos($url, "https") === 0) { - curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); - curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($curl, CURLOPT_SSLVERSION, 1); } } @@ -106,30 +95,16 @@ class HttpService { * @return string */ private static function _setUploadFile(&$data) { - if (is_array($data)) { - foreach ($data as &$value) { - if (!is_string($value) || stripos($value, '@') !== 0) { - continue; - } - $filename = realpath(trim($value, '@')); - $filemime = self::_getFileMine($filename); - $value = class_exists('CURLFile', FALSE) ? new CURLFile($filename, $filemime) : "{$value};type={$filemime}"; - } + if (!is_array($data)) { + return null; } - } - - /** - * 文件上传MIMS设置 - * @param $filename - * @return string - */ - private static function _getFileMine($filename) { - $mimes = Config::get('mines'); - $ext = strtolower(pathinfo($filename, PATHINFO_EXTENSION)); - if (isset($mimes[$ext])) { - return is_array($mimes[$ext]) ? $mimes[$ext][0] : $mimes[$ext]; - } else { - return 'application/octet-stream'; + foreach ($data as &$value) { + if (!(is_string($value) && strlen($value) > 0 && $value[0] === '@')) { + continue; + } + $filename = realpath(trim($value, '@')); + $filemime = FileService::getFileMine(strtolower(pathinfo($filename, PATHINFO_EXTENSION))); + $value = class_exists('CURLFile', false) ? new CURLFile($filename, $filemime) : "{$value};type={$filemime}"; } } diff --git a/extend/service/PayService.php b/extend/service/PayService.php index 080eaa316..02a302e32 100644 --- a/extend/service/PayService.php +++ b/extend/service/PayService.php @@ -15,10 +15,9 @@ namespace service; use Endroid\QrCode\QrCode; -use think\Db; -use think\Log; use Wechat\WechatPay; - +use think\Log; +use think\Db; /** * 支付数据服务 @@ -64,7 +63,6 @@ class PayService { return FileService::getFileUrl($filename, 'local'); } - /** * 创建微信JSAPI支付签名包 * @param WechatPay $pay 支付SDK diff --git a/extend/service/WechatService.php b/extend/service/WechatService.php index a2009ba2c..9366d1f55 100644 --- a/extend/service/WechatService.php +++ b/extend/service/WechatService.php @@ -14,8 +14,8 @@ namespace service; -use think\Db; use think\Log; +use think\Db; /** * 微信数据服务 @@ -68,7 +68,6 @@ class WechatService { # 下载临时文件到本地 $filename = 'wechat/' . join('/', str_split($md5, 16)) . '.' . pathinfo($local_url, PATHINFO_EXTENSION); $upload = FileService::local($filename, file_get_contents($local_url)); - if (!empty($upload) && isset($upload['file']) && file_exists($upload['file'])) { # 上传图片素材 $result = $wechat->uploadForeverMedia(array('media' => "@{$upload['file']}"), $type, $is_video, $video_info); @@ -210,5 +209,4 @@ class WechatService { return !empty($result['next_openid']) ? self::syncBlackFans($result['next_openid']) : true; } - }