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("") @PutMapping("")
public Result register(@RequestBody RegisterBody body) { public Result register(@RequestBody RegisterBody body) {
userService.register(body); return Result.success(userService.register(body));
return Result.success(null);
} }
/** /**

View File

@ -1,20 +1,26 @@
package com.fanxb.bookmark.business.user.entity; package com.fanxb.bookmark.business.user.entity;
import com.fanxb.bookmark.common.entity.User;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
/** /**
* 类功能简述登录返回数据 * 类功能简述登录返回数据
* 类功能详述
* *
* @author fanxb * @author fanxb
* @date 2019/7/6 16:52 * @date 2019/7/6 16:52
*/ */
@Data @Data
@AllArgsConstructor
@NoArgsConstructor
public class LoginRes { public class LoginRes {
/**
* 用户信息
*/
private User user;
/**
* token
*/
private String 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 * @author fanxb
* @date 2019/7/6 11:30 * @date 2019/7/6 11:30
*/ */
public void register(RegisterBody body) { public LoginRes register(RegisterBody body) {
User user = userDao.selectByUsernameOrEmail(body.getUsername(), body.getEmail()); User user = userDao.selectByUsernameOrEmail(body.getUsername(), body.getEmail());
if (user != null) { if (user != null) {
if (user.getUsername().equals(body.getUsername())) { if (user.getUsername().equals(body.getUsername())) {
@ -96,8 +96,13 @@ public class UserService {
user.setIcon(DEFAULT_ICON); user.setIcon(DEFAULT_ICON);
user.setPassword(HashUtil.sha1(HashUtil.md5(body.getPassword()))); user.setPassword(HashUtil.sha1(HashUtil.md5(body.getPassword())));
user.setCreateTime(System.currentTimeMillis()); user.setCreateTime(System.currentTimeMillis());
user.setLastLoginTime(0); user.setLastLoginTime(System.currentTimeMillis());
user.setVersion(0);
userDao.addOne(user); 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); String token = JwtUtil.encode(data, Constant.jwtSecret, body.isRememberMe() ? LONG_EXPIRE_TIME : SHORT_EXPIRE_TIME);
LoginRes res = new LoginRes(); LoginRes res = new LoginRes();
res.setToken(token); res.setToken(token);
res.setUserId(userInfo.getUserId()); res.setUser(userInfo);
res.setUsername(userInfo.getUsername());
res.setEmail(userInfo.getEmail());
res.setIcon(userInfo.getIcon());
userDao.updateLastLoginTime(System.currentTimeMillis(), userInfo.getUserId()); userDao.updateLastLoginTime(System.currentTimeMillis(), userInfo.getUserId());
return res; return res;
} }
@ -175,6 +177,7 @@ public class UserService {
} }
int userId = UserContextHolder.get().getUserId(); int userId = UserContextHolder.get().getUserId();
String fileName = file.getOriginalFilename(); String fileName = file.getOriginalFilename();
assert fileName != null;
String path = Paths.get(FileConstant.iconPath, userId + "." + System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."))).toString(); String path = Paths.get(FileConstant.iconPath, userId + "." + System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf("."))).toString();
Path realPath = Paths.get(Constant.fileSavePath, path); Path realPath = Paths.get(Constant.fileSavePath, path);
FileUtil.ensurePathExist(realPath.getParent().toString()); FileUtil.ensurePathExist(realPath.getParent().toString());

View File

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