Skip to content

Commit a7f2655

Browse files
committed
增加注册,并修改权限初始化数据
1 parent a5775fe commit a7f2655

File tree

5 files changed

+212
-114
lines changed

5 files changed

+212
-114
lines changed

app/Command/InitCommand.php

+7
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,16 @@ public function handle()
6464
'description' => '普通管理员'
6565
];
6666

67+
$tourist_role = [
68+
'name' => 'tourist_admin',
69+
'guard_name' => 'web',
70+
'description' => '游客'
71+
];
72+
6773
//创建默认的两个角色
6874
$super_role = Role::create($super_role);
6975
$default_role = Role::create($default_role);
76+
$tourist_role = Role::create($tourist_role);
7077

7178
//创建权限
7279
$permissionList = config('permissionData.permission_list');

app/Constants/StatusCode.php

+11
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ class StatusCode extends AbstractConstants
9696
*/
9797
const ERR_USER_DISABLE= 2005;
9898

99+
/**
100+
* @Message("用户名已经被使用!")
101+
*/
102+
const ERR_USER_EXIST= 2006;
103+
104+
105+
/**
106+
* @Message("注册失败!")
107+
*/
108+
const ERR_REGISTER_ERROR = 2007;
109+
99110

100111
/**
101112
* @Message("业务逻辑异常!")

app/Controller/Auth/LoginController.php

+47
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,53 @@ public function login()
6161
return $this->success($responseData);
6262
}
6363

64+
/**
65+
* 注册操作
66+
* @RequestMapping(path="register", methods="post")
67+
* @return \Psr\Http\Message\ResponseInterface
68+
*/
69+
public function register()
70+
{
71+
$params = [
72+
'username' => $this->request->input('username') ?? '',
73+
'password' => $this->request->input('password') ?? '',
74+
'password_confirmation' => $this->request->input('password_confirmation') ?? '',
75+
'desc' => $this->request->input('desc') ?? '',
76+
'code_key' => $this->request->input('code_key') ?? '',
77+
'captcha' => $this->request->input('captcha') ?? '',
78+
];
79+
$rules = [
80+
'username' => 'required|min:4|max:18|unique:users',
81+
'password' => 'required|min:6|max:18|confirmed:password_confirmation',
82+
'password_confirmation' => 'required|min:6|max:18',
83+
'desc' => 'required',
84+
'code_key' => 'required',
85+
'captcha' => 'required',
86+
];
87+
$message = [
88+
'username.required' => ' username 缺失',
89+
'username.unique' => '该用户名已经存在',
90+
'username.min' => '[username]最少4位',
91+
'username.max' => '[username]最多18位',
92+
'desc.required' => ' desc 缺失',
93+
'password.required' => ' password 缺失',
94+
'password_confirmation.required' => ' password 缺失',
95+
'password.min' => ' password 最少6位数',
96+
'password_confirmation.min' => ' password 最少6位数',
97+
'password.max' => ' password 最多18位数',
98+
'password_confirmation.max' => ' password 最多18位数',
99+
'code_key.required' => '验证码KEY缺失',
100+
'captcha.required' => '验证码缺失',
101+
];
102+
$this->verifyParams($params, $rules, $message);
103+
104+
$result = LoginService::getInstance()->register($params);
105+
if (!$result) $this->throwExp(StatusCode::ERR_REGISTER_ERROR, '注册失败');
106+
107+
return $this->successByMessage('注册成功, 跳转登陆中...');
108+
}
109+
110+
64111
/**
65112
* 初始化操作
66113
* @RequestMapping(path="initialization", methods="get")

app/Service/Auth/LoginService.php

+33
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use App\Model\Auth\Permission;
99
use App\Model\Auth\User;
1010
use App\Model\System\LoginLog;
11+
use Hyperf\DbConnection\Db;
1112
use Hyperf\Di\Annotation\Inject;
1213
use Hyperf\Utils\ApplicationContext;
1314
use Phper666\JWTAuth\JWT;
@@ -73,6 +74,38 @@ public function login(array $params) : array
7374
return $responseData;
7475
}
7576

77+
/**
78+
* 处理注册逻辑
79+
* @param array $params
80+
* @return array
81+
*/
82+
public function register(array $params) : bool
83+
{
84+
//校验验证码 若是测试环境跳过验证码验证
85+
if (env('APP_TEST')) {
86+
$container = ApplicationContext::getContainer();
87+
$redis = $container->get(\Hyperf\Redis\Redis::class);
88+
$code = $redis->get($params['code_key']);
89+
if (strtolower($params['captcha']) != strtolower($code)) $this->throwExp(StatusCode::ERR_CODE, '验证失败,验证码错误');
90+
}
91+
$postData = $this->request->all();
92+
93+
$user = new User();
94+
$user->username = $postData['username'];
95+
$user->password = md5($postData['password']);
96+
$user->status = User::STATUS_ON;
97+
$user->avatar = 'http://landlord-res.oss-cn-shenzhen.aliyuncs.com/admin_face/face' . rand(1,10) .'.png';
98+
$user->last_login = time();
99+
$user->last_ip = getClientIp($this->request);
100+
$user->creater = '';
101+
$user->desc = $postData['desc'] ?? '';
102+
$user->sex = User::SEX_BY_Female;
103+
104+
if (!$user->save()) $this->throwExp(StatusCode::ERR_EXCEPTION, '注册用户失败');
105+
$user->assignRole('tourist_admin');
106+
return true;
107+
}
108+
76109
/**
77110
* 登陆初始化,获取用户信息以及一些权限菜单
78111
* @return mixed

0 commit comments

Comments
 (0)