OceanEngineService.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace app\service\byte_dance;
  3. use GuzzleHttp\Client;
  4. use traits\think\Instance;
  5. class OceanEngineService{
  6. use Instance;
  7. protected $query=[];
  8. protected $addon=[];
  9. public function __construct($query)
  10. {
  11. $this->query=$query;
  12. }
  13. /**
  14. * @param array $query
  15. */
  16. public static function setQuery(array $query)
  17. {
  18. return self::instance($query);
  19. }
  20. /**
  21. * @param array $addon
  22. */
  23. public function setAddon(array $addon)
  24. {
  25. $this->addon = $addon;
  26. return $this;
  27. }
  28. public function form(){
  29. $this->conversion('form');
  30. }
  31. protected function conversion($evenType){
  32. if(empty($this->query['clickid'])){
  33. return false;
  34. }
  35. $p=$this->client()->post('https://analytics.oceanengine.com/api/v2/conversion',[
  36. 'json'=>[
  37. 'event_type'=>$evenType,
  38. 'context'=>[
  39. 'ad'=>[
  40. 'callback'=>$this->query['clickid'],
  41. ]
  42. ],
  43. 'timestamp'=>time()*1000,
  44. ]
  45. ]);
  46. $result=json_decode($p->getBody()->getContents(),true);
  47. $codeArr=[
  48. 0=>'成功',
  49. 10=>'服务暂时不可用,请稍后重试',
  50. 100=>'无效的请求,请求数据解析失败',
  51. 101=>'鉴权认证失败, 数据会被丢弃',
  52. 102=>'参数:event_type无效',
  53. 103=>'参数:callback无效',
  54. 107=>'鉴权认证失败,请修正签名,当前状态下本次回传不受影响',
  55. ];
  56. user_log('oceanEngineService',[
  57. 'query'=>$this->query,
  58. 'addon'=>$this->addon,
  59. 'response'=>$result,
  60. 'code_text'=>$codeArr[$result['code']??-1]??null
  61. ]);
  62. return $result;
  63. }
  64. public function shopping(){
  65. $this->conversion('shopping');
  66. }
  67. protected function client(){
  68. return new Client();
  69. }
  70. }