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

修改密码api


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

php
// 修改密码
public function rePassword(){
    (new UserValidate())->goCheck('repassword'); 
    (new UserModel())->repassword();
    return self::showResCodeWithOutData('修改密码成功');
}

route层:route\route.php

php
// 用户操作(绑定手机)
Route::group('api/:v1/',function(){
	// 修改密码
    Route::post('repassword','api/v1.User/rePassword');
})->middleware(['ApiUserAuth','ApiUserBindPhone','ApiUserStatus']);

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

php
protected $rule = [
    'oldpassword'=>'require',
    'newpassword'=>'require|alphaDash',
    'renewpassword'=>'require|confirm:newpassword',
];
protected $scene = [
    'repassword'=>['oldpassword','newpassword','renewpassword'],
];

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

php
// 修改密码
public function repassword(){
    // 获取所有参数
    $params = request()->param();
    // 获取用户id
    $userid = request()->userId;
    $user = self::get($userid);
    // 手机注册的用户并没有原密码,直接修改即可
    if ($user['password']) {
        // 判断旧密码是否正确
        $this->checkPassword($params['oldpassword'],$user['password']);
    }
    // 修改密码
    $newpassword = password_hash($params['newpassword'],PASSWORD_DEFAULT);
    $res = $this->save([
        'password'=>$newpassword
    ],['id'=>$userid]);
    if (!$res) TApiException('修改密码失败',20009,200);
    $user['password'] = $newpassword;
    // 更新缓存信息
    Cache::set(request()->Token,$user,config('api.token_expire'));
}