<?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=[], $is_cache = true){

        $url = 'https://oa.shopk.com/api/shopk/'.$url;
//        $url = 'http://local.oa.shopk.com/api/shopk/'.$url;

        // 缓存
        $cacheKey = md5($url.':'.json_encode($data));
        if($is_cache){
            //        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']??[];
    }

}