Browse Source

修改密码

lizhen_gitee 1 year ago
parent
commit
6ee58371e3
2 changed files with 33 additions and 1 deletions
  1. 31 0
      application/api/controller/User.php
  2. 2 1
      application/common/library/Auth.php

+ 31 - 0
application/api/controller/User.php

@@ -302,4 +302,35 @@ class User extends Api
         }
     }
 
+    /**
+     * 修改密码
+     *
+     * @ApiMethod (POST)
+     * @param string $newpassword 新密码
+     * @param string $oldpassword 旧密码
+     */
+    public function changepwd(){
+        $newpassword = input('newpassword');
+        $oldpassword = input('oldpassword','');
+
+        if (!$newpassword) {
+            $this->error(__('Invalid parameters'));
+        }
+        if($this->auth->password && empty($oldpassword)){
+            $this->error('原密码必填');
+        }
+
+        if(empty($this->auth->password)){
+            $ret = $this->auth->changepwd($newpassword, '', true);
+        }else{
+            $ret = $this->auth->changepwd($newpassword,$oldpassword,false);
+        }
+
+        if ($ret) {
+            $this->success(__('Reset password successful'));
+        } else {
+            $this->error($this->auth->getError());
+        }
+    }
+
 }

+ 2 - 1
application/common/library/Auth.php

@@ -368,7 +368,8 @@ class Auth
      */
     public function login($account, $password)
     {
-        $field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, '/^1\d{10}$/') ? 'mobile' : 'username');
+//        $field = Validate::is($account, 'email') ? 'email' : (Validate::regex($account, '/^1\d{10}$/') ? 'mobile' : 'username');
+        $field = 'username';
         $user = User::get([$field => $account]);
         if (!$user) {
             $this->setError('Account is incorrect');