Skip to content

Commit

Permalink
编辑用户信息
Browse files Browse the repository at this point in the history
  • Loading branch information
liyu001989 committed Aug 6, 2020
1 parent 31f6413 commit 0df7163
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 6 deletions.
28 changes: 28 additions & 0 deletions app/Http/Controllers/Api/ImagesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\Image;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Handlers\ImageUploadHandler;
use App\Http\Resources\ImageResource;
use App\Http\Requests\Api\ImageRequest;

class ImagesController extends Controller
{
public function store(ImageRequest $request, ImageUploadHandler $uploader, Image $image)
{
$user = $request->user();

$size = $request->type == 'avatar' ? 416 : 1024;
$result = $uploader->save($request->image, Str::plural($request->type), $user->id, $size);

$image->path = $result['path'];
$image->type = $request->type;
$image->user_id = $user->id;
$image->save();

return new ImageResource($image);
}
}
18 changes: 18 additions & 0 deletions app/Http/Controllers/Api/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api;

use App\Models\User;
use App\Models\Image;
use Illuminate\Http\Request;
use App\Http\Resources\UserResource;
use App\Http\Requests\Api\UserRequest;
Expand Down Expand Up @@ -44,4 +45,21 @@ public function me(Request $request)
{
return (new UserResource($request->user()))->showSensitiveFields();
}

public function update(UserRequest $request)
{
$user = $request->user();

$attributes = $request->only(['name', 'email', 'introduction']);

if ($request->avatar_image_id) {
$image = Image::find($request->avatar_image_id);

$attributes['avatar'] = $image->path;
}

$user->update($attributes);

return (new UserResource($user))->showSensitiveFields();
}
}
29 changes: 29 additions & 0 deletions app/Http/Requests/Api/ImageRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Http\Requests\Api;

class ImageRequest extends FormRequest
{
public function rules()
{

$rules = [
'type' => 'required|string|in:avatar,topic',
];

if ($this->type == 'avatar') {
$rules['image'] = 'required|mimes:jpeg,bmp,png,gif|dimensions:min_width=200,min_height=200';
} else {
$rules['image'] = 'required|mimes:jpeg,bmp,png,gif';
}

return $rules;
}

public function messages()
{
return [
'image.dimensions' => '图片的清晰度不够,宽和高需要 200px 以上',
];
}
}
42 changes: 36 additions & 6 deletions app/Http/Requests/Api/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,34 @@

class UserRequest extends FormRequest
{

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name',
'password' => 'required|alpha_dash|min:6',
'verification_key' => 'required|string',
'verification_code' => 'required|string',
];
switch($this->method()) {
case 'POST':
return [
'name' => 'required|between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name',
'password' => 'required|string|min:6',
'verification_key' => 'required|string',
'verification_code' => 'required|string',
];
break;
case 'PATCH':
$userId = auth('api')->id();

return [
'name' => 'between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name,' .$userId,
'email'=>'email|unique:users,email,'.$userId,
'introduction' => 'max:80',
'avatar_image_id' => 'exists:images,id,type,avatar,user_id,'.$userId,
];
break;
}
}

public function attributes()
Expand All @@ -21,4 +41,14 @@ public function attributes()
'verification_code' => '短信验证码',
];
}

public function messages()
{
return [
'name.unique' => '用户名已被占用,请重新填写',
'name.regex' => '用户名只支持英文、数字、横杆和下划线。',
'name.between' => '用户名必须介于 3 - 25 个字符之间。',
'name.required' => '用户名不能为空。',
];
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/ImageResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ImageResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return parent::toArray($request);
}
}
13 changes: 13 additions & 0 deletions app/Models/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

class Image extends Model
{
protected $fillable = ['type', 'path'];

public function user()
{
return $this->belongsTo(User::class);
}
}
24 changes: 24 additions & 0 deletions database/migrations/2020_08_06_234823_create_images_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateImagesTable extends Migration
{
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->index();
$table->string('type')->index();
$table->string('path');
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('images');
}
}
6 changes: 6 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@
// 当前登录用户信息
Route::get('user', 'UsersController@me')
->name('user.show');
// 编辑登录用户信息
Route::patch('user', 'UsersController@update')
->name('user.update');
// 上传图片
Route::post('images', 'ImagesController@store')
->name('images.store');
});
});
});

0 comments on commit 0df7163

Please sign in to comment.