金币系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.9 KiB

7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
7 months ago
  1. package com.example.demo.serviceImpl;
  2. import com.example.demo.domain.entity.Admin;
  3. import com.example.demo.mapper.AdminMapper;
  4. import com.example.demo.security.SecurityConfig;
  5. import com.example.demo.sevice.AdminService;
  6. import com.github.pagehelper.PageHelper;
  7. import com.github.pagehelper.PageInfo;
  8. import lombok.RequiredArgsConstructor;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.cache.annotation.CacheConfig;
  11. import org.springframework.cache.annotation.CacheEvict;
  12. import org.springframework.cache.annotation.Cacheable;
  13. import org.springframework.security.authentication.AuthenticationManager;
  14. import org.springframework.security.authentication.BadCredentialsException;
  15. import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
  16. import org.springframework.security.core.Authentication;
  17. import org.springframework.security.crypto.password.PasswordEncoder;
  18. import org.springframework.stereotype.Service;
  19. import org.springframework.transaction.annotation.Transactional;
  20. import org.springframework.util.ObjectUtils;
  21. import java.util.List;
  22. @Transactional
  23. @Service
  24. @RequiredArgsConstructor
  25. @CacheConfig(cacheNames = "admin")
  26. public class AdminServiceImpl implements AdminService {
  27. @Autowired
  28. private SecurityConfig securityConfig;
  29. private final AdminMapper adminMapper;
  30. @Autowired
  31. private AuthenticationManager authenticationManager;
  32. @Autowired
  33. private PasswordEncoder passwordEncoder;
  34. @Override
  35. public int add(Admin admin) {
  36. //判断jwcode是否已存在
  37. Admin admin1=adminMapper.selectByJwcode(admin.getJwcode());
  38. if(!ObjectUtils.isEmpty(admin1)){
  39. throw new RuntimeException("用户已存在");
  40. }
  41. //设置密码
  42. if(ObjectUtils.isEmpty(admin.getPassword())){
  43. admin.setPassword("$2a$10$jKrLD.8RTPzk4oe6c27b0ec9QP4DodbCVQLrLIEXS0xeT2vsp9cJK");
  44. }else {
  45. admin.setPassword(admin.getPassword());
  46. }
  47. return adminMapper.insert(admin);
  48. }
  49. @CacheEvict(value = "admin",allEntries = true)
  50. @Override
  51. public int edit(Admin admin) {
  52. return adminMapper.update(admin);
  53. }
  54. @Cacheable(key="#root.method.name")
  55. @Override
  56. public Admin findById(Integer adminId) {
  57. return adminMapper.selectById(adminId);
  58. }
  59. @Cacheable(key="#root.method.name")
  60. @Override
  61. public Admin findByUsername(String username) {
  62. return adminMapper.selectByName(username);
  63. }
  64. @Cacheable(key="#root.method.name")
  65. @Override
  66. public List<Admin> search(Admin admin) {
  67. return adminMapper.select(admin);
  68. }
  69. @Cacheable(key="#root.method.name + ':'+ #pageNum + '-' + #pageSize + '-' + #admin.hashCode() ")
  70. @Override
  71. public PageInfo<Admin> searchForPage(Integer pageNum, Integer pageSize, Admin admin) {
  72. PageHelper.startPage(pageNum, pageSize);
  73. List<Admin> list = adminMapper.select(admin);
  74. return new PageInfo<>(list);
  75. }
  76. @Override
  77. public Admin login(Admin admin) throws Exception {
  78. Admin admin1=adminMapper.selectByJwcode(admin.getJwcode());
  79. String[] machineIds = admin1.getMachineId().split(",");
  80. boolean flag = false;
  81. for(String machineId:machineIds) {
  82. if (admin.getMachineId() != null && admin.getMachineId().equals(machineId))
  83. flag = true;
  84. }
  85. if (!flag) {
  86. throw new RuntimeException("你没有使用该机器的权限!");
  87. }
  88. System.out.println(admin.getJwcode());
  89. System.out.println(admin.getPassword());
  90. UsernamePasswordAuthenticationToken token =
  91. new UsernamePasswordAuthenticationToken(admin.getJwcode(),admin.getPassword());
  92. try {
  93. Authentication authentication = authenticationManager.authenticate(token);
  94. Admin loginAdmin = (Admin) authentication.getPrincipal();
  95. return loginAdmin;
  96. }catch (BadCredentialsException exception){
  97. throw new BadCredentialsException("用户或密码错误");
  98. }
  99. }
  100. @Override
  101. public List<String> store() {
  102. return adminMapper.selectStore();
  103. }
  104. @Override
  105. public List<String> area() {
  106. return adminMapper.selectArea();
  107. }
  108. @Override
  109. public List<Admin> selectNo(Admin admin) {
  110. Admin admin1 = adminMapper.selectByJwcode(admin.getJwcode());
  111. if (ObjectUtils.isEmpty(admin1)) {
  112. throw new RuntimeException("用户不存在");
  113. }
  114. try {
  115. int permission = Integer.parseInt(admin1.getPermission());
  116. if (permission == 1 || permission == 2 || permission == 3) {
  117. throw new RuntimeException("用户已有权限");
  118. }
  119. } catch (NumberFormatException e) {
  120. throw new RuntimeException("权限字段格式错误");
  121. }
  122. return adminMapper.selectNo(admin);
  123. }
  124. }