123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace App\Support\tencentAdSDK;
- use App\Log;
- use App\Models\TencentAdAuth;
- use App\Service\HttpService;
- class UserActionsOpenApi extends BaseOpenApi
- {
- /**
- * 数据上报
- */
- public static function add($data, $retry = 5)
- {
- $interface = 'user_actions/add';
- $url = 'https://api.e.qq.com/v3.0/' . $interface;
- $access_token =TencentAdAuth::getAccessToken($data['account_id']);
- if (empty($access_token)) {
- Log::logError('查询账户accessToken失败', $data, 'UserActionsOpenApi');
- return self::returnFail([], []);
- }
- $common_parameters = [
- 'access_token' => $access_token,
- 'timestamp' => time(),
- 'nonce' => md5(uniqid('', true))
- ];
- // 用户标识
- $userId = array_filter([
- 'wechat_app_id' => $data['wechat_app_id'],
- 'wechat_openid' => $data['wechat_openid'] ?? null,
- 'wechat_unionid' => $data['wechat_unionid'] ?? null
- ]);
- $parameters = [
- 'account_id' => intval($data['account_id']),
- 'user_action_set_id' => intval($data['user_action_set_id']),
- 'actions' => [
- [
- 'action_time' => intval($data['action_time']),
- 'user_id' => $userId,
- 'action_type' => $data['action_type'],
- // 'trace' => [
- // 'click_id' => $data['click_id']
- // ]
- // 'action_param' => [
- // 'value' => $data['value'],
- // 'object' => $data['object'],
- // 'product_name' => $data['product_name'] ?? null
- // ]
- ]
- ]
- ];
- if(isset($data['click_id'])) {
- $parameters['actions'][0]['trace']['click_id'] = $data['click_id'];
- }
- // if(empty($parameters['actions'][0]['action_param']['product_name'])) {
- // unset($parameters['actions'][0]['action_param']['product_name']);
- // }
- $parameters = json_encode($parameters);
- $request_url = $url . '?' . http_build_query($common_parameters);
- do {
- try {
- $res = HttpService::httpPost($request_url, $parameters, true);
- } catch (\Throwable $e) {
- Log::logError('数据回传接口请求异常', [
- 'params' => json_encode($parameters),
- 'msg' => $e->getTraceAsString()
- ], 'UserActionsOpenApi');
- }
- // 记录日志
- Log::logInfo('数据上报API:' . $interface, [
- 'url' => $request_url,
- 'params' => $parameters,
- 'response' => $res,
- ], 'UserActionsOpenApi');
- $resArr = json_decode($res, true);
- if (!$resArr) {
- $resArr = [
- 'code' => 666666,
- 'message' => '请求失败(自定义)'
- ];
- }
- } while(($resArr['code'] != 0) && (--$retry > 0));
- if ($resArr['code'] == 0) {
- return self::returnSuccess([]);
- } else {
- return self::returnFail([], $resArr['message']);
- }
- }
- }
|