diff --git a/app/api/BaseController.php b/app/api/BaseController.php new file mode 100644 index 0000000..6f79d98 --- /dev/null +++ b/app/api/BaseController.php @@ -0,0 +1,102 @@ +app = $app; + $this->request = $this->app->request; + + // 控制器初始化 + $this->initialize(); + } + + // 初始化 + protected function initialize() + { + } + + /** + * 验证数据 + * @access protected + * @param array $data 数据 + * @param string|array $validate 验证器名或者验证规则数组 + * @param array $message 提示信息 + * @param bool $batch 是否批量验证 + * @return array|string|true + * @throws ValidateException + */ + protected function validate(array $data, string|array $validate, array $message = [], bool $batch = false) + { + if (is_array($validate)) { + $v = new Validate(); + $v->rule($validate); + } else { + if (strpos($validate, '.')) { + // 支持场景 + [$validate, $scene] = explode('.', $validate); + } + $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate); + $v = new $class(); + if (!empty($scene)) { + $v->scene($scene); + } + } + + $v->message($message); + + // 是否批量验证 + if ($batch || $this->batchValidate) { + $v->batch(true); + } + + return $v->failException(true)->check($data); + } +} diff --git a/app/api/common.php b/app/api/common.php new file mode 100644 index 0000000..1243615 --- /dev/null +++ b/app/api/common.php @@ -0,0 +1,2 @@ +request->param(); + + if (isset($data['email'])) { + $data['account'] = $data['email']; + } elseif (isset($data['phone'])) { + $data['account'] = $data['phone']; + } + + try { + $this->validate($data, [ + 'account|账号' => 'require|length:3,32', + 'password|密码' => 'require|length:6,32' + ]); + } catch (ValidateException $e) { + return json([ + 'code' => 400, + 'msg' => $e->getError() + ]); + } + + $user = AdminUser::where('account', $data['account']) + ->where('status', 1) + ->where('delete_time', null) + ->find(); + + if (!$user) { + return json([ + 'code' => 401, + 'msg' => '账号不存在或已禁用' + ]); + } + + if (md5($data['password']) !== $user['password']) { + return json([ + 'code' => 401, + 'msg' => '密码错误' + ]); + } + + AdminUser::where('id', $user['id'])->update([ + 'login_count' => $user['login_count'] + 1, + 'last_login_ip' => $this->request->ip() + ]); + + $userInfo = [ + 'id' => $user['id'], + 'account' => $user['account'], + 'name' => $user['name'], + 'group_id' => $user['group_id'] + ]; + + $token = $this->generateToken($userInfo); + + return json([ + 'code' => 200, + 'msg' => '登录成功', + 'data' => [ + 'token' => $token, + 'user' => $userInfo + ] + ]); + } + + /** + * 退出登录 + * @return Json + */ + public function logout(): Json + { + $authHeader = $this->request->header('Authorization', ''); + $userInfo = null; + + return json([ + 'code' => 200, + 'msg' => '退出成功' + ]); + } + + /** + * 获取当前登录用户信息 + * @return Json + */ + public function userInfo(): Json + { + $authHeader = $this->request->header('Authorization', ''); + + if (!preg_match('/Bearer\s+(.+)/i', $authHeader, $matches)) { + return json([ + 'code' => 401, + 'msg' => '未登录' + ]); + } + + $tokenData = $this->verifyToken($matches[1]); + + if (!$tokenData || !isset($tokenData['user'])) { + return json([ + 'code' => 401, + 'msg' => 'Token无效' + ]); + } + + $user = (array)$tokenData['user']; + $user_id = $user['id']; + + $userData = AdminUser::where('id', $user_id) + ->where('delete_time', null) + ->field('id, account, name, phone, qq, sex, group_id, status, create_time, update_time') + ->find(); + + if (!$userData) { + return json([ + 'code' => 404, + 'msg' => '用户不存在' + ]); + } + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => $userData->toArray() + ]); + } + + public function getAdminUserFromToken(): array + { + return JwtService::getUserFromHeader($this->request->header('Authorization', '')); + } + + + + public function loginInfo() + { + + $loginInfo = SystemSiteSettings::select(); + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => $loginInfo + ]); + } +} diff --git a/app/api/controller/UserController.php b/app/api/controller/UserController.php new file mode 100644 index 0000000..5c89786 --- /dev/null +++ b/app/api/controller/UserController.php @@ -0,0 +1,144 @@ +field('id, account, name, phone, birth, email, qq, sex, group_id, status, last_login_ip, login_count, create_time, update_time')->select()->toArray(); + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => [ + 'list' => $users, + 'total' => count($users) + ] + ]); + } + + /** + * 获取用户信息 + * @return Json + */ + public function getUserInfo(int $id) + { + $user = AdminUser::where('id', $id) + ->field('id, account, name, phone, email, birth, qq, sex, group_id, status, create_time, last_login_ip') + ->find(); + + // 记录操作日志 + $this->logSuccess('用户管理', '获取用户信息', ['id' => $id]); + + return json([ + 'code' => 200, + 'msg' => '获取成功', + 'data' => $user + ]); + } + + /** + * 修改密码 + * @return Json + */ + public function changePassword(int $id, string $password) + { + try { + AdminUser::where('id', $id)->update([ + 'password' => md5($password), + 'update_time' => date('Y-m-d H:i:s') + ]); + // 记录操作日志 + $this->logSuccess('用户管理', '修改密码', ['id' => $id]); + return json([ + 'code' => 200, + 'msg' => '修改成功' + ]); + } catch (\Exception $e) { + // 记录失败日志 + $this->logFail('用户管理', '修改密码', $e->getMessage()); + return json([ + 'code' => 500, + 'msg' => '修改失败' + ]); + } + } + + /** + * 添加用户 + * @return Json + */ + public function addUser() + { + $data = request()->param(); + $data['password'] = md5($data['password']); + $data['create_time'] = date('Y-m-d H:i:s'); + $data['update_time'] = $data['create_time']; + $data['group_id'] = 2; + $id = AdminUser::insertGetId($data); + // 记录操作日志 + $this->logSuccess('用户管理', '添加用户', ['data' => $data]); + return json([ + 'code' => 200, + 'msg' => '添加成功', + 'data' => ['id' => $id] + ]); + } + + /** + * 编辑用户 + * @return Json + */ + public function editUser(int $id) + { + $data = request()->param(); + unset($data['_t'], $data['id']); + $data['update_time'] = date('Y-m-d H:i:s'); + AdminUser::where('id', $id)->update($data); + $this->logSuccess('用户管理', '编辑用户', ['id' => $id]); + return json([ + 'code' => 200, + 'msg' => '编辑成功' + ]); + } + + /** + * 删除用户 + * @return Json + */ + public function deleteUser(int $id) + { + $user = AdminUser::where('id', $id)->where('delete_time', null)->find(); + + if (!$user) { + return json([ + 'code' => 404, + 'msg' => '用户不存在或已删除' + ]); + } + + AdminUser::where('id', $id)->update([ + 'delete_time' => date('Y-m-d H:i:s') + ]); + + $this->logSuccess('用户管理', '删除用户', ['id' => $id]); + return json([ + 'code' => 200, + 'msg' => '删除成功' + ]); + } +} diff --git a/app/api/event.php b/app/api/event.php new file mode 100644 index 0000000..4eff890 --- /dev/null +++ b/app/api/event.php @@ -0,0 +1,5 @@ +