Skip to content
关注公众号,获取新课通知
【重要通知】uniapp实战社区交友交流群更换为:602180461,靓仔/靓女可以重新申请加入哦~

编辑头像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();
}