From a66fb6ec520e818c4719885b7866bb8313803f4a Mon Sep 17 00:00:00 2001 From: Chaim Date: Mon, 8 Jun 2020 13:49:29 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=20-=20?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=83=E7=89=9B=E4=BA=91=E4=BA=91=E5=AD=98?= =?UTF-8?q?=E5=82=A8=20-=20=E5=A2=9E=E5=8A=A0=E5=8F=88=E6=8B=8D=E4=BA=91?= =?UTF-8?q?=E4=BA=91=E5=AD=98=E5=82=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cache/Mysql.php | 3 +- src/config.php | 20 +++ src/helper/Requests.php | 2 +- src/helper/Xmls.php | 10 +- src/service/CaptchaService.php | 177 ----------------------- src/service/StorageService.php | 3 +- src/service/aliyun/OssService.php | 6 +- src/service/baidu/BosService.php | 6 +- src/service/bt/ApiService.php | 6 +- src/service/crypt/AesService.php | 3 +- src/service/curl/BtService.php | 9 +- src/service/curl/HttpService.php | 9 +- src/service/douyin/WatermarkService.php | 13 +- src/service/huaweicloud/ObsService.php | 6 +- src/service/jd/UnionService.php | 6 +- src/service/jdcloud/VientianeService.php | 4 +- src/service/pinduoduo/JinBaoService.php | 3 +- src/service/qiniu/KodoService.php | 87 +++++++++++ src/service/taobao/TbkService.php | 3 +- src/service/tencent/CosService.php | 6 +- src/service/upyun/UssService.php | 82 +++++++++++ src/service/wechat/MiniService.php | 3 +- 22 files changed, 221 insertions(+), 246 deletions(-) delete mode 100644 src/service/CaptchaService.php create mode 100644 src/service/qiniu/KodoService.php create mode 100644 src/service/upyun/UssService.php diff --git a/src/cache/Mysql.php b/src/cache/Mysql.php index 27b0793..d8d7f70 100644 --- a/src/cache/Mysql.php +++ b/src/cache/Mysql.php @@ -31,8 +31,7 @@ use think\facade\Db; class Mysql { private $table = "think_cache"; - private $cache_name; - private $cache_expire = 0; + private $cache_name, $cache_expire = 0; /** * 名称 diff --git a/src/config.php b/src/config.php index eeaaec0..005a7bc 100644 --- a/src/config.php +++ b/src/config.php @@ -143,4 +143,24 @@ return [ 'storage' => [ 'path' => '' ], + // 又拍云 + 'upyun' => [ + // 又拍云存储 + 'uss' => [ + 'service_name' => '', + 'operator_name' => '', + 'operator_password' => '', + 'url' => '', + ] + ], + // 七牛云 + 'qiniu' => [ + // 云存储 + 'kodo' => [ + 'access_key' => '', + 'secret_key' => '', + 'bucket' => '', + 'url' => '', + ] + ], ]; diff --git a/src/helper/Requests.php b/src/helper/Requests.php index b9c32fd..a1ecf53 100644 --- a/src/helper/Requests.php +++ b/src/helper/Requests.php @@ -32,7 +32,7 @@ class Requests */ public function isEmpty(array $data, array $arr): array { - foreach ($arr as $k => $v) if (empty(isset($data["$v"]) ? $data["$v"] : '')) return ''; + foreach ($arr as $k => $v) if (empty(isset($data["$v"]) ? $data["$v"] : '')) return []; return $data; } diff --git a/src/helper/Xmls.php b/src/helper/Xmls.php index 593d4c6..1fd01bf 100644 --- a/src/helper/Xmls.php +++ b/src/helper/Xmls.php @@ -37,13 +37,9 @@ class Xmls if (!is_array($values) || count($values) <= 0) throw new Exception('数组数据异常!'); $xml = ""; foreach ($values as $key => $val) { - if (is_array($val)) { - $xml .= "<" . $key . ">" . $this->toXml($val) . ""; - } else if (is_numeric($val)) { - $xml .= "<" . $key . ">" . $val . ""; - } else { - $xml .= "<" . $key . ">"; - } + if (is_array($val)) $xml .= "<" . $key . ">" . $this->toXml($val) . ""; + else if (is_numeric($val)) $xml .= "<" . $key . ">" . $val . ""; + else $xml .= "<" . $key . ">"; } $xml .= ""; return $xml; diff --git a/src/service/CaptchaService.php b/src/service/CaptchaService.php deleted file mode 100644 index d6fe6b3..0000000 --- a/src/service/CaptchaService.php +++ /dev/null @@ -1,177 +0,0 @@ - $v) if (isset($this->$k)) $this->$k = $v; - // 生成验证码序号 - $this->uniqid = uniqid('captcha') . mt_rand(1000, 9999); - // 生成验证码字符串 - $length = strlen($this->charset) - 1; - for ($i = 0; $i < $this->codelen; $i++) { - $this->code .= $this->charset[mt_rand(0, $length)]; - } - // 设置字体文件路径 - $this->font = __DIR__ . '/bin/font.ttf'; - // 缓存验证码字符串 - $this->app->cache->set($this->uniqid, $this->code, 360); - // 返回当前对象 - return $this; - } - - /** - * 动态切换配置 - * @param array $config - * @return $this - */ - public function config($config = []) - { - return $this->initialize($config); - } - - /** - * 获取验证码值 - * @return string - */ - public function getCode() - { - return $this->code; - } - - /** - * 获取图片内容 - * @return string - */ - public function getData() - { - return "data:image/png;base64,{$this->createImage()}"; - } - - /** - * 获取验证码编号 - * @return string - */ - public function getUniqid() - { - return $this->uniqid; - } - - /** - * 获取验证码数据 - * @return array - */ - public function getAttrs() - { - return [ - 'code' => $this->getCode(), - 'data' => $this->getData(), - 'uniqid' => $this->getUniqid(), - ]; - } - - /** - * 检查验证码是否正确 - * @param string $code 需要验证的值 - * @param string $uniqid 验证码编号 - * @return boolean - */ - public function check($code, $uniqid = null) - { - $_uni = is_string($uniqid) ? $uniqid : input('uniqid', '-'); - $_val = $this->app->cache->get($_uni, ''); - if (is_string($_val) && strtolower($_val) === strtolower($code)) { - $this->app->cache->delete($_uni); - return true; - } else { - return false; - } - } - - /** - * 输出图形验证码 - * @return string - */ - public function __toString() - { - return $this->getData(); - } - - /** - * 创建验证码图片 - * @return string - */ - private function createImage() - { - // 生成背景 - $this->img = imagecreatetruecolor($this->width, $this->height); - $color = imagecolorallocate($this->img, mt_rand(220, 255), mt_rand(220, 255), mt_rand(220, 255)); - imagefilledrectangle($this->img, 0, $this->height, $this->width, 0, $color); - // 生成线条 - for ($i = 0; $i < 6; $i++) { - $color = imagecolorallocate($this->img, mt_rand(0, 50), mt_rand(0, 50), mt_rand(0, 50)); - imageline($this->img, mt_rand(0, $this->width), mt_rand(0, $this->height), mt_rand(0, $this->width), mt_rand(0, $this->height), $color); - } - // 生成雪花 - for ($i = 0; $i < 100; $i++) { - $color = imagecolorallocate($this->img, mt_rand(200, 255), mt_rand(200, 255), mt_rand(200, 255)); - imagestring($this->img, mt_rand(1, 5), mt_rand(0, $this->width), mt_rand(0, $this->height), '*', $color); - } - // 生成文字 - $_x = $this->width / $this->codelen; - for ($i = 0; $i < $this->codelen; $i++) { - $this->fontcolor = imagecolorallocate($this->img, mt_rand(0, 156), mt_rand(0, 156), mt_rand(0, 156)); - if (function_exists('imagettftext')) { - imagettftext($this->img, $this->fontsize, mt_rand(-30, 30), $_x * $i + mt_rand(1, 5), $this->height / 1.4, $this->fontcolor, $this->font, $this->code[$i]); - } else { - imagestring($this->img, 15, $_x * $i + mt_rand(10, 15), mt_rand(10, 30), $this->code[$i], $this->fontcolor); - } - } - ob_start(); - imagepng($this->img); - $data = ob_get_contents(); - ob_end_clean(); - imagedestroy($this->img); - return base64_encode($data); - } -} diff --git a/src/service/StorageService.php b/src/service/StorageService.php index 9411d0e..add5943 100644 --- a/src/service/StorageService.php +++ b/src/service/StorageService.php @@ -25,8 +25,7 @@ use DtApp\ThinkLibrary\Service; */ class StorageService extends Service { - private $path = ''; - private $remotely = ''; + private $path = '', $remotely = ''; /** * 文件夹 diff --git a/src/service/aliyun/OssService.php b/src/service/aliyun/OssService.php index cfb2b18..ee99590 100644 --- a/src/service/aliyun/OssService.php +++ b/src/service/aliyun/OssService.php @@ -22,15 +22,13 @@ use OSS\OssClient; /** * 阿里云对象存储 + * https://www.aliyun.com/product/oss * Class OssService * @package DtApp\ThinkLibrary\service\aliyun */ class OssService extends Service { - private $accessKeyId; - private $accessKeySecret; - private $endpoint; - private $bucket; + private $accessKeyId, $accessKeySecret, $endpoint, $bucket; public function accessKeyId(string $accessKeyId) { diff --git a/src/service/baidu/BosService.php b/src/service/baidu/BosService.php index 35dd422..6510a12 100644 --- a/src/service/baidu/BosService.php +++ b/src/service/baidu/BosService.php @@ -22,15 +22,13 @@ use Exception; /** * 百度云对象存储 + * https://cloud.baidu.com/product/bos.html * Class BosService * @package DtApp\ThinkLibrary\service\baidu */ class BosService extends Service { - private $accessKeyId; - private $secretAccessKey; - private $endpoint; - private $bucket; + private $accessKeyId, $secretAccessKey, $endpoint, $bucket; public function accessKeyId(string $accessKeyId) { diff --git a/src/service/bt/ApiService.php b/src/service/bt/ApiService.php index ccf1c44..929286e 100644 --- a/src/service/bt/ApiService.php +++ b/src/service/bt/ApiService.php @@ -22,6 +22,7 @@ use DtApp\ThinkLibrary\service\curl\BtService; /** * 宝塔Api + * https://www.bt.cn/ * Class ApiService * @package DtApp\ThinkLibrary\service\bt */ @@ -32,10 +33,7 @@ class ApiService extends Service private $limit = 15; private $order = 'id desc'; private $where = []; - private $contents; - private $backtrack; - private $key; - private $panel; + private $contents, $backtrack, $key, $panel; public function key(string $key) { diff --git a/src/service/crypt/AesService.php b/src/service/crypt/AesService.php index 42fea04..4d0d873 100644 --- a/src/service/crypt/AesService.php +++ b/src/service/crypt/AesService.php @@ -20,8 +20,7 @@ use DtApp\ThinkLibrary\Service; class AesService extends Service { - private $key; - private $iv; + private $key, $iv; public function key($str) { diff --git a/src/service/curl/BtService.php b/src/service/curl/BtService.php index 39107e2..632f028 100644 --- a/src/service/curl/BtService.php +++ b/src/service/curl/BtService.php @@ -21,17 +21,14 @@ use think\exception\HttpException; /** * 宝塔网络请求接口 + * https://www.bt.cn/ * Class BtService * @package DtApp\ThinkLibrary\service\curl */ class BtService extends Service { - private $url; - private $key; + private $url, $key, $panel, $output, $cookie; private $data = []; - private $panel; - private $output; - private $cookie; private $timeout = 60; /** @@ -107,7 +104,7 @@ class BtService extends Service */ public function toArray(bool $is = true) { - if (empty($this->cookie)) throw new HttpException(404,'请检查cookie内容'); + if (empty($this->cookie)) throw new HttpException(404, '请检查cookie内容'); if (!extension_loaded("curl")) throw new HttpException(404, '请开启curl模块!'); $this->http(); if (empty($is)) return $this->output; diff --git a/src/service/curl/HttpService.php b/src/service/curl/HttpService.php index 57a9f3a..eeb6f9c 100644 --- a/src/service/curl/HttpService.php +++ b/src/service/curl/HttpService.php @@ -26,10 +26,7 @@ use think\exception\HttpException; */ class HttpService extends Service { - private $url; - private $data; - private $cert; - private $output; + private $url, $data, $cert, $output; private $timeout = 60; private $method = 'GET'; private $headers = 'application/json;charset=utf-8'; @@ -52,7 +49,7 @@ class HttpService extends Service */ public function data($str) { - if (is_array($str)) $this->data = json_encode($str,JSON_UNESCAPED_UNICODE); + if (is_array($str)) $this->data = json_encode($str, JSON_UNESCAPED_UNICODE); else $this->data = $str; return $this; } @@ -151,7 +148,7 @@ class HttpService extends Service $this->httpXml(); } else if ($this->method === 'FILE') { $this->httpFile(); - } else throw new HttpException(404,'请求方式异常'); + } else throw new HttpException(404, '请求方式异常'); if (empty($is)) return $this->output; if (is_array($this->output)) return $this->output; return json_decode($this->output, true); diff --git a/src/service/douyin/WatermarkService.php b/src/service/douyin/WatermarkService.php index 1351c2b..8be176a 100644 --- a/src/service/douyin/WatermarkService.php +++ b/src/service/douyin/WatermarkService.php @@ -34,14 +34,7 @@ use stdClass; */ class WatermarkService extends Service { - private $url; - private $api_url; - private $itemId; - private $dytk; - private $contents; - private $backtrack; - private $storage; - private $storagePath; + private $url, $apiUrl, $itemId, $dytk, $contents, $backtrack, $storage, $storagePath; /** * 配置网址 @@ -184,8 +177,8 @@ class WatermarkService extends Service */ public function getApi() { - $this->api_url = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={$this->itemId}&dytk={$this->dytk}"; - $this->contents = $this->getContents($this->api_url); + $this->apiUrl = "https://www.iesdouyin.com/web/api/v2/aweme/iteminfo/?item_ids={$this->itemId}&dytk={$this->dytk}"; + $this->contents = $this->getContents($this->apiUrl); $this->backtrack = $this->contents; return $this; } diff --git a/src/service/huaweicloud/ObsService.php b/src/service/huaweicloud/ObsService.php index 8d691f4..26cef24 100644 --- a/src/service/huaweicloud/ObsService.php +++ b/src/service/huaweicloud/ObsService.php @@ -21,15 +21,13 @@ use Obs\ObsClient; /** * 华为云对象存储 + * https://www.huaweicloud.com/product/obs.html * Class ObsService * @package DtApp\ThinkLibrary\service\huaweicloud */ class ObsService extends Service { - private $key; - private $secret; - private $endpoint; - private $bucket; + private $key, $secret, $endpoint, $bucket; public function key(string $key) { diff --git a/src/service/jd/UnionService.php b/src/service/jd/UnionService.php index 51978fa..aee83ed 100644 --- a/src/service/jd/UnionService.php +++ b/src/service/jd/UnionService.php @@ -38,8 +38,7 @@ class UnionService extends Service * API接口名称 * @var */ - private $method = ''; - private $response = ''; + private $method = '', $response = ''; /** * 联盟分配给应用的appkey @@ -87,8 +86,7 @@ class UnionService extends Service * 需要发送的的参数 * @var */ - private $param; - private $params; + private $param, $params; /** * 联盟分配给应用的appkey diff --git a/src/service/jdcloud/VientianeService.php b/src/service/jdcloud/VientianeService.php index d21c83b..3285841 100644 --- a/src/service/jdcloud/VientianeService.php +++ b/src/service/jdcloud/VientianeService.php @@ -29,9 +29,7 @@ class VientianeService extends Service { private $url = "https://way.jd.com/"; - private $param; - - private $app_key; + private $param, $app_key; /** * 您申请的appkey diff --git a/src/service/pinduoduo/JinBaoService.php b/src/service/pinduoduo/JinBaoService.php index 0d8f012..3e4092b 100644 --- a/src/service/pinduoduo/JinBaoService.php +++ b/src/service/pinduoduo/JinBaoService.php @@ -37,8 +37,7 @@ class JinBaoService extends Service * API接口名称 * @var string */ - private $type = ''; - private $response = ''; + private $type = '', $response = ''; /** * 开放平台分配的clientId diff --git a/src/service/qiniu/KodoService.php b/src/service/qiniu/KodoService.php new file mode 100644 index 0000000..1ca1645 --- /dev/null +++ b/src/service/qiniu/KodoService.php @@ -0,0 +1,87 @@ +accessKey = $accessKey; + return $this; + } + + public function secretKey(string $secretKey) + { + $this->secretKey = $secretKey; + return $this; + } + + public function bucket(string $bucket) + { + $this->bucket = $bucket; + return $this; + } + + /** + * 获取配置信息 + * @return $this + */ + private function getConfig() + { + $this->accessKey = $this->app->config->get('dtapp.qiniu.kodo.access_key'); + $this->secretKey = $this->app->config->get('dtapp.qiniu.kodo.secret_key'); + $this->bucket = $this->app->config->get('dtapp.qiniu.kodo.bucket'); + return $this; + } + + /** + * 上传文件 + * @param $object + * @param $filePath + * @return bool + * @throws Exception + */ + public function upload(string $object, string $filePath) + { + if (empty($this->accessKey)) $this->getConfig(); + if (empty($this->secretKey)) $this->getConfig(); + if (empty($this->bucket)) $this->getConfig(); + // 初始化签权对象 + $auth = new Auth($this->accessKey, $this->secretKey); + // 生成上传Token + $token = $auth->uploadToken($this->bucket); + // 构建 UploadManager 对象 + $uploadMgr = new UploadManager(); + // 调用 UploadManager 的 putFile 方法进行文件的上传。 + list($ret, $err) = $uploadMgr->putFile($token, $object, $filePath); + if ($err !== null) return false; + else return $this->app->config->get('dtapp.qiniu.kodo.url') . $object; + } +} diff --git a/src/service/taobao/TbkService.php b/src/service/taobao/TbkService.php index a6f280a..b72c542 100644 --- a/src/service/taobao/TbkService.php +++ b/src/service/taobao/TbkService.php @@ -50,8 +50,7 @@ class TbkService extends Service * API接口名称 * @var string */ - private $method = ''; - private $response = ''; + private $method = '', $response = ''; /** * 签名的摘要算法 diff --git a/src/service/tencent/CosService.php b/src/service/tencent/CosService.php index 6ca43c0..e4c08cc 100644 --- a/src/service/tencent/CosService.php +++ b/src/service/tencent/CosService.php @@ -22,15 +22,13 @@ use Qcloud\Cos\Client; /** * 腾讯云对象存储 + * https://cloud.tencent.com/product/cos * Class CosService * @package DtApp\ThinkLibrary\service\tencent */ class CosService extends Service { - private $secretId; - private $secretKey; - private $region; - private $bucket; + private $secretId, $secretKey, $region, $bucket; public function secretId(string $secretId) { diff --git a/src/service/upyun/UssService.php b/src/service/upyun/UssService.php new file mode 100644 index 0000000..0c0bb12 --- /dev/null +++ b/src/service/upyun/UssService.php @@ -0,0 +1,82 @@ +serviceName = $serviceName; + return $this; + } + + public function operatorName(string $operatorName) + { + $this->operatorName = $operatorName; + return $this; + } + + public function operatorPassword(string $operatorPassword) + { + $this->operatorPassword = $operatorPassword; + return $this; + } + + /** + * 获取配置信息 + * @return $this + */ + private function getConfig() + { + $this->serviceName = $this->app->config->get('dtapp.upyun.uss.service_name'); + $this->operatorName = $this->app->config->get('dtapp.upyun.uss.operator_name'); + $this->operatorPassword = $this->app->config->get('dtapp.upyun.uss.operator_password'); + return $this; + } + + /** + * 上传文件 + * @param string $object + * @param string $filePath + * @return bool + * @throws Exception + */ + public function upload(string $object, string $filePath) + { + if (empty($this->serviceName)) $this->getConfig(); + if (empty($this->operatorName)) $this->getConfig(); + if (empty($this->operatorPassword)) $this->getConfig(); + $serviceConfig = new Config($this->serviceName, $this->operatorName, $this->operatorPassword); + $client = new Upyun($serviceConfig); + $file = fopen($filePath, 'r'); + $client->write($object, $file); + return $this->app->config->get('dtapp.upyun.uss.url') . $object; + } +} diff --git a/src/service/wechat/MiniService.php b/src/service/wechat/MiniService.php index 6971693..3149b9d 100644 --- a/src/service/wechat/MiniService.php +++ b/src/service/wechat/MiniService.php @@ -32,8 +32,7 @@ class MiniService extends Service { private $api_url = "https://api.weixin.qq.com/"; - private $app_id; - private $app_secret; + private $app_id, $app_secret; private $grant_type = "client_credential"; /**