Company.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use Exception;
  6. use think\exception\DbException;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use fast\Random;
  10. /**
  11. * 维保公司管理
  12. *
  13. * @icon fa fa-circle-o
  14. */
  15. class Company extends Backend
  16. {
  17. /**
  18. * Company模型对象
  19. * @var \app\admin\model\Company
  20. */
  21. protected $model = null;
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = new \app\admin\model\Company;
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. }
  28. /**
  29. * 添加
  30. *
  31. * @return string
  32. * @throws \think\Exception
  33. */
  34. public function add()
  35. {
  36. if (false === $this->request->isPost()) {
  37. return $this->view->fetch();
  38. }
  39. $params = $this->request->post('row/a');
  40. if (empty($params)) {
  41. $this->error(__('Parameter %s can not be empty', ''));
  42. }
  43. $params = $this->preExcludeFields($params);
  44. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  45. $params[$this->dataLimitField] = $this->auth->id;
  46. }
  47. $result = false;
  48. Db::startTrans();
  49. try {
  50. $adminmobile = input('adminmobile','');
  51. if (!$adminmobile || !\think\Validate::regex($adminmobile, "^1\d{10}$")) {
  52. $this->error('最高级管理员的手机号不正确');
  53. }
  54. $check = Db::name('pc_admin')->where('username',$adminmobile)->find();
  55. if(!empty($check)){
  56. $this->error('该手机号已被其他管理员注册');
  57. }
  58. //是否采用模型验证
  59. if ($this->modelValidate) {
  60. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  61. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  62. $this->model->validateFailException()->validate($validate);
  63. }
  64. $result = $this->model->allowField(true)->save($params);
  65. //第一个管理员
  66. $company_id = $this->model->id;
  67. //添加一个管理组
  68. $auth_group = ['company_id'=>$company_id,'pid'=>0,'name'=>'最高级管理','code'=>'super_admin','rules'=>'*','createtime'=>time(),'updatetime'=>time(),'status'=>'normal',];
  69. $group_id = Db::name('pc_auth_group')->insertGetId($auth_group);
  70. //添加一个管理员
  71. $password = 123456;$salt = Random::alnum();
  72. $password = $this->getEncryptPassword($password, $salt);
  73. $admin = ['company_id'=>$company_id,'username'=>$adminmobile,'nickname'=>$adminmobile,'gonghao'=>$adminmobile,'mobile'=>$adminmobile,'password'=>$password,'salt'=>$salt,'avatar'=>'/assets/img/avatar.png','createtime'=>time(),'status'=>1,'is_kefu'=>1];
  74. $admin_id = Db::name('pc_admin')->insertGetId($admin);
  75. //关联管理员和组
  76. $zu = ['uid'=>$admin_id,'group_id'=>$group_id];
  77. Db::name('pc_auth_group_access')->insertGetId($zu);
  78. Db::commit();
  79. } catch (ValidateException|PDOException|Exception $e) {
  80. Db::rollback();
  81. $this->error($e->getMessage());
  82. }
  83. if ($result === false) {
  84. $this->error(__('No rows were inserted'));
  85. }
  86. $this->success();
  87. }
  88. /**
  89. * 获取密码加密后的字符串
  90. * @param string $password 密码
  91. * @param string $salt 密码盐
  92. * @return string
  93. */
  94. private function getEncryptPassword($password, $salt = '')
  95. {
  96. return md5(md5($password) . $salt);
  97. }
  98. }