企微短剧业务系统

QyReport.php 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Created by:PhpStorm
  4. * Author:chenzhiyuan
  5. * Date: 2022/5/31
  6. * Time: 11:34 上午
  7. */
  8. namespace App\Support\qyApi;
  9. use App\Service\HttpService;
  10. use App\Log;
  11. use App\Models\AuthorizeCorp;
  12. use App\RedisModel;
  13. class QyReport
  14. {
  15. /**
  16. * 获取员工联系客户的会话统计数据
  17. * 文档地址:https://developer.work.weixin.qq.com/document/path/92275
  18. * 规则:查询跨度最大30天,最多统计180天内的数据
  19. * @param $corpid
  20. * @param $userid
  21. * @param $start_time
  22. * @param $end_time
  23. */
  24. public static function customer_conversation_report($corpid, $userid, $start_time, $end_time, $retry = 0){
  25. $accessToken = AuthorizeCorp::getAccessToken($corpid, '获取联系客户统计数据');
  26. if(empty($accessToken)) {
  27. Log::logError('获取联系客户统计数据 参数:'.json_encode(['corpid' => $corpid, 'user_id' => $userid,'start_time'=>$start_time,'end_time'=>$end_time]),
  28. ['errmsg' => '令牌获取失败'], 'qyWechat');
  29. return false;
  30. }
  31. $postData = [
  32. 'userid' => [$userid],
  33. 'start_time' => strtotime($start_time),
  34. 'end_time' => strtotime($end_time),
  35. ];
  36. $requestUri = config('qyWechat.user_conversation_report');
  37. $requestUri .= $accessToken;
  38. $response = HttpService::httpPost($requestUri, json_encode($postData));
  39. if(false === $response && $retry < 5) {
  40. sleep(1);
  41. $retry++;
  42. return self::customer_conversation_report($corpid, $userid, $start_time, $end_time, $retry);
  43. }
  44. $responseData = json_decode($response, true);
  45. Log::logInfo('获取联系客户统计数据', [
  46. 'params' => ['corpid' => $corpid, 'user_id' => $userid,'start_time'=>$start_time,'end_time'=>$end_time],
  47. 'response' => $responseData
  48. ], 'qyWechat');
  49. if(isset($responseData['errcode']) && -1 == $responseData['errcode'] && $retry<5) {
  50. sleep(1);
  51. $retry++;
  52. return self::customer_conversation_report($corpid, $userid, $start_time, $end_time, $retry);
  53. }
  54. return $responseData;
  55. }
  56. }