refactor:注册时返回用户信息

This commit is contained in:
fanxb 2020-07-14 14:37:48 +08:00
parent 0b9f38436a
commit 8c493c0868
5 changed files with 37 additions and 24 deletions

View File

@ -49,8 +49,7 @@ public class UserController {
*/
@PutMapping("")
public Result register(@RequestBody RegisterBody body) {
userService.register(body);
return Result.success(null);
return Result.success(userService.register(body));
}
/**

View File

@ -1,20 +1,26 @@
package com.fanxb.bookmark.business.user.entity;
import com.fanxb.bookmark.common.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* 类功能简述登录返回数据
* 类功能详述
*
* @author fanxb
* @date 2019/7/6 16:52
*/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginRes {
/**
* 用户信息
*/
private User user;
/**
* token
*/
private String token;
private int userId;
private String username;
private String email;
private String lastLoginTime;
private String icon;
}

View File

@ -80,7 +80,7 @@ public class UserService {
* @author fanxb
* @date 2019/7/6 11:30
*/
public void register(RegisterBody body) {
public LoginRes register(RegisterBody body) {
User user = userDao.selectByUsernameOrEmail(body.getUsername(), body.getEmail());
if (user != null) {
if (user.getUsername().equals(body.getUsername())) {
@ -96,8 +96,13 @@ public class UserService {
user.setIcon(DEFAULT_ICON);
user.setPassword(HashUtil.sha1(HashUtil.md5(body.getPassword())));
user.setCreateTime(System.currentTimeMillis());
user.setLastLoginTime(0);
user.setLastLoginTime(System.currentTimeMillis());
user.setVersion(0);
userDao.addOne(user);
Map<String, String> data = new HashMap<>(1);
data.put("userId", String.valueOf(user.getUserId()));
String token = JwtUtil.encode(data, Constant.jwtSecret, LONG_EXPIRE_TIME);
return new LoginRes(user, token);
}
/**
@ -121,10 +126,7 @@ public class UserService {
String token = JwtUtil.encode(data, Constant.jwtSecret, body.isRememberMe() ? LONG_EXPIRE_TIME : SHORT_EXPIRE_TIME);
LoginRes res = new LoginRes();
res.setToken(token);
res.setUserId(userInfo.getUserId());
res.setUsername(userInfo.getUsername());
res.setEmail(userInfo.getEmail());
res.setIcon(userInfo.getIcon());
res.setUser(userInfo);
userDao.updateLastLoginTime(System.currentTimeMillis(), userInfo.getUserId());
return res;
}
@ -175,6 +177,7 @@ public class UserService {
}
int userId = UserContextHolder.get().getUserId();
String fileName = file.getOriginalFilename();
assert fileName != null;
String path = Paths.get(FileConstant.iconPath, userId + "." + System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."))).toString();
Path realPath = Paths.get(Constant.fileSavePath, path);
FileUtil.ensurePathExist(realPath.getParent().toString());

View File

@ -3,20 +3,22 @@
<mapper namespace="com.fanxb.bookmark.business.user.dao.UserDao">
<insert id="addOne">
insert into user (username, email, icon, password, createTime, lastLoginTime)
value
(#{username}, #{email}, #{icon}, #{password}, #{createTime}, #{lastLoginTime})
<insert id="addOne" useGeneratedKeys="true" keyColumn="userId" keyProperty="userId">
insert into user (username, email, icon, password, createTime, lastLoginTime,version)
value
(#{username}, #{email}, #{icon}, #{password}, #{createTime}, #{lastLoginTime},#{version})
</insert>
<select id="selectByUsernameOrEmail" resultType="com.fanxb.bookmark.common.entity.User">
select
userId,
username,
email,
icon,
password,
createTime
userId,
username,
email,
icon,
password,
createTime,
lastLoginTime,
version
from user
where username = #{name} or email = #{email}
limit 1

View File

@ -0,0 +1,3 @@
ALTER TABLE `bookmark`.`user`
ADD UNIQUE INDEX `userNameIndex`(`username`) USING BTREE COMMENT '用户名索引',
ADD UNIQUE INDEX `emailIndex`(`email`) USING BTREE COMMENT '邮箱索引';