Common.Class.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * 接口请求公共处理
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2020/11/12
  7. * Time: 3:58 PM
  8. */
  9. namespace Util\DaDa;
  10. use Mall\Framework\Core\ResultWrapper;
  11. use Mall\Framework\Core\ErrorCode;
  12. class Common
  13. {
  14. // 授权KEY
  15. private $appKey = '';
  16. // 授权密钥
  17. private $appSecret = '';
  18. // 店铺编号
  19. private $sourceId = 0;
  20. public function __construct($appKey, $appSecret, $sourceId)
  21. {
  22. $this->appKey = $appKey;
  23. $this->appSecret = $appSecret;
  24. $this->sourceId = $sourceId;
  25. }
  26. /**
  27. * 官网文档地址: http://newopen.imdada.cn/#/quickStart/develop/mustRead?_k=iuc6mw
  28. * 公共请求参数
  29. */
  30. public function CommonRequestParams()
  31. {
  32. return [
  33. 'app_key' => $this->appKey,
  34. 'timestamp' => time(),
  35. 'format' => 'json',
  36. 'v' => '1.0',
  37. 'source_id' => $this->sourceId,
  38. ];
  39. }
  40. /**
  41. * 官方文档地址: http://newopen.imdada.cn/#/quickStart/develop/safety?_k=6bwyag
  42. * 生成签名
  43. */
  44. public function getSignature($parm)
  45. {
  46. if(ksort($parm)){
  47. $stringA = '';
  48. //拼接成字符串stringA
  49. foreach($parm as $key => $value){
  50. if($value == 0 || !empty($value)){
  51. $stringA .=$key.$value;
  52. }
  53. }
  54. }else{
  55. echo "对参数排序出错";
  56. exit();
  57. }
  58. //拼接后的字符串首尾加上app_secret秘钥
  59. $stringSignTemp = $this->appSecret.$stringA.$this->appSecret;
  60. //签名字符串进行MD5加密并且将签名生成的32位字符串转换为大写
  61. $signValue = strtoupper(md5($stringSignTemp));
  62. return $signValue;
  63. }
  64. /**
  65. * 公共处理返回结果
  66. */
  67. public function commonResponse($response)
  68. {
  69. if ($response['httpcode'] != 200) {
  70. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  71. }
  72. $responseData = json_decode($response['content'], true);
  73. if ($responseData['code']) {
  74. return ResultWrapper::fail($responseData['msg'], $responseData['code']);
  75. }
  76. return ResultWrapper::success($responseData['result']);
  77. }
  78. }