Youzan.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\common;
  3. use think\facade\Cache;
  4. class Youzan {
  5. private $Client;
  6. private $clientId;
  7. private $clientSecret;
  8. private $apiVersion='3.0.0';
  9. private $authorityId;
  10. private $methodlist = [
  11. 'item_online' => 'youzan.item.update.listing',//上架商品
  12. 'item_update' => 'youzan.item.update',//更新商品信息接口
  13. 'youzan.retail.open.spu.update',//更新单个商品库商品信息
  14. 'youzan.retail.open.sku.update.price',//更新商品价格
  15. 'youzan.item.sku.batch.update',//批量更新sku信息
  16. 'youzan.item.delete',//删除商品
  17. 'youzan.item.search',//分页查询微商城销售中和已售罄商品列表
  18. 'youzan.item.update',//更新商品信息
  19. 'item_add' => 'youzan.item.add',//商品新建,1.0.1
  20. 'cat_list' => 'youzan.itemcategories.get',//商品类目二维列表(v2版本)
  21. 'cat_v4_list' => 'youzan.category.listchildren',//新版商品类目列表(v4版本)
  22. 'item_detail' => 'youzan.item.detail.get',//单个商品明细
  23. 'youzan.item.full.sku.update',//商品sku信息全量更新,
  24. 'item_delisting' => 'youzan.item.update.delisting',//商品下架,
  25. 'youzan.item.sku.update',//更新sku信息微商城单店
  26. 'youzan.retail.open.category.create',//商品分类信息新增,
  27. 'youzan.retail.open.sku.query',//商品查询 sku
  28. 'youzan.items.onsale.get',//获取出售中得商品
  29. 'youzan.retail.open.brand.delete',//品牌删除
  30. 'category' => 'youzan.retail.open.category.query',//商品分类信息查询
  31. 'brand_list' => 'youzan.retail.open.brand.querypage',//品牌查询接口
  32. 'express_list' => 'youzan.logistics.express.get',//快递公司列表接口
  33. 'regions_list' => 'youzan.regions.get',//区域地名列表接口
  34. 'img_upload' => 'youzan.materials.storage.platform.img.upload',//上传图片接口
  35. 'sku_get' => 'youzan.item.sku.get',//通过商品或者skuId查询规格明细
  36. 'logistics'=>'youzan.logistics.online.confirm',//订单发货接口
  37. 'logistics_update'=>'youzan.logistics.online.update',//修改物流信息
  38. 'querybyorderno'=>'youzan.trade.dc.query.querybyorderno',//发货单查询
  39. 'getUserInfo'=>'youzan.users.info.query',//询买方信息
  40. 'promocode'=>'youzan.ump.promocode.add',// 生成优惠码
  41. 'promocode_delete'=>'youzan.ump.voucheractivity.delete',//优惠码/券删除
  42. 'promocode_query'=>'youzan.ump.voucheractivity.query',//优惠码/券查询
  43. "coupon_search"=>"youzan.ump.voucheractivity.manage.info.search"
  44. ];
  45. //youzan.ump.voucheractivity.query.3.0.2 根据活动查询选项查询活动详情信息
  46. //youzan.ump.voucheractivity.manage.info.search.1.0.1 :查询优惠券/码活动管理信息
  47. //'youzan.ump.voucheractivity.delete',//优惠码/券删除
  48. public function __construct()
  49. {
  50. $this->clientId =env('yz.clientid');
  51. $this->clientSecret=env('yz.clientsecret');
  52. $this->authorityId =env('yz.authorityid');
  53. $this->Client = new \Youzan\Open\Client($this->getToken());
  54. }
  55. public function getToken(){
  56. $cache =Cache::store("redis2");
  57. $token = $cache->get('YouZanToken'.$this->authorityId);
  58. if(!isset($token['access_token'])|| $token['access_token']==''){
  59. $yzToken = new \Youzan\Open\Token($this->clientId,$this->clientSecret);
  60. $token =$yzToken->getSelfAppToken($this->authorityId,['refresh'=>true]);
  61. $cache->set('YouZanToken'.$this->authorityId,$token,(new \DateTime($token['expires']/100))->format("Y-m-d H:i:s"));
  62. }
  63. return $token['access_token'];
  64. }
  65. public function getData($method,$params=[],$version=''){
  66. if($version!=='') $this->apiVersion=$version;
  67. return $this->Client->post($this->methodlist[$method]??$method,$this->apiVersion, $params);
  68. }
  69. }