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.
142 lines
4.9 KiB
142 lines
4.9 KiB
package com.example.demo.serviceImpl;
|
|
|
|
|
|
import com.example.demo.domain.entity.Admin;
|
|
import com.example.demo.mapper.AdminMapper;
|
|
import com.example.demo.security.SecurityConfig;
|
|
import com.example.demo.sevice.AdminService;
|
|
import com.github.pagehelper.PageHelper;
|
|
import com.github.pagehelper.PageInfo;
|
|
import lombok.RequiredArgsConstructor;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import org.springframework.cache.annotation.CacheConfig;
|
|
import org.springframework.cache.annotation.CacheEvict;
|
|
import org.springframework.cache.annotation.CachePut;
|
|
import org.springframework.cache.annotation.Cacheable;
|
|
import org.springframework.security.authentication.AuthenticationManager;
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
|
import org.springframework.security.core.Authentication;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
import java.util.List;
|
|
@Transactional
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
@CacheConfig(cacheNames = "admin")
|
|
public class AdminServiceImpl implements AdminService {
|
|
@Autowired
|
|
private SecurityConfig securityConfig;
|
|
private final AdminMapper adminMapper;
|
|
@Autowired
|
|
private AuthenticationManager authenticationManager;
|
|
@Autowired
|
|
private PasswordEncoder passwordEncoder;
|
|
|
|
@Override
|
|
public int add(Admin admin) {
|
|
//判断jwcode是否已存在
|
|
Admin admin1=adminMapper.selectByJwcode(admin);
|
|
if(!ObjectUtils.isEmpty(admin1)){
|
|
throw new RuntimeException("用户已存在");
|
|
}
|
|
//设置密码
|
|
if(ObjectUtils.isEmpty(admin.getPassword())){
|
|
admin.setPassword(passwordEncoder.encode("$2a$10$jKrLD.8RTPzk4oe6c27b0ec9QP4DodbCVQLrLIEXS0xeT2vsp9cJK"));
|
|
}else {
|
|
admin.setPassword(passwordEncoder.encode(admin.getPassword()));
|
|
}
|
|
return adminMapper.insert(admin);
|
|
}
|
|
@CacheEvict(value = "admin",allEntries = true)
|
|
@Override
|
|
public int edit(Admin admin) {
|
|
return adminMapper.update(admin);
|
|
}
|
|
@Cacheable(key="#root.method.name")
|
|
@Override
|
|
public Admin findById(Integer adminId) {
|
|
return adminMapper.selectById(adminId);
|
|
}
|
|
@Cacheable(key="#root.method.name")
|
|
@Override
|
|
public Admin findByUsername(String username) {
|
|
return adminMapper.selectByName(username);
|
|
}
|
|
@Cacheable(key="#root.method.name")
|
|
@Override
|
|
public List<Admin> search(Admin admin) {
|
|
return adminMapper.select(admin);
|
|
}
|
|
@Cacheable(key="#root.method.name + ':'+ #pageNum + '-' + #pageSize + '-' + #admin.hashCode() ")
|
|
@Override
|
|
public PageInfo<Admin> searchForPage(Integer pageNum, Integer pageSize, Admin admin) {
|
|
PageHelper.startPage(pageNum, pageSize);
|
|
List<Admin> list = adminMapper.select(admin);
|
|
return new PageInfo<>(list);
|
|
|
|
}
|
|
@Override
|
|
public Admin login(Admin admin) throws Exception {
|
|
|
|
Admin admin1=adminMapper.selectByJwcode(admin);
|
|
String[] machineIds = admin1.getMachineId().split(",");
|
|
boolean flag = false;
|
|
for(String machineId:machineIds) {
|
|
if (admin.getMachineId() != null && admin.getMachineId().equals(machineId))
|
|
flag = true;
|
|
}
|
|
if (!flag) {
|
|
throw new RuntimeException("你没有使用该机器的权限!");
|
|
}
|
|
System.out.println(admin.getJwcode());
|
|
System.out.println(admin.getPassword());
|
|
UsernamePasswordAuthenticationToken token =
|
|
new UsernamePasswordAuthenticationToken(admin.getJwcode(),admin.getPassword());
|
|
try {
|
|
Authentication authentication = authenticationManager.authenticate(token);
|
|
Admin loginAdmin = (Admin) authentication.getPrincipal();
|
|
|
|
return loginAdmin;
|
|
}catch (BadCredentialsException exception){
|
|
throw new BadCredentialsException("用户或密码错误");
|
|
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public List<String> store() {
|
|
return adminMapper.selectStore();
|
|
}
|
|
|
|
@Override
|
|
public List<String> area() {
|
|
return adminMapper.selectArea();
|
|
}
|
|
|
|
@Override
|
|
public List<Admin> selectNo(Admin admin) {
|
|
Admin admin1 = adminMapper.selectByJwcode(admin);
|
|
if (ObjectUtils.isEmpty(admin1)) {
|
|
throw new RuntimeException("用户不存在");
|
|
}
|
|
try {
|
|
int permission = Integer.parseInt(admin1.getPermission());
|
|
if (permission == 1 || permission == 2 || permission == 3) {
|
|
throw new RuntimeException("用户已有权限");
|
|
}
|
|
} catch (NumberFormatException e) {
|
|
throw new RuntimeException("权限字段格式错误");
|
|
}
|
|
return adminMapper.selectNo(admin);
|
|
}
|
|
|
|
|
|
|
|
}
|