SmmUserAccount.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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)
  23. {
  24. $model = new Model();
  25. // 查找匹配的社媒记录
  26. $mediaRecord = $model->where('title', $mediaName)->first();
  27. if (!$mediaRecord) {
  28. return null;
  29. }
  30. // 创建新账号并关联父级
  31. $data = [
  32. 'account_id' => $accountId,
  33. 'title' => $accountName,
  34. 'access_token' => $accessToken,
  35. 'parent_id' => $mediaRecord->id,
  36. 'created_at' => Carbon::now(), // 自动生成时间戳
  37. 'updated_at' => Carbon::now(),
  38. ];
  39. return $model->insert($data);
  40. }
  41. }