SmmUserAccount.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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','>', 0)->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 getAccountsByIds($ids)
  79. {
  80. $model = new Model();
  81. $accounts = $model->whereIn('id', $ids)->get();
  82. return $accounts;
  83. }
  84. public static function getAccountById($id)
  85. {
  86. $model = new Model();
  87. $account = $model->find($id);
  88. return $account;
  89. }
  90. /*
  91. * 返回子账号数量
  92. */
  93. public static function returnChildCount($id)
  94. {
  95. $model = new Model();
  96. $count = $model->where('parent_id', $id)->count();
  97. return $count;
  98. }
  99. public static function getYoutubeCategory()
  100. {
  101. $model = new Model();
  102. $categories = $model->where('name', 'YouTube')->first();
  103. $categories = json_decode($categories->backup_field1,true);
  104. return $categories;
  105. }
  106. }