JinBaoService.php 15.1 KB
Newer Older
李光春's avatar
李光春 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?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
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
// +----------------------------------------------------------------------

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

李光春's avatar
李光春 已提交
19
use DtApp\ThinkLibrary\exception\DtaException;
李光春's avatar
李光春 已提交
20
use DtApp\ThinkLibrary\Service;
李光春's avatar
李光春 已提交
21
use think\exception\HttpException;
李光春's avatar
李光春 已提交
22

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

    /**
     * API接口名称
     * @var string
     */
    private $type = '';
李光春's avatar
李光春 已提交
41
    private $response = '';
李光春's avatar
李光春 已提交
42 43 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

    /**
     * 开放平台分配的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
李光春 已提交
121
    public function param(array $param)
李光春's avatar
李光春 已提交
122
    {
李光春's avatar
李光春 已提交
123
        $this->param = $param;
李光春's avatar
李光春 已提交
124 125 126 127 128
        return $this;
    }

    /**
     * 网络请求
李光春's avatar
李光春 已提交
129 130
     * @return JinBaoService
     * @throws DtaException
李光春's avatar
李光春 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
     */
    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;
    }

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

李光春's avatar
李光春 已提交
158 159 160 161 162
    /**
     * 获取商品信息 - 多多进宝商品查询
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.goods.search
     * @return array|mixed
     */
李光春's avatar
李光春 已提交
163
    public function goodsSearch()
李光春's avatar
李光春 已提交
164 165
    {
        $this->type = 'pdd.ddk.goods.search';
李光春's avatar
李光春 已提交
166
        $this->response = 'goods_search_response';
李光春's avatar
李光春 已提交
167 168 169 170 171 172 173 174 175 176 177
        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
李光春 已提交
178
        $this->response = 'p_id_generate_response';
李光春's avatar
李光春 已提交
179 180 181 182 183 184 185 186 187 188 189
        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
李光春 已提交
190
        $this->response = 'p_id_query_response';
李光春's avatar
李光春 已提交
191 192 193 194 195 196 197 198 199 200 201
        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
李光春 已提交
202
        $this->response = 'order_detail_response';
李光春's avatar
李光春 已提交
203 204 205 206 207 208 209 210 211 212 213
        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
李光春 已提交
214
        $this->response = 'order_list_get_response';
李光春's avatar
李光春 已提交
215 216 217 218 219 220 221 222 223 224 225
        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
李光春 已提交
226
        $this->response = 'order_list_get_response';
李光春's avatar
李光春 已提交
227 228 229 230 231
        return $this;
    }

    /**
     * CPA效果数据 - 查询CPA数据
李光春's avatar
李光春 已提交
232
     * https://jinbao.pinduoduo.com/third-party/api-detail?apiName=pdd.ddk.finance.cpa.query
李光春's avatar
李光春 已提交
233 234 235 236 237
     * @return array|mixed
     */
    public function financeCpaQuery()
    {
        $this->type = 'pdd.ddk.finance.cpa.query';
李光春's avatar
李光春 已提交
238
        $this->response = 'finance_cpa_query_response';
李光春's avatar
李光春 已提交
239 240 241 242 243 244 245 246 247 248 249
        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
李光春 已提交
250
        $this->response = 'goods_promotion_url_generate_response';
李光春's avatar
李光春 已提交
251 252 253 254 255 256 257 258 259 260 261
        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
李光春 已提交
262
        $this->response = 'weapp_qrcode_generate_response';
李光春's avatar
李光春 已提交
263 264 265 266 267 268 269 270 271 272 273
        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
李光春 已提交
274
        $this->response = 'goods_zs_unit_generate_response';
李光春's avatar
李光春 已提交
275 276 277 278 279 280 281 282 283 284 285
        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
李光春 已提交
286
        $this->response = 'resource_url_response';
李光春's avatar
李光春 已提交
287 288 289 290 291 292 293 294 295 296 297
        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
李光春 已提交
298
        $this->response = 'theme_promotion_url_generate_response';
李光春's avatar
李光春 已提交
299 300 301 302 303 304 305 306 307 308 309
        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
李光春 已提交
310
        $this->response = 'mall_coupon_generate_url_response';
李光春's avatar
李光春 已提交
311 312 313 314 315 316 317 318 319 320 321
        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
李光春 已提交
322
        $this->response = 'rp_promotion_url_generate_response';
李光春's avatar
李光春 已提交
323 324 325 326 327 328 329 330 331 332 333
        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
李光春 已提交
334
        $this->response = 'goods_detail_response';
李光春's avatar
李光春 已提交
335 336 337 338 339 340 341 342 343 344 345
        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
李光春 已提交
346
        $this->response = 'ddk_goods_unit_query_response';
李光春's avatar
李光春 已提交
347 348 349 350 351 352 353 354 355 356 357
        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
李光春 已提交
358
        $this->response = 'goods_basic_detail_response';
李光春's avatar
李光春 已提交
359 360 361 362 363 364 365 366 367 368 369
        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
李光春 已提交
370
        $this->response = 'ddk_coupon_info_query_response';
李光春's avatar
李光春 已提交
371 372 373 374 375 376 377 378 379 380 381
        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
李光春 已提交
382
        $this->response = 'goods_info_list_response';
李光春's avatar
李光春 已提交
383 384 385 386 387 388 389 390 391 392 393
        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
李光春 已提交
394
        $this->response = 'top_goods_list_get_response';
李光春's avatar
李光春 已提交
395 396 397 398 399 400 401 402 403 404 405
        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
李光春 已提交
406
        $this->response = 'goods_basic_detail_response';
李光春's avatar
李光春 已提交
407 408 409 410 411 412 413 414 415 416 417
        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
李光春 已提交
418
        $this->response = 'theme_list_get_response';
李光春's avatar
李光春 已提交
419 420 421 422 423 424 425 426 427 428 429
        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
李光春 已提交
430
        $this->response = 'theme_list_get_response';
李光春's avatar
李光春 已提交
431 432 433 434 435 436
        return $this;
    }

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

    /**
     * @param $object
     * @return array
     */
    private function object2array(&$object)
    {
        if (is_object($object)) $arr = (array)($object);
        else $arr = &$object;
        if (is_array($arr)) foreach ($arr as $varName => $varValue) $arr[$varName] = $this->object2array($varValue);
        return $arr;
    }

    /**
     * 签名
     * @return string
李光春's avatar
李光春 已提交
488
     * @throws DtaException
李光春's avatar
李光春 已提交
489 490 491
     */
    private function createSign()
    {
492
        if (empty($this->client_secret)) $this->getConfig();
李光春's avatar
李光春 已提交
493
        if (empty($this->client_secret)) throw new DtaException('请检查client_secret参数');
李光春's avatar
李光春 已提交
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509

        $sign = $this->client_secret;
        ksort($this->param);
        foreach ($this->param as $key => $val) if ($key != '' && $val != '') $sign .= $key . $val;
        $sign .= $this->client_secret;
        $sign = strtoupper(md5($sign));
        return $sign;
    }

    /**
     * 组参
     * @return string
     */
    private function createStrParam()
    {
        $strParam = '';
李光春's avatar
李光春 已提交
510
        foreach ($this->param as $key => $val) if ($key != '' && $val != '' && !is_array($val)) $strParam .= $key . '=' . urlencode($val) . '&';
李光春's avatar
李光春 已提交
511 512 513
        return $strParam;
    }
}