SmmPostLog.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Distributor\Repositories;
  3. use App\Models\SmmPostLog as Model;
  4. use Carbon\Carbon;
  5. use Dcat\Admin\Repositories\EloquentRepository;
  6. use Illuminate\Support\Facades\Log;
  7. class SmmPostLog extends EloquentRepository
  8. {
  9. /**
  10. * Model.
  11. *
  12. * @var string
  13. */
  14. protected $eloquentClass = Model::class;
  15. /*
  16. * 创建日志
  17. */
  18. public static function createLog($data)
  19. {
  20. $log = new Model();
  21. $log->post_id = $data['post_id'];
  22. $log->account_id = $data['account_id'];
  23. $log->account_name = $data['account_name'];
  24. $log->status = $data['status'];
  25. $log->remark = $data['remark'];
  26. $log->created_at = $data['created_at'];
  27. $log->updated_at = $data['updated_at'];
  28. $log->dist_id = $data['dist_id'];
  29. $log->media_name = $data['media_name'];
  30. $log->response_ids = $data['response_ids'];
  31. $log->send_time = $data['send_time'];
  32. $log->save();
  33. }
  34. /*
  35. * 找状态为0并且发送时间小于当前时间的日志
  36. */
  37. public static function getSendLog($limit = 5)
  38. {
  39. $log = new Model();
  40. $result = $log->where('status', 1)->first();//查找状态为1的日志,发送中的日志
  41. if ($result) {
  42. //updated_at与当前时间差距30分钟以上,则更新日志状态为3
  43. $nowTime = Carbon::now();
  44. $diffTime = $nowTime->diffInMinutes($result->updated_at);
  45. if ($diffTime >= 30) {
  46. Log::info('发送中的帖子超过30分钟,更新状态为3,发送失败');
  47. $result->status = 3;
  48. $result->request_count = $result->request_count + 1;
  49. $result->save();
  50. }
  51. Log::info('有正在发送中的帖子,返回[]');
  52. return [];
  53. }
  54. //找出是否有帖子正在上传到Oss如果有,则不发送
  55. $ossUploadPost = SmmPost::getOssUploadPost();
  56. if ($ossUploadPost->isNotEmpty()) {
  57. Log::info('有帖子正在上传到oss,不发送帖子');
  58. return [];
  59. }
  60. //找出待发送与发送失败的日志
  61. $result = $log->wherein('status', [0,3])->where('request_count', '<=', 2)->where('send_time', '<', Carbon::now())->limit($limit)->get();
  62. return $result;
  63. }
  64. /*
  65. * 计算Twitter月配额,每月只能发送100个帖子,(只限发送帖子,其他接口,每个用户1个请求,15分钟内只允许发送一次)
  66. *
  67. */
  68. public static function getPostQuota($mediaName = 'Twitter')
  69. {
  70. $log = new Model();
  71. if ($mediaName === 'Twitter') {
  72. // 获取当月第一天 00:00:00 的时间戳
  73. $startOfMonth = Carbon::now()->startOfMonth();
  74. $count = $log->where('media_name', 'Twitter')
  75. ->where('created_at', '>=', $startOfMonth->format('Y-m-d H:i:s'))
  76. ->count();
  77. return 100 - $count;
  78. } elseif ($mediaName === 'YouTube') {
  79. // 获取当天 00:00:00 的时间戳
  80. $startOfDay = Carbon::now()->startOfDay();
  81. $count = $log->where('media_name', 'YouTube')
  82. ->where('created_at', '>=', $startOfDay->format('Y-m-d H:i:s'))
  83. ->count();
  84. // 计算可用配额(向下取整)
  85. $quota = floor((10000 - 1600 * $count) / 1600);
  86. return max($quota, 0); // 保证最小返回0
  87. } else {
  88. return 999;
  89. }
  90. }
  91. /*
  92. * 返回Twitter可以发送的时间
  93. */
  94. public static function getTwitterSendTime()
  95. {
  96. // 将字符串转换为Carbon实例
  97. $sendTimeCarbon = Carbon::now();
  98. $log = new Model();
  99. $query = $log->where('status', 0)
  100. ->where('media_name', 'Twitter')
  101. ->where('send_time', '>=',$sendTimeCarbon->copy()->subMinutes(15))
  102. ->orderByDesc('id')
  103. ->first();
  104. if ($query) {
  105. // 返回格式化的时间字符串(与原输入格式保持一致)
  106. $query_send_time = Carbon::parse($query->send_time);
  107. return $query_send_time
  108. ->addMinutes(16)
  109. ->toDateTimeString();
  110. }
  111. // 没有记录时返回原始字符串
  112. return Carbon::now();
  113. }
  114. /*
  115. * 判断是否可以发送
  116. *
  117. */
  118. public static function twitterCanSend()
  119. {
  120. $log = new Model();
  121. //查找最后一条发送记录
  122. $lastSendLog = $log->wherein('status', [2,3])->where('media_name', 'Twitter')->orderByDesc('send_time')->first();
  123. //如果时间间隔小于15分钟,则不允许发送
  124. if ($lastSendLog) {
  125. $lastSendTime = Carbon::parse($lastSendLog->send_time);
  126. $nowTime = Carbon::now();
  127. $diffTime = $nowTime->diffInMinutes($lastSendTime);
  128. if ($diffTime <= 15) {
  129. return false;
  130. }
  131. }
  132. return true;
  133. }
  134. }