mirror of
https://gitee.com/zoujingli/ThinkAdmin.git
synced 2025-04-06 03:58:04 +08:00
[更新]整理服务组件代码
This commit is contained in:
parent
ed15337c0e
commit
91b5423d27
@ -15,11 +15,11 @@
|
|||||||
namespace service;
|
namespace service;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use think\Log;
|
||||||
|
use think\Config;
|
||||||
use Qiniu\Auth;
|
use Qiniu\Auth;
|
||||||
use Qiniu\Storage\BucketManager;
|
use Qiniu\Storage\BucketManager;
|
||||||
use Qiniu\Storage\UploadManager;
|
use Qiniu\Storage\UploadManager;
|
||||||
use think\Config;
|
|
||||||
use think\Log;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 系统文件服务
|
* 系统文件服务
|
||||||
@ -31,21 +31,20 @@ use think\Log;
|
|||||||
class FileService {
|
class FileService {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取文件MINE信息
|
* 根据文件后缀获取文件MINE
|
||||||
* @param string $exts
|
* @param array $exts 文件后缀
|
||||||
|
* @param array $mine 文件后缀MINE信息
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
static public function getFileMine($exts) {
|
public static function getFileMine($exts, $mine = []) {
|
||||||
$_exts = is_string($exts) ? explode(',', $exts) : $exts;
|
|
||||||
$_mines = [];
|
|
||||||
$mines = Config::get('mines');
|
$mines = Config::get('mines');
|
||||||
foreach ($_exts as $_e) {
|
foreach (is_string($exts) ? explode(',', $exts) : $exts as $_e) {
|
||||||
if (isset($mines[strtolower($_e)])) {
|
if (isset($mines[strtolower($_e)])) {
|
||||||
$_exinfo = $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
|
* @param string|null $storage
|
||||||
* @return bool|string
|
* @return bool|string
|
||||||
*/
|
*/
|
||||||
static public function getFileUrl($filename, $storage = null) {
|
public static function getFileUrl($filename, $storage = null) {
|
||||||
if (self::hasFile($filename, $storage) === false) {
|
if (self::hasFile($filename, $storage) === false) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -112,7 +111,7 @@ class FileService {
|
|||||||
* 获取服务器URL前缀
|
* 获取服务器URL前缀
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
static public function getBaseUriLocal() {
|
public static function getBaseUriLocal() {
|
||||||
$request = request();
|
$request = request();
|
||||||
$base = $request->root();
|
$base = $request->root();
|
||||||
$root = strpos($base, '.') ? ltrim(dirname($base), DS) : $base;
|
$root = strpos($base, '.') ? ltrim(dirname($base), DS) : $base;
|
||||||
@ -126,7 +125,7 @@ class FileService {
|
|||||||
* 获取七牛云URL前缀
|
* 获取七牛云URL前缀
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
static public function getBaseUriQiniu() {
|
public static function getBaseUriQiniu() {
|
||||||
return (sysconf('storage_qiniu_is_https') ? 'https' : 'http') . '://' . sysconf('storage_qiniu_domain') . '/';
|
return (sysconf('storage_qiniu_is_https') ? 'https' : 'http') . '://' . sysconf('storage_qiniu_domain') . '/';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,7 +135,7 @@ class FileService {
|
|||||||
* @param string|null $storage
|
* @param string|null $storage
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
static public function hasFile($filename, $storage = null) {
|
public static function hasFile($filename, $storage = null) {
|
||||||
switch (empty($storage) ? sysconf('storage_type') : $storage) {
|
switch (empty($storage) ? sysconf('storage_type') : $storage) {
|
||||||
case 'local':
|
case 'local':
|
||||||
return file_exists(ROOT_PATH . 'static/upload/' . $filename);
|
return file_exists(ROOT_PATH . 'static/upload/' . $filename);
|
||||||
@ -155,7 +154,7 @@ class FileService {
|
|||||||
* @param string|null $storage
|
* @param string|null $storage
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
static public function readFile($filename, $storage = null) {
|
public static function readFile($filename, $storage = null) {
|
||||||
switch (empty($storage) ? sysconf('storage_type') : $storage) {
|
switch (empty($storage) ? sysconf('storage_type') : $storage) {
|
||||||
case 'local':
|
case 'local':
|
||||||
$filepath = ROOT_PATH . 'static/upload/' . $filename;
|
$filepath = ROOT_PATH . 'static/upload/' . $filename;
|
||||||
@ -177,7 +176,7 @@ class FileService {
|
|||||||
* @param string|null $file_storage
|
* @param string|null $file_storage
|
||||||
* @return array|false
|
* @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;
|
$type = empty($file_storage) ? sysconf('storage_type') : $file_storage;
|
||||||
if (!method_exists(__CLASS__, $type)) {
|
if (!method_exists(__CLASS__, $type)) {
|
||||||
Log::error("保存存储失败,调用{$type}存储引擎不存在!");
|
Log::error("保存存储失败,调用{$type}存储引擎不存在!");
|
||||||
@ -192,9 +191,9 @@ class FileService {
|
|||||||
* @param string $bodycontent
|
* @param string $bodycontent
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
static public function local($filename, $bodycontent) {
|
public static function local($filename, $bodycontent) {
|
||||||
$filepath = ROOT_PATH . 'static/upload/' . $filename;
|
|
||||||
try {
|
try {
|
||||||
|
$filepath = ROOT_PATH . 'static/upload/' . $filename;
|
||||||
!file_exists(dirname($filepath)) && mkdir(dirname($filepath), '0755', true);
|
!file_exists(dirname($filepath)) && mkdir(dirname($filepath), '0755', true);
|
||||||
if (file_put_contents($filepath, $bodycontent)) {
|
if (file_put_contents($filepath, $bodycontent)) {
|
||||||
return [
|
return [
|
||||||
@ -216,7 +215,7 @@ class FileService {
|
|||||||
* @param string $bodycontent
|
* @param string $bodycontent
|
||||||
* @return string
|
* @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'));
|
$auth = new Auth(sysconf('storage_qiniu_access_key'), sysconf('storage_qiniu_secret_key'));
|
||||||
$token = $auth->uploadToken(sysconf('storage_qiniu_bucket'));
|
$token = $auth->uploadToken(sysconf('storage_qiniu_bucket'));
|
||||||
$uploadMgr = new UploadManager();
|
$uploadMgr = new UploadManager();
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
namespace service;
|
namespace service;
|
||||||
|
|
||||||
|
use CURLFile;
|
||||||
use think\Config;
|
use think\Config;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -35,25 +36,19 @@ class HttpService {
|
|||||||
*/
|
*/
|
||||||
public static function get($url, $data = array(), $second = 30, $header = []) {
|
public static function get($url, $data = array(), $second = 30, $header = []) {
|
||||||
if (!empty($data)) {
|
if (!empty($data)) {
|
||||||
$url .= (stripos($url, '?') === FALSE ? '?' : '&');
|
$url .= (stripos($url, '?') === false ? '?' : '&');
|
||||||
$url .= (is_array($data) ? http_build_query($data) : $data);
|
$url .= (is_array($data) ? http_build_query($data) : $data);
|
||||||
}
|
}
|
||||||
$curl = curl_init();
|
$curl = curl_init();
|
||||||
curl_setopt($curl, CURLOPT_TIMEOUT, $second);
|
curl_setopt($curl, CURLOPT_TIMEOUT, $second);
|
||||||
curl_setopt($curl, CURLOPT_URL, $url);
|
curl_setopt($curl, CURLOPT_URL, $url);
|
||||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
|
||||||
if (!empty($header)) {
|
!empty($header) && curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
|
||||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
|
|
||||||
}
|
|
||||||
self::_setSsl($curl, $url);
|
self::_setSsl($curl, $url);
|
||||||
$content = curl_exec($curl);
|
$content = curl_exec($curl);
|
||||||
$status = curl_getinfo($curl);
|
$status = curl_getinfo($curl);
|
||||||
curl_close($curl);
|
curl_close($curl);
|
||||||
if (intval($status["http_code"]) == 200) {
|
return (intval($status["http_code"]) === 200) ? $content : false;
|
||||||
return $content;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -69,22 +64,16 @@ class HttpService {
|
|||||||
$curl = curl_init();
|
$curl = curl_init();
|
||||||
curl_setopt($curl, CURLOPT_TIMEOUT, $second);
|
curl_setopt($curl, CURLOPT_TIMEOUT, $second);
|
||||||
curl_setopt($curl, CURLOPT_URL, $url);
|
curl_setopt($curl, CURLOPT_URL, $url);
|
||||||
curl_setopt($curl, CURLOPT_HEADER, FALSE);
|
curl_setopt($curl, CURLOPT_HEADER, false);
|
||||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
|
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
|
||||||
curl_setopt($curl, CURLOPT_POST, TRUE);
|
curl_setopt($curl, CURLOPT_POST, true);
|
||||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
|
||||||
if (!empty($header)) {
|
!empty($header) && curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
|
||||||
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
|
|
||||||
}
|
|
||||||
self::_setSsl($curl, $url);
|
self::_setSsl($curl, $url);
|
||||||
$content = curl_exec($curl);
|
$content = curl_exec($curl);
|
||||||
$status = curl_getinfo($curl);
|
$status = curl_getinfo($curl);
|
||||||
curl_close($curl);
|
curl_close($curl);
|
||||||
if (intval($status["http_code"]) == 200) {
|
return (intval($status["http_code"]) === 200) ? $content : false;
|
||||||
return $content;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -94,8 +83,8 @@ class HttpService {
|
|||||||
*/
|
*/
|
||||||
private static function _setSsl(&$curl, $url) {
|
private static function _setSsl(&$curl, $url) {
|
||||||
if (stripos($url, "https") === 0) {
|
if (stripos($url, "https") === 0) {
|
||||||
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
|
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
|
||||||
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
|
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
|
||||||
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
|
curl_setopt($curl, CURLOPT_SSLVERSION, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -106,30 +95,16 @@ class HttpService {
|
|||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private static function _setUploadFile(&$data) {
|
private static function _setUploadFile(&$data) {
|
||||||
if (is_array($data)) {
|
if (!is_array($data)) {
|
||||||
foreach ($data as &$value) {
|
return null;
|
||||||
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}";
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
foreach ($data as &$value) {
|
||||||
|
if (!(is_string($value) && strlen($value) > 0 && $value[0] === '@')) {
|
||||||
/**
|
continue;
|
||||||
* 文件上传MIMS设置
|
}
|
||||||
* @param $filename
|
$filename = realpath(trim($value, '@'));
|
||||||
* @return string
|
$filemime = FileService::getFileMine(strtolower(pathinfo($filename, PATHINFO_EXTENSION)));
|
||||||
*/
|
$value = class_exists('CURLFile', false) ? new CURLFile($filename, $filemime) : "{$value};type={$filemime}";
|
||||||
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';
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,10 +15,9 @@
|
|||||||
namespace service;
|
namespace service;
|
||||||
|
|
||||||
use Endroid\QrCode\QrCode;
|
use Endroid\QrCode\QrCode;
|
||||||
use think\Db;
|
|
||||||
use think\Log;
|
|
||||||
use Wechat\WechatPay;
|
use Wechat\WechatPay;
|
||||||
|
use think\Log;
|
||||||
|
use think\Db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 支付数据服务
|
* 支付数据服务
|
||||||
@ -64,7 +63,6 @@ class PayService {
|
|||||||
return FileService::getFileUrl($filename, 'local');
|
return FileService::getFileUrl($filename, 'local');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 创建微信JSAPI支付签名包
|
* 创建微信JSAPI支付签名包
|
||||||
* @param WechatPay $pay 支付SDK
|
* @param WechatPay $pay 支付SDK
|
||||||
|
@ -14,8 +14,8 @@
|
|||||||
|
|
||||||
namespace service;
|
namespace service;
|
||||||
|
|
||||||
use think\Db;
|
|
||||||
use think\Log;
|
use think\Log;
|
||||||
|
use think\Db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 微信数据服务
|
* 微信数据服务
|
||||||
@ -68,7 +68,6 @@ class WechatService {
|
|||||||
# 下载临时文件到本地
|
# 下载临时文件到本地
|
||||||
$filename = 'wechat/' . join('/', str_split($md5, 16)) . '.' . pathinfo($local_url, PATHINFO_EXTENSION);
|
$filename = 'wechat/' . join('/', str_split($md5, 16)) . '.' . pathinfo($local_url, PATHINFO_EXTENSION);
|
||||||
$upload = FileService::local($filename, file_get_contents($local_url));
|
$upload = FileService::local($filename, file_get_contents($local_url));
|
||||||
|
|
||||||
if (!empty($upload) && isset($upload['file']) && file_exists($upload['file'])) {
|
if (!empty($upload) && isset($upload['file']) && file_exists($upload['file'])) {
|
||||||
# 上传图片素材
|
# 上传图片素材
|
||||||
$result = $wechat->uploadForeverMedia(array('media' => "@{$upload['file']}"), $type, $is_video, $video_info);
|
$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;
|
return !empty($result['next_openid']) ? self::syncBlackFans($result['next_openid']) : true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user