Browse Source

员工管理同步到管理员

lizhen_gitee 1 year ago
parent
commit
2a030f1db5

+ 86 - 17
application/admin/controller/CompanyStaff.php

@@ -8,7 +8,7 @@ use think\Exception;
 use think\exception\PDOException;
 use think\exception\ValidateException;
 use think\Validate;
-
+use think\Db;
 /**
  * 商家员工
  *
@@ -92,14 +92,15 @@ class CompanyStaff extends Backend
             if (!$params) {
                 $this->error(__('Parameter %s can not be empty', ''));
             }
-            $result = false;
-            try {
+
+
                 //是否采用模型验证
                 if ($this->modelValidate) {
                     $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                     $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
                     $this->model->validateFailException(true)->validate($validate);
                 }
+                //密码和盐
                 if (isset($params['password'])) {
                     if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
                         $this->error(__("Please input correct password"));
@@ -107,13 +108,53 @@ class CompanyStaff extends Backend
                     $params['salt'] = Random::alnum();
                     $params['password'] = md5(md5($params['password']) . $params['salt']);
                 }
+                //检查
+                $check2 = Db::name('company_staff')->where('mobile',$params['mobile'])->find();
+                if($check2){
+                    $this->error('该手机已经被注册为员工或商户管理员');
+                }
+                $check2 = Db::name('admin')->where('username',$params['mobile'])->find();
+                if($check2){
+                    $this->error('该手机已经被注册为员工或商户管理员');
+                }
+
+                Db::startTrans();
+                //保存
                 $result = $this->model->allowField(true)->save($params);
-            } catch (ValidateException|PDOException|Exception $e) {
-                $this->error($e->getMessage());
-            }
-            if ($result == false) {
-               $this->error(__('No rows were inserted'));
-            }
+                if ($result == false) {
+                    Db::rollback();
+                    $this->error(__('No rows were inserted'));
+                }
+
+                //同步到admin
+                $admin = [
+                    'username' => $params['mobile'],
+                    'nickname' => $params['truename'],
+                    'password' => $params['password'],
+                    'salt'     => $params['salt'],
+
+                    'avatar'     => '/assets/img/avatar.png',
+                    'mobile'     => $params['mobile'],
+                    'createtime' => time(),
+                    'status'     => 'normal',
+                    'company_id' => $params['company_id'],
+                    'staff_id'   => $result->id,
+                ];
+                $admin_id = Db::name('admin')->insertGetId($admin);
+                if(!$admin_id){
+                    Db::rollback();
+                    $this->error('添加员工失败');
+                }
+
+                //管理员加组
+                $access[] = [
+                    'uid' => $admin_id,
+                    'group_id' => $params['type'] == 2 ? 8 : 6, //8员工组,6管理组
+                ];
+                model('AuthGroupAccess')->saveAll($access);
+
+
+            Db::commit();
             $this->success();
         }
         return $this->view->fetch();
@@ -140,14 +181,15 @@ class CompanyStaff extends Backend
                 $this->error(__('Parameter %s can not be empty', ''));
             }
             $params = $this->preExcludeFields($params);
-            $result = false;
-            try {
+
+
                 //是否采用模型验证
                 if ($this->modelValidate) {
                     $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
                     $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
                     $row->validateFailException(true)->validate($validate);
                 }
+                //密码和盐
                 if (isset($params['password'])) {
                     if (!Validate::is($params['password'], "/^[\S]{6,30}$/")) {
                         $this->error(__("Please input correct password"));
@@ -155,13 +197,40 @@ class CompanyStaff extends Backend
                     $params['salt'] = Random::alnum();
                     $params['password'] = md5(md5($params['password']) . $params['salt']);
                 }
+                //检查
+                $check2 = Db::name('company_staff')->where('id','neq',$ids)->where('mobile',$params['mobile'])->find();
+                if($check2){
+                    $this->error('该手机已经被注册为员工或商户管理员');
+                }
+                $check2 = Db::name('admin')->where('staff_id','neq',$ids)->where('username',$params['mobile'])->find();
+                if($check2){
+                    $this->error('该手机已经被注册为员工或商户管理员');
+                }
+                Db::startTrans();
+                //保存
                 $result = $row->allowField(true)->save($params);
-            } catch (ValidateException|PDOException|Exception $e) {
-                $this->error($e->getMessage());
-            }
-            if ($result == false) {
-                $this->error(__('No rows were updated'));
-            }
+                if ($result == false) {
+                    Db::rollback();
+                    $this->error(__('No rows were updated'));
+                }
+
+                //同步到admin
+                $admin = [
+                    'username' => $params['mobile'],
+                    'nickname' => $params['truename'],
+                    'password' => $params['password'],
+                    'salt'     => $params['salt'],
+                    'mobile'   => $params['mobile'],
+                    'updatetime' => time(),
+                ];
+                $admin_rs = Db::name('admin')->where('staff_id',$ids)->update($admin);
+                if($admin_rs === false){
+                    Db::rollback();
+                    $this->error('修改员工失败');
+                }
+
+
+            Db::commit();
             $this->success();
         }
         $this->view->assign("row", $row);

+ 8 - 0
application/api/controller/company/Staff.php

@@ -49,6 +49,10 @@ class Staff extends Apic
         if($check2){
             $this->error('该手机已经被注册为员工或商户管理员');
         }
+        $check2 = Db::name('admin')->where('username',$data['mobile'])->find();
+        if($check2){
+            $this->error('该手机已经被注册为员工或商户管理员');
+        }
 
         Db::startTrans();
         $staff_id = Db::name('company_staff')->insertGetId($data);
@@ -131,6 +135,10 @@ class Staff extends Apic
         if($check2){
             $this->error('该手机已经被注册为员工或商户管理员');
         }
+        $check2 = Db::name('admin')->where('staff_id','neq',$id)->where('username',$data['mobile'])->find();
+        if($check2){
+            $this->error('该手机已经被注册为员工或商户管理员');
+        }
 
         Db::startTrans();
         $staff_rs = Db::name('company_staff')->where('id',$id)->update($data);