金币系统后端

142 lines
4.9 KiB

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