58 lines
1.9 KiB
Java
58 lines
1.9 KiB
Java
|
package com.jdc.jdcproject.service.impl;
|
||
|
|
||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||
|
import com.jdc.jdcproject.entity.Users;
|
||
|
import com.jdc.jdcproject.exceptionhandler.JdcException;
|
||
|
import com.jdc.jdcproject.mapper.UsersMapper;
|
||
|
import com.jdc.jdcproject.service.IUsersService;
|
||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||
|
import com.jdc.jdcproject.utils.JwtUtils;
|
||
|
import com.jdc.jdcproject.utils.MD5;
|
||
|
import org.springframework.stereotype.Service;
|
||
|
import org.springframework.util.StringUtils;
|
||
|
|
||
|
/**
|
||
|
* <p>
|
||
|
* 服务实现类
|
||
|
* </p>
|
||
|
*
|
||
|
* @author haoyanlu
|
||
|
* @since 2025-04-26
|
||
|
*/
|
||
|
@Service
|
||
|
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {
|
||
|
|
||
|
@Override
|
||
|
public String login(Users user) {
|
||
|
|
||
|
System.out.println(user.toString());
|
||
|
String password = user.getPassword();
|
||
|
String name = user.getUsername();
|
||
|
String phone = user.getUsername();
|
||
|
if (user.getUsername().matches("^\\d{11}$")){
|
||
|
phone = user.getUsername();
|
||
|
}else {
|
||
|
name = user.getUsername();
|
||
|
}
|
||
|
|
||
|
System.out.println(user.toString());
|
||
|
|
||
|
if ((StringUtils.isEmpty(phone) ^ StringUtils.isEmpty(name)) || StringUtils.isEmpty(password)) {
|
||
|
throw new JdcException(20001, "登陆失败");
|
||
|
}
|
||
|
QueryWrapper<Users> wrapper = new QueryWrapper<>();
|
||
|
wrapper.eq("username", name).or().eq("tel", phone);
|
||
|
Users users = baseMapper.selectOne(wrapper);
|
||
|
|
||
|
if (users == null) {
|
||
|
throw new JdcException(20001, "登陆失败");
|
||
|
}
|
||
|
System.out.println(MD5.encrypt(password));
|
||
|
if (!MD5.encrypt(password).equals(users.getPassword())) {
|
||
|
throw new JdcException(20001, "登陆失败");
|
||
|
}
|
||
|
String token = JwtUtils.getJwtToken(users.getId(), users.getUsername());
|
||
|
return token;
|
||
|
}
|
||
|
}
|