Base.php
1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
namespace App\Http\Controllers\V2;
use Illuminate\Support\Facades\Cache;
/**
 * v2 版的功能基础类
 * @author:dc
 * @time 2022/12/28 9:25
 * Class Base
 * @package App\Http\Controllers\V2
 */
class Base {
    /**
     * 构造
     * Base constructor.
     */
    public function __construct()
    {
    }
    /**
     * 获取数据
     * @param $url
     * @param array $data
     * @return array|mixed
     * @author:dc
     * @time 2022/12/28 9:32
     */
    protected final function getData($url,$data=[]){
        $url = 'https://oa.shopk.com/api/shopk/'.$url;
//        $url = 'http://local.oa.shopk.com/api/shopk/'.$url;
        // 缓存
        $cacheKey = md5($url.':'.json_encode($data));
//        if(Cache::has($cacheKey)){
//            return Cache::get($cacheKey);
//        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_TIMEOUT, 30);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        $a = curl_exec($ch);
//        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);
        $a = json_decode($a,true);
        // 缓存1小时
        Cache::set($cacheKey,$a['data']??[],empty($a['data'])?120:3600);
        return $a['data']??[];
    }
}