Skip to content

Commit

Permalink
user update
Browse files Browse the repository at this point in the history
  • Loading branch information
liyu001989 committed May 11, 2019
1 parent 6b40334 commit 25b65ca
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 14 deletions.
27 changes: 27 additions & 0 deletions app/Http/Controllers/Api/ImagesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Controllers\Api;

use App\Models\Image;
use Illuminate\Http\Request;
use App\Handlers\ImageUploadHandler;
use App\Transformers\ImageTransformer;
use App\Http\Requests\Api\ImageRequest;

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

$size = $request->type == 'avatar' ? 362 : 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 $this->response->item($image, new ImageTransformer())->setStatusCode(201);
}
}
34 changes: 26 additions & 8 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\Transformers\UserTransformer;
use App\Http\Requests\Api\UserRequest;
Expand All @@ -18,8 +19,8 @@ public function store(UserRequest $request)
}

if (!hash_equals($verifyData['code'], $request->verification_code)) {
// 返回401
return $this->response->errorUnauthorized('验证码错误');
// 返回401
return $this->response->errorUnauthorized('验证码错误');
}

$user = User::create([
Expand All @@ -32,16 +33,33 @@ public function store(UserRequest $request)
\Cache::forget($request->verification_key);

return $this->response->item($user, new UserTransformer())
->setMeta([
'access_token' => \Auth::guard('api')->fromUser($user),
'token_type' => 'Bearer',
'expires_in' => \Auth::guard('api')->factory()->getTTL() * 60
])
->setStatusCode(201);
->setMeta([
'access_token' => \Auth::guard('api')->fromUser($user),
'token_type' => 'Bearer',
'expires_in' => \Auth::guard('api')->factory()->getTTL() * 60
])
->setStatusCode(201);
}

public function me()
{
return $this->response->item($this->user(), new UserTransformer());
}

public function update(UserRequest $request)
{
$user = $this->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 $this->response->item($user, new UserTransformer());
}
}
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 以上',
];
}
}
37 changes: 31 additions & 6 deletions app/Http/Requests/Api/UserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,44 @@ class UserRequest extends FormRequest
{
public function rules()
{
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',
];
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::guard('api')->id();

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

public function attributes()
{
return [
'verification_key' => '短信验证码 key',
'verification_code' => '短信验证码',
'introduction' => '个人简介',
];
}

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

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

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

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

namespace App\Transformers;

use App\Models\Image;
use League\Fractal\TransformerAbstract;

class ImageTransformer extends TransformerAbstract
{
public function transform(Image $image)
{
return [
'id' => $image->id,
'user_id' => $image->user_id,
'type' => $image->type,
'path' => $image->path,
'created_at' => (string) $image->created_at,
'updated_at' => (string) $image->updated_at,
];
}
}
34 changes: 34 additions & 0 deletions database/migrations/2019_05_11_220518_create_images_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

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

class CreateImagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
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();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
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 @@ -61,6 +61,12 @@
// 当前登录用户信息
$api->get('user', 'UsersController@me')
->name('api.user.show');
// 编辑登录用户信息
$api->patch('user', 'UsersController@update')
->name('api.user.update');
// 图片资源
$api->post('images', 'ImagesController@store')
->name('api.images.store');
});
});
});

0 comments on commit 25b65ca

Please sign in to comment.