修改用户头像和部分资料

This commit is contained in:
fthvgb1 2018-06-10 17:25:41 +08:00
parent 4c45d9b92a
commit b908987973
8 changed files with 209 additions and 6 deletions

View File

@ -0,0 +1,23 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\ImageRequest;
use App\Models\Image;
use App\Tools\ImageUploadTool;
use App\Transformers\ImageTransformer;
class ImagesController extends Controller
{
public function store(ImageRequest $request, ImageUploadTool $uploadTool, Image $image)
{
$user = $this->user();
$size = $request->get('type') == 'avatar' ? 362 : 1024;
$result = $uploadTool->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);
}
}

View File

@ -3,6 +3,7 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use App\Http\Requests\UserRequest; use App\Http\Requests\UserRequest;
use App\Models\Image;
use App\Models\User; use App\Models\User;
use App\TransFormers\UserTransformer; use App\TransFormers\UserTransformer;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
@ -31,6 +32,18 @@ class UsersController extends Controller
->setStatusCode(201); ->setStatusCode(201);
} }
public function update(UserRequest $request)
{
$user = $this->user();
$attributes = $request->only(['name', 'email', 'introduction']);
if ($request->get('avatar_image_id')) {
$image = Image::find($request['avatar_image_id']);
$attributes['avatar'] = $image->path;
}
$user->update($attributes);
return $this->response->item($user, new UserTransformer());
}
public function me() public function me()
{ {
return $this->response->item($this->user(), new UserTransformer()); return $this->response->item($this->user(), new UserTransformer());

View File

@ -0,0 +1,45 @@
<?php
namespace App\Http\Requests\Api;
use Illuminate\Foundation\Http\FormRequest;
class ImageRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
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 以上',
];
}
}

View File

@ -23,12 +23,29 @@ class UserRequest extends FormRequest
*/ */
public function rules() public function rules()
{ {
return [
'name' => 'required|string|max:255', switch ($this->method()) {
'password' => 'required|string|min:6', case 'POSt':
'verification_key' => 'required|string', return [
'verification_code' => 'required|string', 'name' => 'required|string|max:255',
]; 'password' => 'required|string|min:6',
'verification_key' => 'required|string',
'verification_code' => 'required|string',
];
case 'PATCH':
$user_id = \Auth::guard('api')->id();
return [
'name' => 'between:3,25|regex:/^[A-Za-z0-9\-\_]+$/|unique:users,name,' . $user_id,
'email' => 'email',
'introduction' => 'max:80',
'avatar_image_id' => 'exists:images,id,type,avatar,user_id,' . $user_id,
];
default:
return [];
}
} }
public function attributes() public function attributes()
@ -36,6 +53,17 @@ class UserRequest extends FormRequest
return [ return [
'verification_key' => '短信验证码 key', 'verification_key' => '短信验证码 key',
'verification_code' => '短信验证码', 'verification_code' => '短信验证码',
'introduction' => '个人简介',
];
}
public function messages()
{
return [
'name.unique' => '用户名已被占用,请重新填写',
'name.regex' => '用户名只支持英文、数字、横杆和下划线。',
'name.between' => '用户名必须介于 3 - 25 个字符之间。',
'name.required' => '用户名不能为空。',
]; ];
} }
} }

25
app/Models/Image.php Normal file
View File

@ -0,0 +1,25 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
/**
* Class Image
* @property int id
* @property int user_id
* @property string created_at
* @property string updated_at
* @property string type
* @property string path
* @package App\Models
*/
class Image extends Model
{
protected $fillable = ['type', 'path'];
public function user()
{
return $this->belongsTo(User::class);
}
}

View File

@ -0,0 +1,29 @@
<?php
/**
* Created by PhpStorm.
* User: xing
* Date: 2018/6/10
* Time: 16:47
*/
namespace App\Transformers;
use App\Models\Image;
use Carbon\Carbon;
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' => $image->created_at instanceof Carbon ? $image->created_at->toDateTimeString() : $image->created_at,
'updated_at' => $image->updated_at instanceof Carbon ? $image->updated_at->toDateTimeString() : $image->updated_at,
];
}
}

View File

@ -0,0 +1,34 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateImagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->index();
$table->enum('type', ['avatar', 'topic'])->index();
$table->string('path')->index();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('images');
}
}

View File

@ -54,6 +54,12 @@ $api->version('v1', [
// 当前登录用户信息 // 当前登录用户信息
$api->get('user', 'UsersController@me') $api->get('user', 'UsersController@me')
->name('api.user.show'); ->name('api.user.show');
// 图片资源
$api->post('images', 'ImagesController@store')
->name('api.images.store');
// 编辑登录用户信息
$api->patch('user', 'UsersController@update')
->name('api.user.update');
}); });
}); });