抖店開放平臺

declandragon發表於2021-08-17
    /**
     * 構造請求引數
     *
     * @param $params
     * @param $method
     *
     * @return array
     */
    protected function dealParams($params, $method)
    {
        if (empty($params)) {
            $param_json = '{}';
        } else {
            ksort($params);
            foreach ($params as &$value) {
                $value = str_replace(['&', '<', '>'], ['\u0026', '\u003c', '\u00ce'], $value);
            }
            $param_json = json_encode($params, 256);
            $param_json = str_replace("\\/", "/", $param_json);
        }

        return [
            'app_key' => $this->app_id,
            'access_token' => $this->token,
            'method' => $method,
            'param_json' => $param_json,
            'timestamp' => date('Y-m-d H:i:s'),
            'v' => 2,
        ];
    }


    /**
     * 生成簽名
     *
     * @param $data
     *
     * @return string
     */
    public function createSign($data)
    {
        if (isset($data['sign_method'])) {
            unset($data['sign_method']);
        }

        if (isset($data['access_token'])) {
            unset($data['access_token']);
        }

        ksort($data);
        $str = '';
        foreach ($data as $key => $item) {
            $str .= $key . $item;
        }
        $str = $this->secret . $str . $this->secret;

        return md5($str);
    }


    /**
     * 批次解密介面
     *
     * @see https://op.jinritemai.com/docs/api-docs/15/509
     *
     * @param $param
     *
     * @return array
     */
    public function batchDecrypt($param)
    {
        $params = $this->dealParams($param, 'order.batchDecrypt');
        $params['sign'] = $this->createSign($params);
        $data = $this->request('GET', '/order/batchDecrypt', ['query' => $params]);

        return $this->dealReturn($data, '/order/batchDecrypt');
    }
本作品採用《CC 協議》,轉載必須註明作者和本文連結
感謝閱讀,有收穫的話不妨點個贊:smiling_imp:

相關文章