GoodsConvertor.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 config
  18. {
  19. const CONVERT_URL = "https://passport.lrlz.com/mobile/index.php";
  20. const APP_ID = 'DHKJ_CHANNEL';
  21. const APP_KEY = '13e5b127aed296ef6d490cd7cc4c161e';
  22. const RSA_PATH = BASE_ROOT_PATH . '/helper/openapi/keys/dhkj_pri.pem';
  23. const FIELDS = ['appid','appkey','convert_sn','convert_type','time','mobile','return_url'];
  24. }
  25. class GoodsConvertor
  26. {
  27. static $pri_key = null;
  28. public function __construct()
  29. {
  30. if(self::$pri_key == null) {
  31. $pri_key = file_get_contents(config::RSA_PATH);
  32. self::$pri_key = openssl_get_privatekey($pri_key);
  33. }
  34. }
  35. public function fcode($params)
  36. {
  37. $body = $this->presign_body($params,config::FIELDS);
  38. $signed = $this->sign($body);
  39. $params['signed'] = $signed;
  40. $params['act'] = 'convert';
  41. $params['op'] = 'fcode';
  42. return config::CONVERT_URL . "?" . http_build_query($params);
  43. }
  44. public function user($params)
  45. {
  46. $body = $this->presign_body($params,config::FIELDS);
  47. $signed = $this->sign($body);
  48. $params['signed'] = $signed;
  49. $params['act'] = 'convert';
  50. $params['op'] = 'user';
  51. return config::CONVERT_URL . "?" . http_build_query($params);
  52. }
  53. private function sign($body)
  54. {
  55. openssl_sign($body, $signed, self::$pri_key);
  56. $signed = base64_encode($signed);
  57. return $signed;
  58. }
  59. private function presign_body($input,$fields)
  60. {
  61. ksort($input);
  62. reset($input);
  63. $params = [];
  64. foreach ($input as $key => $val)
  65. {
  66. if(in_array($key,$fields)) {
  67. $params[] = "{$key}={$val}";
  68. }
  69. }
  70. return implode('&',$params);
  71. }
  72. }