Staff.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\api\controller\company;
  3. use app\common\controller\Apic;
  4. use think\Db;
  5. use fast\Random;
  6. /**
  7. * 员工管理
  8. */
  9. class Staff extends Apic
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = '*';
  13. //列表
  14. public function lists(){
  15. $list = Db::name('company_staff')->field('id,company_id,truename,mobile')->where('company_id',$this->auth->company_id)->autopage()->select();
  16. $this->success('success',$list);
  17. }
  18. //新增
  19. public function add(){
  20. //验证
  21. if($this->auth->type != 1){
  22. $this->error('只有门店老板才能设置');
  23. }
  24. $data = [
  25. 'truename' => input('truename',''),
  26. 'mobile' => input('mobile',''),
  27. 'company_id' => $this->auth->company_id,
  28. 'type' => 2,
  29. ];
  30. //密码
  31. $password = input('password','123456');
  32. $salt = Random::alnum();
  33. $newpassword = $this->getEncryptPassword($password, $salt);
  34. $data['password'] = $newpassword;
  35. $data['salt'] = $salt;
  36. //检查
  37. $check2 = Db::name('company_staff')->where('mobile',$data['mobile'])->find();
  38. if($check2){
  39. $this->error('该手机已经被注册为员工或商户管理员');
  40. }
  41. $check2 = Db::name('admin')->where('username',$data['mobile'])->find();
  42. if($check2){
  43. $this->error('该手机已经被注册为员工或商户管理员');
  44. }
  45. Db::startTrans();
  46. $staff_id = Db::name('company_staff')->insertGetId($data);
  47. if(!$staff_id){
  48. Db::rollback();
  49. $this->error('添加员工失败');
  50. }
  51. //同步到admin
  52. $admin = [
  53. 'username' => $data['mobile'],
  54. 'nickname' => $data['truename'],
  55. 'password' => $data['password'],
  56. 'salt' => $data['salt'],
  57. 'avatar' => '/assets/img/avatar.png',
  58. 'mobile' => $data['mobile'],
  59. 'createtime' => time(),
  60. 'status' => 'normal',
  61. 'company_id' => $data['company_id'],
  62. 'staff_id' => $staff_id,
  63. ];
  64. $admin_id = Db::name('admin')->insertGetId($admin);
  65. if(!$admin_id){
  66. Db::rollback();
  67. $this->error('添加员工失败');
  68. }
  69. //管理员加组
  70. $access[] = [
  71. 'uid' => $admin_id,
  72. 'group_id' => 8, //门店员工组
  73. ];
  74. model('AuthGroupAccess')->saveAll($access);
  75. Db::commit();
  76. $this->success('添加成功');
  77. }
  78. /**
  79. * 获取密码加密后的字符串
  80. * @param string $password 密码
  81. * @param string $salt 密码盐
  82. * @return string
  83. */
  84. public function getEncryptPassword($password, $salt = '')
  85. {
  86. return md5(md5($password) . $salt);
  87. }
  88. //详情
  89. public function info(){
  90. $id = input('id',0);
  91. $info = Db::name('company_staff')->field('id,company_id,truename,mobile')->where('id',$id)->find();
  92. $this->success(1,$info);
  93. }
  94. //编辑
  95. public function edit(){
  96. //验证
  97. if($this->auth->type != 1){
  98. $this->error('只有门店老板才能设置');
  99. }
  100. $id = input('id',0);
  101. $data = [
  102. 'truename' => input('truename',''),
  103. 'mobile' => input('mobile',''),
  104. ];
  105. //密码
  106. $password = input('password','123456');
  107. $salt = Random::alnum();
  108. $newpassword = $this->getEncryptPassword($password, $salt);
  109. $data['password'] = $newpassword;
  110. $data['salt'] = $salt;
  111. //检查
  112. $check2 = Db::name('company_staff')->where('id','neq',$id)->where('mobile',$data['mobile'])->find();
  113. if($check2){
  114. $this->error('该手机已经被注册为员工或商户管理员');
  115. }
  116. $check2 = Db::name('admin')->where('staff_id','neq',$id)->where('username',$data['mobile'])->find();
  117. if($check2){
  118. $this->error('该手机已经被注册为员工或商户管理员');
  119. }
  120. Db::startTrans();
  121. $staff_rs = Db::name('company_staff')->where('id',$id)->update($data);
  122. if($staff_rs === false){
  123. Db::rollback();
  124. $this->error('修改员工失败');
  125. }
  126. //同步到admin
  127. $admin = [
  128. 'username' => $data['mobile'],
  129. 'nickname' => $data['truename'],
  130. 'password' => $data['password'],
  131. 'salt' => $data['salt'],
  132. 'mobile' => $data['mobile'],
  133. 'updatetime' => time(),
  134. ];
  135. $admin_rs = Db::name('admin')->where('staff_id',$id)->update($admin);
  136. if($admin_rs === false){
  137. Db::rollback();
  138. $this->error('修改员工失败');
  139. }
  140. Db::commit();
  141. $this->success('修改成功');
  142. }
  143. }