CommonService.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. /**
  3. * 通用基础方法
  4. * @author jim <jim458734573@gmail.com>
  5. * @date 2020-01-22 22:37:21
  6. */
  7. declare(strict_types=1);
  8. namespace App\Repositories\Service;
  9. use App\Repositories\ConfigRepoistory;
  10. use App\Repositories\Utils\M3u8CacheRepository;
  11. use Hyperf\Guzzle\ClientFactory;
  12. class CommonService
  13. {
  14. /**
  15. * 获取当前请求的客户端IP
  16. * @return string
  17. */
  18. public function getIp()
  19. {
  20. $headerIp = request()->getHeader('x-forwarded-for');
  21. $headerIp2 = request()->getHeader('x-real-ip');
  22. if ($headerIp2) {
  23. $ip = $headerIp[0];
  24. } elseif (!empty($headerIp)) {
  25. $ip = $headerIp2[0];
  26. } else {
  27. $sever = request()->getServerParams();
  28. $ip = $sever['remote_addr'] ?? '';
  29. }
  30. if (strpos($ip, ',')) {
  31. $temp = explode(',', $ip);
  32. $ip = $ip[0];
  33. $temp = null;
  34. unset($temp);
  35. }
  36. return $ip;
  37. }
  38. /**
  39. * 发起http get 请求
  40. * @param $url
  41. * @return string
  42. */
  43. public function httpGet($url)
  44. {
  45. $options = array(
  46. 'timeout' => 20, // 超时,单位秒.
  47. 'verify' => false, // 是否校验SSL证书.
  48. 'http_errors' => false, // 如果跟随http异常头部是否中断.
  49. 'debug' => false, // 是否启用调试模式.
  50. 'allow_redirects' => true, // 不跟随301/302跳转.
  51. );
  52. $client = make(ClientFactory::class)->create($options);
  53. try {
  54. $result = $client->get($url);
  55. $code = $result->getStatusCode();
  56. if ($code == 200) {
  57. return $result->getBody()->getContents();
  58. }
  59. } catch (\Exception $e) {
  60. }
  61. return '';
  62. }
  63. /**
  64. * 发起 http post 请求
  65. * @param $url
  66. * @param $data
  67. * @return string
  68. */
  69. public function httpPost($url, $data)
  70. {
  71. $options = array(
  72. 'timeout' => 20, // 超时,单位秒.
  73. 'verify' => false, // 是否校验SSL证书.
  74. 'http_errors' => false, // 如果跟随http异常头部是否中断.
  75. 'debug' => false, // 是否启用调试模式.
  76. 'allow_redirects' => true, // 不跟随301/302跳转.
  77. );
  78. $client = make(ClientFactory::class)->create($options);
  79. try {
  80. $params = [
  81. 'form_params'=>$data,
  82. ];
  83. $result = $client->post($url, $params);
  84. $code = $result->getStatusCode();
  85. if ($code == 200) {
  86. return $result->getBody()->getContents();
  87. }
  88. } catch (\Exception $e) {
  89. }
  90. return '';
  91. }
  92. /**
  93. * 发起 http post 请求
  94. * @param $url
  95. * @param $data
  96. * @return string
  97. */
  98. public function httpPostJson($url, $data, $hearder = [])
  99. {
  100. $options = array(
  101. 'timeout' => 20, // 超时,单位秒.
  102. 'verify' => false, // 是否校验SSL证书.
  103. 'http_errors' => false, // 如果跟随http异常头部是否中断.
  104. 'debug' => false, // 是否启用调试模式.
  105. 'allow_redirects' => true, // 不跟随301/302跳转.
  106. );
  107. $client = make(ClientFactory::class)->create($options);
  108. try {
  109. $params = [
  110. 'headers' => array_merge($hearder, ['Content-Type'=>'application/json']),
  111. 'json' => $data,
  112. ];
  113. $result = $client->post($url, $params);
  114. $code = $result->getStatusCode();
  115. if ($code == 200) {
  116. return $result->getBody()->getContents();
  117. }
  118. } catch (\Exception $e) {
  119. logger('post_error', ['url' => $url, $data], 'error');
  120. }
  121. return '';
  122. }
  123. /**
  124. * 生成订单流水号、可指定前缀
  125. * @param string $prefix
  126. * @return string
  127. */
  128. public function createOrderSn($prefix = '')
  129. {
  130. return $prefix . date('YmdHis') . mt_rand(1111, 9999);
  131. }
  132. /**
  133. * 过滤关键词
  134. * @param $content
  135. * @return string|string[]|null
  136. */
  137. public function filterString($content)
  138. {
  139. $content = preg_replace('# #', '', $content); //清除空格
  140. $content = $this->sbc2Dbc($content);
  141. $content = preg_replace('#-#', '', $content); //清除空格
  142. $content = preg_replace('#-#', '', $content); //清除空格
  143. $content = preg_replace('#,#', ',', $content); //清除空格
  144. $content = preg_replace('#。#', '.', $content); //清除空格
  145. $matchs = array();
  146. preg_match('/(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?/is', $content, $matchs);
  147. if (!empty($matchs[0])) {
  148. $content = str_replace($matchs[0], '***', $content);
  149. }
  150. $badword = array(
  151. '习近平','幼女','App','軟件','软件','破处','破處','vpn','翻墙','翻墻','快狐','黄播','出售','棋牌','菠菜','博彩','ios','android','下載','下载','萝莉',
  152. '平台','伽扣','加扣','免费','外挂','看片','神器','.com','.me','.info','.tv','.site','https','http','.cn','://','.net','.com.cn','.xyz','.club','.cc','.live','.pw',
  153. '.me','汾','┆аρρ┆','ǎ','á','成仌','亼','浼費','費','www','email','18禁','狼友','分享群','tg','电报','外挂','外掛','進群','安卓','蘋果','苹果','无毒','値播','Ⓐ','ⓐ',
  154. '抖阴','69热','撸先生','草榴','巫妖王','蝌蚪','资源','透视','快猫','.la','.im','@163','@126','@qq','@gmail','@sina','网站','QQ', '茶','肉','引流', 'wx','微信',
  155. '上门', '🆀', '🅧','🆇','🆆', '🅦 ','🅆','🅇', '🅀', '🇽', '🇼', '🇶', '🄦', '🄧', '🄠', '.in', '.top', 'ᴡ', 'ᴠ', 'ᵂ', '🄥', '🅅', '🅥', '🆅', '🇻',
  156. );
  157. $badword1 = array_combine($badword, array_fill(0, count($badword), '***'));
  158. $content = strtr($content, $badword1);
  159. return $content;
  160. }
  161. /**
  162. * 将unicode转换成字符
  163. * @param int $unicode
  164. * @return string UTF-8字符
  165. **/
  166. public function unicode2Char($unicode)
  167. {
  168. if ($unicode < 128) {
  169. return chr($unicode);
  170. }
  171. if ($unicode < 2048) {
  172. return chr(($unicode >> 6) + 192) .
  173. chr(($unicode & 63) + 128);
  174. }
  175. if ($unicode < 65536) {
  176. return chr(($unicode >> 12) + 224) .
  177. chr((($unicode >> 6) & 63) + 128) .
  178. chr(($unicode & 63) + 128);
  179. }
  180. if ($unicode < 2097152) {
  181. return chr(($unicode >> 18) + 240) .
  182. chr((($unicode >> 12) & 63) + 128) .
  183. chr((($unicode >> 6) & 63) + 128) .
  184. chr(($unicode & 63) + 128);
  185. }
  186. return false;
  187. }
  188. /**
  189. * 将字符转换成unicode
  190. * @param string $char 必须是UTF-8字符
  191. * @return int
  192. **/
  193. public function char2Unicode($char)
  194. {
  195. switch (strlen($char)) {
  196. case 1:
  197. return ord($char);
  198. case 2:
  199. return (ord($char[1]) & 63) |
  200. ((ord($char[0]) & 31) << 6);
  201. case 3:
  202. return (ord($char[2]) & 63) |
  203. ((ord($char[1]) & 63) << 6) |
  204. ((ord($char[0]) & 15) << 12);
  205. case 4:
  206. return (ord($char[3]) & 63) |
  207. ((ord($char[2]) & 63) << 6) |
  208. ((ord($char[1]) & 63) << 12) |
  209. ((ord($char[0]) & 7) << 18);
  210. default:
  211. // trigger_error('Character is not UTF-8!', E_USER_WARNING);
  212. return '';
  213. }
  214. }
  215. /**
  216. * 全角转半角
  217. * @param string $str
  218. * @return string
  219. **/
  220. public function sbc2Dbc($str)
  221. {
  222. $DBC = Array(
  223. '0' , '1' , '2' , '3' , '4' ,
  224. '5' , '6' , '7' , '8' , '9' ,
  225. 'A' , 'B' , 'C' , 'D' , 'E' ,
  226. 'F' , 'G' , 'H' , 'I' , 'J' ,
  227. 'K' , 'L' , 'M' , 'N' , 'O' ,
  228. 'P' , 'Q' , 'R' , 'S' , 'T' ,
  229. 'U' , 'V' , 'W' , 'X' , 'Y' ,
  230. 'Z' , 'a' , 'b' , 'c' , 'd' ,
  231. 'e' , 'f' , 'g' , 'h' , 'i' ,
  232. 'j' , 'k' , 'l' , 'm' , 'n' ,
  233. 'o' , 'p' , 'q' , 'r' , 's' ,
  234. 't' , 'u' , 'v' , 'w' , 'x' ,
  235. 'y' , 'z' , '-' , ' ' , ':' ,
  236. '.' , ',' , '/' , '%' , '#' ,
  237. '!' , '@' , '&' , '(' , ')' ,
  238. '<' , '>' , '"' , ''' , '?' ,
  239. '[' , ']' , '{' , '}' , '\' ,
  240. '|' , '+' , '=' , '_' , '^' ,
  241. '¥' , ' ̄' , '`'
  242. );
  243. //半角
  244. $SBC = Array(
  245. '0', '1', '2', '3', '4',
  246. '5', '6', '7', '8', '9',
  247. 'A', 'B', 'C', 'D', 'E',
  248. 'F', 'G', 'H', 'I', 'J',
  249. 'K', 'L', 'M', 'N', 'O',
  250. 'P', 'Q', 'R', 'S', 'T',
  251. 'U', 'V', 'W', 'X', 'Y',
  252. 'Z', 'a', 'b', 'c', 'd',
  253. 'e', 'f', 'g', 'h', 'i',
  254. 'j', 'k', 'l', 'm', 'n',
  255. 'o', 'p', 'q', 'r', 's',
  256. 't', 'u', 'v', 'w', 'x',
  257. 'y', 'z', '-', ' ', ':',
  258. '.', ',', '/', '%', '#',
  259. '!', '@', '&', '(', ')',
  260. '<', '>', '"', '\'','?',
  261. '[', ']', '{', '}', '\\',
  262. '|', '+', '=', '_', '^',
  263. '$', '~', '`'
  264. );
  265. return str_replace($DBC, $SBC, $str);
  266. }
  267. /**
  268. * 检测一个字符串是否为时间格式
  269. * @param string $time
  270. * @return false|int
  271. */
  272. public function isTime($time)
  273. {
  274. $pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/';
  275. return preg_match($pattern, $time);
  276. }
  277. }