JinBaoService.php 18.3 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php

// +----------------------------------------------------------------------
// | ThinkLibrary 6.0 for ThinkPhP 6.0
// +----------------------------------------------------------------------
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
// +----------------------------------------------------------------------
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
李光春's avatar
李光春 已提交
14 15 16
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
李光春's avatar
李光春 已提交
17 18 19
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

李光春's avatar
李光春 已提交
20
namespace DtApp\ThinkLibrary\service\pinduoduo;
李光春's avatar
李光春 已提交
21

李光春's avatar
李光春 已提交
22
use DtApp\ThinkLibrary\exception\DtaException;
李光春's avatar
李光春 已提交
23
use DtApp\ThinkLibrary\Service;
李光春's avatar
李光春 已提交
24
use think\exception\HttpException;
李光春's avatar
李光春 已提交
25

李光春's avatar
李光春 已提交
26 27 28 29 30
/**
 * 进宝
 * Class JinBaoService
 * @package DtApp\ThinkLibrary\service\PinDuoDuo
 */
李光春's avatar
李光春 已提交
31 32 33 34 35 36 37 38 39 40 41 42
class JinBaoService extends Service
{
    /**
     * 接口地址
     * @var
     */
    private $url = 'http://gw-api.pinduoduo.com/api/router';

    /**
     * API接口名称
     * @var string
     */
李光春's avatar
李光春 已提交
43
    private $type = '', $response = '';
李光春's avatar
李光春 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122

    /**
     * 开放平台分配的clientId
     * @var string
     */
    private $client_id = '';

    /**
     * 开放平台分配的clientSecret
     * @var string
     */
    private $client_secret = '';

    /**
     * 通过code获取的access_token(无需授权的接口,该字段不参与sign签名运算)
     * @var string
     */
    private $access_token = '';

    /**
     * 响应格式,即返回数据的格式,JSON或者XML(二选一),默认JSON,注意是大写
     * @var string
     */
    private $data_type = 'JSON';

    /**
     * API协议版本号,默认为V1,可不填
     * @var string
     */
    private $version = 'v1';

    /**
     * 需要发送的的参数
     * @var
     */
    private $param;

    /**
     * 响应内容
     * @var
     */
    private $output;

    /*
     * 配置开放平台分配的clientId
     */
    public function clientId(string $clientId)
    {
        $this->client_id = $clientId;
        return $this;
    }

    /**
     * 配置开放平台分配的clientSecret
     * @param string $clientSecret
     * @return $this
     */
    public function clientSecret(string $clientSecret)
    {
        $this->client_secret = $clientSecret;
        return $this;
    }

    /**
     * 响应格式,即返回数据的格式,JSON或者XML(二选一),默认JSON,注意是大写
     * @param string $dataType
     * @return $this
     */
    public function dataType(string $dataType)
    {
        $this->data_type = $dataType;
        return $this;
    }

    /**
     * 请求参数
     * @param array $param
     * @return $this
     */
李光春's avatar
李光春 已提交
123
    public function param(array $param)
李光春's avatar
李光春 已提交
124
    {
李光春's avatar
李光春 已提交
125
        $this->param = $param;
李光春's avatar
李光春 已提交
126 127 128 129 130
        return $this;
    }

    /**
     * 网络请求
李光春's avatar
李光春 已提交
131 132
     * @return JinBaoService
     * @throws DtaException
李光春's avatar
李光春 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
     */
    private function http()
    {
        //生成签名
        $sign = $this->createSign();
        //组织参数
        $strParam = $this->createStrParam();
        $strParam .= 'sign=' . $sign;
        //访问服务
        $url = "{$this->url}?" . $strParam;
        $result = file_get_contents($url);
        $result = json_decode($result, true);
        $this->output = $result;
        return $this;
    }

149 150 151 152 153 154
    /**
     * 获取配置信息
     * @return $this
     */
    private function getConfig()
    {
155 156
        $this->client_id = $this->app->config->get('dtapp.pinduoduo.jinbao.client_id');
        $this->client_secret = $this->app->config->get('dtapp.pinduoduo.jinbao.client_secret');
157 158 159
        return $this;
    }

李光春's avatar
李光春 已提交
160 161 162 163 164
    /**
     * 获取商品信息 - 多多进宝商品查询
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.search
     * @return array|mixed
     */
李光春's avatar
李光春 已提交
165
    public function goodsSearch()
李光春's avatar
李光春 已提交
166 167
    {
        $this->type = 'pdd.ddk.goods.search';
李光春's avatar
李光春 已提交
168
        $this->response = 'goods_search_response';
李光春's avatar
李光春 已提交
169 170 171 172 173 174 175 176 177 178 179
        return $this;
    }

    /**
     * 新增推广位 - 创建多多进宝推广位
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.pid.generate
     * @return array|mixed
     */
    public function goodsPidGenerate()
    {
        $this->type = 'pdd.ddk.goods.pid.generate';
李光春's avatar
李光春 已提交
180
        $this->response = 'p_id_generate_response';
李光春's avatar
李光春 已提交
181 182 183 184 185 186 187 188 189 190 191
        return $this;
    }

    /**
     * 管理推广位 - 查询已经生成的推广位信息
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.pid.query
     * @return array|mixed
     */
    public function goodsPidQuery()
    {
        $this->type = 'pdd.ddk.goods.pid.query';
李光春's avatar
李光春 已提交
192
        $this->response = 'p_id_query_response';
李光春's avatar
李光春 已提交
193 194 195 196 197 198 199 200 201 202 203
        return $this;
    }

    /**
     * CPS订单数据 - 查询订单详情
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.order.detail.get
     * @return array|mixed
     */
    public function orderDetailGet()
    {
        $this->type = 'pdd.ddk.order.detail.get';
李光春's avatar
李光春 已提交
204
        $this->response = 'order_detail_response';
李光春's avatar
李光春 已提交
205 206 207 208 209 210 211 212 213 214 215
        return $this;
    }

    /**
     * CPS订单数据 - 最后更新时间段增量同步推广订单信息
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.order.list.increment.get
     * @return array|mixed
     */
    public function orderListIncrementGet()
    {
        $this->type = 'pdd.ddk.order.list.increment.get';
李光春's avatar
李光春 已提交
216
        $this->response = 'order_list_get_response';
李光春's avatar
李光春 已提交
217 218 219 220 221 222 223 224 225 226 227
        return $this;
    }

    /**
     * CPS订单数据 - 用时间段查询推广订单接口
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.order.list.range.get
     * @return array|mixed
     */
    public function orderListRangeGet()
    {
        $this->type = 'pdd.ddk.order.list.range.get';
李光春's avatar
李光春 已提交
228
        $this->response = 'order_list_get_response';
李光春's avatar
李光春 已提交
229 230 231 232 233
        return $this;
    }

    /**
     * CPA效果数据 - 查询CPA数据
李光春's avatar
李光春 已提交
234
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.finance.cpa.query
李光春's avatar
李光春 已提交
235 236 237 238 239
     * @return array|mixed
     */
    public function financeCpaQuery()
    {
        $this->type = 'pdd.ddk.finance.cpa.query';
李光春's avatar
李光春 已提交
240
        $this->response = 'finance_cpa_query_response';
李光春's avatar
李光春 已提交
241 242 243 244 245 246 247 248 249 250 251
        return $this;
    }

    /**
     * 单品推广- 多多进宝推广链接生成
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.promotion.url.generate
     * @return array|mixed
     */
    public function goodsPromotionUrlGenerate()
    {
        $this->type = 'pdd.ddk.goods.promotion.url.generate';
李光春's avatar
李光春 已提交
252
        $this->response = 'goods_promotion_url_generate_response';
李光春's avatar
李光春 已提交
253 254 255 256 257 258 259 260 261 262 263
        return $this;
    }

    /**
     * 单品推广- 多多客生成单品推广小程序二维码url
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.weapp.qrcode.url.gen
     * @return array|mixed
     */
    public function weAppQrcodeUrlGen()
    {
        $this->type = 'pdd.ddk.weapp.qrcode.url.gen';
李光春's avatar
李光春 已提交
264
        $this->response = 'weapp_qrcode_generate_response';
李光春's avatar
李光春 已提交
265 266 267 268 269 270 271 272 273 274 275
        return $this;
    }

    /**
     * 单品推广- 多多进宝转链接口
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.zs.unit.url.gen
     * @return array|mixed
     */
    public function goodsZsUitUrlGen()
    {
        $this->type = 'pdd.ddk.goods.zs.unit.url.gen';
李光春's avatar
李光春 已提交
276
        $this->response = 'goods_zs_unit_generate_response';
李光春's avatar
李光春 已提交
277 278 279 280 281 282 283 284 285 286 287
        return $this;
    }

    /**
     * 活动转链 - 生成多多进宝频道推广
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.resource.url.gen
     * @return array|mixed
     */
    public function resourceUrlGen()
    {
        $this->type = 'pdd.ddk.resource.url.gen';
李光春's avatar
李光春 已提交
288
        $this->response = 'resource_url_response';
李光春's avatar
李光春 已提交
289 290 291 292 293 294 295 296 297 298 299
        return $this;
    }

    /**
     * 活动转链 - 多多进宝主题推广链接生成
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.theme.prom.url.generate
     * @return array|mixed
     */
    public function themePromUrlGenerate()
    {
        $this->type = 'pdd.ddk.theme.prom.url.generate';
李光春's avatar
李光春 已提交
300
        $this->response = 'theme_promotion_url_generate_response';
李光春's avatar
李光春 已提交
301 302 303 304 305 306 307 308 309 310 311
        return $this;
    }

    /**
     * 店铺推广 - 多多客生成店铺推广链接
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.mall.url.gen
     * @return array|mixed
     */
    public function mallUrlGen()
    {
        $this->type = 'pdd.ddk.mall.url.gen';
李光春's avatar
李光春 已提交
312
        $this->response = 'mall_coupon_generate_url_response';
李光春's avatar
李光春 已提交
313 314 315 316 317 318 319 320 321 322 323
        return $this;
    }

    /**
     * 营销工具 - 生成营销工具推广链接
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.rp.prom.url.generate
     * @return array|mixed
     */
    public function rpPromUrlGenerate()
    {
        $this->type = 'pdd.ddk.rp.prom.url.generate';
李光春's avatar
李光春 已提交
324
        $this->response = 'rp_promotion_url_generate_response';
李光春's avatar
李光春 已提交
325 326 327 328 329 330 331 332 333 334 335
        return $this;
    }

    /**
     * 获取商品信息 - 多多进宝商品详情查询
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.detail
     * @return array|mixed
     */
    public function goodsDetail()
    {
        $this->type = 'pdd.ddk.goods.detail';
李光春's avatar
李光春 已提交
336
        $this->response = 'goods_detail_response';
李光春's avatar
李光春 已提交
337 338 339 340 341 342 343 344 345 346 347
        return $this;
    }

    /**
     * 获取商品信息 - 查询商品的推广计划
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.unit.query
     * @return array|mixed
     */
    public function goodsUnitQuery()
    {
        $this->type = 'pdd.ddk.goods.unit.query';
李光春's avatar
李光春 已提交
348
        $this->response = 'ddk_goods_unit_query_response';
李光春's avatar
李光春 已提交
349 350 351 352 353 354 355 356 357 358 359
        return $this;
    }

    /**
     * 商品&店铺检索 - 获取商品基本信息接口
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.basic.info.get
     * @return array|mixed
     */
    public function goodsBasicInfoGet()
    {
        $this->type = 'pdd.ddk.goods.basic.info.get';
李光春's avatar
李光春 已提交
360
        $this->response = 'goods_basic_detail_response';
李光春's avatar
李光春 已提交
361 362 363 364 365 366 367 368 369 370 371
        return $this;
    }

    /**
     * 商品&店铺检索 - 查询优惠券信息
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.coupon.info.query
     * @return array|mixed
     */
    public function couponInfoQuery()
    {
        $this->type = 'pdd.ddk.coupon.info.query';
李光春's avatar
李光春 已提交
372
        $this->response = 'ddk_coupon_info_query_response';
李光春's avatar
李光春 已提交
373 374 375 376 377 378 379 380 381 382 383
        return $this;
    }

    /**
     * 商品&店铺检索 - 查询店铺商品
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.mall.goods.list.get
     * @return array|mixed
     */
    public function goodsListGet()
    {
        $this->type = 'pdd.ddk.mall.goods.list.get';
李光春's avatar
李光春 已提交
384
        $this->response = 'goods_info_list_response';
李光春's avatar
李光春 已提交
385 386 387 388 389 390 391 392 393 394 395
        return $this;
    }

    /**
     * 多多客获取爆款排行商品接口
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.top.goods.list.query
     * @return array|mixed
     */
    public function topGoodsListQuery()
    {
        $this->type = 'pdd.ddk.top.goods.list.query';
李光春's avatar
李光春 已提交
396
        $this->response = 'top_goods_list_get_response';
李光春's avatar
李光春 已提交
397 398 399 400 401 402 403 404 405 406 407
        return $this;
    }

    /**
     * 爆品推荐 - 多多进宝商品推荐API
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.recommend.get
     * @return array|mixed
     */
    public function goodsRecommendGet()
    {
        $this->type = 'pdd.ddk.goods.recommend.get';
李光春's avatar
李光春 已提交
408
        $this->response = 'goods_basic_detail_response';
李光春's avatar
李光春 已提交
409 410 411 412 413 414 415 416 417 418 419
        return $this;
    }

    /**
     * 爆品推荐 - 多多进宝主题列表查询
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.theme.list.get
     * @return array|mixed
     */
    public function themeListGet()
    {
        $this->type = 'pdd.ddk.theme.list.get';
李光春's avatar
李光春 已提交
420
        $this->response = 'theme_list_get_response';
李光春's avatar
李光春 已提交
421 422 423 424 425 426 427 428 429 430 431
        return $this;
    }

    /**
     * 活动选品库 - 多多进宝主题商品查询
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.theme.goods.search
     * @return array|mixed
     */
    public function themeGoodsSearch()
    {
        $this->type = 'pdd.ddk.theme.goods.search';
李光春's avatar
李光春 已提交
432
        $this->response = 'theme_list_get_response';
李光春's avatar
李光春 已提交
433 434 435 436 437 438
        return $this;
    }

    /**
     * 返回数组数据
     * @return array|mixed
李光春's avatar
李光春 已提交
439
     * @throws DtaException
李光春's avatar
李光春 已提交
440 441 442 443
     */
    public function toArray()
    {
        //首先检测是否支持curl
李光春's avatar
李光春 已提交
444 445 446 447 448 449 450 451 452
        if (!extension_loaded("curl")) {
            throw new HttpException(404, '请开启curl模块!');
        }
        if (empty($this->client_id)) {
            $this->getConfig();
        }
        if (empty($this->client_id)) {
            throw new DtaException('请检查client_id参数');
        }
李光春's avatar
李光春 已提交
453 454 455 456 457 458
        $this->param['type'] = $this->type;
        $this->param['client_id'] = $this->client_id;
        $this->param['timestamp'] = time();
        $this->param['data_type'] = $this->data_type;
        $this->param['version'] = $this->version;
        $this->http();
李光春's avatar
李光春 已提交
459 460
        if (isset($this->output['error_response'])) {
            // 错误
李光春's avatar
李光春 已提交
461 462 463 464 465 466
            if (is_array($this->output)) {
                return $this->output;
            }
            if (is_object($this->output)) {
                return $this->object2array($this->output);
            }
李光春's avatar
李光春 已提交
467 468 469 470
            return json_decode($this->output, true);
        } else {
            // 正常
            if (is_array($this->output)) {
李光春's avatar
李光春 已提交
471 472 473
                if (isset($this->output["{$this->response}"])) {
                    return $this->output["{$this->response}"];
                }
李光春's avatar
李光春 已提交
474 475 476 477
                return $this->output;
            }
            if (is_object($this->output)) {
                $this->output = $this->object2array($this->output);
李光春's avatar
李光春 已提交
478 479 480
                if (isset($this->output["$this->response"])) {
                    return $this->output["$this->response"];
                }
李光春's avatar
李光春 已提交
481 482 483
                return $this->output;
            }
            $this->output = json_decode($this->output, true);
李光春's avatar
李光春 已提交
484 485 486
            if (isset($this->output["$this->response"])) {
                return $this->output["$this->response"];
            }
李光春's avatar
李光春 已提交
487 488
            return $this->output;
        }
李光春's avatar
李光春 已提交
489 490 491 492 493 494 495 496
    }

    /**
     * @param $object
     * @return array
     */
    private function object2array(&$object)
    {
李光春's avatar
李光春 已提交
497 498 499 500 501
        if (is_object($object)) {
            $arr = (array)($object);
        } else {
            $arr = &$object;
        }
李光春's avatar
李光春 已提交
502 503 504 505 506
        if (is_array($arr)) {
            foreach ($arr as $varName => $varValue) {
                $arr[$varName] = $this->object2array($varValue);
            }
        }
李光春's avatar
李光春 已提交
507 508 509 510 511 512
        return $arr;
    }

    /**
     * 签名
     * @return string
李光春's avatar
李光春 已提交
513
     * @throws DtaException
李光春's avatar
李光春 已提交
514 515 516
     */
    private function createSign()
    {
李光春's avatar
李光春 已提交
517 518 519 520 521 522
        if (empty($this->client_secret)) {
            $this->getConfig();
        }
        if (empty($this->client_secret)) {
            throw new DtaException('请检查client_secret参数}');
        }
李光春's avatar
李光春 已提交
523 524
        $sign = $this->client_secret;
        ksort($this->param);
李光春's avatar
李光春 已提交
525 526 527 528 529
        foreach ($this->param as $key => $val) {
            if ($key != '' && $val != '') {
                $sign .= $key . $val;
            }
        }
李光春's avatar
李光春 已提交
530 531 532 533 534 535 536 537 538 539 540 541
        $sign .= $this->client_secret;
        $sign = strtoupper(md5($sign));
        return $sign;
    }

    /**
     * 组参
     * @return string
     */
    private function createStrParam()
    {
        $strParam = '';
李光春's avatar
李光春 已提交
542 543 544 545 546
        foreach ($this->param as $key => $val) {
            if ($key != '' && $val != '' && !is_array($val)) {
                $strParam .= $key . '=' . urlencode($val) . '&';
            }
        }
李光春's avatar
李光春 已提交
547 548
        return $strParam;
    }
李光春's avatar
李光春 已提交
549 550

    /**
李光春's avatar
李光春 已提交
551
     * 获取频道ID
李光春's avatar
李光春 已提交
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
     * @return array[]
     */
    public function getChannelTypeList()
    {
        return [
            [
                // https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.recommend.get
                'name' => '商品推荐',
                'list' => [
                    [
                        'name' => '1.9包邮',
                        'channel_type' => 0
                    ],
                    [
                        'name' => '今日爆款',
                        'channel_type' => 1
                    ],
                    [
                        'name' => '品牌清仓',
                        'channel_type' => 2
                    ],
                    [
                        'name' => '相似商品推荐',
                        'channel_type' => 3
                    ],
                    [
                        'name' => '猜你喜欢',
                        'channel_type' => 4
                    ],
                    [
                        'name' => '实时热销',
                        'channel_type' => 5
                    ],
                    [
                        'name' => '实时收益',
                        'channel_type' => 6
                    ],
                    [
                        'name' => '今日畅销',
                        'channel_type' => 7
                    ],
                    [
                        'name' => '高佣榜单',
                        'channel_type' => 8
                    ],
                ]
            ],
        ];
    }
李光春's avatar
李光春 已提交
601 602 603 604 605 606 607 608 609 610 611 612 613 614

    /**
     * 获取频道来源ID
     * @return array[]
     */
    public function getResourceTypeList()
    {
        return [
            [
                // https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.resource.url.gen
                'name' => '频道推广',
                'list' => [
                    [
                        'name' => '限时秒杀',
李光春's avatar
李光春 已提交
615
                        'resource_type' => 4
李光春's avatar
李光春 已提交
616 617 618
                    ],
                    [
                        'name' => '充值中心',
李光春's avatar
李光春 已提交
619
                        'resource_type' => 39997
李光春's avatar
李光春 已提交
620 621 622
                    ],
                    [
                        'name' => '转链',
李光春's avatar
李光春 已提交
623
                        'resource_type' => 39998
李光春's avatar
李光春 已提交
624 625 626
                    ],
                    [
                        'name' => '百亿补贴',
李光春's avatar
李光春 已提交
627
                        'resource_type' => 39996
李光春's avatar
李光春 已提交
628 629 630 631 632
                    ],
                ]
            ],
        ];
    }
李光春's avatar
李光春 已提交
633
}