ScrmController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Http\Controllers\Api\Third;
  3. use App\Http\Controllers\Api\ApiBaseController;
  4. use Illuminate\Http\Request;
  5. use GuzzleHttp\Client;
  6. use Illuminate\Support\Facades\Cache;
  7. use App\Models\HukeCallback;
  8. class ScrmController extends ApiBaseController
  9. {
  10. /**
  11. * @var Client
  12. */
  13. private $httpClient;
  14. private $app_key;
  15. private $app_secret;
  16. /**
  17. * SmsService constructor.
  18. * @param $app_key
  19. * @param $app_secret
  20. * @param $sign_key
  21. */
  22. public function __construct()
  23. {
  24. $this->app_key = '817267c4c2f3476b863522525b97ac78';
  25. $this->app_secret = '559f778b1b4f4e47b0265fa5fbedfb54';
  26. $this->httpClient=new Client([
  27. 'http_errors' => false
  28. ]);
  29. }
  30. public function pullData(Request $request)
  31. {
  32. $log = [
  33. 'content' => json_encode($request->all()),
  34. ];
  35. HukeCallback::create($log);
  36. }
  37. public function get_customer_fields($update = false)
  38. {
  39. if(!$update){
  40. if(Cache::has("huke_customer_fields")){
  41. return Cache::get("huke_customer_fields");
  42. }
  43. }
  44. $params = [];
  45. $params = json_encode($params);
  46. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  47. $time = time();
  48. $sign = md5($params);
  49. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  50. $headers = [
  51. 'Content-Type' => 'application/json',
  52. 'ur-appkey' => $this->app_key,
  53. 'ur-sign' => $sign,
  54. 'ur-curtime' => $time,
  55. 'ur-checksum' => $checksum
  56. ];
  57. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/fields', ['body'=>$params,'headers' => $headers]);
  58. $res = json_decode($response->getBody()->getContents(),true);
  59. $fields = [];
  60. foreach ($res['data'] as $k => $v){
  61. $fields[$v['id']] = $v;
  62. }
  63. Cache::put('huke_customer_fields',$fields,'86400');
  64. return $fields;
  65. }
  66. public function get_customer_list($start,$end,$page,$page_size)
  67. {
  68. $params = [
  69. 'startTime' => str_pad(strtotime($start),13,'0',STR_PAD_RIGHT),
  70. 'endTime' => str_pad(strtotime($end),13,'0',STR_PAD_RIGHT),
  71. 'page'=>$page,
  72. 'pageSize'=>$page_size
  73. ];
  74. $params = json_encode($params);
  75. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  76. $time = time();
  77. $sign = md5($params);
  78. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  79. $headers = [
  80. 'Content-Type' => 'application/json',
  81. 'ur-appkey' => $this->app_key,
  82. 'ur-sign' => $sign,
  83. 'ur-curtime' => $time,
  84. 'ur-checksum' => $checksum
  85. ];
  86. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/list', ['body'=>$params,'headers' => $headers]);
  87. $res = json_decode($response->getBody()->getContents(),true);
  88. dd($res);
  89. }
  90. public function test()
  91. {
  92. $res = $this->get_customer_fields();
  93. $params = [
  94. 'type' => 0,
  95. 'targets' => [
  96. '15759560669'
  97. ]
  98. ];
  99. $params = json_encode($params);
  100. $appSecret = substr(openssl_digest(openssl_digest($this->app_secret, 'sha1', true), 'sha1', true), 0, 16);
  101. $time = time();
  102. $sign = md5($params);
  103. $checksum = bin2hex(openssl_encrypt($this->app_key. $sign. $time, 'AES-128-ECB', $appSecret, OPENSSL_RAW_DATA));
  104. $headers = [
  105. 'Content-Type' => 'application/json',
  106. 'ur-appkey' => $this->app_key,
  107. 'ur-sign' => $sign,
  108. 'ur-curtime' => $time,
  109. 'ur-checksum' => $checksum
  110. ];
  111. $response = $this->httpClient->post('https://huke.163.com/openapi/customer/query', ['body'=>$params,'headers' => $headers]);
  112. $res = json_decode($response->getBody()->getContents(),true);
  113. dd($res);
  114. }
  115. }