On this page
编辑头像api
controller层:application\api\controller\v1\User.php
php
// 修改头像
public function editUserpic(){
(new UserValidate())->goCheck('edituserpic');
(new UserModel())->editUserpic();
return self::showResCodeWithOutData('修改头像成功');
}
route层:route\route.php
php
// 用户操作(绑定手机)
Route::group('api/:v1/',function(){
// 编辑头像
Route::post('edituserpic','api/v1.User/editUserpic');
})->middleware(['ApiUserAuth','ApiUserBindPhone','ApiUserStatus']);
validate层:application\common\model\UserValidate.php
php
protected $rule = [
'userpic'=>'image',
];
protected $scene = [
'edituserpic'=>['userpic'],
];
model层:application\common\model\User.php
php
// 修改头像
public function editUserpic(){
// 获取所有参数
$params = request()->param();
// 获取用户id
$userid=request()->userId;
$image = (new Image())->upload($userid,'userpic');
// 修改用户头像
$user = self::get($userid);
$user->userpic = getFileUrl($image->url);
if($user->save()) return true;
TApiException();
}