Staff.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. Db::startTrans();
  42. $staff_id = Db::name('company_staff')->insertGetId($data);
  43. if(!$staff_id){
  44. Db::rollback();
  45. $this->error('添加员工失败');
  46. }
  47. //同步到admin
  48. $admin = [
  49. 'username' => $data['mobile'],
  50. 'nickname' => $data['truename'],
  51. 'password' => $data['password'],
  52. 'salt' => $data['salt'],
  53. 'avatar' => '/assets/img/avatar.png',
  54. 'mobile' => $data['mobile'],
  55. 'createtime' => time(),
  56. 'status' => 'normal',
  57. 'company_id' => $data['company_id'],
  58. 'staff_id' => $staff_id,
  59. ];
  60. $admin_id = Db::name('admin')->insertGetId($admin);
  61. if(!$admin_id){
  62. Db::rollback();
  63. $this->error('添加员工失败');
  64. }
  65. //管理员加组
  66. $access[] = [
  67. 'uid' => $admin_id,
  68. 'group_id' => 8, //门店员工组
  69. ];
  70. model('AuthGroupAccess')->saveAll($access);
  71. Db::commit();
  72. $this->success('添加成功');
  73. }
  74. /**
  75. * 获取密码加密后的字符串
  76. * @param string $password 密码
  77. * @param string $salt 密码盐
  78. * @return string
  79. */
  80. public function getEncryptPassword($password, $salt = '')
  81. {
  82. return md5(md5($password) . $salt);
  83. }
  84. //详情
  85. public function info(){
  86. $id = input('id',0);
  87. $info = Db::name('company_staff')->field('id,company_id,truename,mobile')->where('id',$id)->find();
  88. $this->success(1,$info);
  89. }
  90. //编辑
  91. public function edit(){
  92. //验证
  93. if($this->auth->type != 1){
  94. $this->error('只有门店老板才能设置');
  95. }
  96. $id = input('id',0);
  97. $data = [
  98. 'truename' => input('truename',''),
  99. 'mobile' => input('mobile',''),
  100. ];
  101. //密码
  102. $password = input('password','123456');
  103. $salt = Random::alnum();
  104. $newpassword = $this->getEncryptPassword($password, $salt);
  105. $data['password'] = $newpassword;
  106. $data['salt'] = $salt;
  107. //检查
  108. $check2 = Db::name('company_staff')->where('id','neq',$id)->where('mobile',$data['mobile'])->find();
  109. if($check2){
  110. $this->error('该手机已经被注册为员工或商户管理员');
  111. }
  112. Db::startTrans();
  113. $staff_rs = Db::name('company_staff')->where('id',$id)->update($data);
  114. if($staff_rs === false){
  115. Db::rollback();
  116. $this->error('修改员工失败');
  117. }
  118. //同步到admin
  119. $admin = [
  120. 'username' => $data['mobile'],
  121. 'nickname' => $data['truename'],
  122. 'password' => $data['password'],
  123. 'salt' => $data['salt'],
  124. 'mobile' => $data['mobile'],
  125. 'updatetime' => time(),
  126. ];
  127. $admin_rs = Db::name('admin')->where('staff_id',$id)->update($admin);
  128. if($admin_rs === false){
  129. Db::rollback();
  130. $this->error('修改员工失败');
  131. }
  132. Db::commit();
  133. $this->success('修改成功');
  134. }
  135. }