| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- <?php
- declare(strict_types=1);
- namespace App\Repositories\Service;
- use App\Models\MerchantModel;
- use GuzzleHttp\Client;
- use Illuminate\Support\Facades\Request;
- 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;
- }
- /**
- * 生成订单流水号、可指定前缀
- * @param string $prefix
- * @return string
- */
- public function createOrderSn($prefix = '')
- {
- return $prefix . date('YmdHis') . mt_rand(1111, 9999);
- }
- /**
- * 检测一个字符串是否为时间格式
- * @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);
- }
- /**
- * post请求
- *
- * @param $data
- * @param $url
- * @return string
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function httpPost($data = [], $url = '', $headers = ['Content-Type' => 'application/json'])
- {
- $client = new Client();
- $params = [
- 'headers' => $headers,
- 'json' => $data,
- ];
- try {
- $response = $client->request('POST', $url, $params);
- $code = $response->getStatusCode();
- if ($code == 200) {
- return $response->getBody()->getContents();
- } else {
- logger($response->getBody()->getContents());
- return '';
- }
- } catch (\Exception $e) {
- logger($e->getMessage());
- }
- return '';
- }
- /**
- * get请求
- *
- * @param $data
- * @param $url
- * @return array|string
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function httpGet($data = [], $url = '')
- {
- $client = new Client();
- $params = [
- 'query' => $data
- ];
- try {
- $response = $client->request('GET', $url, $params);
- $code = $response->getStatusCode();
- if ($code == 200) {
- return $response->getBody()->getContents();
- } else {
- logger($response->getBody()->getContents());
- return '';
- }
- } catch (\Exception $e) {
- logger($e->getMessage());
- }
- return '';
- }
- /**
- * 清除商户缓存
- *
- * @param $key
- * @param $cacheNoticeUrl
- * @return void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function clearMerchantCache($key, $cacheNoticeUrl)
- {
- $this->httpPost(['key' => $key], $cacheNoticeUrl);
- }
- /**
- * 批量删除商户缓存
- *
- * @return void
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- public function clearAllMerchantCache()
- {
- $data = MerchantModel::query()->where('status', 1)->get(['key', 'cache_notice_url']);
- foreach ($data as $v) {
- $this->clearMerchantCache($v->key, $v->cache_notice_url);
- }
- }
- }
|