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.
88 lines
2.8 KiB
88 lines
2.8 KiB
package com.kob.backend.service.impl.user.account; |
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
|
import com.kob.backend.pojo.User; |
|
import com.kob.backend.service.user.account.RegisterService; |
|
import com.kob.backend.mapper.UserMapper; |
|
import org.springframework.beans.factory.annotation.Autowired; |
|
import org.springframework.security.crypto.password.PasswordEncoder; |
|
import org.springframework.stereotype.Service; |
|
|
|
import java.util.HashMap; |
|
import java.util.List; |
|
import java.util.Map; |
|
|
|
/** |
|
* @author zfp |
|
*/ |
|
@Service |
|
public class RegisterServiceImpl implements RegisterService { |
|
@Autowired |
|
private UserMapper userMapper; |
|
|
|
@Autowired |
|
private PasswordEncoder passwordEncoder; |
|
@Override |
|
public Map<String, String> register(String username, String password, String confirmedPassword) { |
|
int MAX_USERNAME_LEN = 100; |
|
Map<String, String> map = new HashMap<>(); |
|
if (username == null) { |
|
map.put("error_msg","用户名不能为空!"); |
|
return map; |
|
} |
|
|
|
if (password == null || confirmedPassword == null) { |
|
map.put("error_msg","密码不能为空!"); |
|
return map; |
|
} |
|
|
|
// 去除首尾空格 |
|
username = username.trim(); |
|
|
|
if (username.length() == 0) { |
|
map.put("error_msg","用户名不能为空!"); |
|
return map; |
|
} |
|
|
|
if (password.length() == 0 || confirmedPassword.length() == 0) { |
|
map.put("error_msg","密码不能为空!"); |
|
return map; |
|
} |
|
|
|
if (username.length() > MAX_USERNAME_LEN) { |
|
map.put("error_msg","用户名长度不能大于100!"); |
|
return map; |
|
} |
|
|
|
if (password.length() > MAX_USERNAME_LEN || confirmedPassword.length() > MAX_USERNAME_LEN) { |
|
map.put("error_msg","密码长度不能超过100!"); |
|
return map; |
|
} |
|
|
|
if (!password.equals(confirmedPassword)) { |
|
map.put("error_msg","两次输入的密码不一致!"); |
|
return map; |
|
} |
|
|
|
QueryWrapper<User> qw = new QueryWrapper<>(); |
|
// 查询是否该用户名已被使用 |
|
qw.eq("username",username); |
|
List<User> list = userMapper.selectList(qw); |
|
|
|
if (!list.isEmpty()) { |
|
map.put("error_msg","该用户名已被使用!"); |
|
return map; |
|
} |
|
// 将用户密码加密 |
|
String encodedPassword = passwordEncoder.encode(password); |
|
String photo = "https://cdn.acwing.com/media/user/profile/photo/41648_lg_8d1ad446b6.jpg"; |
|
User user = new User(null,username,encodedPassword,photo,1500,null); |
|
// 将用户数据存入数据库 |
|
userMapper.insert(user); |
|
|
|
// 返回success表示注册成功 |
|
map.put("error_msg","success"); |
|
return map; |
|
|
|
} |
|
}
|
|
|