model = new \app\admin\model\Company; $this->view->assign("statusList", $this->model->getStatusList()); } /** * 添加 * * @return string * @throws \think\Exception */ public function add() { if (false === $this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $params = $this->preExcludeFields($params); if ($this->dataLimit && $this->dataLimitFieldAutoFill) { $params[$this->dataLimitField] = $this->auth->id; } $result = false; Db::startTrans(); try { $adminmobile = input('adminmobile',''); if (!$adminmobile || !\think\Validate::regex($adminmobile, "^1\d{10}$")) { abort(500,'最高级管理员的手机号不正确'); } $check = Db::name('pc_admin')->where('username',$adminmobile)->find(); if(!empty($check)){ abort(500,'该手机号已被其他管理员注册'); } //是否采用模型验证 if ($this->modelValidate) { $name = str_replace("\\model\\", "\\validate\\", get_class($this->model)); $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate; $this->model->validateFailException()->validate($validate); } $result = $this->model->allowField(true)->save($params); //第一个管理员 $company_id = $this->model->id; //注册到im //user_用户端小程序,master_师傅,kefu_客服 $tenim = new Tenim(); $rs = $tenim->register('kefu_'. $company_id, $params['companyname'], localpath_to_netpath($params['avatar'])); //添加一个管理组 $auth_group = ['company_id'=>$company_id,'pid'=>0,'name'=>'最高级管理','code'=>'super_admin','rules'=>'*','createtime'=>time(),'updatetime'=>time(),'status'=>'normal',]; $group_id = Db::name('pc_auth_group')->insertGetId($auth_group); //添加一个管理员 $password = 123456;$salt = Random::alnum(); $password = $this->getEncryptPassword($password, $salt); $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]; $admin_id = Db::name('pc_admin')->insertGetId($admin); //关联管理员和组 $zu = ['uid'=>$admin_id,'group_id'=>$group_id]; Db::name('pc_auth_group_access')->insertGetId($zu); Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } if ($result === false) { $this->error(__('No rows were inserted')); } $this->success(); } /** * 获取密码加密后的字符串 * @param string $password 密码 * @param string $salt 密码盐 * @return string */ private function getEncryptPassword($password, $salt = '') { return md5(md5($password) . $salt); } }