Browse Source

重置密码

lizhen_gitee 1 year ago
parent
commit
bee112839f
1 changed files with 43 additions and 23 deletions
  1. 43 23
      application/api/controller/company/User.php

+ 43 - 23
application/api/controller/company/User.php

@@ -15,7 +15,7 @@ use think\Db;
  */
 class User extends Apic
 {
-    protected $noNeedLogin = ['mobilelogin','accountlogin'];
+    protected $noNeedLogin = ['accountlogin','resetpwd'];
     protected $noNeedRight = '*';
 
     public function _initialize()
@@ -24,6 +24,7 @@ class User extends Apic
 
     }
 
+    //员工手机+密码登录
     public function accountlogin(){
         $mobile   = $this->request->post('mobile');
         $password = $this->request->post('password');
@@ -32,7 +33,7 @@ class User extends Apic
         }
         $ret = $this->auth->login($mobile, $password);
         if ($ret) {
-            $data = ['userinfo' => $this->auth->getUserinfo()];
+            $data = $this->auth->getUserinfo();
             $this->success(__('Logged in successful'), $data);
         } else {
             $this->error($this->auth->getError());
@@ -63,28 +64,47 @@ class User extends Apic
         $this->success(__('success'),$info);
     }
 
-    //用户申请资料
-    public function getUserapplyinfo(){
-        $field = [
-            'company_name',
-            'company_code',
-            'company_registerdate',
-            'company_address',
-            'company_image',
-
-            'truename',
-            'idcard',
-            'idcard_images',
-
-            'bank_name',
-            'bank_branchname',
-            'bank_account',
-            'bank_card',
-        ];
+    /**
+     * 重置密码
+     *
+     * @ApiMethod (POST)
+     * @param string $mobile      手机号
+     * @param string $captcha     验证码
+     * @param string $newpassword 新密码
+     */
+    public function resetpwd()
+    {
+        $mobile      = $this->request->post('mobile');
+        $captcha     = $this->request->post('captcha');
+        $newpassword = $this->request->post("newpassword");
+        if (!$mobile || !$captcha || !$newpassword) {
+            $this->error(__('Invalid parameters'));
+        }
+        //验证Token
+        if (!Validate::make()->check(['newpassword' => $newpassword], ['newpassword' => 'require|regex:\S{6,30}'])) {
+            $this->error(__('Password must be 6 to 30 characters'));
+        }
+        if (!Validate::regex($mobile, "^1\d{10}$")) {
+            $this->error(__('Mobile is incorrect'));
+        }
+        $user = \app\common\model\CompanyStaff::getByMobile($mobile);
+        if (!$user) {
+            $this->error(__('User not found'));
+        }
+        $ret = Sms::check($mobile, $captcha, 'resetpwd');
+        if (!$ret) {
+            $this->error(__('Captcha is incorrect'));
+        }
+        Sms::flush($mobile, 'resetpwd');
 
-        $info = Db::name('company')->field($field)->where('id',$this->auth->id)->find();
-        $info = info_domain_image($info,['company_image','idcard_images']);
-        $this->success(1,$info);
+        //模拟一次登录
+        $this->auth->direct($user->id);
+        $ret = $this->auth->changepwd($newpassword, '', true);
+        if ($ret) {
+            $this->success(__('Reset password successful'));
+        } else {
+            $this->error($this->auth->getError());
+        }
     }