`
~~~
<?php
// +----------------------------------------------------------------------
// | Created by PHPstorm: JRKAdmin框架 [ JRKAdmin ]
// +----------------------------------------------------------------------
// | Copyright (c) 2019~2022 [LuckyHHY] All rights reserved.
// +----------------------------------------------------------------------
// | SiteUrl: http://www.luckyhhy.cn
// +----------------------------------------------------------------------
// | Author: LuckyHhy <jackhhy520@qq.com>
// +----------------------------------------------------------------------
// | Date: 2020/1/3-16:13
// +----------------------------------------------------------------------
// | Description:
// +----------------------------------------------------------------------
namespace Jrk;
use think\facade\Request;
class Util
{
/**
* @param $params
* @param Request|null $request
* @param bool $suffix
* @return array
* @author: hhygyl
* @name: postMore
* @describe:獲取post請求的數據
*/
public static function postMore($params, Request $request = null, $suffix = false)
{
if($request===null)
$request = Request::instance();
$p = [];
$i = 0;
foreach($params as $param){
if(!is_array($param)) {
$p[$suffix==true ? $i++ : $param] = $request->post($param);
}
else {
if(!isset($param[1]))
$param[1] = null;
if(!isset($param[2]))
$param[2] = '';
$name = is_array($param[1]) ? $param[0].'/a' : $param[0];
$p[$suffix==true ? $i++ : (isset($param[3]) ? $param[3] : $param[0])] = $request->post($name, $param[1], $param[2]);
}
}
return $p;
}
/**
* @param $params
* @param Request|null $request
* @param bool $suffix
* @return array
* @author: hhygyl
* @name: getMore
* @describe:獲取get請求的數據
*/
public static function getMore($params, Request $request = null, $suffix = false)
{
if($request===null)
$request = Request::instance();
$p = [];
$i = 0;
foreach($params as $param){
if(!is_array($param)) {
$p[$suffix==true ? $i++ : $param] = $request->get($param);
}
else {
if(!isset($param[1]))
$param[1] = null;
if(!isset($param[2]))
$param[2] = '';
$name = is_array($param[1]) ? $param[0].'/a' : $param[0];
$p[$suffix==true ? $i++ : (isset($param[3]) ? $param[3] : $param[0])] = $request->get($name, $param[1], $param[2]);
}
}
return $p;
}
/**
* @param int $type 返回類型 0 返回IP地址 1 返回IPV4地址數字
* @param bool $adv 是否進行高級模式獲取(有可能被偽裝)
* @return mixed
* @author: hhygyl <hhygyl520@qq.com>
* @name: get_client_ip
* @describe:獲取客戶端IP地址
*/
public static function get_client_ip($type = 0, $adv = false) {
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) {
return $ip[$type];
}
if ($adv) {
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$pos = array_search('unknown', $arr);
if (false !== $pos) {
unset($arr[$pos]);
}
$ip = trim($arr[0]);
} elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
} elseif (isset($_SERVER['REMOTE_ADDR'])) {
$ip = $_SERVER['REMOTE_ADDR'];
}
// IP地址合法驗證
$long = sprintf("%u", ip2long($ip));
$ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
return $ip[$type];
}
/**
* @param $ip
* @return array|mixed
* @Author: LuckyHhy <jackhhy520@qq.com>
* @name: getAddress
* @describe: 獲取IP地址詳細信息
*/
public static function getAddress($ip){
$url = file_get_contents("http://api.map.baidu.com/location/ip?ip=$ip&ak=Eay7nMQ0htmL357Yh1GSFHcN9qVVoGsg");
$res1 = json_decode($url,true);
if ($res1['status']==0){ //獲取到地址信息
return $res1;
}else{
return [];
}
}
/**
* @param int $length
* @return string
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/17
* @name: uniqidNumberCode
* @describe:唯一數字編碼
*/
public static function uniqidNumberCode($length = 10)
{
$time = time() . '';
if ($length < 10) $length = 10;
$string = ($time[0] + $time[1]) . substr($time, 2) . rand(0, 9);
while (strlen($string) < $length) $string .= rand(0, 9);
return $string;
}
/**
* @param int $length
* @return string
* @author: LuckyHhy <jackhhy520@qq.com>
* @date: 2020/3/17
* @name: uniqidDateCode
* @describe: 唯一日期編碼
*/
public static function uniqidDateCode($length = 14)
{
if ($length < 14) $length = 14;
$string = date('Ymd') . (date('H') + date('i')) . date('s');
while (strlen($string) < $length) $string .= rand(0, 9);
return $string;
}
/**
* @param $data
* @param StringService $key
* @param int $expire
* @return mixed
* @author: hhygyl < hhygyl520@qq.com >
* @name: think_encrypt
* @describe:系統加密方法
*/
public static function think_encrypt($data, $key = 'http://www.jackhhy.cn', $expire = 0)
{
$key = md5(empty($key) ? 'lucky_jrkadmin' : $key);
$data = base64_encode($data);
$x = 0;
$len = strlen($data);
$l = strlen($key);
$char = '';
for($i = 0; $i < $len; $i++){
if($x==$l)
$x = 0;
$char .= substr($key, $x, 1);
$x++;
}
$str = sprintf('%010d', $expire ? $expire + time() : 0);
for($i = 0; $i < $len; $i++){
$str .= chr(ord(substr($data, $i, 1)) + (ord(substr($char, $i, 1)))%256);
}
return str_replace(['+', '/', '='], ['-', '_', ''], base64_encode($str));
}
/**
* @param $data
* @param StringService $key
* @return bool|StringService
* @author: hhygyl <hhygyl520@qq.com>
* @name: think_decrypt
* @describe:系統解密方法
*/
public static function think_decrypt($data, $key = 'http://www.jackhhy.cn')
{
$key = md5(empty($key) ? 'lucky_jrkadmin' : $key);
$data = str_replace(['-', '_'], ['+', '/'], $data);
$mod4 = strlen($data)%4;
if($mod4) {
$data .= substr('====', $mod4);
}
$data = base64_decode($data);
$expire = substr($data, 0, 10);
$data = substr($data, 10);
if($expire > 0 && $expire < time()) {
return '';
}
$x = 0;
$len = strlen($data);
$l = strlen($key);
$char = $str = '';
for($i = 0; $i < $len; $i++){
if($x==$l)
$x = 0;
$char .= substr($key, $x, 1);
$x++;
}
for($i = 0; $i < $len; $i++){
if(ord(substr($data, $i, 1)) < ord(substr($char, $i, 1))) {
$str .= chr((ord(substr($data, $i, 1)) + 256) - ord(substr($char, $i, 1)));
}
else {
$str .= chr(ord(substr($data, $i, 1)) - ord(substr($char, $i, 1)));
}
}
return base64_decode($str);
}
/**
* @param $url
* @return bool
* @author: hhygyl <jackhhy520@qq.com>
* @name: is_url
* @describe:驗證url
*/
public static function is_url($url){
if(empty($url)) return false;
if(preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$url)){
return true;
}else{
return false;
}
}
/**
* @param $path
* @return StringService
* @author: hhygyl
* @name: pathToUrl
* @describe:路徑轉url路徑
*/
public static function pathToUrl($path)
{
return trim(str_replace(DS, '/', $path), '.');
}
/**
* @param $url
* @return StringService
* @author: hhygyl
* @name: urlToPath
* @describe:url轉換路徑
*/
public static function urlToPath($url)
{
return ROOT_PATH.trim(str_replace('/', DS, $url), DS);
}
/**
* @param $str
* @return mixed
* @author: hhygyl
* @name: hide_phone
* @describe:替換手機號碼中間四位數字
*/
public static function hide_phone($str)
{
$resstr = substr_replace($str, '****', 3, 4);
return $resstr;
}
/**
* 格式化字節大小
* @param number $size 字節數
* @param StringService $delimiter 數字和單位分隔符
* @return StringService 格式化后的帶單位的大小
*/
public static function format_bytes($size, $delimiter = '')
{
$units = ['B', 'KB', 'MB', 'GB', 'TB', 'PB'];
for($i = 0; $size >= 1024 && $i < 5; $i++)
$size /= 1024;
return round($size, 2).$delimiter.$units[$i];
}
/**
* @return bool
* @author: hhygyl
* @name: isWechatBrowser
* @describe:是否為微信內部瀏覽器
*/
public static function isWechatBrowser()
{
return (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger')!==false);
}
/**
* @param $name
* @return StringService
* @author: hhygyl
* @name: anonymity
* @describe:匿名處理
*/
public static function anonymity($name)
{
$strLen = mb_strlen($name, 'UTF-8');
$min = 3;
if($strLen <= 1)
return '*';
if($strLen <= $min)
return mb_substr($name, 0, 1, 'UTF-8').str_repeat('*', $min - 1);
else
return mb_substr($name, 0, 1, 'UTF-8').str_repeat('*', $strLen - 1).mb_substr($name, -1, 1, 'UTF-8');
}
/**
* @param $card
* @return bool
* @author: hhygyl
* @name: setCard
* @describe:身份證驗證
*/
public static function setCard($card)
{
$city = [11 => "北京", 12 => "天津", 13 => "河北", 14 => "山西", 15 => "內蒙古", 21 => "遼寧", 22 => "吉林", 23 => "黑龍江 ", 31 => "上海", 32 => "江蘇", 33 => "浙江", 34 => "安徽", 35 => "福建", 36 => "江西", 37 => "山東", 41 => "河南", 42 => "湖北 ", 43 => "湖南", 44 => "廣東", 45 => "廣西", 46 => "海南", 50 => "重慶", 51 => "四川", 52 => "貴州", 53 => "云南", 54 => "西藏 ", 61 => "陜西", 62 => "甘肅", 63 => "青海", 64 => "寧夏", 65 => "新疆", 71 => "臺灣", 81 => "香港", 82 => "澳門", 91 => "國外 "];
$tip = "";
$match = "/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/";
$pass = true;
if(!$card || !preg_match($match, $card)) {
//身份證格式錯誤
$pass = false;
}
else if(!$city[substr($card, 0, 2)]) {
//地址錯誤
$pass = false;
}
else {
//18位身份證需要驗證最后一位校驗位
if(strlen($card)==18) {
$card = str_split($card);
//∑(ai×Wi)(mod 11)
//加權因子
$factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
//校驗位
$parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
$sum = 0;
$ai = 0;
$wi = 0;
for($i = 0; $i < 17; $i++){
$ai = $card[$i];
$wi = $factor[$i];
$sum += $ai*$wi;
}
$last = $parity[$sum%11];
if($parity[$sum%11]!=$card[17]) {
// $tip = "校驗位錯誤";
$pass = false;
}
}
else {
$pass = false;
}
}
if(!$pass)
return false;/* 身份證格式錯誤*/
return true;/* 身份證格式正確*/
}
/**
* @return bool
* @author: hhygyl
* @name: is_mobile
* @describe:判斷是否為手機訪問
*/
public static function is_mobile()
{
static $is_mobile;
if(isset($is_mobile)) {
return $is_mobile;
}
if(empty($_SERVER['HTTP_USER_AGENT'])) {
$is_mobile = false;
}
else if(strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile')!==false || strpos($_SERVER['HTTP_USER_AGENT'], 'Android')!==false || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/')!==false || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle')!==false || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry')!==false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini')!==false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi')!==false) {
$is_mobile = true;
}
else {
$is_mobile = false;
}
return $is_mobile;
}
/**
* @return StringService
* @author: hhygyl
* @name: getOS
* @describe:獲取客戶端操作系統
*/
public static function getOS()
{
$agent = strtolower($_SERVER['HTTP_USER_AGENT']);
if(strpos($agent, 'windows')) {
$platform = 'windows';
}
else if(strpos($agent, 'macintosh')) {
$platform = 'mac';
}
else if(strpos($agent, 'ipod')) {
$platform = 'ipod';
}
else if(strpos($agent, 'ipad')) {
$platform = 'ipad';
}
else if(strpos($agent, 'iphone')) {
$platform = 'iphone';
}
else if(strpos($agent, 'android')) {
$platform = 'android';
}
else if(strpos($agent, 'unix')) {
$platform = 'unix';
}
else if(strpos($agent, 'linux')) {
$platform = 'linux';
}
else {
$platform = 'other';
}
return $platform;
}
/**
* @return array
* @author: hhygyl <hhygyl520@qq.com>
* @name: getBrowser
* @describe:獲取瀏覽器模式
*/
public static function getBrowser()
{
// 獲取用戶代理基本信息
$flag = $_SERVER['HTTP_USER_AGENT'];
// 定義一個空數組
$para = array();
// 檢查操作系統
if (preg_match('/Chrome\/[\d\.\w]*/', $flag, $match)) {
// 檢查Chrome
$para['browser'] = $match[0];
} elseif (preg_match('/Safari\/[\d\.\w]*/', $flag, $match)) {
// 檢查Safari
$para['browser'] = $match[0];
} elseif (preg_match('/MSIE [\d\.\w]*/', $flag, $match)) {
// IE
$para['browser'] = $match[0];
} elseif (preg_match('/Opera\/[\d\.\w]*/', $flag, $match)) {
// opera
$para['browser'] = $match[0];
} elseif (preg_match('/Firefox\/[\d\.\w]*/', $flag, $match)) {
// Firefox
$para['browser'] = $match[0];
} elseif (preg_match('/OmniWeb\/(v*)([^\s|;]+)/i', $flag, $match)) {
//OmniWeb
$para['browser'] = $match[2];
} elseif (preg_match('/Netscape([\d]*)\/([^\s]+)/i', $flag, $match)) {
//Netscape
$para['browser'] = $match[2];
} elseif (preg_match('/Lynx\/([^\s]+)/i', $flag, $match)) {
//Lynx
$para['browser'] = $match[1];
} elseif (preg_match('/360SE/i', $flag, $match)) {
//360SE
$para['browser'] = '360安全瀏覽器';
} elseif (preg_match('/SE 2.x/i', $flag, $match)) {
//搜狗
$para['browser'] = '搜狗瀏覽器';
} else {
$para['browser'] = 'unkown';
}
// 數據返回
return $para['browser'];
}
/**
* @return string
* @author: hhygyl <hhygyl520@qq.com>
* @name: get_url
* @describe:獲取當前頁面完整URL地址
*/
public static function get_url()
{
$sys_protocal = isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT']=='443' ? 'https://' : 'http://';
$php_self = $_SERVER['PHP_SELF'] ? safe_replace($_SERVER['PHP_SELF']) : safe_replace($_SERVER['SCRIPT_NAME']);
$path_info = isset($_SERVER['PATH_INFO']) ? safe_replace($_SERVER['PATH_INFO']) : '';
$relate_url = isset($_SERVER['REQUEST_URI']) ? safe_replace($_SERVER['REQUEST_URI']) : $php_self.(isset($_SERVER['QUERY_STRING']) ? '?'.safe_replace($_SERVER['QUERY_STRING']) : $path_info);
return $sys_protocal.HTTP_HOST.$relate_url;
}
/**
* @return mixed|string
* @author: hhygyl <hhygyl520@qq.com>
* @name: getip
* @describe:獲取IP地址
*/
public static function getip()
{
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$ip = getenv('HTTP_CLIENT_IP');
}
else if(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
else if(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$ip = getenv('REMOTE_ADDR');
}
else if(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_match('/[\d\.]{7,15}/', $ip, $matches) ? $matches [0] : '127.0.0.1';
}
/**
* @param $email
* @return bool
* @author: hhygyl <hhygyl520@qq.com>
* @name: is_email
* @describe:判斷email格式是否正確
*/
public static function is_email($email)
{
return strlen($email) > 6 && preg_match("/^[\w\-\.]+@[\w\-\.]+(\.\w+)+$/", $email);
}
/**
* @return bool
* @author: hhygyl <hhygyl520@qq.com>
* @name: is_ie
* @describe:IE瀏覽器判斷
*/
public static function is_ie()
{
$useragent = strtolower($_SERVER['HTTP_USER_AGENT']);
if((strpos($useragent, 'opera')!==false) || (strpos($useragent, 'konqueror')!==false))
return false;
if(strpos($useragent, 'msie ')!==false)
return true;
return false;
}
}
~~~
`
- 空白目錄
- thinkphp5
- tools-常用類庫
- redis類庫
- Excel類庫
- File文件操作類庫
- Http請求類庫
- Maile郵件發送
- Hooks行為鉤子
- 七牛云
- 隨機數和字符串生成
- 字符串處理
- 時間類處理
- tree型轉換
- 工具類庫
- 文件打包下載
- 常用功能
- 文件上傳
- php生成word文檔
- elasticsearch 基本搜索
- 使用jwt開發API接口
- 安裝模及搭建
- ApiCheck.php
- ApiCheckLogin.php
- common.php
- Login.php
- Comment.php
- 漢字轉拼音
- 安裝使用
- Pinyin類
- elasticsearch操作
- 常用方法
- 數據源生成layui-select
- 獲取自定義配置項
- 百度編輯器
- 格式化文件大小
- 多語言設置
- hook監聽
- 域名綁定到模塊
- thinkphp6
- 文件上傳
- tp5totp6
- 創建路徑
- 獲取類所有方法
- password_hash加密驗證
- 生成 qrcode
- 郵件發送
- 獲取QQ信息
- GoogleAuthenticator
- redis限流
- redis 加鎖
- 百度翻譯
- QueryList爬取數據
- 獲取時間類
- 命令
- Git常用命令
- easyswoole
- pix_qrcode
- 驗證 cpf,cnpj
- php常用方法
- 日志
- 卡通頭像
- 兩位小數
- 圖片轉base64
- auth加密解密
- phpoffice/phpspreadsheet導入導出
- fastadmin
- 樹結構
- 單選框
- 復選框
- 二級搜索
- select選擇框
- selectpage選中回調事件
- 標簽添加
- 修改where條件
- 表格列表中添加input框
- selectpage事件
- fieldlist
- js操作
- test_js
- 多表格
- template模板