12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2018/12/12
- * Time: 11:13 AM
- */
- namespace openapi;
- class config
- {
- const CONVERT_FCODE_API = "https://passport.lrlz.com/mobile/index.php";
- const APP_ID = 'JYC_CHANNEL';
- const APP_KEY = 'aa161e84685b42a6862dfa5552195240';
- const RSA_PATH = BASE_ROOT_PATH . '/helper/openapi/keys/jyc_pri.pem';
- const FCODE_FIELDS = ['appid','appkey','convert_sn','convert_type','batch_code','commonid','time','mobile','return_url'];
- }
- class GoodsConvertor
- {
- static $pri_key = null;
- public function __construct()
- {
- if(self::$pri_key == null) {
- $pri_key = file_get_contents(config::RSA_PATH);
- self::$pri_key = openssl_get_privatekey($pri_key);
- }
- }
- public function fcode($params)
- {
- $body = $this->presign_body($params,config::FCODE_FIELDS);
- $signed = $this->sign($body);
- $params['signed'] = $signed;
- $params['act'] = 'fcode';
- $params['op'] = 'convert';
- return config::CONVERT_FCODE_API . "?" . http_build_query($params);
- }
- private function sign($body)
- {
- openssl_sign($body, $signed, self::$pri_key);
- $signed = base64_encode($signed);
- return $signed;
- }
- private function presign_body($input,$fields)
- {
- ksort($input);
- reset($input);
- $params = [];
- foreach ($input as $key => $val)
- {
- if(in_array($key,$fields)) {
- $params[] = "{$key}={$val}";
- }
- }
- return implode('&',$params);
- }
- }
|