<?php

namespace app\controller\api\store\product;

use think\App;
use crmeb\basic\BaseController;

class Yunxuan extends BaseController
{
    public $yunxuan_config = [];
    public $url = '';

    public function __construct(App $app)
    {

        parent::__construct($app);

//        KA_ID:10064230 SHARE_ID:185760270950 SECRET:44008bbf1b834c40ba50de9fd8b0caa6 APP_ID:bi887a279d398d47c9 SANDBOX_APP_ID:bid850c35362954588 SANDBOX_SECRET:aa711b1a126a41b78958994d8dad61a2
//APP_KEY:APIDkigoa0AUW83V3plNo185BU8GLCG3DS4o7iad APP_SECRET:4QI20u5mnKkSqtiF9F5L5o2npfvEUyerjet1354v APP_ID:app-47hi0tq1
        //1.0秘钥
        $config['KA_ID']=10064230;
        $config['SHARE_ID']=185760270950;
        $config['SECRET']='44008bbf1b834c40ba50de9fd8b0caa6';
        $config['APP_ID']='bi887a279d398d47c9';
        $config['SANDBOX_APP_ID']='bid850c35362954588';
        $config['SANDBOX_SECRET']='aa711b1a126a41b78958994d8dad61a2';

        //2.1秘钥
        $config['APP_KEY']='APIDkigoa0AUW83V3plNo185BU8GLCG3DS4o7iad';
        $config['APP_SECRET']='4QI20u5mnKkSqtiF9F5L5o2npfvEUyerjet1354v';
        $config['APP_ID_2']='app-47hi0tq1';
        $this->yunxuan_config=$config;

//        $this->url='https://test.zhls.qq.com';//后端 API 测试环境 url：https://test.zhls.qq.com/
        $this->url='https://zhls.qq.com';//后端 API 生产环境 url：https://zhls.qq.com/

    }

    //1.获取类目接口
    public function lst(){
        $data=$this->category_list();



        $request['request_id']=rand(1,999999).time().rand(1,999999);
        $request['page_index']=1;
        $request['page_size']=60;
        $arr = $this->curl('/cps/sku/list',$request);

//        dump($data);
//       die;
//        return app('json')->success('ok', $arr['data']);
        return app('json')->success('ok', $data['data']);
    }

    /**
     * @param int $parent_id
     * @param int $page_index
     * @param int $page_size
     * @return mixed|string
     * 获取云选联盟获取类目列表
     */
    public function category_list($parent_id=0,$page_index=1,$page_size=60,$level=1){
        $request['request_id']=rand(1,999999).time().rand(1,999999);
        $request['page_index']=$page_index;
        $request['page_size']=$page_size;
        $request['parent_id']=$parent_id;
        $arr = $this->curl('/cps/category/list',$request);
        if(isset($arr['data']) && $level<3){
            foreach ($arr['data'] as $k=>&$v){
                if($k<1){
                    $level_data=$this->category_list($v['id'],$page_index,$page_size,($level+1));
                    $v['list']=$level_data['data'];
                }
//
//                $request2['request_id']=rand(1,999999).time();
//                $request2['page_index']=1;
//                $request2['page_size']=60;
//                $request2['parent_id']=$v['id'];
//                $v['list']= $this->curl('/cps/category/list',$request2);
            }
        }
        return $arr;
    }

    //通过curl发送post请求带body传参
    public function curl($api, $postFields = null)
    {
        $app_id = $this->yunxuan_config['APP_ID'];
        $app_secret = $this->yunxuan_config['SECRET'];
        $date = strtotime(date('YmdHis'));
        $nonce = rand(1000000, 100000000);
        $str = "app_id=" . $app_id . "&nonce=" . $nonce . "&sign=sha256&timestamp=" . $date;
        $sign = hash_hmac('sha256', $str, $app_secret, false);

        $url=$this->url.$api.'?app_id='.$app_id.'&nonce='.$nonce.'&timestamp='.$date.'&sign=sha256&signature='.$sign;


        $jsonData=json_encode($postFields,256);
        $header = array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($jsonData)
        );

        $curl = curl_init();
        //设置抓取的url
        curl_setopt($curl, CURLOPT_URL, $url);
        //设置头文件的信息作为数据流输出
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
        // 超时设置,以秒为单位
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);
        //body传参
        curl_setopt($curl, CURLOPT_POSTFIELDS, $jsonData);
        // 超时设置，以毫秒为单位
//        curl_setopt($curl, CURLOPT_TIMEOUT_MS, 500);
        // 设置请求头
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        //设置获取的信息以文件流的形式返回，而不是直接输出。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        //执行命令
        $data = curl_exec($curl);
        // 显示错误信息
        if (curl_error($curl)) {
            return curl_error($curl);
        } else {
            curl_close($curl);
            return json_decode($data, true);
        }
    }
}