yii框架封装拼多多开放平台sdk

yii框架封装拼多多开放平台sdk

 'https://mms.pinduoduo.com/open.html?',
        'H5'       => 'https://mai.pinduoduo.com/h5-login.html?',
        'JINBAO'   => 'https://jinbao.pinduoduo.com/open.html?',
    ];

    //无需授权
    /** @var string 获取购买应用订单列表 */
    const API_PDD_VAS_ORDER_SEARCH = 'pdd.vas.order.search';

    //需授权
    /** @var string 获取商品列表 */
    const API_PDD_GOODS_LIST_GET = 'pdd.goods.list.get';
    /** @var string 获取商品列表 */
    const API_PDD_GOODS_DETAIL_GET = 'pdd.goods.detail.get';
    /** @var string 修改商品信息 */
    const API_PDD_GOODS_INFORMATION_UPDATE = 'pdd.goods.information.update';
    /** @var string 上传图片 */
    const API_PDD_GOODS_IMAGE_UPLOAD = 'pdd.goods.image.upload';
    /** @var string 商品编辑结果查询 */
    const API_PDD_GOODS_COMMIT_DETAIL_GET = 'pdd.goods.commit.detail.get';
    /** @var string 获取店铺信息 */
    const API_PDD_MALL_INFO_GET = 'pdd.mall.info.get';

    /** @var array 拼多多请求接口是否需要授权 */
    const REQUEST_API_LIST_NEED_TOKEN = [
        self::API_PDD_GOODS_LIST_GET => true,
        self::API_PDD_GOODS_DETAIL_GET => true,
        self::API_PDD_GOODS_INFORMATION_UPDATE => true,
        self::API_PDD_GOODS_IMAGE_UPLOAD => true,
        self::API_PDD_GOODS_COMMIT_DETAIL_GET => true,
        self::API_PDD_VAS_ORDER_SEARCH => false,
        self::API_PDD_MALL_INFO_GET => true,
    ];

    /** @var int 商品编辑请求失败状态 */
    const GOODS_UPDATE_COMMIT_FAIL_STATUS = 3;
    /** @var float|int 拼多多允许上传的图片大小 */
    const OPEN_PIN_DUO_DUO_UPLOAD_IMG_SIZE = 1024 *1024;

    /** @var float|int 应用免费试用时间 */
    const SERVICE_PROBATION_TIME = 15 * 24 * 3600;

    /** @var string 购买或续费应用入口 */
    const SERVICE_BUY_ENTRANCE = 'https://mms.pinduoduo.com/service-market/service-detail?detailId=411';
    /** @var string 服务市场入口 */
    const SERVICE_MARKET_ENTRANCE = 'https://mms.pinduoduo.com/service-market/';
    /**
     * @throws \Exception
     * @author: SWZ
     * @time: 14:16
     * @date: 2019/10/15
     * @describe:
     */
    public function init()
    {
        parent::init();
        if (!$this->clientId) {
            throw new \Exception('clientId is empty');
        }
        if (!$this->clientSecret) {
            throw new \Exception('clientSecret is empty');
        }
        if (!$this->memberType) {
            throw new \Exception('memberType is empty');
        }
        //$this->redirectUrl = \Yii::$app->request->hostInfo.'/open/access-token/pinDuoDuo';
    }
    /**
     * 生成签名
     * @param $params
     * @return string
     * @author: SWZ
     * @time: 17:46
     * @date: 2019/10/22
     * @describe:
     */
    private function signature($params)
    {
        ksort($params);
        $paramsStr = '';
        array_walk($params, function ($item, $key) use (&$paramsStr) {
            if ('@' != substr($item, 0, 1)) {
                $paramsStr .= sprintf('%s%s', $key, $item);
            }
        });

        return strtoupper(md5(sprintf('%s%s%s', $this->clientSecret, $paramsStr, $this->clientSecret)));
    }
    /**
     * 接口请求
     * @param $method
     * @param $params
     * @param string $access_token
     * @param string $data_type
     * @return mixed|string
     * @author: SWZ
     * @time: 17:49
     * @date: 2019/10/22
     * @describe:
     */
    public function request($apiName, $params,$accessToken = '',$data_type = 'JSON')
    {
        $http = $this->http;
        $params = $this->paramsHandle($params);
        if (static::REQUEST_API_LIST_NEED_TOKEN[$apiName]) {
            $params['access_token'] = $accessToken;
        }
        $params['client_id'] = $this->clientId;
        $params['sign_method'] = 'md5';
        $params['type'] = $apiName;
        $params['data_type'] = $data_type;
        $params['timestamp'] = strval(time());
        $params['sign'] = $this->signature($params);
        $response = call_user_func_array([$http, 'post'], [self::URL, $params]);
        $responseBody = strval($response->getBody());
        return strtolower($data_type) == 'json' ? json_decode($responseBody, true) : $responseBody;
    }

    /**
     * @param array $params
     *
     * @return array
     */
    protected function paramsHandle(array $params)
    {
        array_walk($params, function (&$item) {
            if (is_array($item)) {
                $item = json_encode($item);
            }
            if (is_bool($item)) {
                $item = ['false', 'true'][intval($item)];
            }
        });

        return $params;
    }
    /**
     * 跳转到拼多多授权页面获取code
     * @param string $state
     * @param null $view
     * @author: SWZ
     * @time: 17:32
     * @date: 2019/10/22
     * @describe:
     */
    public function authorizationRedirect($state = 'state', $view = null)
    {
        $url = $this->authorizationUrl($state, $view);
        header('Location:'.$url);
    }

    /**
     * 获取拼多多授权登录url
     * @param null $state
     * @param null $view
     * @return string
     * @author: SWZ
     * @time: 17:33
     * @date: 2019/10/22
     * @describe:
     */
    public function authorizationUrl($state = null, $view = null)
    {
        return self::AUTHORIZE_API_ARR[strtoupper($this->memberType)].http_build_query([
                'client_id'     => $this->clientId,
                'response_type' => 'code',
                'state'         => $state,
                'redirect_uri'  => $this->redirectUrl,
                'view'          => $view,
            ]);
    }

    /**
     * 获取access_token
     * @param null $code
     * @param null $state
     * @return mixed
     * @author: SWZ
     * @time: 17:32
     * @date: 2019/10/22
     * @describe:
     */
    public function getAccessToken($code = null, $state = null)
    {
        return $this->token([
            'client_id'     => $this->clientId,
            'client_secret' => $this->clientSecret,
            'grant_type'    => 'authorization_code',
            'code'          => $code ?: \Yii::$app->request->get('code'),
            'redirect_uri'  => $this->redirectUrl,
            'state'         => $state,
        ]);
    }

    /**
     * 使用refresh_token刷新access_token
     * @param $refreshToken
     * @param null $state
     * @return mixed
     * @author: SWZ
     * @time: 17:34
     * @date: 2019/10/22
     * @describe:
     */
    public function refreshToken($refreshToken, $state = null)
    {
        return $this->token([
            'client_id'     => $this->clientId,
            'client_secret' => $this->clientSecret,
            'grant_type'    => 'refresh_token',
            'refresh_token' => $refreshToken,
            'state'         => $state,
        ]);
    }
    public function token($params)
    {
        $response = $this->http->json(self::TOKEN_API, $params);

        return json_decode(strval($response->getBody()), true);
    }
    /**
     * 获取http实例
     * @return Http
     * @author: SWZ
     * @time: 17:36
     * @date: 2019/10/22
     * @describe:
     */
    public function getHttp()
    {
        return  new Http();
    }
}

然后是config配置:

  'pinDuoDuoOpen' => [
            'class' => 'common\components\vendor\PinDuoDuoOpen',
            'clientId' => 'xxxxxxxxx',
            'clientSecret' => 'xxxxxxxxxx',
            'memberType' => 'MERCHANT',
            'redirectUrl' => 'xxxxxxxx',
        ],

使用方式—授权跳转

 $pinDuoDuo = \Yii::$app->pinDuoDuoOpen;
 $pinDuoDuo->authorizationRedirect();

使用方式----获取token:

 $pinDuoDuo = \Yii::$app->pinDuoDuoOpen;
 $tokenData = $pinDuoDuo->getAccessToken();

使用方式-----接口调用:

  //拼多多获取商品列表
                    $pinDuoDuo = \Yii::$app->pinDuoDuoOpen;
                    $pinDuoDuoGoodsData = $pinDuoDuo->request(PinDuoDuoOpen::API_PDD_GOODS_LIST_GET, ['page' => $this->page, 'page_size' => $this->page_size], ‘用户的access_token’);

你可能感兴趣的:(php及后台框架)