| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- <?php
- namespace app\api\controller;
- use app\api\service\auth\MysqlAdapter;
- use app\api\service\auth\ApiAuthService;
- use app\api\validate\User as UserValidate;
- use think\exception\ValidateException;
- use app\common\model\User as UserModel;
- class User extends Base
- {
- protected $noNeedLogin = ['login'];
- public function userinfo(UserModel $userModel)
- {
- $user = $userModel->find($this->userinfo['id']);
- return $this->success('ok', $user);
- }
- //编辑用户
- public function edit(UserModel $userModel){
- $data = $this->request->post();
- try{
- validate(UserValidate::class)->scene('edit')->check($data);
- $user = $userModel->find($this->userinfo['id']);
- if(!$user){
- return $this->error('用户不存在');
- }
- $user->save($data);
- return $this->success('ok');
- }catch (ValidateException $e) {
- return $this->error($e->getError());
- }
- }
- //修改密码
- public function password(UserModel $userModel){
- $data = $this->request->post();
- try{
- validate(UserValidate::class)->scene('password')->check($data);
- $user = $userModel->find($this->userinfo['id']);
- if(!$user){
- return $this->error('用户不存在');
- }
- if($user->password != md5(md5($data['oldpassword'].$user->salt))){
- return $this->error('旧密码错误');
- }
- $user->password = md5(md5($data['password'].$user->salt));
- $user->save();
- return $this->success('ok');
- }catch (ValidateException $e) {
- return $this->error($e->getError());
- }
- }
- public function login(ApiAuthService $authService,UserModel $userModel)
- {
- $data = $this->request->post();
- try{
-
- validate(UserValidate::class)->scene('login')->check($data);
- $user = $authService->login($data['username'],$data['password']);
- }catch (ValidateException $e) {
-
- return $this->error($e->getError());
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success('ok', $user);
- }
- //退出登录
- public function logout()
- {
- MysqlAdapter::logout($this->userinfo['id']);
- return $this->success('ok');
- }
- }
|