GoodsConvertor.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/12/12
  6. * Time: 11:13 AM
  7. */
  8. namespace openapi;
  9. class config
  10. {
  11. const CONVERT_FCODE_API = "https://passport.lrlz.com/mobile/index.php";
  12. const APP_ID = 'JYC_CHANNEL';
  13. const APP_KEY = 'aa161e84685b42a6862dfa5552195240';
  14. const RSA_PATH = BASE_ROOT_PATH . '/helper/openapi/keys/jyc_pri.pem';
  15. const FCODE_FIELDS = ['appid','appkey','convert_sn','convert_type','batch_code','commonid','time','mobile','return_url'];
  16. }
  17. class GoodsConvertor
  18. {
  19. static $pri_key = null;
  20. public function __construct()
  21. {
  22. if(self::$pri_key == null) {
  23. $pri_key = file_get_contents(config::RSA_PATH);
  24. self::$pri_key = openssl_get_privatekey($pri_key);
  25. }
  26. }
  27. public function fcode($params)
  28. {
  29. $body = $this->presign_body($params,config::FCODE_FIELDS);
  30. $signed = $this->sign($body);
  31. $params['signed'] = $signed;
  32. $params['act'] = 'fcode';
  33. $params['op'] = 'convert';
  34. return config::CONVERT_FCODE_API . "?" . http_build_query($params);
  35. }
  36. private function sign($body)
  37. {
  38. openssl_sign($body, $signed, self::$pri_key);
  39. $signed = base64_encode($signed);
  40. return $signed;
  41. }
  42. private function presign_body($input,$fields)
  43. {
  44. ksort($input);
  45. reset($input);
  46. $params = [];
  47. foreach ($input as $key => $val)
  48. {
  49. if(in_array($key,$fields)) {
  50. $params[] = "{$key}={$val}";
  51. }
  52. }
  53. return implode('&',$params);
  54. }
  55. }