<?php


namespace app\admin\common;
use think\facade\Cache;
use Youzan\Open\Client;
use Youzan\Open\Token;

class YouZan
{
    private $Client;
    private $Token;
    private  $clientId='deaaebe6484c129787';
    private  $clientSecret='3c654aa2fbc1b5da788ffdba45fb96f0';
//    private $method='https://open.youzanyun.com/api/';
    private $apiVersion='3.0.0';
    private $authorityId='109334129';
    private $methodlist=[
        "youzan.item.update.listing",//上架商品
        "youzan.item.incremental.update",//增量更新商品信息接口
        "youzan.retail.open.spu.update",//更新单个商品库商品信息
        "youzan.retail.open.sku.update.price",//更新商品价格
        "youzan.item.sku.batch.update",//批量更新sku信息
        "youzan.item.delete",//删除商品
        "youzan.item.search",//分页查询微商城销售中和已售罄商品列表
        "youzan.item.update",//更新商品信息
        "youzan.item.add",//商品新建
        "youzan.itemcategories.get",//商品类目二维列表
        "youzan.item.detail.get",//单个商品明细
        "youzan.item.full.sku.update",//商品sku信息全量更新,
        "youzan.item.update.delisting",//商品下架,
        "youzan.item.sku.update",//更新sku信息微商城单店
        "youzan.retail.open.category.create",//商品分类信息新增,
        "youzan.retail.open.brand.add",//新建品牌
        "youzan.retail.open.sku.query",//商品查询 sku
        "youzan.items.onsale.get",//获取出售中得商品
        "youzan.retail.open.brand.delete",//品牌删除
        "youzan.retail.open.category.query",//商品分类信息查询
        "youzan.retail.open.brand.querypage"//品牌查询接口
    ];
    public function __construct()
    {
        $this->Token =new Token($this->clientId,$this->clientSecret);
        $this->Client=new Client($this->GetToken());
    }

    public function GetData($method,$params=[]){
        return $this->Client->post($method,$this->apiVersion, $params);
    }
    private function GetToken(){
        $token = Cache::get("YouZanToken");
        if(isset($token)&& $token!=""){
            return  $token['access_token'];
        }
        $token =$this->Token->getSelfAppToken($this->authorityId,["refresh"=>true]);
        Cache::set("YouZanToken",$token,3500);
        return $token['access_token'];
    }


}