diff --git a/app/controller/AdminUser.php b/app/controller/AdminUser.php index 1d67780..4d9a3f5 100644 --- a/app/controller/AdminUser.php +++ b/app/controller/AdminUser.php @@ -5,6 +5,7 @@ namespace app\controller; use app\BaseController; use app\middleware\CheckAdmin; +use think\exception\ValidateException; use think\facade\Cache; use think\facade\Db; use think\facade\Filesystem; @@ -18,6 +19,32 @@ class AdminUser extends BaseController protected $relationType = [1 => '用户', 2 => '代理']; + /** + * 新增管理 + * @return array + */ + public function addUser() + { + $param = Request::param(); + + try { + + validate(\app\validate\Passport::class)->scene('adminLogin')->check($param); + + $User = new AdminModel(); + + $result = $User::createAdmin($param['account_number'],$param['password']); + + if (!$result) throw new ValidateException('管理已存在'); + + return $this->renderSuccess('添加成功'); + } catch (ValidateException $validateException) { + return $this->renderError($validateException->getMessage()); + } catch (\Exception $e) { + return $this->renderError('操作失败'); + } + } + /** * 管理员信息 * @return array diff --git a/app/model/AdminUser.php b/app/model/AdminUser.php index bcbbe39..f02a804 100644 --- a/app/model/AdminUser.php +++ b/app/model/AdminUser.php @@ -52,14 +52,16 @@ class AdminUser extends Model } } - public static function createAdmin($account_number = 'root', $password = '123456') + public static function createAdmin($account_number = 'root', $password = 'guaguale@2023') { $AdminUser = new AdminUser(); + $query = $AdminUser->where('account_number',$account_number)->find(); + if ($query) return 0; $salt = generate_random_str(6); - $save = $AdminUser->save([ + $AdminUser->save([ 'account_number' => $account_number, 'password' => $AdminUser->generateHashedPassword($password,$salt), 'salt' => $salt, @@ -67,7 +69,7 @@ class AdminUser extends Model 'create_time' => date("Y-m-d H:i:s",time()) ]); - return $save->id; + return $AdminUser->id; } /**