企微短剧业务系统

UserActionsOpenApi.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace App\Support\tencentAdSDK;
  3. use App\Log;
  4. use App\Models\TencentAdAuth;
  5. use App\Service\HttpService;
  6. class UserActionsOpenApi extends BaseOpenApi
  7. {
  8. /**
  9. * 数据上报
  10. */
  11. public static function add($data, $retry = 5)
  12. {
  13. $interface = 'user_actions/add';
  14. $url = 'https://api.e.qq.com/v3.0/' . $interface;
  15. $access_token =TencentAdAuth::getAccessToken($data['account_id']);
  16. if (empty($access_token)) {
  17. Log::logError('查询账户accessToken失败', $data, 'UserActionsOpenApi');
  18. return self::returnFail([], []);
  19. }
  20. $common_parameters = [
  21. 'access_token' => $access_token,
  22. 'timestamp' => time(),
  23. 'nonce' => md5(uniqid('', true))
  24. ];
  25. // 用户标识
  26. $userId = array_filter([
  27. 'wechat_app_id' => $data['wechat_app_id'],
  28. 'wechat_openid' => $data['wechat_openid'] ?? null,
  29. 'wechat_unionid' => $data['wechat_unionid'] ?? null
  30. ]);
  31. $parameters = [
  32. 'account_id' => intval($data['account_id']),
  33. 'user_action_set_id' => intval($data['user_action_set_id']),
  34. 'actions' => [
  35. [
  36. 'action_time' => intval($data['action_time']),
  37. 'user_id' => $userId,
  38. 'action_type' => $data['action_type'],
  39. // 'trace' => [
  40. // 'click_id' => $data['click_id']
  41. // ]
  42. // 'action_param' => [
  43. // 'value' => $data['value'],
  44. // 'object' => $data['object'],
  45. // 'product_name' => $data['product_name'] ?? null
  46. // ]
  47. ]
  48. ]
  49. ];
  50. if(isset($data['click_id'])) {
  51. $parameters['actions'][0]['trace']['click_id'] = $data['click_id'];
  52. }
  53. // if(empty($parameters['actions'][0]['action_param']['product_name'])) {
  54. // unset($parameters['actions'][0]['action_param']['product_name']);
  55. // }
  56. $parameters = json_encode($parameters);
  57. $request_url = $url . '?' . http_build_query($common_parameters);
  58. do {
  59. try {
  60. $res = HttpService::httpPost($request_url, $parameters, true);
  61. } catch (\Throwable $e) {
  62. Log::logError('数据回传接口请求异常', [
  63. 'params' => json_encode($parameters),
  64. 'msg' => $e->getTraceAsString()
  65. ], 'UserActionsOpenApi');
  66. }
  67. // 记录日志
  68. Log::logInfo('数据上报API:' . $interface, [
  69. 'url' => $request_url,
  70. 'params' => $parameters,
  71. 'response' => $res,
  72. ], 'UserActionsOpenApi');
  73. $resArr = json_decode($res, true);
  74. if (!$resArr) {
  75. $resArr = [
  76. 'code' => 666666,
  77. 'message' => '请求失败(自定义)'
  78. ];
  79. }
  80. } while(($resArr['code'] != 0) && (--$retry > 0));
  81. if ($resArr['code'] == 0) {
  82. return self::returnSuccess([]);
  83. } else {
  84. return self::returnFail([], $resArr['message']);
  85. }
  86. }
  87. }