ByteDance.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\service\byte_dance;
  3. use GuzzleHttp\Client;
  4. class ByteDance{
  5. public static function accessToken(){
  6. return self::single(ByteDanceAccessToken::class)
  7. ->setAppId(self::appId())
  8. ->setAppSecret(self::appSecret())
  9. ->get();
  10. }
  11. private static function single($class){
  12. static $container=[];
  13. if (!isset($container[$class])){
  14. $container[$class]=new $class;
  15. }
  16. return $container[$class];
  17. }
  18. public static function appId(){
  19. return config('site.byte_dance_mapp_appId');
  20. }
  21. public static function appSecret(){
  22. return config('site.byte_dance_mapp_appSecret');
  23. }
  24. public static function appIdTwo(){
  25. return config('site.byte_dance_mapp_two_appld');
  26. }
  27. public static function appSecretTwo(){
  28. return config('site.byte_dance _mapp_two_appSecret');
  29. }
  30. public static function appIdThree(){
  31. return config('site.byte_dance_mapp_three_appId');
  32. }
  33. public static function appSecretThree(){
  34. return config('site.byte_dance_mapp_three_secret');
  35. }
  36. public static function httpGet($url){
  37. $request=(new Client)
  38. ->get($url);
  39. $requestData = json_decode($request->getBody()->getContents(),true);
  40. ByteDance::throwIf(!empty($requestData['err_no']),$requestData['err_tips']);
  41. return $requestData['data'];
  42. }
  43. public static function httpPost($url,$params,$header=[],$getData=true){
  44. $request=(new Client)
  45. ->request('post',$url,[
  46. 'json'=>$params,
  47. 'headers'=>$header
  48. ]);
  49. $requestData = json_decode($request->getBody()->getContents(),true);
  50. if(!$getData){
  51. return $requestData;
  52. }
  53. ByteDance::throwIf(!empty($requestData['err_no']),$requestData['err_tips']);
  54. return $requestData['data']??[];
  55. }
  56. public static function throwIf($condition,$msg){
  57. if($condition){
  58. throw new ByteDanceException($msg);
  59. }
  60. }
  61. public static function sign($map) {
  62. $rList = array();
  63. foreach($map as $k =>$v) {
  64. if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
  65. continue;
  66. $value = trim(strval($v));
  67. $len = strlen($value);
  68. if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\"")
  69. $value = substr($value,1, $len-1);
  70. $value = trim($value);
  71. if ($value == "" || $value == "null")
  72. continue;
  73. array_push($rList, $value);
  74. }
  75. array_push($rList, config('site.byte_dance_mapp_pay_salt'));
  76. sort($rList, 2);
  77. return md5(implode('&', $rList));
  78. }
  79. public static function signWangKa($map) {
  80. $rList = array();
  81. foreach($map as $k =>$v) {
  82. if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
  83. continue;
  84. $value = trim(strval($v));
  85. $len = strlen($value);
  86. if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\"")
  87. $value = substr($value,1, $len-1);
  88. $value = trim($value);
  89. if ($value == "" || $value == "null")
  90. continue;
  91. array_push($rList, $value);
  92. }
  93. array_push($rList, config('site.byte_dance_mapp_two_pay_salt'));
  94. sort($rList, 2);
  95. return md5(implode('&', $rList));
  96. }
  97. public static function signHaoYuan($map) {
  98. $rList = array();
  99. foreach($map as $k =>$v) {
  100. if ($k == "other_settle_params" || $k == "app_id" || $k == "sign" || $k == "thirdparty_id")
  101. continue;
  102. $value = trim(strval($v));
  103. $len = strlen($value);
  104. if ($len > 1 && substr($value, 0,1)=="\"" && substr($value,$len, $len-1)=="\"")
  105. $value = substr($value,1, $len-1);
  106. $value = trim($value);
  107. if ($value == "" || $value == "null")
  108. continue;
  109. array_push($rList, $value);
  110. }
  111. array_push($rList, config('site.byte_dance_mapp_three_pay_salt'));
  112. sort($rList, 2);
  113. return md5(implode('&', $rList));
  114. }
  115. }