123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- namespace app\api\controller\company;
- use app\common\controller\Apic;
- use think\Db;
- use fast\Random;
- /**
- * 员工管理
- */
- class Staff extends Apic
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- //列表
- public function lists(){
- $list = Db::name('company_staff')->field('id,company_id,truename,mobile')->where('company_id',$this->auth->company_id)->autopage()->select();
- $this->success('success',$list);
- }
- //新增
- public function add(){
- //验证
- if($this->auth->type != 1){
- $this->error('只有门店老板才能设置');
- }
- $data = [
- 'truename' => input('truename',''),
- 'mobile' => input('mobile',''),
- 'company_id' => $this->auth->company_id,
- 'type' => 2,
- ];
- //密码
- $password = input('password','123456');
- $salt = Random::alnum();
- $newpassword = $this->getEncryptPassword($password, $salt);
- $data['password'] = $newpassword;
- $data['salt'] = $salt;
- //检查
- $check2 = Db::name('company_staff')->where('mobile',$data['mobile'])->find();
- 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);
- 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('添加成功');
- }
- /**
- * 获取密码加密后的字符串
- * @param string $password 密码
- * @param string $salt 密码盐
- * @return string
- */
- public function getEncryptPassword($password, $salt = '')
- {
- return md5(md5($password) . $salt);
- }
- //详情
- public function info(){
- $id = input('id',0);
- $info = Db::name('company_staff')->field('id,company_id,truename,mobile')->where('id',$id)->find();
- $this->success(1,$info);
- }
- //编辑
- public function edit(){
- //验证
- if($this->auth->type != 1){
- $this->error('只有门店老板才能设置');
- }
- $id = input('id',0);
- $data = [
- 'truename' => input('truename',''),
- 'mobile' => input('mobile',''),
- ];
- //密码
- $password = input('password','123456');
- $salt = Random::alnum();
- $newpassword = $this->getEncryptPassword($password, $salt);
- $data['password'] = $newpassword;
- $data['salt'] = $salt;
- //检查
- $check2 = Db::name('company_staff')->where('id','neq',$id)->where('mobile',$data['mobile'])->find();
- 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);
- 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('修改员工失败');
- }
- Db::commit();
- $this->success('修改成功');
- }
- }
|