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); } } }