Browse Source

接口添加编辑员工影响到管理员

lizhen_gitee 1 year ago
parent
commit
ac96eb6771

+ 59 - 3
application/api/controller/company/Staff.php

@@ -50,7 +50,41 @@ class Staff extends Apic
             $this->error('该手机已经被注册为员工或商户管理员');
         }
 
-        Db::name('company_staff')->insertGetId($data);
+        Db::startTrans();
+        $staff_id = Db::name('company_staff')->insertGetId($data);
+        if(!$staff_id){
+            Db::rollback();
+            $this->error('添加员工失败');
+        }
+
+        //同步到admin
+        $admin = [
+            'username' => $data['mobile'],
+            'nickname' => $data['truename'],
+            'password' => $data['password'],
+            'salt'     => $data['salt'],
+
+            'avatar'     => '/assets/img/avatar.png',
+            'mobile'     => $data['mobile'],
+            'createtime' => time(),
+            'status'     => 'normal',
+            'company_id' => $data['company_id'],
+            'staff_id'   => $staff_id,
+        ];
+        $admin_id = Db::name('admin')->insertGetId($admin);
+        if(!$admin_id){
+            Db::rollback();
+            $this->error('添加员工失败');
+        }
+
+        //管理员加组
+        $access[] = [
+            'uid' => $admin_id,
+            'group_id' => 8, //门店员工组
+        ];
+        model('AuthGroupAccess')->saveAll($access);
+
+        Db::commit();
         $this->success('添加成功');
     }
 
@@ -98,8 +132,30 @@ class Staff extends Apic
             $this->error('该手机已经被注册为员工或商户管理员');
         }
 
-        Db::name('company_staff')->where('id',$id)->update($data);
+        Db::startTrans();
+        $staff_rs = Db::name('company_staff')->where('id',$id)->update($data);
+        if($staff_rs === false){
+            Db::rollback();
+            $this->error('修改员工失败');
+        }
+
+
+        //同步到admin
+        $admin = [
+            'username' => $data['mobile'],
+            'nickname' => $data['truename'],
+            'password' => $data['password'],
+            'salt'     => $data['salt'],
+            'mobile'   => $data['mobile'],
+            'updatetime' => time(),
+        ];
+        $admin_rs = Db::name('admin')->where('staff_id',$id)->update($admin);
+        if($admin_rs === false){
+            Db::rollback();
+            $this->error('修改员工失败');
+        }
 
-        $this->success('编辑成功');
+        Db::commit();
+        $this->success('修改成功');
     }
 }

+ 1 - 1
application/api/library/ExceptionHandle.php

@@ -13,7 +13,7 @@ class ExceptionHandle extends Handle
 
     public function render(Exception $e)
     {
-        //return parent::render($e);
+        return parent::render($e);
         $statuscode = $code = 500;
         $msg = $e->getMessage();
         // 验证异常

+ 10 - 0
application/common/model/AuthGroupAccess.php

@@ -0,0 +1,10 @@
+<?php
+
+namespace app\common\model;
+
+use think\Model;
+
+class AuthGroupAccess extends Model
+{
+    //
+}