123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- /*
- * +----------------------------------------------------------------------+
- * | ThinkSNS Plus |
- * +----------------------------------------------------------------------+
- * | Copyright (c) 2016-Present ZhiYiChuangXiang Technology Co., Ltd. |
- * +----------------------------------------------------------------------+
- * | This source file is subject to enterprise private license, that is |
- * | bundled with this package in the file LICENSE, and is available |
- * | through the world-wide-web at the following url: |
- * | https://github.com/slimkit/plus/blob/master/LICENSE |
- * +----------------------------------------------------------------------+
- * | Author: Slim Kit Group <master@zhiyicx.com> |
- * | Homepage: www.thinksns.com |
- * +----------------------------------------------------------------------+
- */
- namespace Zhiyi\Plus\Observers;
- use Illuminate\Http\Exceptions\HttpResponseException;
- use Illuminate\Http\UploadedFile;
- use Zhiyi\Plus\FileStorage\StorageInterface;
- use Zhiyi\Plus\Http\Controllers\APIs\V2\FilesController;
- use Zhiyi\Plus\Models\DdUser;
- use Zhiyi\Plus\Models\User;
- use Zhiyi\Plus\Models\Famous;
- use Zhiyi\Plus\Support\Configuration;
- class UserObserver
- {
- /**
- * Handle the user "created" event.
- *
- * @param User $user
- *
- * @return void
- */
- public function created(User $user)
- {
- // 处理默认关注和默认相互关注
- $famous = Famous::query()->with('user')->get()
- ->groupBy('type');
- $famous
- ->map(function ($type, $key) use ($user) {
- $users = $type->filter(function ($famou) {
- return $famou->user !== null;
- })->pluck('user');
- $user->followings()->attach($users->pluck('id'));
- // 相互关注
- if ($key === 'each') {
- $users->map(function ($source) use ($user) {
- $source->followings()->attach($user);
- });
- }
- });
- }
- public function creating(User $user){
- $user->avatar=self::getDefaultAvatar();
- }
- public static function getDefaultAvatar(){
- $path=config('resource.def_avatar');
- if(!$path) {
- $source = resource_path('images/def_avatar.png');
- $file=new UploadedFile($source,'def_avatar.png');
- $filePath = now()->format('Y/m/d/Hi');
- $fileSavePath=$file->store($filePath,'public');
- $path=config('app.url').'/storage/'.$fileSavePath;
- app(Configuration::class)->set('resource.def_avatar',$path);
- }
- return $path;
- }
- public function updating(User $user){
- if($user->isDirty('name')){
- $exists=DdUser::query()->where('name',$user->name)->exists();
- if($exists){
- throw new HttpResponseException(response(['message'=>'用户名已被保留,不能使用'],422));
- }
- }
- }
- }
|