提交 a66fb6ec 编写于 作者: 李光春's avatar 李光春

- 优化代码

- 增加七牛云云存储
- 增加又拍云云存储
上级 47146575
......@@ -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;
/**
* 名称
......
......@@ -143,4 +143,24 @@ return [
'storage' => [
'path' => ''
],
// 又拍云
'upyun' => [
// 又拍云存储
'uss' => [
'service_name' => '',
'operator_name' => '',
'operator_password' => '',
'url' => '',
]
],
// 七牛云
'qiniu' => [
// 云存储
'kodo' => [
'access_key' => '',
'secret_key' => '',
'bucket' => '',
'url' => '',
]
],
];
......@@ -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;
}
......
......@@ -37,13 +37,9 @@ class Xmls
if (!is_array($values) || count($values) <= 0) throw new Exception('数组数据异常!');
$xml = "<xml>";
foreach ($values as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . $this->toXml($val) . "</" . $key . ">";
} else if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
if (is_array($val)) $xml .= "<" . $key . ">" . $this->toXml($val) . "</" . $key . ">";
else if (is_numeric($val)) $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
else $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
$xml .= "</xml>";
return $xml;
......
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\service;
use DtApp\ThinkLibrary\Service;
/**
* 图形验证码服务
* Class CaptchaService
* @package DtApp\ThinkLibrary\service
*/
class CaptchaService extends Service
{
private $code; // 验证码
private $uniqid; // 唯一序号
private $charset = 'ABCDEFGHKMNPRSTUVWXYZ23456789'; // 随机因子
private $codelen = 4; // 验证码长度
private $width = 130; // 宽度
private $height = 50; // 高度
private $img; // 图形资源句柄
private $font; // 指定的字体
private $fontsize = 20; // 指定字体大小
private $fontcolor; // 指定字体颜色
/**
* 服务初始化
* @param array $config
* @return CaptchaService
*/
protected function initialize($config = [])
{
// 动态配置属性
foreach ($config as $k => $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);
}
}
......@@ -25,8 +25,7 @@ use DtApp\ThinkLibrary\Service;
*/
class StorageService extends Service
{
private $path = '';
private $remotely = '';
private $path = '', $remotely = '';
/**
* 文件夹
......
......@@ -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)
{
......
......@@ -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)
{
......
......@@ -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)
{
......
......@@ -20,8 +20,7 @@ use DtApp\ThinkLibrary\Service;
class AesService extends Service
{
private $key;
private $iv;
private $key, $iv;
public function key($str)
{
......
......@@ -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;
......
......@@ -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);
......
......@@ -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;
}
......
......@@ -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)
{
......
......@@ -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
......
......@@ -29,9 +29,7 @@ class VientianeService extends Service
{
private $url = "https://way.jd.com/";
private $param;
private $app_key;
private $param, $app_key;
/**
* 您申请的appkey
......
......@@ -37,8 +37,7 @@ class JinBaoService extends Service
* API接口名称
* @var string
*/
private $type = '';
private $response = '';
private $type = '', $response = '';
/**
* 开放平台分配的clientId
......
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\service\qiniu;
use DtApp\ThinkLibrary\Service;
use Exception;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
/**
* 七牛云对象存储
* https://www.qiniu.com/products/kodo
* Class KodoService
* @package DtApp\ThinkLibrary\service\qiniu
*/
class KodoService extends Service
{
private $accessKey, $secretKey, $bucket;
public function accessKey(string $accessKey)
{
$this->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;
}
}
......@@ -50,8 +50,7 @@ class TbkService extends Service
* API接口名称
* @var string
*/
private $method = '';
private $response = '';
private $method = '', $response = '';
/**
* 签名的摘要算法
......
......@@ -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)
{
......
<?php
// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------
namespace DtApp\ThinkLibrary\service\upyun;
use DtApp\ThinkLibrary\Service;
use Exception;
use Upyun\Config;
use Upyun\Upyun;
/**
* 又拍云存储
* https://www.upyun.com/products/file-storage
* Class UssService
* @package DtApp\ThinkLibrary\service\upyun
*/
class UssService extends Service
{
private $serviceName, $operatorName, $operatorPassword;
public function serviceName(string $serviceName)
{
$this->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;
}
}
......@@ -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";
/**
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册