diff --git a/CHANGELOG.md b/CHANGELOG.md index a36bb55046fd9f98e9fa0a791f5d46cb50ef8afd..6a257558df18371381496ec70f89a6af76b8a8c9 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 6a5abfbf740807a9dc14527c0022f4473500d6ec..6d81031ccfc5cfb44e58e6390e1d75d288e7a13e 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 4dfc6a8dbe16e30fbfbc9fb57b047b218c0cb51f..5f3c8f070c75fa0d8c4fd01d1a991f51e62b865e 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 ab022af62e594e91c5d3d5de4fb1298aea2fbddc..f0b4d6cae682624087f32e278617f93b108636b5 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 4a0fb6b0613b4a091a96df8cf7b4d9235339e947..e2d2893ed17ebe05a91d8d9bb631143abf7e5db5 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 db4f44a95122db2e637f3764eaaee7e8008423b2..82a033a4092a3d2d6b260c9d11b3cff08de87e72 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 d696b8525217c47ada312a4aef2abef00025b62a..8b6297555e51a7696cc8193137430a8e90289272 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数据库文件存放位置'); }