package com.example.controller;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.entity.Account;
import com.example.entity.AuthorityInfo;
import com.example.exception.CustomException;
import com.example.entity.AdminInfo;
import com.example.entity.UserInfo;
import com.example.service.AdminInfoService;
import com.example.service.UserInfoService;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import cn.hutool.json.JSONUtil;
import java.util.*;
import java.util.stream.Collectors;
@RestController
public class AccountController {
@Value("${authority.info}")
private String authorityStr;
@Resource
private AdminInfoService adminInfoService;
@Resource
private UserInfoService userInfoService;
@PostMapping("/login")
public Result<Account> login(@RequestBody Account account, HttpServletRequest request) {
if (StrUtil.isBlank(account.getName()) || StrUtil.isBlank(account.getPassword()) || account.getLevel() == null) {
throw new CustomException(ResultCode.PARAM_LOST_ERROR);
}
Integer level = account.getLevel();
Account login = new Account();
if (1 == level) {
login = adminInfoService.login(account.getName(), account.getPassword());
}
if (2 == level) {
login = userInfoService.login(account.getName(), account.getPassword());
}
request.getSession().setAttribute("user", login);
return Result.success(login);
}
@PostMapping("/register")
public Result<Account> register(@RequestBody Account account) {
Integer level = account.getLevel();
Account login = new Account();
if (1 == level) {
AdminInfo info = new AdminInfo();
BeanUtils.copyProperties(account, info);
login = adminInfoService.add(info);
}
if (2 == level) {
UserInfo info = new UserInfo();
BeanUtils.copyProperties(account, info);
login = userInfoService.add(info);
}
return Result.success(login);
}
@GetMapping("/logout")
public Result logout(HttpServletRequest request) {
request.getSession().setAttribute("user", null);
return Result.success();
}
@GetMapping("/auth")
public Result getAuth(HttpServletRequest request) {
Object user = request.getSession().getAttribute("user");
if(user == null) {
return Result.error("401", "未登录");
}
return Result.success(user);
}
@GetMapping("/getAccountInfo")
public Result<Object> getAccountInfo(HttpServletRequest request) {
Account account = (Account) request.getSession().getAttribute("user");
if (account == null) {
return Result.success(new Object());
}
Integer level = account.getLevel();
if (1 == level) {
return Result.success(adminInfoService.findById(account.getId()));
}
if (2 == level) {
return Result.success(userInfoService.findById(account.getId()));
}
return Result.success(new Object());
}
@GetMapping("/getSession")
public Result<Map<String, String>> getSession(HttpServletRequest request) {
Account account = (Account) request.getSession().getAttribute("user");
if (account == null) {
return Result.success(new HashMap<>(1));
}
Map<String, String> map = new HashMap<>(1);
map.put("username", account.getName());
return Result.success(map);
}
@GetMapping("/getAuthority")
public Result<List<AuthorityInfo>> getAuthorityInfo() {
List<AuthorityInfo> authorityInfoList = JSONUtil.toList(JSONUtil.parseArray(authorityStr), AuthorityInfo.class);
return Result.success(authorityInfoList);
}
/**
* 获取当前用户所能看到的模块信息
* @param request
* @return
*/
@GetMapping("/authority")
public Result<List<Integer>> getAuthorityInfo(HttpServletRequest request) {
Account user = (Account) request.getSession().getAttribute("user");
if (user == null) {
return Result.success(new ArrayList<>());
}
JSONArray objects = JSONUtil.parseArray(authorityStr);
for (Object object : objects) {
JSONObject jsonObject = (JSONObject) object;
if (user.getLevel().equals(jsonObject.getInt("level"))) {
JSONArray array = JSONUtil.parseArray(jsonObject.getStr("models"));
List<Integer> modelIdList = array.stream().map((o -> {
JSONObject obj = (JSONObject) o;
return obj.getInt("modelId");
})).collect(Collectors.toList());
return Result.success(modelIdList);
}
}
return Result.success(new ArrayList<>());
}
@GetMapping("/permission/{modelId}")
public Result<List<Integer>> getPermission(@PathVariable Integer modelId, HttpServletRequest request) {
List<AuthorityInfo> authorityInfoList = JSONUtil.toList(JSONUtil.parseArray(authorityStr), AuthorityInfo.class);
Account user = (Account) request.getSession().getAttribute("user");
if (user == null) {
return Result.success(new ArrayList<>());
}
Optional<AuthorityInfo> optional = authorityInfoList.stream().filter(x -> x.getLevel().equals(user.getLevel())).findFirst();
if (optional.isPresent()) {
Optional<AuthorityInfo.Model> firstOption = optional.get().getModels().stream().filter(x -> x.getModelId().equals(modelId)).findFirst();
if (firstOption.isPresent()) {
List<Integer> info = firstOption.get().getOperation();
return Result.success(info);
}
}
return Result.success(new ArrayList<>());
}
@PutMapping("/updatePassword")
public Result updatePassword(@RequestBody Account info, HttpServletRequest request) {
Account account = (Account) request.getSession().getAttribute("user");
if (account == null) {
return Result.error(ResultCode.USER_NOT_EXIST_ERROR.code, ResultCode.USER_NOT_EXIST_ERROR.msg);
}
String oldPassword = SecureUtil.md5(info.getPassword());
if (!oldPassword.equals(account.getPassword())) {
return Result.error(ResultCode.PARAM_PASSWORD_ERROR.code, ResultCode.PARAM_PASSWORD_ERROR.msg);
}
info.setPassword(SecureUtil.md5(info.getNewPassword()));
Integer level = account.getLevel();
if (1 == level) {
AdminInfo adminInfo = new AdminInfo();
BeanUtils.copyProperties(info, adminInfo);
adminInfoService.update(adminInfo);
}
if (2 == level) {
UserInfo userInfo = new UserInfo();
BeanUtils.copyProperties(info, userInfo);
userInfoService.update(userInfo);
}
info.setLevel(level);
info.setName(account.getName());
// 清空session,让用户重新登录
request.getSession().setAttribute("user", null);
return Result.success();
}
@PostMapping("/resetPassword")
public Result resetPassword(@RequestBody Account account) {
Integer level = account.getLevel();
if (1 == level) {
AdminInfo info = adminInfoService.findByUserName(account.getName());
if (info == null) {
return Result.error(ResultCode.USER_NOT_EXIST_ERROR.code, ResultCode.USER_NOT_EXIST_ERROR.msg);
}
info.setPassword(SecureUtil.md5("123456"));
adminInfoService.update(info);
}
if (2 == level) {
UserInfo info = userInfoService.findByUserName(account.getName());
if (info == nu
没有合适的资源?快使用搜索试试~ 我知道了~
(基于springboot的Java毕业设计)智能小区管家服务系统(源码)
共475个文件
xml:98个
class:81个
java:78个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 126 浏览量
2023-05-14
12:45:11
上传
评论
收藏 15.47MB RAR 举报
温馨提示
(基于springboot的Java毕业设计)智能小区管家服务系统(源码) 【项目技术】 开发语言:Java 框架:springboot 架构:B/S 数据库:mysql 开发软件:eclipse/myeclipse/idea 浏览器:谷歌浏览器 【实现功能】 该系统的主要使用者分为管理员和业主,业主端主要包括报修信息、投诉信息、停车位信息、业主动态信息、留言信息等功能模块,管理员端主要包括管理员信息、业主信息、用具信息、业主功能模块管理、公告信息和个人信息等功能模块
资源推荐
资源详情
资源评论
收起资源包目录
(基于springboot的Java毕业设计)智能小区管家服务系统(源码) (475个子文件)
$PRODUCT_WORKSPACE_FILE$ 489B
AccountController.class 12KB
EchartsController.class 9KB
AdminInfoController.class 8KB
UserInfoController.class 8KB
NxSystemFileController.class 8KB
AdvertiserInfoController.class 8KB
TypeInfoController.class 7KB
TingcheweiInfoController.class 5KB
MenuController.class 5KB
SubmitInfoController.class 5KB
TousuInfoInfoController.class 5KB
TingcheweiInfoCommentService.class 4KB
DongtaiInfoCommentService.class 4KB
SubmitInfoCommentService.class 4KB
TingcheweiInfoCommentController.class 4KB
AdminInfoService.class 4KB
UserInfoService.class 4KB
DongtaiInfoCommentController.class 4KB
SubmitInfoCommentController.class 4KB
MessageInfoService.class 4KB
MessageInfoController.class 3KB
AdminInfo.class 3KB
DongtaiInfoController.class 3KB
AdvertiserInfoService.class 3KB
SubmitInfoService.class 3KB
TousuInfoService.class 3KB
UserInfo.class 3KB
SubmitInfo.class 3KB
TousuInfo.class 3KB
DongtaiInfoService.class 3KB
TypeInfoService.class 3KB
Account.class 3KB
NxSystemFileInfoService.class 3KB
TingcheweiInfoService.class 3KB
Result.class 2KB
TingcheweiInfo.class 2KB
EchartsData.class 2KB
ResultCode.class 2KB
TingcheweiInfoComment.class 2KB
DongtaiInfoComment.class 2KB
SubmitInfoComment.class 2KB
MyInterceptor.class 2KB
GlobalExceptionHandler.class 2KB
MessageInfo.class 2KB
EchartsData$Data.class 2KB
WebMvcConfig.class 2KB
AdvertiserInfo.class 2KB
EchartsData$Series.class 1KB
AuthorityInfo.class 1KB
MyInterceptorConfig.class 1KB
NxSystemFileInfo.class 1KB
DongtaiInfo.class 1KB
TingcheweiInfoCommentVo.class 1KB
DongtaiInfoCommentVo.class 1KB
AuthorityInfo$Model.class 1KB
SubmitInfoCommentVo.class 1KB
CustomException.class 1KB
TypeInfo.class 1KB
AdminInfoDao.class 920B
TingcheweiInfoCommentDao.class 918B
UserInfoDao.class 915B
DongtaiInfoCommentDao.class 903B
SubmitInfoCommentDao.class 898B
SubmitInfoDao.class 865B
TousuInfoDao.class 860B
MessageInfoVo.class 831B
MessageInfoDao.class 798B
Application.class 793B
NxSystemFileInfoDao.class 773B
TingcheweiInfoDao.class 706B
TypeInfoDao.class 665B
AdvertiserInfoDao.class 649B
DongtaiInfoDao.class 637B
AdvertiserInfoVo.class 320B
TingcheweiInfoVo.class 320B
DongtaiInfoVo.class 308B
SubmitInfoVo.class 304B
TousuInfoVo.class 300B
AdminInfoVo.class 300B
UserInfoVo.class 296B
TypeInfoVo.class 296B
index.css 227KB
index.css 227KB
index.css 227KB
index.css 227KB
bootstrap.min.css 119KB
bootstrap.min.css 119KB
bootstrap.css 115KB
bootstrap.css 115KB
font-awesome.css 28KB
font-awesome.css 28KB
quill.snow.css 24KB
quill.snow.css 24KB
font-awesome.css 21KB
font-awesome.css 21KB
bootstrap-theme.min.css 19KB
bootstrap-theme.min.css 19KB
swiper.min.css 13KB
swiper.min.css 13KB
共 475 条
- 1
- 2
- 3
- 4
- 5
资源评论
岛上程序猿
- 粉丝: 5612
- 资源: 4237
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功