123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- <?php
- namespace App\Service\Wx;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Redis;
- use Illuminate\Support\Facades\Storage;
- /**
- * 公用的公共服务
- * @package App\Models
- */
- class Service
- {
- public $log;
- public $total = 0;
- public function __construct()
- {
- }
- function curlOpen($url, $config = array())
- {
- $arr = array('post' => false,'referer' => $url,'cookie' => '', 'useragent' => 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)', 'timeout' => 100, 'return' => true, 'proxy' => '', 'userpwd' => '', 'nobody' => false,'header'=>array(),'gzip'=>true,'ssl'=>true,'isupfile'=>false,'returnheader'=>false,'request'=>'post');
- $arr = array_merge($arr, $config);
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, $arr['return']);
- curl_setopt($ch, CURLOPT_NOBODY, $arr['nobody']);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_USERAGENT, $arr['useragent']);
- curl_setopt($ch, CURLOPT_REFERER, $arr['referer']);
- curl_setopt($ch, CURLOPT_TIMEOUT, $arr['timeout']);
- curl_setopt($ch, CURLOPT_HEADER, $arr['returnheader']);//��ȡheader
- if($arr['gzip']) curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
- if($arr['ssl'])
- {
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
- }
- if(!empty($arr['cookie']))
- {
- curl_setopt($ch, CURLOPT_COOKIEJAR, $arr['cookie']);
- curl_setopt($ch, CURLOPT_COOKIEFILE, $arr['cookie']);
- }
- if(!empty($arr['proxy']))
- {
- //curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
- curl_setopt ($ch, CURLOPT_PROXY, $arr['proxy']);
- if(!empty($arr['userpwd']))
- {
- curl_setopt($ch,CURLOPT_PROXYUSERPWD,$arr['userpwd']);
- }
- }
- //ip�Ƚ����⣬�ü�ֵ��ʾ
- if(!empty($arr['header']['ip']))
- {
- array_push($arr['header'],'X-FORWARDED-FOR:'.$arr['header']['ip'],'CLIENT-IP:'.$arr['header']['ip']);
- unset($arr['header']['ip']);
- }
- $arr['header'] = array_filter($arr['header']);
- if(!empty($arr['header']))
- {
- curl_setopt($ch, CURLOPT_HTTPHEADER, $arr['header']);
- }
- if($arr['request'] === 'put'){
- curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "PUT");
- curl_setopt($ch, CURLOPT_POSTFIELDS,$arr['post']);
- }elseif($arr['post'] != false)
- {
- curl_setopt($ch, CURLOPT_POST, true);
- if(is_array($arr['post']) && $arr['isupfile'] === false)
- {
- $post = http_build_query($arr['post']);
- }
- else
- {
- $post = $arr['post'];
- }
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
- }
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- //分页共用
- public function limit($db, $columns, $request)
- {
- $per_page = $request['page_size'] ?? 9999;
- $page = $request['page_index'] ?? 1;
- $return = $db->paginate($per_page, $columns, 'page', $page)->toArray();
- $data['total'] = $return['total'];
- $data['data'] = $return['data'];
- return $data;
- }
- //判断是否为空
- protected function isEmpty($data, $word)
- {
- if (isset($data[$word]) && (!empty($data[$word]) || $data[$word] === '0'|| $data[$word] === 0)) return false;
- return true;
- }
- //后台端 某些需要限制请求频率的接口
- public function limitingSendRequestBackg($key,$ttl = 5){
- if($ttl < 5) $ttl = 5;
- // 使用Redis Facade设置,当键名不存在时才设置成功
- if (Redis::setnx($key, 1)) {
- Redis::expire($key, $ttl); //多少秒后过期
- return [true, ''];
- }
- return [false,'操作频繁, 请在 ' . $ttl . '秒后重试'];
- }
- public function delStorageFile($old, $new = [], $dir = "upload_files/"){
- foreach ($old as $value){
- if(! in_array($value, $new)){
- $filename_rep = "/api/uploadFiles/";
- $filename = str_replace($filename_rep, "", $value);
- $filePath = $dir . $filename;
- if (Storage::disk('public')->exists($filePath)) {
- // 文件存在 进行删除操作
- Storage::disk('public')->delete($filePath);
- }
- }
- }
- }
- }
|