SmmUserAccount.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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)
  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->save();
  37. } else {
  38. // 创建新账号并关联父级
  39. $data = [
  40. 'account_id' => $accountId,
  41. 'name' => $accountName,
  42. 'access_token' => $accessToken,
  43. 'expires_at' => $expiresAt,
  44. 'parent_id' => $mediaRecord->id,
  45. 'dist_id' => getDistributorId(),
  46. 'created_at' => Carbon::now(), // 自动生成时间戳
  47. 'updated_at' => Carbon::now(),
  48. ];
  49. $model->insert($data);
  50. }
  51. return true;
  52. }
  53. /*
  54. * 查出dist_id=0和parent_id=0的账号
  55. */
  56. public static function getRootAccounts()
  57. {
  58. $model = new Model();
  59. $accounts = $model->where('dist_id', 0)->where('parent_id', 0)->get();
  60. return $accounts;
  61. }
  62. /*
  63. * 查找所有用户账号(只显示有效的账号)
  64. */
  65. public static function getUserAccounts()
  66. {
  67. $model = new Model();
  68. $accounts = $model->where('dist_id','>', 0)->where('parent_id','>',0)->where('expires_at','>', Carbon::now())->orderBy('parent_id', 'asc')->get();
  69. return $accounts;
  70. }
  71. /*
  72. * 查找所有用户账号
  73. */
  74. public static function getAccountsByIds($ids)
  75. {
  76. $model = new Model();
  77. $accounts = $model->whereIn('id', $ids)->get();
  78. return $accounts;
  79. }
  80. public static function getAccountById($id)
  81. {
  82. $model = new Model();
  83. $account = $model->find($id);
  84. return $account;
  85. }
  86. /*
  87. * 返回子账号数量
  88. */
  89. public static function returnChildCount($id)
  90. {
  91. $model = new Model();
  92. $count = $model->where('parent_id', $id)->count();
  93. return $count;
  94. }
  95. }