ClientService.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/1/7
  8. * Time: 15:15
  9. */
  10. namespace app\outside\services;
  11. use app\outside\modes\OutsideAppSetting;
  12. class ClientService
  13. {
  14. private $route; //方法
  15. private $app_id; //应用AppID
  16. private $secret; //密钥字符串
  17. private $data = []; //data参数
  18. private $sign_type = "MD5"; //SHA256 MD5
  19. public function __construct()
  20. {
  21. $this->init();
  22. }
  23. /**
  24. * 配置信息
  25. */
  26. public function init()
  27. {
  28. $outsideApp = OutsideAppSetting::current();
  29. $this->app_id = $outsideApp->app_id;
  30. $this->secret = $outsideApp->app_secret;
  31. }
  32. public function setRoute($route)
  33. {
  34. $this->route = $route;
  35. }
  36. public function getRoute()
  37. {
  38. return $this->route;
  39. }
  40. /**
  41. * 请求参数
  42. * @return array
  43. */
  44. public function getAllParameter()
  45. {
  46. //去除秘钥
  47. $vars = array_except(get_object_vars($this), ['secret','data']);
  48. $result = array_merge($vars, $this->getAllData());
  49. return $result;
  50. }
  51. public function initData($data)
  52. {
  53. $this->data = $data;
  54. }
  55. public function setData($key, $value)
  56. {
  57. $this->data[$key] = $value;
  58. }
  59. /**
  60. * 通过键获取 data 指定值
  61. * @param $key
  62. * @return mixed|string
  63. */
  64. public function getData($key)
  65. {
  66. return $this->data[$key] ? $this->data[$key] : '';
  67. }
  68. public function getAllData()
  69. {
  70. return $this->data;
  71. }
  72. /**
  73. * @param $url
  74. * @param $data
  75. *
  76. */
  77. public function post($url = '')
  78. {
  79. $requestData = $this->getAllParameter();
  80. $requestData['sign'] = $this->autograph($requestData['sign_type']);
  81. return $requestData;
  82. }
  83. protected function autograph($type)
  84. {
  85. if (strtoupper($type) == 'SHA256') {
  86. return $this->verifySha256();
  87. }
  88. return $this->verifyMd5();
  89. }
  90. protected function verifySha256()
  91. {
  92. $hashSign = hash_hmac('sha256', $this->toQueryString($this->getAllParameter()), $this->secret);
  93. return $hashSign;
  94. }
  95. protected function verifyMd5()
  96. {
  97. return strtoupper(md5($this->toQueryString($this->getAllParameter()).'&secret='.$this->secret));
  98. }
  99. /**
  100. * 将参数转换成k=v拼接的形式
  101. * @param $parameter
  102. * @return string
  103. */
  104. public function toQueryString($parameter)
  105. {
  106. //按key的字典序升序排序,并保留key值
  107. ksort($parameter);
  108. $strQuery="";
  109. foreach ($parameter as $k=>$v){
  110. //不参与签名、验签
  111. if($k == "sign"){
  112. continue;
  113. }
  114. if($v === null) {$v = '';}
  115. $strQuery .= strlen($strQuery) == 0 ? "" : "&";
  116. $strQuery.=$k."=".$v;
  117. }
  118. return $strQuery;
  119. }
  120. }