SmmUserAccount.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Distributor\Repositories;
  3. use App\Models\SmmUserAccount as Model;
  4. use Carbon\Carbon;
  5. use Dcat\Admin\Repositories\EloquentRepository;
  6. class SmmUserAccount extends EloquentRepository
  7. {
  8. /**
  9. * Model.
  10. *
  11. * @var string
  12. */
  13. protected $eloquentClass = Model::class;
  14. /**
  15. * 插入新的社媒账号(仅在找到对应社媒时插入)
  16. *
  17. * @param string $mediaName 要查找的社媒名称
  18. * @param string $accountName 要插入的账号名称
  19. * @param string $accessToken 访问令牌
  20. * @return Model|null 新创建的模型实例或null
  21. */
  22. public static function createAccountIfMediaExists($mediaName, $accountId,$accountName, $accessToken,$expiresAt,$refreshToken = '',$backupField1 = '')
  23. {
  24. $model = new Model();
  25. // 查找匹配的社媒记录
  26. $mediaRecord = $model->where('name', $mediaName)->first();
  27. if (!$mediaRecord) {
  28. return null;
  29. }
  30. // 查找是否存在相同的账号
  31. $userRow = $model->where('account_id', $accountId)->first();
  32. if ($userRow) {
  33. $userRow->access_token = $accessToken;
  34. $userRow->expires_at = $expiresAt;
  35. $userRow->name = $accountName;
  36. $userRow->refresh_token = $refreshToken;
  37. $userRow->backup_field1 = $backupField1;
  38. $userRow->save();
  39. } else {
  40. // 创建新账号并关联父级
  41. $data = [
  42. 'account_id' => $accountId,
  43. 'name' => $accountName,
  44. 'access_token' => $accessToken,
  45. 'expires_at' => $expiresAt,
  46. 'parent_id' => $mediaRecord->id,
  47. 'dist_id' => getDistributorId(),
  48. 'created_at' => Carbon::now(), // 自动生成时间戳
  49. 'updated_at' => Carbon::now(),
  50. 'refresh_token' => $refreshToken,
  51. 'backup_field1' => $backupField1, //备用字段
  52. ];
  53. $model->insert($data);
  54. }
  55. return true;
  56. }
  57. /*
  58. * 查出dist_id=0和parent_id=0的账号
  59. */
  60. public static function getRootAccounts()
  61. {
  62. $model = new Model();
  63. $accounts = $model->where('dist_id', 0)->where('parent_id', 0)->get();
  64. return $accounts;
  65. }
  66. /*
  67. * 查找当前用户下所有用户账号(只显示有效的账号)
  68. */
  69. public static function getUserAccounts()
  70. {
  71. $model = new Model();
  72. $accounts = $model->where('dist_id','=', getDistributorId())->where('parent_id','>',0)->where('expires_at','>', Carbon::now())->orderBy('parent_id', 'asc')->get();
  73. return $accounts;
  74. }
  75. /*
  76. * 查找所有用户账号 (全站账号)
  77. */
  78. public static function getAllYouTubeUserAccounts()
  79. {
  80. $model = new Model();
  81. $youTube = $model->where('name', 'YouTube')->first();
  82. $twitter = $model->where('name', 'Twitter')->first();
  83. $parentIds = [$youTube->id,$twitter->id];
  84. //15分钟前
  85. $time = Carbon::now()->addMinutes(15);
  86. $accounts = $model->whereIn('parent_id',$parentIds)->where('expires_at','<', $time)->where('expires_at','>', Carbon::now())->orderBy('id', 'asc')->get();
  87. //dd($accounts->toArray());
  88. return $accounts;
  89. }
  90. /*
  91. * 查找所有用户账号
  92. */
  93. public static function getAccountsByIds($ids)
  94. {
  95. $model = new Model();
  96. $accounts = $model->whereIn('id', $ids)->get();
  97. return $accounts;
  98. }
  99. public static function getAccountById($id)
  100. {
  101. $model = new Model();
  102. $account = $model->find($id);
  103. return $account;
  104. }
  105. /*
  106. * 返回子账号数量
  107. */
  108. public static function returnChildCount($id, $distId)
  109. {
  110. $model = new Model();
  111. $count = $model->where('parent_id', $id)->where('dist_id', $distId)->where('expires_at','>', Carbon::now())->count();
  112. return $count;
  113. }
  114. public static function getYoutubeCategory()
  115. {
  116. $model = new Model();
  117. $categories = $model->where('name', 'YouTube')->first();
  118. $categories = json_decode($categories->backup_field1,true);
  119. return $categories;
  120. }
  121. public static function deleteAccount($id, $distId)
  122. {
  123. $model = new Model();
  124. $account = $model->find($id);
  125. if ($account && $account->dist_id == $distId && $account->parent_id > 0) {
  126. $account->delete();
  127. return true;
  128. }
  129. return false;
  130. }
  131. /*
  132. * 查找用户IDS中有无YouTube的帐号
  133. */
  134. public static function findYoutubeAccount($ids, $distId)
  135. {
  136. $model = new Model();
  137. $youTubeRs = $model->where('name', 'YouTube')->first();
  138. $result = $model->where('parent_id', $youTubeRs->id)->whereIn('id', $ids)->where('dist_id', $distId)->get();
  139. return $result;
  140. }
  141. /*
  142. * 判断用户是否可以发送POST
  143. * 根据IDS查找用户账号,找到是否超
  144. */
  145. public static function checkAccountCanSendPost($ids, $distId) {
  146. $model = new Model();
  147. $result = $model->whereIn('id', $ids)->where('dist_id', $distId)->get();
  148. $youTubeCount = 0;
  149. $twitterCount = 0;
  150. foreach ($result as $row) {
  151. if ($row->getParent->name == 'YouTube') {
  152. $youTubeCount++;
  153. }
  154. if ($row->getParent->name == 'Twitter') {
  155. $twitterCount++;
  156. }
  157. }
  158. if (SmmPostLog::getPostQuota('YouTube') < $youTubeCount) {
  159. return ['status'=>false,'mediaName'=>'YouTube'];
  160. }
  161. if (SmmPostLog::getPostQuota('Twitter') < $twitterCount) {
  162. return ['status'=>false,'mediaName'=>'Twitter'];
  163. }
  164. return ['status'=>true];
  165. }
  166. }