| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- <?php
- /**
- * 通用基础方法
- * @author jim <jim458734573@gmail.com>
- * @date 2020-01-22 22:37:21
- */
- declare(strict_types=1);
- namespace App\Repositories\Service;
- use App\Repositories\ConfigRepoistory;
- use App\Repositories\Utils\M3u8CacheRepository;
- use Hyperf\Guzzle\ClientFactory;
- class CommonService
- {
- /**
- * 获取当前请求的客户端IP
- * @return string
- */
- public function getIp()
- {
- $headerIp = request()->getHeader('x-forwarded-for');
- $headerIp2 = request()->getHeader('x-real-ip');
- if ($headerIp2) {
- $ip = $headerIp[0];
- } elseif (!empty($headerIp)) {
- $ip = $headerIp2[0];
- } else {
- $sever = request()->getServerParams();
- $ip = $sever['remote_addr'] ?? '';
- }
- if (strpos($ip, ',')) {
- $temp = explode(',', $ip);
- $ip = $ip[0];
- $temp = null;
- unset($temp);
- }
- return $ip;
- }
- /**
- * 发起http get 请求
- * @param $url
- * @return string
- */
- public function httpGet($url)
- {
- $options = array(
- 'timeout' => 20, // 超时,单位秒.
- 'verify' => false, // 是否校验SSL证书.
- 'http_errors' => false, // 如果跟随http异常头部是否中断.
- 'debug' => false, // 是否启用调试模式.
- 'allow_redirects' => true, // 不跟随301/302跳转.
- );
- $client = make(ClientFactory::class)->create($options);
- try {
- $result = $client->get($url);
- $code = $result->getStatusCode();
- if ($code == 200) {
- return $result->getBody()->getContents();
- }
- } catch (\Exception $e) {
- }
- return '';
- }
- /**
- * 发起 http post 请求
- * @param $url
- * @param $data
- * @return string
- */
- public function httpPost($url, $data)
- {
- $options = array(
- 'timeout' => 20, // 超时,单位秒.
- 'verify' => false, // 是否校验SSL证书.
- 'http_errors' => false, // 如果跟随http异常头部是否中断.
- 'debug' => false, // 是否启用调试模式.
- 'allow_redirects' => true, // 不跟随301/302跳转.
- );
- $client = make(ClientFactory::class)->create($options);
- try {
- $params = [
- 'form_params'=>$data,
- ];
- $result = $client->post($url, $params);
- $code = $result->getStatusCode();
- if ($code == 200) {
- return $result->getBody()->getContents();
- }
- } catch (\Exception $e) {
- }
- return '';
- }
- /**
- * 发起 http post 请求
- * @param $url
- * @param $data
- * @return string
- */
- public function httpPostJson($url, $data, $hearder = [])
- {
- $options = array(
- 'timeout' => 20, // 超时,单位秒.
- 'verify' => false, // 是否校验SSL证书.
- 'http_errors' => false, // 如果跟随http异常头部是否中断.
- 'debug' => false, // 是否启用调试模式.
- 'allow_redirects' => true, // 不跟随301/302跳转.
- );
- $client = make(ClientFactory::class)->create($options);
- try {
- $params = [
- 'headers' => array_merge($hearder, ['Content-Type'=>'application/json']),
- 'json' => $data,
- ];
- $result = $client->post($url, $params);
- $code = $result->getStatusCode();
- if ($code == 200) {
- return $result->getBody()->getContents();
- }
- } catch (\Exception $e) {
- logger('post_error', ['url' => $url, $data], 'error');
- }
- return '';
- }
- /**
- * 生成订单流水号、可指定前缀
- * @param string $prefix
- * @return string
- */
- public function createOrderSn($prefix = '')
- {
- return $prefix . date('YmdHis') . mt_rand(1111, 9999);
- }
- /**
- * 过滤关键词
- * @param $content
- * @return string|string[]|null
- */
- public function filterString($content)
- {
- $content = preg_replace('# #', '', $content); //清除空格
- $content = $this->sbc2Dbc($content);
- $content = preg_replace('#-#', '', $content); //清除空格
- $content = preg_replace('#-#', '', $content); //清除空格
- $content = preg_replace('#,#', ',', $content); //清除空格
- $content = preg_replace('#。#', '.', $content); //清除空格
- $matchs = array();
- preg_match('/(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?/is', $content, $matchs);
- if (!empty($matchs[0])) {
- $content = str_replace($matchs[0], '***', $content);
- }
- $badword = array(
- '习近平','幼女','App','軟件','软件','破处','破處','vpn','翻墙','翻墻','快狐','黄播','出售','棋牌','菠菜','博彩','ios','android','下載','下载','萝莉',
- '平台','伽扣','加扣','免费','外挂','看片','神器','.com','.me','.info','.tv','.site','https','http','.cn','://','.net','.com.cn','.xyz','.club','.cc','.live','.pw',
- '.me','汾','┆аρρ┆','ǎ','á','成仌','亼','浼費','費','www','email','18禁','狼友','分享群','tg','电报','外挂','外掛','進群','安卓','蘋果','苹果','无毒','値播','Ⓐ','ⓐ',
- '抖阴','69热','撸先生','草榴','巫妖王','蝌蚪','资源','透视','快猫','.la','.im','@163','@126','@qq','@gmail','@sina','网站','QQ', '茶','肉','引流', 'wx','微信',
- '上门', '🆀', '🅧','🆇','🆆', '🅦 ','🅆','🅇', '🅀', '🇽', '🇼', '🇶', '🄦', '🄧', '🄠', '.in', '.top', 'ᴡ', 'ᴠ', 'ᵂ', '🄥', '🅅', '🅥', '🆅', '🇻',
- );
- $badword1 = array_combine($badword, array_fill(0, count($badword), '***'));
- $content = strtr($content, $badword1);
- return $content;
- }
- /**
- * 将unicode转换成字符
- * @param int $unicode
- * @return string UTF-8字符
- **/
- public function unicode2Char($unicode)
- {
- if ($unicode < 128) {
- return chr($unicode);
- }
- if ($unicode < 2048) {
- return chr(($unicode >> 6) + 192) .
- chr(($unicode & 63) + 128);
- }
- if ($unicode < 65536) {
- return chr(($unicode >> 12) + 224) .
- chr((($unicode >> 6) & 63) + 128) .
- chr(($unicode & 63) + 128);
- }
- if ($unicode < 2097152) {
- return chr(($unicode >> 18) + 240) .
- chr((($unicode >> 12) & 63) + 128) .
- chr((($unicode >> 6) & 63) + 128) .
- chr(($unicode & 63) + 128);
- }
- return false;
- }
- /**
- * 将字符转换成unicode
- * @param string $char 必须是UTF-8字符
- * @return int
- **/
- public function char2Unicode($char)
- {
- switch (strlen($char)) {
- case 1:
- return ord($char);
- case 2:
- return (ord($char[1]) & 63) |
- ((ord($char[0]) & 31) << 6);
- case 3:
- return (ord($char[2]) & 63) |
- ((ord($char[1]) & 63) << 6) |
- ((ord($char[0]) & 15) << 12);
- case 4:
- return (ord($char[3]) & 63) |
- ((ord($char[2]) & 63) << 6) |
- ((ord($char[1]) & 63) << 12) |
- ((ord($char[0]) & 7) << 18);
- default:
- // trigger_error('Character is not UTF-8!', E_USER_WARNING);
- return '';
- }
- }
- /**
- * 全角转半角
- * @param string $str
- * @return string
- **/
- public function sbc2Dbc($str)
- {
- $DBC = Array(
- '0' , '1' , '2' , '3' , '4' ,
- '5' , '6' , '7' , '8' , '9' ,
- 'A' , 'B' , 'C' , 'D' , 'E' ,
- 'F' , 'G' , 'H' , 'I' , 'J' ,
- 'K' , 'L' , 'M' , 'N' , 'O' ,
- 'P' , 'Q' , 'R' , 'S' , 'T' ,
- 'U' , 'V' , 'W' , 'X' , 'Y' ,
- 'Z' , 'a' , 'b' , 'c' , 'd' ,
- 'e' , 'f' , 'g' , 'h' , 'i' ,
- 'j' , 'k' , 'l' , 'm' , 'n' ,
- 'o' , 'p' , 'q' , 'r' , 's' ,
- 't' , 'u' , 'v' , 'w' , 'x' ,
- 'y' , 'z' , '-' , ' ' , ':' ,
- '.' , ',' , '/' , '%' , '#' ,
- '!' , '@' , '&' , '(' , ')' ,
- '<' , '>' , '"' , ''' , '?' ,
- '[' , ']' , '{' , '}' , '\' ,
- '|' , '+' , '=' , '_' , '^' ,
- '¥' , ' ̄' , '`'
- );
- //半角
- $SBC = Array(
- '0', '1', '2', '3', '4',
- '5', '6', '7', '8', '9',
- 'A', 'B', 'C', 'D', 'E',
- 'F', 'G', 'H', 'I', 'J',
- 'K', 'L', 'M', 'N', 'O',
- 'P', 'Q', 'R', 'S', 'T',
- 'U', 'V', 'W', 'X', 'Y',
- 'Z', 'a', 'b', 'c', 'd',
- 'e', 'f', 'g', 'h', 'i',
- 'j', 'k', 'l', 'm', 'n',
- 'o', 'p', 'q', 'r', 's',
- 't', 'u', 'v', 'w', 'x',
- 'y', 'z', '-', ' ', ':',
- '.', ',', '/', '%', '#',
- '!', '@', '&', '(', ')',
- '<', '>', '"', '\'','?',
- '[', ']', '{', '}', '\\',
- '|', '+', '=', '_', '^',
- '$', '~', '`'
- );
- return str_replace($DBC, $SBC, $str);
- }
- /**
- * 检测一个字符串是否为时间格式
- * @param string $time
- * @return false|int
- */
- public function isTime($time)
- {
- $pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/';
- return preg_match($pattern, $time);
- }
- }
|