首页内核与基础Helper-设备网络与杂项族
Helper-设备网络与杂项族
设备判断、IP/UA、幂等重试、限流、加解密等实用方法
本页内容
  1. 设备与环境判断
  2. 网络与重试
  3. 编码与加解密
  4. 数组处理

设备与环境判断

方法说明
isMobile()移动端判断(VIA/ACCEPT/UA 正则)
isPC()PC 端判断(isMobile 反向)
isWX()微信环境(micromessenger/miniprogram/wxwork)
isWindows() / isLinux()系统环境判断
IP($withProxy=true)客户端 IP(支持代理头)
UA()当前请求 User-Agent
hasClass($classname)类是否存在(需完整类名)

网络与重试

方法签名说明
IdempotentCallIdempotentCall($closer,$mstime=1000,$maxCount=4)回调返回 true 结束,否则指数退避重试(最多 maxCount 次)
speedLimitspeedLimit($code,$speedLimit)两次调用间隔小于 speedLimit 秒则 usleep 等待(基于 askcache)
isSubmitisSubmit($timer)防频繁提交:$timer 毫秒内重复调用返回 true(基于 session)

编码与加解密

方法签名说明
base64encode / base64decode($stream)自定义字母序 base64(异形字母表)
opensslEncrypt / opensslDecrypt($data,$iv)AES 通用加解密(自定义 iv)
jsonencodejsonencode($stream,$isOriginal=false)JSON 编码(unescaped unicode)
password_encodepassword_encode($str)密码加密(转发 Kuoqi::password)
getRandomstringgetRandomstring($len=16)双重 sha1 截取的随机字符串

数组处理

方法说明
arrayNonull($array)去除空字符串/空值(保留 0 与整数),并 trim
arrayUnique($array)深层去重(去空、trim、键值去重)
arrayTourl($arr)数组转 URL(Url::buildUrl)
典型用法
// 设备分流
if(\Kuoqi\Helper::isMobile()){
    return $this->redirect(url("Mobile/Index"));
}
if(\Kuoqi\Helper::isWX()){
    // 微信内访问处理
}

// 防重复提交
if(\Kuoqi\Helper::isSubmit(3000)){
    return $this->jsonReturn(0, "操作过于频繁");
}

// 幂等重试
\Kuoqi\Helper::IdempotentCall(function(){
    return file_get_contents("https://example.com/sync") !== false;
}, 2000, 3);
KuoqiPHP v1.05 · 离线静态文档 · 界面基于 KuoqiUI · 返回首页