Company.php 4.0 KB

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