SmmUserAccount.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. public static function createAccountIfMediaExistsCookie($mediaName, $accountId,$accountName, $cookieId,$cookieData)
  58. {
  59. if (empty($accountId) || empty($cookieId) || empty($cookieData) || empty($mediaName)) {
  60. return false;
  61. }
  62. $model = new Model();
  63. // 查找匹配的社媒记录
  64. $mediaRecord = $model->where('name', $mediaName)->first();
  65. if (!$mediaRecord) {
  66. return null;
  67. }
  68. // 查找是否存在相同的账号
  69. $userRow = $model->where('account_id', $accountId)->where('dist_id',getDistributorId())->first();
  70. if ($userRow) {
  71. $userRow->cookie_id = $cookieId;
  72. $userRow->cookie_data = $cookieData;
  73. $userRow->name = $accountName;
  74. $userRow->save();
  75. } else {
  76. // 创建新账号并关联父级
  77. $data = [
  78. 'account_id' => $accountId,
  79. 'name' => $accountName,
  80. 'parent_id' => $mediaRecord->id,
  81. 'dist_id' => getDistributorId(),
  82. 'created_at' => Carbon::now(), // 自动生成时间戳
  83. 'updated_at' => Carbon::now(),
  84. 'cookie_id' => $cookieId,
  85. 'cookie_data' => $cookieData, //备用字段
  86. ];
  87. $model->insert($data);
  88. }
  89. return true;
  90. }
  91. /*
  92. * 查出dist_id=0和parent_id=0的账号
  93. */
  94. public static function getRootAccounts()
  95. {
  96. $model = new Model();
  97. $accounts = $model->where('dist_id', 0)->where('parent_id', 0)->get();
  98. return $accounts;
  99. }
  100. /*
  101. * 查找当前用户下所有用户账号(只显示有效的账号)
  102. */
  103. public static function getUserAccounts()
  104. {
  105. $model = new Model();
  106. $accounts = $model->where('dist_id','=', getDistributorId())->where('parent_id','>',0)->where('expires_at','>', Carbon::now())->orderBy('parent_id', 'asc')->get();
  107. return $accounts;
  108. }
  109. /*
  110. * 查找所有用户账号 (全站账号)
  111. */
  112. public static function getAllYouTubeUserAccounts()
  113. {
  114. $model = new Model();
  115. $youTube = $model->where('name', 'YouTube')->first();
  116. $twitter = $model->where('name', 'Twitter')->first();
  117. $parentIds = [$youTube->id,$twitter->id];
  118. //15分钟前
  119. $time = Carbon::now()->addMinutes(15);
  120. $accounts = $model->whereIn('parent_id',$parentIds)->where('expires_at','<', $time)->where('expires_at','>', Carbon::now())->orderBy('id', 'asc')->get();
  121. //dd($accounts->toArray());
  122. return $accounts;
  123. }
  124. /*
  125. * 查找所有用户账号
  126. */
  127. public static function getAccountsByIds($ids)
  128. {
  129. $model = new Model();
  130. $accounts = $model->whereIn('id', $ids)->get();
  131. return $accounts;
  132. }
  133. public static function getAccountById($id)
  134. {
  135. $model = new Model();
  136. $account = $model->find($id);
  137. return $account;
  138. }
  139. /*
  140. * 返回子账号数量
  141. */
  142. public static function returnChildCount($id, $distId)
  143. {
  144. $model = new Model();
  145. $count = $model->where('parent_id', $id)->where('dist_id', $distId)->where('expires_at','>', Carbon::now())->count();
  146. return $count;
  147. }
  148. public static function getYoutubeCategory()
  149. {
  150. $model = new Model();
  151. $categories = $model->where('name', 'YouTube')->first();
  152. $categories = json_decode($categories->backup_field1,true);
  153. return $categories;
  154. }
  155. public static function deleteAccount($id, $distId)
  156. {
  157. $model = new Model();
  158. $account = $model->find($id);
  159. if ($account && $account->dist_id == $distId && $account->parent_id > 0) {
  160. $account->delete();
  161. return true;
  162. }
  163. return false;
  164. }
  165. /*
  166. * 查找用户IDS中有无YouTube的帐号
  167. */
  168. public static function findYoutubeAccount($ids, $distId)
  169. {
  170. $model = new Model();
  171. $youTubeRs = $model->where('name', 'YouTube')->first();
  172. $result = $model->where('parent_id', $youTubeRs->id)->whereIn('id', $ids)->where('dist_id', $distId)->get();
  173. return $result;
  174. }
  175. /*
  176. * 判断用户是否可以发送POST
  177. * 根据IDS查找用户账号,找到是否超
  178. */
  179. public static function checkAccountCanSendPost($ids, $distId) {
  180. $model = new Model();
  181. $result = $model->whereIn('id', $ids)->where('dist_id', $distId)->get();
  182. $youTubeCount = 0;
  183. $twitterCount = 0;
  184. foreach ($result as $row) {
  185. if ($row->getParent->name == 'YouTube') {
  186. $youTubeCount++;
  187. }
  188. if ($row->getParent->name == 'Twitter') {
  189. $twitterCount++;
  190. }
  191. }
  192. if (SmmPostLog::getPostQuota('YouTube') < $youTubeCount) {
  193. return ['status'=>false,'mediaName'=>'YouTube'];
  194. }
  195. if (SmmPostLog::getPostQuota('Twitter') < $twitterCount) {
  196. return ['status'=>false,'mediaName'=>'Twitter'];
  197. }
  198. return ['status'=>true];
  199. }
  200. public static function getAccountByName($name)
  201. {
  202. $model = new Model();
  203. $account = $model->where('name', $name)->first();
  204. return $account;
  205. }
  206. }