SmmPostLog.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. namespace App\Distributor\Repositories;
  3. use App\Libraries\CommonHelper;
  4. use App\Models\SmmPostLog as Model;
  5. use Carbon\Carbon;
  6. use Dcat\Admin\Repositories\EloquentRepository;
  7. use Illuminate\Support\Facades\Log;
  8. class SmmPostLog extends EloquentRepository
  9. {
  10. /**
  11. * Model.
  12. *
  13. * @var string
  14. */
  15. protected $eloquentClass = Model::class;
  16. /*
  17. * 创建日志
  18. */
  19. public static function createLog($data)
  20. {
  21. $log = new Model();
  22. $log->post_id = $data['post_id'];
  23. $log->account_id = $data['account_id'];
  24. $log->account_name = $data['account_name'];
  25. $log->status = $data['status'];
  26. $log->remark = $data['remark'];
  27. $log->created_at = $data['created_at'];
  28. $log->updated_at = $data['updated_at'];
  29. $log->dist_id = $data['dist_id'];
  30. $log->media_name = $data['media_name'];
  31. $log->response_ids = $data['response_ids'];
  32. $log->send_time = $data['send_time'];
  33. $log->save();
  34. }
  35. /*
  36. * 找状态为0并且发送时间小于当前时间的日志
  37. */
  38. public static function getSendLog($limit = 5)
  39. {
  40. $log = new Model();
  41. $result = $log->where('status', 1)->first();//查找状态为1的日志,发送中的日志
  42. if ($result) {
  43. //updated_at与当前时间差距30分钟以上,则更新日志状态为3
  44. $nowTime = Carbon::now();
  45. $diffTime = $nowTime->diffInMinutes($result->updated_at);
  46. if ($diffTime >= 30) {
  47. Log::info('发送中的帖子超过30分钟,更新状态为3,发送失败');
  48. $result->status = 3;
  49. $result->save();
  50. }
  51. Log::info('有正在发送中的帖子,返回[]');
  52. return [];
  53. }
  54. //找出待发送与发送失败的日志,重试2次
  55. $result = $log->wherein('status', [0,3])->where('request_count', '<=', 2)->where('send_time', '<', Carbon::now())->limit($limit)->get();
  56. return $result;
  57. }
  58. /*
  59. * 计算Twitter月配额,每月只能发送100个帖子,(只限发送帖子,其他接口,每个用户1个请求,15分钟内只允许发送一次)
  60. * 全站统计
  61. */
  62. public static function getPostQuota($mediaName = 'Twitter')
  63. {
  64. $log = new Model();
  65. if ($mediaName === 'Twitter') {
  66. // 获取当月第一天 00:00:00 的时间戳
  67. $startOfMonth = Carbon::now()->startOfMonth();
  68. $rows = $log->where('media_name', 'Twitter')
  69. ->where('created_at', '>=', $startOfMonth->format('Y-m-d H:i:s'))
  70. ->get();
  71. $count = 0;
  72. foreach ($rows as $row) {
  73. if ($row->status == 0) {
  74. $count++;
  75. } else {
  76. $count = $count + $row->request_count;
  77. }
  78. }
  79. return 100 - $count;
  80. } elseif ($mediaName === 'YouTube') {
  81. // 获取当天 00:00:00 的时间戳
  82. $startOfDay = Carbon::now()->startOfDay();
  83. $rows = $log->where('media_name', 'YouTube')
  84. ->where('created_at', '>=', $startOfDay->format('Y-m-d H:i:s'))
  85. ->get();
  86. $count = 0;
  87. foreach ($rows as $row) {
  88. if ($row->status == 0) {
  89. $count++;
  90. } else {
  91. $count = $count + $row->request_count;
  92. }
  93. }
  94. // 计算可用配额(向下取整)
  95. $quota = floor((10000 - 1600 * $count) / 1600);
  96. return max($quota, 0); // 保证最小返回0
  97. } else {
  98. return 999;
  99. }
  100. }
  101. /*
  102. * 返回Twitter可以发送的时间
  103. */
  104. public static function getTwitterSendTime()
  105. {
  106. // 将字符串转换为Carbon实例
  107. $sendTimeCarbon = Carbon::now();
  108. $log = new Model();
  109. $query = $log->where('status', 0)
  110. ->where('media_name', 'Twitter')
  111. ->where('send_time', '>=',$sendTimeCarbon->copy()->subMinutes(15))
  112. ->orderByDesc('id')
  113. ->first();
  114. if ($query) {
  115. // 返回格式化的时间字符串(与原输入格式保持一致)
  116. $query_send_time = Carbon::parse($query->send_time);
  117. return $query_send_time
  118. ->addMinutes(16)
  119. ->toDateTimeString();
  120. }
  121. // 没有记录时返回原始字符串
  122. return Carbon::now();
  123. }
  124. /*
  125. * 判断是否可以发送
  126. *
  127. */
  128. public static function twitterCanSend()
  129. {
  130. $log = new Model();
  131. //查找最后一条发送记录
  132. $lastSendLog = $log->wherein('status', [2,3])->where('media_name', 'Twitter')->orderByDesc('send_time')->first();
  133. //如果时间间隔小于15分钟,则不允许发送
  134. if ($lastSendLog) {
  135. $lastSendTime = Carbon::parse($lastSendLog->send_time);
  136. $nowTime = Carbon::now();
  137. $diffTime = $nowTime->diffInMinutes($lastSendTime);
  138. if ($diffTime <= 15) {
  139. return false;
  140. }
  141. }
  142. return true;
  143. }
  144. /*
  145. * 得到当前用户要发送帖子的日志
  146. */
  147. public static function getSendLogDist($limit = 5)
  148. {
  149. $log = new Model();
  150. $result = $log->where('status', 1)->where('dist_id', getDistributorId())->first();//查找状态为1的日志,发送中的日志
  151. if ($result) {
  152. //updated_at与当前时间差距30分钟以上,则更新日志状态为3
  153. $nowTime = Carbon::now();
  154. $diffTime = $nowTime->diffInMinutes($result->updated_at);
  155. if ($diffTime >= 30) {
  156. Log::info('发送中的帖子超过30分钟,更新状态为3,发送失败');
  157. $result->status = 3;
  158. $result->save();
  159. }
  160. return [];
  161. }
  162. //找出待发送与发送失败的日志,重试2次
  163. //$result = $log->wherein('status', [0,3])->where('request_count', '<=', 10)->where('send_time', '<', Carbon::now())->where('dist_id', getDistributorId())->limit($limit)->get();
  164. $result = $log->where('id',22)->get();
  165. $resultArray = $result->toArray();
  166. foreach ($result as $k => $v) {
  167. $image_video_url = explode(',', $v->post->image_video_url);
  168. $tmp = [];
  169. foreach ($image_video_url as $key => $value) {
  170. $tmp[] = CommonHelper::ossUrl($value);
  171. }
  172. $image_video_url = implode(',', $tmp);
  173. $resultArray[$k]['message'] = $v->post->message;
  174. $resultArray[$k]['post_type'] = $v->post->post_type;
  175. $resultArray[$k]['image_video_url'] = $image_video_url;
  176. $resultArray[$k]['cookie_data'] = $v->account->cookie_data;
  177. $resultArray[$k]['account_id'] = $v->account->account_id;
  178. $resultArray[$k]['post_backup_field1'] = $v->post->backup_field1;
  179. }
  180. return $resultArray;
  181. }
  182. public static function updateSendLogDist($id, $status, $request_count = 0,$remark = '')
  183. {
  184. $log = new Model();
  185. $result = $log->where('id', $id)->first();
  186. if ($result) {
  187. $result->status = $status;
  188. $request_count = intval($request_count);
  189. if ($request_count > 0) {
  190. $result->request_count = $request_count;
  191. } else {
  192. $result->request_count = $result->request_count + 1;
  193. }
  194. $result->remark = $remark;
  195. $result->save();
  196. return true;
  197. }
  198. return false;
  199. }
  200. }