|
@@ -4,6 +4,13 @@ namespace app\admin\controller;
|
|
|
|
|
|
use app\common\controller\Backend;
|
|
|
|
|
|
+use think\Db;
|
|
|
+use Exception;
|
|
|
+use think\exception\DbException;
|
|
|
+use think\exception\PDOException;
|
|
|
+use think\exception\ValidateException;
|
|
|
+use fast\Random;
|
|
|
+
|
|
|
/**
|
|
|
* 维保公司管理
|
|
|
*
|
|
@@ -28,10 +35,83 @@ class Company extends Backend
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
|
|
|
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
|
|
|
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
|
|
|
+ * 添加
|
|
|
+ *
|
|
|
+ * @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}$")) {
|
|
|
+ $this->error('最高级管理员的手机号不正确');
|
|
|
+ }
|
|
|
+ $check = Db::name('pc_admin')->where('username',$adminmobile)->find();
|
|
|
+ if(!empty($check)){
|
|
|
+ $this->error('该手机号已被其他管理员注册');
|
|
|
+ }
|
|
|
+
|
|
|
+ //是否采用模型验证
|
|
|
+ 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;
|
|
|
+ //添加一个管理组
|
|
|
+ $auth_group = ['company_id'=>$company_id,'pid'=>0,'name'=>'最高级管理','code'=>'super_admin','rules'=>'*','createtime'=>time(),'updatetime'=>time(),'status'=>'normal',];
|
|
|
+ $group_id = Db::name('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('auth_group')->insertGetId($admin);
|
|
|
+
|
|
|
+ //关联管理员和组
|
|
|
+ $zu = ['uid'=>$admin_id,'group_id'=>$group_id];
|
|
|
+ Db::name('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);
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|