diff --git a/app/Http/Controllers/Api/ImagesController.php b/app/Http/Controllers/Api/ImagesController.php new file mode 100644 index 0000000..75035f1 --- /dev/null +++ b/app/Http/Controllers/Api/ImagesController.php @@ -0,0 +1,23 @@ +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); + } +} diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 49b177c..17afac7 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Api; use App\Http\Requests\UserRequest; +use App\Models\Image; use App\Models\User; use App\TransFormers\UserTransformer; use Illuminate\Support\Facades\Cache; @@ -31,6 +32,18 @@ class UsersController extends Controller ->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() { return $this->response->item($this->user(), new UserTransformer()); diff --git a/app/Http/Requests/Api/ImageRequest.php b/app/Http/Requests/Api/ImageRequest.php new file mode 100644 index 0000000..51298a8 --- /dev/null +++ b/app/Http/Requests/Api/ImageRequest.php @@ -0,0 +1,45 @@ + '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 以上', + ]; + } +} diff --git a/app/Http/Requests/UserRequest.php b/app/Http/Requests/UserRequest.php index d87d013..60b28bc 100644 --- a/app/Http/Requests/UserRequest.php +++ b/app/Http/Requests/UserRequest.php @@ -23,12 +23,29 @@ class UserRequest extends FormRequest */ public function rules() { - return [ - 'name' => 'required|string|max:255', - 'password' => 'required|string|min:6', - 'verification_key' => 'required|string', - 'verification_code' => 'required|string', - ]; + + switch ($this->method()) { + case 'POSt': + return [ + '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() @@ -36,6 +53,17 @@ class UserRequest extends FormRequest return [ 'verification_key' => '短信验证码 key', 'verification_code' => '短信验证码', + 'introduction' => '个人简介', + ]; + } + + public function messages() + { + return [ + 'name.unique' => '用户名已被占用,请重新填写', + 'name.regex' => '用户名只支持英文、数字、横杆和下划线。', + 'name.between' => '用户名必须介于 3 - 25 个字符之间。', + 'name.required' => '用户名不能为空。', ]; } } diff --git a/app/Models/Image.php b/app/Models/Image.php new file mode 100644 index 0000000..14943bd --- /dev/null +++ b/app/Models/Image.php @@ -0,0 +1,25 @@ +belongsTo(User::class); + } +} diff --git a/app/Transformers/ImageTransformer.php b/app/Transformers/ImageTransformer.php new file mode 100644 index 0000000..0f93a83 --- /dev/null +++ b/app/Transformers/ImageTransformer.php @@ -0,0 +1,29 @@ + $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, + ]; + } +} \ No newline at end of file diff --git a/database/migrations/2018_06_10_164049_create_images_table.php b/database/migrations/2018_06_10_164049_create_images_table.php new file mode 100644 index 0000000..1f0b146 --- /dev/null +++ b/database/migrations/2018_06_10_164049_create_images_table.php @@ -0,0 +1,34 @@ +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'); + } +} diff --git a/routes/api.php b/routes/api.php index f691895..06d9582 100644 --- a/routes/api.php +++ b/routes/api.php @@ -54,6 +54,12 @@ $api->version('v1', [ // 当前登录用户信息 $api->get('user', 'UsersController@me') ->name('api.user.show'); + // 图片资源 + $api->post('images', 'ImagesController@store') + ->name('api.images.store'); + // 编辑登录用户信息 + $api->patch('user', 'UsersController@update') + ->name('api.user.update'); }); });