UserObserver.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /*
  3. * +----------------------------------------------------------------------+
  4. * | ThinkSNS Plus |
  5. * +----------------------------------------------------------------------+
  6. * | Copyright (c) 2016-Present ZhiYiChuangXiang Technology Co., Ltd. |
  7. * +----------------------------------------------------------------------+
  8. * | This source file is subject to enterprise private license, that is |
  9. * | bundled with this package in the file LICENSE, and is available |
  10. * | through the world-wide-web at the following url: |
  11. * | https://github.com/slimkit/plus/blob/master/LICENSE |
  12. * +----------------------------------------------------------------------+
  13. * | Author: Slim Kit Group <master@zhiyicx.com> |
  14. * | Homepage: www.thinksns.com |
  15. * +----------------------------------------------------------------------+
  16. */
  17. namespace Zhiyi\Plus\Observers;
  18. use Illuminate\Http\Exceptions\HttpResponseException;
  19. use Illuminate\Http\UploadedFile;
  20. use Zhiyi\Plus\FileStorage\StorageInterface;
  21. use Zhiyi\Plus\Http\Controllers\APIs\V2\FilesController;
  22. use Zhiyi\Plus\Models\DdUser;
  23. use Zhiyi\Plus\Models\User;
  24. use Zhiyi\Plus\Models\Famous;
  25. use Zhiyi\Plus\Support\Configuration;
  26. class UserObserver
  27. {
  28. /**
  29. * Handle the user "created" event.
  30. *
  31. * @param User $user
  32. *
  33. * @return void
  34. */
  35. public function created(User $user)
  36. {
  37. // 处理默认关注和默认相互关注
  38. $famous = Famous::query()->with('user')->get()
  39. ->groupBy('type');
  40. $famous
  41. ->map(function ($type, $key) use ($user) {
  42. $users = $type->filter(function ($famou) {
  43. return $famou->user !== null;
  44. })->pluck('user');
  45. $user->followings()->attach($users->pluck('id'));
  46. // 相互关注
  47. if ($key === 'each') {
  48. $users->map(function ($source) use ($user) {
  49. $source->followings()->attach($user);
  50. });
  51. }
  52. });
  53. }
  54. public function creating(User $user){
  55. $user->avatar=self::getDefaultAvatar();
  56. }
  57. public static function getDefaultAvatar(){
  58. $path=config('resource.def_avatar');
  59. if(!$path) {
  60. $source = resource_path('images/def_avatar.png');
  61. $file=new UploadedFile($source,'def_avatar.png');
  62. $filePath = now()->format('Y/m/d/Hi');
  63. $fileSavePath=$file->store($filePath,'public');
  64. $path=config('app.url').'/storage/'.$fileSavePath;
  65. app(Configuration::class)->set('resource.def_avatar',$path);
  66. }
  67. return $path;
  68. }
  69. public function updating(User $user){
  70. if($user->isDirty('name')){
  71. $exists=DdUser::query()->where('name',$user->name)->exists();
  72. if($exists){
  73. throw new HttpResponseException(response(['message'=>'用户名已被保留,不能使用'],422));
  74. }
  75. }
  76. }
  77. }