CommonService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories\Service;
  4. use App\Models\MerchantModel;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Support\Facades\Request;
  7. class CommonService
  8. {
  9. /**
  10. * 获取当前请求的客户端IP
  11. * @return string
  12. */
  13. public function getIp()
  14. {
  15. $headerIp = request()->getHeader('x-forwarded-for');
  16. $headerIp2 = request()->getHeader('x-real-ip');
  17. if ($headerIp2) {
  18. $ip = $headerIp[0];
  19. } elseif (!empty($headerIp)) {
  20. $ip = $headerIp2[0];
  21. } else {
  22. $sever = request()->getServerParams();
  23. $ip = $sever['remote_addr'] ?? '';
  24. }
  25. if (strpos($ip, ',')) {
  26. $temp = explode(',', $ip);
  27. $ip = $ip[0];
  28. $temp = null;
  29. unset($temp);
  30. }
  31. return $ip;
  32. }
  33. /**
  34. * 生成订单流水号、可指定前缀
  35. * @param string $prefix
  36. * @return string
  37. */
  38. public function createOrderSn($prefix = '')
  39. {
  40. return $prefix . date('YmdHis') . mt_rand(1111, 9999);
  41. }
  42. /**
  43. * 检测一个字符串是否为时间格式
  44. * @param string $time
  45. * @return false|int
  46. */
  47. public function isTime($time)
  48. {
  49. $pattern = '/[\d]{4}-[\d]{1,2}-[\d]{1,2}\s[\d]{1,2}:[\d]{1,2}:[\d]{1,2}/';
  50. return preg_match($pattern, $time);
  51. }
  52. /**
  53. * post请求
  54. *
  55. * @param $data
  56. * @param $url
  57. * @return string
  58. * @throws \GuzzleHttp\Exception\GuzzleException
  59. */
  60. public function httpPost($data = [], $url = '', $headers = ['Content-Type' => 'application/json'])
  61. {
  62. $client = new Client();
  63. $params = [
  64. 'headers' => $headers,
  65. 'json' => $data,
  66. ];
  67. try {
  68. $response = $client->request('POST', $url, $params);
  69. $code = $response->getStatusCode();
  70. if ($code == 200) {
  71. return $response->getBody()->getContents();
  72. } else {
  73. logger($response->getBody()->getContents());
  74. return '';
  75. }
  76. } catch (\Exception $e) {
  77. logger($e->getMessage());
  78. }
  79. return '';
  80. }
  81. /**
  82. * get请求
  83. *
  84. * @param $data
  85. * @param $url
  86. * @return array|string
  87. * @throws \GuzzleHttp\Exception\GuzzleException
  88. */
  89. public function httpGet($data = [], $url = '')
  90. {
  91. $client = new Client();
  92. $params = [
  93. 'query' => $data
  94. ];
  95. try {
  96. $response = $client->request('GET', $url, $params);
  97. $code = $response->getStatusCode();
  98. if ($code == 200) {
  99. return $response->getBody()->getContents();
  100. } else {
  101. logger($response->getBody()->getContents());
  102. return '';
  103. }
  104. } catch (\Exception $e) {
  105. logger($e->getMessage());
  106. }
  107. return '';
  108. }
  109. /**
  110. * 清除商户缓存
  111. *
  112. * @param $key
  113. * @param $cacheNoticeUrl
  114. * @return void
  115. * @throws \GuzzleHttp\Exception\GuzzleException
  116. */
  117. public function clearMerchantCache($key, $cacheNoticeUrl)
  118. {
  119. $this->httpPost(['key' => $key], $cacheNoticeUrl);
  120. }
  121. /**
  122. * 批量删除商户缓存
  123. *
  124. * @return void
  125. * @throws \GuzzleHttp\Exception\GuzzleException
  126. */
  127. public function clearAllMerchantCache()
  128. {
  129. $data = MerchantModel::query()->where('status', 1)->get(['key', 'cache_notice_url']);
  130. foreach ($data as $v) {
  131. $this->clearMerchantCache($v->key, $v->cache_notice_url);
  132. }
  133. }
  134. }