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

编辑资料api


controller层:application\api\controller\v1\User.php

php
// 修改资料
public function editinfo(){
    (new UserValidate())->goCheck('edituserinfo');
    (new UserModel())->editUserinfo();
    return self::showResCodeWithOutData('修改成功');
}

route层:route\route.php

php
// 用户操作(绑定手机)
Route::group('api/:v1/',function(){
	// 编辑资料
    Route::post('edituserinfo','api/v1.User/editinfo');
})->middleware(['ApiUserAuth','ApiUserBindPhone','ApiUserStatus']);

validate层:application\common\validate\UserValidate.php

php
protected $rule = [
    'name'=>'require|chsDash',
    'sex'=>'require|in:0,1,2',
    'qg'=>'require|in:0,1,2',
    'job'=>'require|chsAlpha',
    'birthday'=>'require|dateFormat:Y-m-d',
    'path'=>'require|chsDash',
];
protected $scene = [
    'edituserinfo'=>['name','sex','qg','job','birthday','path'],
];

model层:application\common\model\User.php

php
// 修改资料
public function editUserinfo(){
    // 获取所有参数
    $params = request()->param();
    // 获取用户id
    $userid=request()->userId;
    // 修改...
    $user = $this->get($userid);
    $user->username = $params['name'];
    $user->save();
    // 修改用户信息表
    $userinfo = $user->userinfo()->find();
    $userinfo->sex = $params['sex'];
    $userinfo->qg = $params['qg'];
    $userinfo->job = $params['job'];
    $userinfo->birthday = $params['birthday'];
    $userinfo->path = $params['path'];
    $userinfo->save();
    return true;
}