Browse Source

设置密码

master
wanghongjun 2 years ago
parent
commit
c1b220f8a8
  1. 18
      app/api/controller/User.php
  2. 16
      app/api/model/User.php
  3. 16
      app/api/service/User.php
  4. 3
      app/api/validate/passport/Login.php

18
app/api/controller/User.php

@ -89,4 +89,22 @@ class User extends Controller
}
return $this->renderSuccess('恭喜您,信息修改成功');
}
/**
* 设置密码
* @return Json
* @throws BaseException
*/
public function setPassword(): Json
{
$UserService = new UserService();
if (!$UserService->validatePassword($this->postForm())) {
return $this->renderSuccess($UserService->getError());
}
$model = new UserModel;
if (!$model->savePassword($this->postForm())) {
return $this->renderSuccess($model->getError() ?: '操作失败');
}
return $this->renderSuccess('恭喜您,密码设置成功');
}
}

16
app/api/model/User.php

@ -126,4 +126,20 @@ class User extends UserModel
throwError('很抱歉,该手机号已绑定其他账户');
}
}
/**
* 保存新密码
* @param array $data
* @return bool
* @throws BaseException
*/
public function savePassword(array $data): bool
{
// 当前登录的用户信息
$userInfo = UserService::getCurrentLoginUser(true);
// 默认数据
$data['password'] = encryption_hash($data['password']);
// 更新用户记录
return $userInfo->save($data);
}
}

16
app/api/service/User.php

@ -14,6 +14,7 @@ namespace app\api\service;
use app\api\model\User as UserModel;
use app\api\model\UserOauth as UserOauthModel;
use app\api\validate\passport\Login as ValidateLogin;
use app\common\service\BaseService;
use cores\exception\BaseException;
@ -124,4 +125,19 @@ class User extends BaseService
return $token;
}
/**
* 密码验证
* @param $data
* @return bool
*/
public function validatePassword($data): bool
{
// 数据验证
$validate = new ValidateLogin;
if (!$validate->scene('upPassword')->check($data)) {
$this->error = $validate->getError();
return false;
}
return true;
}
}

3
app/api/validate/passport/Login.php

@ -47,6 +47,7 @@ class Login extends Validate
protected $scene = [
'login' => ['smsCode','mobile'],
'login_pw' => ['mobile','password'],
'register' => ['smsCode','mobile','password','repassword']
'register' => ['smsCode','mobile','password','repassword'],
'upPassword' => ['password']
];
}
Loading…
Cancel
Save