<?php
declare (strict_types=1);
namespace DtApp\ThinkLibrary\helper;
class Requests
{
public function isEmpty(array $data, array $arr): array
{
foreach ($arr as $k => $v) {
if (empty($data[(string)$v] ?? '')) {
return [];
}
}
return $data;
}
public function isEmptyRet(array $data, array $arr): array
{
foreach ($arr as $k => $v) {
if (empty($data[(string)$v] ?? '')) {
(new Returns)->jsonError('请检查参数', 102);
}
}
return $data;
}
public function isGet(): bool
{
return request()->isGet();
}
public function isPost(): bool
{
return request()->isPost();
}
public function isPut(): bool
{
return request()->isPut();
}
public function isDelete(): bool
{
return request()->isDelete();
}
public function isAjax(): bool
{
return request()->isAjax();
}
public function isMobile(): bool
{
if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
return true;
}
if (isset($_SERVER['HTTP_VIA'])) {
return stripos(request()->server('HTTP_VIA'), "wap") !== false;
}
if (isset($_SERVER['HTTP_USER_AGENT'])) {
$clientkeywords = [
'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp',
'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone', 'ipod', 'blackberry', 'meizu',
'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi',
'openwave', 'nexusone', 'cldc', 'midp', 'wap', 'mobile', 'alipay'
];
if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower(request()->server('HTTP_USER_AGENT')))) {
return true;
}
}
return isset($_SERVER['HTTP_ACCEPT']) && (strpos(request()->server('HTTP_ACCEPT'), 'vnd.wap.wml') !== false) && (strpos(request()->server('HTTP_ACCEPT'), 'text/html') === false || (strpos(request()->server('HTTP_ACCEPT'), 'vnd.wap.wml') < strpos(request()->server('HTTP_ACCEPT'), 'text/html')));
}
public function isWeiXin(): bool
{
return strpos(request()->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false;
}
public function isWeiXinMp(): bool
{
return strpos(request()->server('HTTP_USER_AGENT'), 'miniProgram') !== false;
}
public function isAliPay(): bool
{
return strpos(request()->server('HTTP_USER_AGENT'), 'Alipay') !== false;
}
public function isQQ(): bool
{
return (strpos(request()->server('HTTP_USER_AGENT'), 'QQ') !== false) && strpos(request()->server('HTTP_USER_AGENT'), '_SQ_') !== false;
}
public function isQQBrowser(): bool
{
if (strpos(request()->server('HTTP_USER_AGENT'), 'QQ') !== false) {
return !(strpos(request()->server('HTTP_USER_AGENT'), '_SQ_') !== false);
}
return false;
}
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;
}
public function getMobileType()
{
$agent = strtolower(request()->server('HTTP_USER_AGENT'));
$type = 'other';
if (strpos($agent, 'iphone') || strpos($agent, 'ipad')) {
$type = 'ios';
}
if (strpos($agent, 'android')) {
$type = 'android';
}
return $type;
}
public function getWebsiteAddress(): string
{
$http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https')) ? 'https://' : 'http://';
return $http_type . $_SERVER['HTTP_HOST'] . "/";
}
}