<?php
namespace DtApp\ThinkLibrary\service\taobao;
use DtApp\ThinkLibrary\exception\DtaException;
use DtApp\ThinkLibrary\facade\Times;
use DtApp\ThinkLibrary\Service;
use think\exception\HttpException;
class TbkService extends Service
{
private $sandbox = false;
private $app_key, $app_secret = "";
private $method = '';
private $sign_method = "md5";
private $param;
private $format = "json";
private $v = "2.0";
private $output;
private $protocol = 'http';
public function setProtocol($protocol = 'http'): self
{
$this->protocol = $protocol;
return $this;
}
public function sandbox(): self
{
$this->sandbox = true;
return $this;
}
public function appKey(string $appKey): self
{
$this->app_key = $appKey;
return $this;
}
public function appSecret(string $appSecret): self
{
$this->app_secret = $appSecret;
return $this;
}
public function signMethod(string $signMethod): self
{
$this->sign_method = $signMethod;
return $this;
}
public function param(array $param): self
{
$this->param = $param;
return $this;
}
private function getConfig(): self
{
$this->app_key = config('dtapp.taobao.tbk.app_key');
$this->app_secret = config('dtapp.taobao.tbk.app_secret');
return $this;
}
public function orderDetailsGet(): self
{
$this->method = 'taobao.tbk.order.details.get';
return $this;
}
public function scOrderDetailsGet(): self
{
$this->method = 'taobao.tbk.sc.order.details.get';
return $this;
}
public function scTpwdConvert(): self
{
$this->method = 'taobao.tbk.sc.tpwd.convert';
return $this;
}
public function scRelationRefund(): self
{
$this->method = 'taobao.tbk.sc.relation.refund';
return $this;
}
public function scShopConvert(): self
{
$this->method = 'taobao.tbk.sc.shop.convert';
return $this;
}
public function jzfConvert(): self
{
$this->method = 'taobao.tbk.jzf.convert';
return $this;
}
public function relationRefund(): self
{
$this->method = 'taobao.tbk.relation.refund';
return $this;
}
public function scVegasTljCreate(): self
{
$this->method = 'taobao.tbk.sc.vegas.tlj.create';
return $this;
}
public function itemRuleGet(): self
{
$this->method = 'qimen.taobao.tbk.item.rule.get';
return $this;
}
public function dgPunishOrderGet(): self
{
$this->method = 'taobao.tbk.dg.punish.order.get';
return $this;
}
public function tpwdParse(): self
{
$this->method = 'taobao.tbk.tpwd.parse';
return $this;
}
public function dgNewUserOrderGet(): self
{
$this->method = 'taobao.tbk.dg.newuser.order.get';
return $this;
}
public function scNewuserOrderGet(): self
{
$this->method = 'taobao.tbk.sc.newuser.order.get';
return $this;
}
public function dgNewUserOrderSum(): self
{
$this->method = 'taobao.tbk.dg.newuser.order.sum';
return $this;
}
public function dgVegasSendReport(): self
{
$this->method = 'taobao.tbk.dg.vegas.send.report';
return $this;
}
public function activityInfoGet(): self
{
$this->method = 'taobao.tbk.activity.info.get';
return $this;
}
public function scActivityInfoGet(): self
{
$this->method = 'taobao.tbk.sc.activity.info.get';
return $this;
}
public function textTpwdCreate(): self
{
$this->method = 'taobao.tbk.text.tpwd.create';
return $this;
}
public function activityLinkGet(): self
{
$this->method = 'taobao.tbk.activitylink.get';
return $this;
}
public function tpWdCreate(): self
{
$this->method = 'taobao.tbk.tpwd.create';
return $this;
}
public function spreadGet(): self
{
$this->method = 'taobao.tbk.spread.get';
return $this;
}
public function itemsSearch(): self
{
$this->method = 'taobao.ju.items.search';
return $this;
}
public function juTqgGet(): self
{
$this->method = 'taobao.tbk.ju.tqg.get';
return $this;
}
public function dgVegasTljCreate(): self
{
$this->method = 'taobao.tbk.dg.vegas.tlj.create';
return $this;
}
public function lightshopTbpswdParse(): self
{
$this->method = 'taobao.tbk.lightshop.tbpswd.parse';
return $this;
}
public function dgVegasTljInstanceReport(): self
{
$this->method = 'taobao.tbk.dg.vegas.tlj.instance.report';
return $this;
}
public function scGroupchatMessageSend(): self
{
$this->method = 'taobao.tbk.sc.groupchat.message.send';
return $this;
}
public function scGroupchatCreate(): self
{
$this->method = 'taobao.tbk.sc.groupchat.create';
return $this;
}
public function scGroupchatGet(): self
{
$this->method = 'taobao.tbk.sc.groupchat.get';
return $this;
}
public function tbinfoGet(): self
{
$this->method = 'taobao.tbk.tbinfo.get';
return $this;
}
public function tbkinfoGet(): self
{
$this->method = 'taobao.tbk.tbkinfo.get';
return $this;
}
public function scInvIteCodeGet(): self
{
$this->method = 'taobao.tbk.sc.invitecode.get';
return $this;
}
public function scPublisherInfoGet(): self
{
$this->method = 'taobao.tbk.sc.publisher.info.get';
return $this;
}
public function scPublisherInfoSave(): self
{
$this->method = 'taobao.tbk.sc.publisher.info.save';
return $this;
}
public function itemInfoGet(): self
{
$this->method = 'taobao.tbk.item.info.get';
return $this;
}
public function couponGet(): self
{
$this->method = 'taobao.tbk.coupon.get';
return $this;
}
public function dgMaterialOptional(): self
{
$this->method = 'taobao.tbk.dg.material.optional';
return $this;
}
public function shopGet(): self
{
$this->method = 'taobao.tbk.shop.get';
return $this;
}
public function dgOpTiUsMaterial(): self
{
$this->method = 'taobao.tbk.dg.optimus.material';
return $this;
}
public function contentGet(): self
{
$this->method = 'taobao.tbk.content.get';
return $this;
}
public function contentEffectGet(): self
{
$this->method = 'taobao.tbk.content.effect.get';
return $this;
}
public function itemWordGet(): self
{
$this->method = 'taobao.tbk.item.word.get';
return $this;
}
public function itemConvert(): self
{
$this->method = 'taobao.tbk.item.convert';
return $this;
}
public function itemClickExtract(): self
{
$this->method = 'taobao.tbk.item.click.extract';
return $this;
}
public function itemRecommendGet(): self
{
$this->method = 'taobao.tbk.item.recommend.get';
return $this;
}
public function shopRecommendGet(): self
{
$this->method = 'taobao.tbk.shop.recommend.get';
return $this;
}
public function uaTmFavoritesItemGet(): self
{
$this->method = 'taobao.tbk.uatm.favorites.item.get';
return $this;
}
public function uaTmFavoritesGet(): self
{
$this->method = 'taobao.tbk.uatm.favorites.get';
return $this;
}
public function scActivityLinkToolGet(): self
{
$this->method = 'taobao.tbk.sc.activitylink.toolget';
return $this;
}
public function scPunishOrderGet(): self
{
$this->method = 'taobao.tbk.sc.punish.order.get';
return $this;
}
public function adZoneCreate(): self
{
$this->method = 'taobao.tbk.adzone.create';
return $this;
}
public function tpwdMixCreate(): self
{
$this->method = 'taobao.tbk.tpwd.mix.create';
return $this;
}
public function traceBtocAddtrace(): self
{
$this->method = 'taobao.tbk.trace.btoc.addtrace';
return $this;
}
public function traceLogininfoAdd(): self
{
$this->method = 'taobao.tbk.trace.logininfo.add';
return $this;
}
public function traceShopitemAddtrace(): self
{
$this->method = 'taobao.tbk.trace.shopitem.addtrace';
return $this;
}
public function itemShareConvert(): self
{
$this->method = 'taobao.tbk.item.share.convert';
return $this;
}
public function shopConvert(): self
{
$this->method = 'taobao.tbk.shop.convert';
return $this;
}
public function shopShareConvert(): self
{
$this->method = 'taobao.tbk.shop.share.convert';
return $this;
}
public function rebateAuthGet(): self
{
$this->method = 'taobao.tbk.rebate.auth.get';
return $this;
}
public function rebateOrderGet(): self
{
$this->method = 'taobao.tbk.rebate.order.get';
return $this;
}
public function itemidCouponGet(): self
{
$this->method = 'taobao.tbk.itemid.coupon.get';
return $this;
}
public function dataReport(): self
{
$this->method = 'taobao.tbk.data.report';
return $this;
}
public function couponConvert(): self
{
$this->method = 'taobao.tbk.coupon.convert';
return $this;
}
public function tpwdShareConvert(): self
{
$this->method = 'taobao.tbk.tpwd.share.convert';
return $this;
}
public function tpwdConvert(): self
{
$this->method = 'taobao.tbk.tpwd.convert';
return $this;
}
public function scAdzoneCreate(): self
{
$this->method = 'taobao.tbk.sc.adzone.create';
return $this;
}
public function scOptimusMaterial(): self
{
$this->method = 'taobao.tbk.sc.optimus.material';
return $this;
}
public function scMaterialOptional(): self
{
$this->method = 'taobao.tbk.sc.material.optional';
return $this;
}
public function scNewuserOrderSum(): self
{
$this->method = 'taobao.tbk.sc.newuser.order.sum';
return $this;
}
public function setMethod($method = ''): self
{
$this->method = $method;
return $this;
}
public function toArray()
{
if (!extension_loaded("curl")) {
throw new HttpException(404, '请开启curl模块!');
}
$this->format = "json";
if (empty($this->app_key)) {
$this->getConfig();
}
if (empty($this->app_key)) {
throw new DtaException('请检查app_key参数');
}
if (empty($this->method)) {
throw new DtaException('请检查method参数');
}
$this->param['app_key'] = $this->app_key;
$this->param['method'] = $this->method;
$this->param['format'] = $this->format;
$this->param['v'] = $this->v;
$this->param['sign_method'] = $this->sign_method;
$this->param['timestamp'] = Times::getData();
$this->http();
if (isset($this->output['error_response'])) {
if (is_array($this->output)) {
return $this->output;
}
if (is_object($this->output)) {
$this->output = json_encode($this->output, JSON_UNESCAPED_UNICODE);
}
return json_decode($this->output, true);
}
if (is_array($this->output)) {
return $this->output;
}
if (is_object($this->output)) {
$this->output = json_encode($this->output, JSON_UNESCAPED_UNICODE);
}
$this->output = json_decode($this->output, true);
return $this->output;
}
public function toXml()
{
if (!extension_loaded("curl")) {
throw new HttpException('请开启curl模块!', E_USER_DEPRECATED);
}
$this->format = "xml";
$this->http();
return $this->output;
}
private function http(): void
{
$sign = $this->createSign();
$strParam = $this->createStrParam();
$strParam .= 'sign=' . $sign;
if ($this->protocol === 'http') {
if (empty($this->sandbox)) {
$url = 'http://gw.api.taobao.com/router/rest?' . $strParam;
} else {
$url = 'http://gw.api.tbsandbox.com/router/rest?' . $strParam;
}
}
if ($this->protocol === 'https') {
if (empty($this->sandbox)) {
$url = 'https://eco.taobao.com/router/rest?' . $strParam;
} else {
$url = 'https://gw.api.tbsandbox.com/router/rest?' . $strParam;
}
}
$result = file_get_contents($url);
$result = json_decode($result, true);
$this->output = $result;
}
private function createSign(): string
{
if (empty($this->app_secret)) {
$this->getConfig();
}
if (empty($this->app_secret)) {
throw new DtaException('请检查app_secret参数');
}
$sign = $this->app_secret;
ksort($this->param);
foreach ($this->param as $key => $val) {
if ($key !== '' && $val !== '') {
$sign .= $key . $val;
}
}
$sign .= $this->app_secret;
$sign = strtoupper(md5($sign));
return $sign;
}
private function createStrParam(): string
{
$strParam = '';
foreach ($this->param as $key => $val) {
if ($key !== '' && $val !== '') {
$strParam .= $key . '=' . urlencode($val) . '&';
}
}
return $strParam;
}
public function getActivityMaterialIdList(): array
{
return [
[
'name' => '口碑',
'list' => [
[
'name' => '口碑主会场活动(2.3%佣金起)',
'material_id' => 1583739244161
],
[
'name' => '生活服务分会场活动(2.3%佣金起)',
'material_id' => 1583739244162
]
]
],
[
'name' => '饿了么',
'list' => [
[
'name' => '聚合页(6%佣金起)',
'material_id' => 1571715733668
],
[
'name' => '新零售(4%佣金起)',
'material_id' => 1585018034441
],
[
'name' => '餐饮',
'material_id' => 1579491209717
],
]
],
[
'name' => '卡券(饭票)',
'list' => [
[
'name' => '饿了么卡券(1元以下商品)',
'material_id' => 32469
],
[
'name' => '饿了么卡券投放全网商品库',
'material_id' => 32470
],
[
'name' => '饿了么卡券(5折以下)',
'material_id' => 32603
],
[
'name' => '饿了么头部全国KA商品库',
'material_id' => 32663
],
[
'name' => '饿了么卡券招商爆品库',
'material_id' => 32738
],
]
],
];
}
public function getMaterialIdList(): array
{
return [
[
'name' => '相似推荐',
'list' => [
[
'name' => '相似推荐',
'material_id' => 13256
]
]
],
[
'name' => '官方推荐',
'list' => [
[
'name' => '聚划算满减满折',
'material_id' => 32366
],
[
'name' => '猫超满减满折',
'material_id' => 27160
]
]
],
[
'name' => '猜你喜欢',
'list' => [
[
'name' => '含全部商品',
'material_id' => 6708
],
[
'name' => '营销商品库商品(此为具备“私域用户管理-会员运营管理功能”的媒体专用)',
'material_id' => 28017
]
]
],
[
'name' => '好券直播',
'list' => [
[
'name' => '综合',
'material_id' => 3756
],
[
'name' => '女装',
'material_id' => 3767
],
[
'name' => '家居家装',
'material_id' => 3758
],
[
'name' => '数码家电',
'material_id' => 3759
],
[
'name' => '鞋包配饰',
'material_id' => 3762
],
[
'name' => '美妆个护',
'material_id' => 3763
],
[
'name' => '男装',
'material_id' => 3764
],
[
'name' => '内衣',
'material_id' => 3765
],
[
'name' => '母婴',
'material_id' => 3760
],
[
'name' => '食品',
'material_id' => 3761
],
[
'name' => '运动户外',
'material_id' => 3766
]
]
],
[
'name' => '实时热销榜',
'list' => [
[
'name' => '综合',
'material_id' => 28026
],
[
'name' => '大服饰',
'material_id' => 28029
],
[
'name' => '大快消',
'material_id' => 28027
],
[
'name' => '电器美家',
'material_id' => 28028
]
]
],
[
'name' => '本地化生活',
'list' => [
[
'name' => '今日爆款(综合类目)',
'material_id' => 30443
],
[
'name' => '淘票票(电影代金券)',
'material_id' => 19812
],
[
'name' => '大麦网(演出/演唱会/剧目/会展)',
'material_id' => 25378
],
[
'name' => '优酷会员(视频年卡)',
'material_id' => 28636
],
[
'name' => '有声内容(喜马拉雅年卡,儿童节目等)',
'material_id' => 29105
],
[
'name' => '阿里健康(hpv疫苗预约)',
'material_id' => 25885
],
[
'name' => '阿里健康(体检)',
'material_id' => 25886
],
[
'name' => '阿里健康(口腔)',
'material_id' => 25888
],
[
'name' => '阿里健康(基因检测)',
'material_id' => 25890
],
[
'name' => '飞猪(签证)',
'material_id' => 26077
],
[
'name' => '飞猪(酒店)',
'material_id' => 27913
],
[
'name' => '飞猪(自助餐)',
'material_id' => 27914
],
[
'name' => '飞猪(门票)',
'material_id' => 19811
],
[
'name' => '口碑(肯德基/必胜客/麦当劳)',
'material_id' => 19810
],
[
'name' => '口碑(生活服务)',
'material_id' => 28888
],
[
'name' => '天猫无忧购(家政服务)',
'material_id' => 19814
],
[
'name' => '汽车定金(汽车定金)',
'material_id' => 28397
],
]
],
[
'name' => '大额券',
'list' => [
[
'name' => '综合',
'material_id' => 27446
],
[
'name' => '女装',
'material_id' => 27448
],
[
'name' => '食品',
'material_id' => 27451
],
[
'name' => '美妆个护',
'material_id' => 27453
],
[
'name' => '家居家装',
'material_id' => 27798
],
[
'name' => '母婴',
'material_id' => 27454
]
]
],
[
'name' => '高佣榜',
'list' => [
[
'name' => '综合',
'material_id' => 13366
],
[
'name' => '女装',
'material_id' => 13367
],
[
'name' => '家居家装',
'material_id' => 13368
],
[
'name' => '数码家电',
'material_id' => 13369
],
[
'name' => '鞋包配饰',
'material_id' => 13370
],
[
'name' => '美妆个护',
'material_id' => 13371
],
[
'name' => '男装',
'material_id' => 13372
],
[
'name' => '内衣',
'material_id' => 13373
],
[
'name' => '母婴',
'material_id' => 13374
],
[
'name' => '食品',
'material_id' => 13375
],
[
'name' => '运动户外',
'material_id' => 13376
]
]
],
[
'name' => '品牌券',
'list' => [
[
'name' => '综合',
'material_id' => 3786
],
[
'name' => '女装',
'material_id' => 3788
],
[
'name' => '家居家装',
'material_id' => 3792
],
[
'name' => '数码家电',
'material_id' => 3793
],
[
'name' => '鞋包配饰',
'material_id' => 3796
],
[
'name' => '美妆个护',
'material_id' => 3794
],
[
'name' => '男装',
'material_id' => 3790
],
[
'name' => '内衣',
'material_id' => 3787
],
[
'name' => '母婴',
'material_id' => 3789
],
[
'name' => '食品',
'material_id' => 3791
],
[
'name' => '运动户外',
'material_id' => 3795
],
]
],
[
'name' => '猫超优质爆款',
'list' => [
[
'name' => '猫超1元购凑单',
'material_id' => 27162
],
[
'name' => '猫超第二件0元',
'material_id' => 27161
],
[
'name' => '猫超单件满减包邮',
'material_id' => 27160
],
]
],
[
'name' => '聚划算单品爆款',
'list' => [
[
'name' => '开团热卖中',
'material_id' => 31371
],
[
'name' => '预热',
'material_id' => 31370
],
]
],
[
'name' => '天天特卖',
'list' => [
[
'name' => '开团热卖中',
'material_id' => 31362
],
]
],
[
'name' => '母婴主题',
'list' => [
[
'name' => '备孕',
'material_id' => 4040
],
[
'name' => '0至6个月',
'material_id' => 4041
],
[
'name' => '4至6岁',
'material_id' => 4044
],
[
'name' => '7至12个月',
'material_id' => 4042
],
[
'name' => '1至3岁',
'material_id' => 4043
],
[
'name' => '7至12岁',
'material_id' => 4045
],
]
],
[
'name' => '有好货',
'list' => [
[
'name' => '有好货',
'material_id' => 4092
],
]
],
[
'name' => '潮流范',
'list' => [
[
'name' => '潮流范',
'material_id' => 4093
],
]
],
[
'name' => '特惠',
'list' => [
[
'name' => '特惠',
'material_id' => 4094
],
]
],
];
}
}