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

- 优化代码

- 优化Api控制器的加密相关参数
- 请求门面增加获取客户端类型和获取手机设备类型
上级 a74522a7
## v6.0.88 / 2020-07-18
- 优化代码
- 优化Api控制器的加密相关参数
- 请求门面增加获取客户端类型和获取手机设备类型
## v6.0.87 / 2020-07-17 ## v6.0.87 / 2020-07-17
- 优化代码 - 优化代码
......
...@@ -49,6 +49,13 @@ class ApiController extends stdClass ...@@ -49,6 +49,13 @@ class ApiController extends stdClass
*/ */
private $aes_decrypt_data; private $aes_decrypt_data;
/**
* 加密相关的东西
* @var string
*/
private $aes_md5 = '';
private $aes_md5_iv = '';
/** /**
* ApiController constructor. * ApiController constructor.
* @param App $app * @param App $app
...@@ -97,19 +104,41 @@ class ApiController extends stdClass ...@@ -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 $data 返回数据
* @param mixed $msg 消息内容 * @param mixed $msg 消息内容
* @param integer $code 返回代码 * @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(); $timestamp = time();
throw new HttpResponseException(json([ throw new HttpResponseException(json([
'code' => $code, 'msg' => $msg, 'timestamp' => $timestamp, 'data' => [ '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 ...@@ -202,14 +231,21 @@ class ApiController extends stdClass
/** /**
* 加密 * 加密
* @param $data * @param $data
* @param string $name
* @param int $timestamp * @param int $timestamp
* @return bool|string * @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); if (empty($this->aes_md5)) {
return urlencode(base64_encode(openssl_encrypt($data, 'AES-128-CBC', config("dtapp.md5.{$name}"), 1, config("dtapp.md5.bcw") . $timestamp))); $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 ...@@ -221,6 +257,12 @@ class ApiController extends stdClass
*/ */
private function decrypt(string $data, string $name, int $timestamp) 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);
} }
} }
...@@ -28,7 +28,7 @@ use think\db\exception\ModelNotFoundException; ...@@ -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')) { if (!function_exists('get_ip_info')) {
/** /**
......
...@@ -44,6 +44,8 @@ use think\Facade; ...@@ -44,6 +44,8 @@ use think\Facade;
* @method static bool isAliPay() 判断是否支付宝内置浏览器访问 * @method static bool isAliPay() 判断是否支付宝内置浏览器访问
* @method static bool isQQ() 判断是否QQ内置浏览器访问 * @method static bool isQQ() 判断是否QQ内置浏览器访问
* @method static bool isQQBrowser() 判断是否QQ浏览器访问 * @method static bool isQQBrowser() 判断是否QQ浏览器访问
* @method static string getDeviceType() 获取客户端类型
* @method static string getMobileType() 获取手机设备类型
* @method static string getWebsiteAddress() 获取域名地址 * @method static string getWebsiteAddress() 获取域名地址
*/ */
class Requests extends Facade class Requests extends Facade
......
...@@ -210,6 +210,38 @@ class Requests ...@@ -210,6 +210,38 @@ class Requests
return false; 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 * @return string
......
...@@ -46,7 +46,7 @@ class IpIpService extends Service ...@@ -46,7 +46,7 @@ class IpIpService extends Service
*/ */
public function __construct(App $app) public function __construct(App $app)
{ {
$this->ipPath = config('dtapp.ip_path', ''); $this->ipPath = $this->app->config->get('dtapp.ip_path', '');
if (empty($this->ipPath)) { if (empty($this->ipPath)) {
throw new DtaException('请检查配置文件是否配置了IP数据库文件存放位置'); throw new DtaException('请检查配置文件是否配置了IP数据库文件存放位置');
} }
......
...@@ -74,7 +74,7 @@ class QqWryService extends Service ...@@ -74,7 +74,7 @@ class QqWryService extends Service
*/ */
public function __construct(App $app) public function __construct(App $app)
{ {
$this->ipPath = config('dtapp.ip_path', ''); $this->ipPath = $this->app->config->get('dtapp.ip_path', '');
if (empty($this->ipPath)) { if (empty($this->ipPath)) {
throw new DtaException('请检查配置文件是否配置了IP数据库文件存放位置'); throw new DtaException('请检查配置文件是否配置了IP数据库文件存放位置');
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册