From ac60be73d65b697b0f60b8603bf8c829a0110818 Mon Sep 17 00:00:00 2001 From: Chaim Date: Sat, 18 Jul 2020 09:46:19 +0800 Subject: [PATCH] =?UTF-8?q?-=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=20-=20?= =?UTF-8?q?=E4=BC=98=E5=8C=96Api=E6=8E=A7=E5=88=B6=E5=99=A8=E7=9A=84?= =?UTF-8?q?=E5=8A=A0=E5=AF=86=E7=9B=B8=E5=85=B3=E5=8F=82=E6=95=B0=20-=20?= =?UTF-8?q?=E8=AF=B7=E6=B1=82=E9=97=A8=E9=9D=A2=E5=A2=9E=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E5=AE=A2=E6=88=B7=E7=AB=AF=E7=B1=BB=E5=9E=8B=E5=92=8C?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=89=8B=E6=9C=BA=E8=AE=BE=E5=A4=87=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 5 +++ src/ApiController.php | 58 +++++++++++++++++++++++++++++----- src/common.php | 2 +- src/facade/Requests.php | 2 ++ src/helper/Requests.php | 32 +++++++++++++++++++ src/service/Ip/IpIpService.php | 2 +- src/service/QqWryService.php | 2 +- 7 files changed, 92 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a36bb55..6a25755 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## v6.0.88 / 2020-07-18 +- 优化代码 +- 优化Api控制器的加密相关参数 +- 请求门面增加获取客户端类型和获取手机设备类型 + ## v6.0.87 / 2020-07-17 - 优化代码 diff --git a/src/ApiController.php b/src/ApiController.php index 6a5abfb..6d81031 100644 --- a/src/ApiController.php +++ b/src/ApiController.php @@ -49,6 +49,13 @@ class ApiController extends stdClass */ private $aes_decrypt_data; + /** + * 加密相关的东西 + * @var string + */ + private $aes_md5 = ''; + private $aes_md5_iv = ''; + /** * ApiController constructor. * @param App $app @@ -97,19 +104,41 @@ class ApiController extends stdClass ])); } + /** + * key + * @param string $name 参数名 + * @return $this + */ + public function setAesMd5($name = 'sniff_h5') + { + $value = $this->app->config->get("dtapp.md5.{$name}"); + $this->aes_md5 = $value; + return $this; + } + + /** + * iv + * @return $this + */ + private function setAesMd5Iv() + { + $value = $this->app->config->get("dtapp.md5.bcw"); + $this->aes_md5_iv = $value; + return $this; + } + /** * 返回成功的操作 * @param mixed $data 返回数据 * @param mixed $msg 消息内容 * @param integer $code 返回代码 - * @param string $name 参数名 */ - public function aesSuccess($data = [], $msg = 'success', $code = 0, $name = 'sniff_h5') + public function aesSuccess($data = [], $msg = 'success', $code = 0) { $timestamp = time(); throw new HttpResponseException(json([ 'code' => $code, 'msg' => $msg, 'timestamp' => $timestamp, 'data' => [ - 'aes' => $this->encrypt($data, $name, $timestamp) + 'aes' => $this->encrypt($data, $timestamp) ], ])); } @@ -202,14 +231,21 @@ class ApiController extends stdClass /** * 加密 * @param $data - * @param string $name * @param int $timestamp * @return bool|string */ - private function encrypt($data, string $name, int $timestamp) + private function encrypt($data, int $timestamp) { - if (!empty(is_array($data))) $data = json_encode($data); - return urlencode(base64_encode(openssl_encrypt($data, 'AES-128-CBC', config("dtapp.md5.{$name}"), 1, config("dtapp.md5.bcw") . $timestamp))); + if (empty($this->aes_md5)) { + $this->setAesMd5(); + } + if (empty($this->aes_md5_iv)) { + $this->setAesMd5Iv(); + } + if (!empty(is_array($data))) { + $data = json_encode($data); + } + return urlencode(base64_encode(openssl_encrypt($data, 'AES-128-CBC', $this->aes_md5, 1, $this->aes_md5_iv . $timestamp))); } /** @@ -221,6 +257,12 @@ class ApiController extends stdClass */ private function decrypt(string $data, string $name, int $timestamp) { - return openssl_decrypt(base64_decode(urldecode($data)), "AES-128-CBC", config("dtapp.md5.{$name}"), true, config("dtapp.md5.bcw") . $timestamp); + if (empty($this->aes_md5)) { + $this->setAesMd5(); + } + if (empty($this->aes_md5_iv)) { + $this->setAesMd5Iv(); + } + return openssl_decrypt(base64_decode(urldecode($data)), "AES-128-CBC", $this->aes_md5, true, $this->aes_md5_iv . $timestamp); } } diff --git a/src/common.php b/src/common.php index 4dfc6a8..5f3c8f0 100644 --- a/src/common.php +++ b/src/common.php @@ -28,7 +28,7 @@ use think\db\exception\ModelNotFoundException; /** * 定义当前版本 */ -const VERSION = '6.0.87'; +const VERSION = '6.0.88'; if (!function_exists('get_ip_info')) { /** diff --git a/src/facade/Requests.php b/src/facade/Requests.php index ab022af..f0b4d6c 100644 --- a/src/facade/Requests.php +++ b/src/facade/Requests.php @@ -44,6 +44,8 @@ use think\Facade; * @method static bool isAliPay() 判断是否支付宝内置浏览器访问 * @method static bool isQQ() 判断是否QQ内置浏览器访问 * @method static bool isQQBrowser() 判断是否QQ浏览器访问 + * @method static string getDeviceType() 获取客户端类型 + * @method static string getMobileType() 获取手机设备类型 * @method static string getWebsiteAddress() 获取域名地址 */ class Requests extends Facade diff --git a/src/helper/Requests.php b/src/helper/Requests.php index 4a0fb6b..e2d2893 100644 --- a/src/helper/Requests.php +++ b/src/helper/Requests.php @@ -210,6 +210,38 @@ class Requests return false; } + /** + * 获取客户端类型 + * @return string + */ + public function getDeviceType() + { + $agent = strtolower(request()->server('HTTP_USER_AGENT')); + if (strpos($agent, 'iphone') || strpos($agent, 'ipad') || strpos($agent, 'android')) { + $type = 'mobile'; + } else { + $type = 'computer'; + } + return $type; + } + + /** + * 获取手机设备类型 + * @return string + */ + public function getMobileType() + { + $agent = strtolower(request()->server('HTTP_USER_AGENT')); + $type = 'other'; + if (strpos($agent, 'iphone') || strpos($agent, 'ipad')) { + $type = 'mobile'; + } + if (strpos($agent, 'android')) { + $type = 'android'; + } + return $type; + } + /** * 获取域名地址 * @return string diff --git a/src/service/Ip/IpIpService.php b/src/service/Ip/IpIpService.php index db4f44a..82a033a 100644 --- a/src/service/Ip/IpIpService.php +++ b/src/service/Ip/IpIpService.php @@ -46,7 +46,7 @@ class IpIpService extends Service */ public function __construct(App $app) { - $this->ipPath = config('dtapp.ip_path', ''); + $this->ipPath = $this->app->config->get('dtapp.ip_path', ''); if (empty($this->ipPath)) { throw new DtaException('请检查配置文件是否配置了IP数据库文件存放位置'); } diff --git a/src/service/QqWryService.php b/src/service/QqWryService.php index d696b85..8b62975 100644 --- a/src/service/QqWryService.php +++ b/src/service/QqWryService.php @@ -74,7 +74,7 @@ class QqWryService extends Service */ public function __construct(App $app) { - $this->ipPath = config('dtapp.ip_path', ''); + $this->ipPath = $this->app->config->get('dtapp.ip_path', ''); if (empty($this->ipPath)) { throw new DtaException('请检查配置文件是否配置了IP数据库文件存放位置'); } -- GitLab