JosBaseInfo.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace ACES\Common\domain;
  3. class JosBaseInfo
  4. {
  5. private $appKey;
  6. private $appSecret;
  7. private $accessToken;
  8. private $timestamp;
  9. private $v = "2.0";
  10. private $serverUrl;
  11. /**
  12. * JosBaseInfo constructor.
  13. * @param $method
  14. * @param $app_key
  15. * @param $access_token
  16. */
  17. public function __construct($appKey, $appSecret, $access_token, $serverUrl)
  18. {
  19. $this->appKey = $appKey;
  20. $this->appSecret = $appSecret;
  21. $this->accessToken = $access_token;
  22. $this->serverUrl = $serverUrl;
  23. }
  24. /**
  25. * @return string
  26. */
  27. public function getAppKey()
  28. {
  29. return $this->appKey;
  30. }
  31. /**
  32. * @return mixed
  33. */
  34. public function getAppSecret()
  35. {
  36. return $this->appSecret;
  37. }
  38. /**
  39. * @return string
  40. */
  41. public function getAccessToken()
  42. {
  43. return $this->accessToken;
  44. }
  45. /**
  46. * @return string
  47. */
  48. public function getV()
  49. {
  50. return $this->v;
  51. }
  52. /**
  53. * @return string
  54. */
  55. public function getServerUrl()
  56. {
  57. return $this->serverUrl;
  58. }
  59. /**
  60. * @param bool $init
  61. * @return string
  62. */
  63. public function getTimestamp($init = true)
  64. {
  65. if ($init || !isset($this->timestamp)) {
  66. //TODO delete date_default_timezone_set
  67. date_default_timezone_set("prc");
  68. $this->timestamp = date('Y-m-d H:i:s');
  69. }
  70. return $this->timestamp;
  71. }
  72. /**
  73. * @param \ACES\Common\ProduceRequest $josReuqest
  74. * @return array
  75. */
  76. public function getFormParams($josReuqest)
  77. {
  78. $request = array();
  79. $request['360buy_param_json'] = $josReuqest->to360buyParamJson();
  80. $request['app_key'] = $this->getAppKey();
  81. $request['access_token'] = $this->getAccessToken();
  82. $request['timestamp'] = $this->getTimestamp();
  83. $request['v'] = $this->getV();
  84. $request['method'] = $josReuqest->getJosMethod();
  85. $request['sign'] = $this->generateSign($request);
  86. return $request;
  87. }
  88. private function generateSign($params)
  89. {
  90. ksort($params);
  91. $stringToBeSigned = $this->appSecret;
  92. foreach ($params as $k => $v)
  93. {
  94. $stringToBeSigned .= $k . $v;
  95. }
  96. $stringToBeSigned .= $this->appSecret;
  97. return strtoupper(md5($stringToBeSigned));
  98. }
  99. }