Skip to content

Commit

Permalink
优化验证码问题,添加查询个人信息接口
Browse files Browse the repository at this point in the history
  • Loading branch information
TreasureJade committed Nov 4, 2019
1 parent d826b12 commit 01a2e9d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/swpu/uchain/blog/dao/UserMapper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.swpu.uchain.blog.dao;

import com.swpu.uchain.blog.entity.User;
import com.swpu.uchain.blog.vo.UserVO;

import java.util.List;

public interface UserMapper {
Expand All @@ -15,4 +17,6 @@ public interface UserMapper {
int updateByPrimaryKey(User record);

User getUserByPhone(String phoneNum);

UserVO selectByPhoneNum(String phoneNumber);
}
50 changes: 44 additions & 6 deletions src/main/java/com/swpu/uchain/blog/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

import com.swpu.uchain.blog.entity.User;
import com.swpu.uchain.blog.form.LoginForm;
import com.swpu.uchain.blog.form.UpdatePwForm;
import com.swpu.uchain.blog.form.UpdateUserForm;
import com.swpu.uchain.blog.form.UserInsertForm;
import com.swpu.uchain.blog.vo.ResultVO;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

Expand All @@ -13,15 +17,29 @@
*/
public interface UserService {

/**
* 添加用户
*
* @param user
* @return boolean
*/
boolean insert(User user);

/**
* 更新用户
*
* @param user
* @return boolean
*/
boolean update(User user);

/**
* 根据id查询用户
*
* @param userId
* @return
*/
User selectByUserId(Long userId);
User selectByUserId(Long userId);

/**
* 根据手机号码查询用户
Expand All @@ -42,25 +60,45 @@ public interface UserService {

/**
* 用户登录
*
* @param loginForm
* @param response
* @author hobo
* @return java.lang.Object
* @author hobo
*/
Object login(LoginForm loginForm, HttpServletResponse response);
ResultVO login(LoginForm loginForm, HttpServletResponse response);

/**
* 用户注册
*
* @param userInsertForm
* @return java.lang.Object
*/
Object insertUser(UserInsertForm userInsertForm);
ResultVO insertUser(UserInsertForm userInsertForm);

/**
*
* @param phoneNumber
* @return java.lang.Object
*/
Object getValidationCode(String phoneNumber);
ResultVO getValidationCode(String phoneNumber);

/***
* 修改密码
* @param form
* @return java.lang.Object
*/
ResultVO updatePw(UpdatePwForm form);

/**
* 用户查看个人信息
* @return
*/
ResultVO getOwnerMsg();

/**
* 用户更新个人信息
* @param form
* @return
*/
ResultVO updateUser(UpdateUserForm form, MultipartFile file);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.swpu.uchain.blog.enums.TemplateCodeEnum;
import com.swpu.uchain.blog.exception.GlobalException;
import com.swpu.uchain.blog.form.LoginForm;
import com.swpu.uchain.blog.form.UpdatePwForm;
import com.swpu.uchain.blog.form.UpdateUserForm;
import com.swpu.uchain.blog.form.UserInsertForm;
import com.swpu.uchain.blog.redis.RedisService;
import com.swpu.uchain.blog.redis.key.PhoneCodeKey;
Expand All @@ -16,6 +18,9 @@
import com.swpu.uchain.blog.util.AliyunSmsUtils;
import com.swpu.uchain.blog.util.JwtTokenUtil;
import com.swpu.uchain.blog.util.ResultVOUtil;
import com.swpu.uchain.blog.util.UploadFileUtil;
import com.swpu.uchain.blog.vo.ResultVO;
import com.swpu.uchain.blog.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -29,6 +34,7 @@
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
Expand All @@ -44,8 +50,11 @@
@Slf4j
public class UserServiceImpl implements UserService {

@Value("${file.headPic}")
private String headPicPath;
@Value("${file.head-pic.default-pic}")
private String headDefaultPicPath;

@Value("${file.head-pic.upload-pic}")
private String headPicUploadPath;

@Autowired
private UserMapper userMapper;
Expand Down Expand Up @@ -78,7 +87,12 @@ public boolean insert(User user) {
}

@Override
public User selectByUserId(Long userId) {
public boolean update(User user) {
return userMapper.updateByPrimaryKey(user) == 1;
}

@Override
public User selectByUserId(Long userId) {
return userMapper.selectByPrimaryKey(userId);
}

Expand All @@ -99,7 +113,7 @@ public User getCurrentUser() {
}

@Override
public Object login(LoginForm loginForm, HttpServletResponse response) {
public ResultVO login(LoginForm loginForm, HttpServletResponse response) {
User user = userMapper.getUserByPhone(loginForm.getPhoneNum());
if (user == null) {
return ResultVOUtil.error(ResultEnum.USER_NOT_EXIST);
Expand All @@ -124,26 +138,32 @@ public Object login(LoginForm loginForm, HttpServletResponse response) {
}

@Override
public Object insertUser(UserInsertForm userInsertForm) {
public ResultVO insertUser(UserInsertForm userInsertForm) {
if (userMapper.selectByPhoneNum(userInsertForm.getPhoneNumber()) != null) {
return ResultVOUtil.error(ResultEnum.USER_ALREADY_EXIST);
}
String code = redisService.get(PhoneCodeKey.phoneCodeKey, userInsertForm.getPhoneNumber(), String.class);
if (code == null) {
return ResultVOUtil.error(ResultEnum.CODE_IS_NULL);
}
if (!code.equals(userInsertForm.getCode())) {
throw new GlobalException(ResultEnum.PHONE_CODE_ERROR);
return ResultVOUtil.error(ResultEnum.PHONE_CODE_ERROR);
}
String password = userInsertForm.getPassword();
password = new BCryptPasswordEncoder().encode(password);
userInsertForm.setPassword(password);
User user = new User();
BeanUtils.copyProperties(userInsertForm, user);
user.setRole(1);
user.setHeadPortrait(headPicPath);
user.setHeadPortrait(headDefaultPicPath);
if (insert(user)) {
return ResultVOUtil.success();
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

@Override
public Object getValidationCode(String phoneNumber) {
public ResultVO getValidationCode(String phoneNumber) {
String code = AliyunSmsUtils.setCode();
try {
AliyunSmsUtils.sendInsertUserMsg(phoneNumber, code, TemplateCodeEnum.INSERTUSER.getValue());
Expand All @@ -155,5 +175,59 @@ public Object getValidationCode(String phoneNumber) {
}
}

@Override
public ResultVO updatePw(UpdatePwForm form) {
User user = userMapper.getUserByPhone(form.getPhoneNum());
if (user == null) {
return ResultVOUtil.error(ResultEnum.USER_NOT_EXIST);
}
String code = redisService.get(PhoneCodeKey.phoneCodeKey, form.getPhoneNum(), String.class);
if (code == null) {
return ResultVOUtil.error(ResultEnum.CODE_IS_NULL);
}
if (!code.equals(form.getCode())) {
return ResultVOUtil.error(ResultEnum.PHONE_CODE_ERROR);
}
user.setPassword(form.getNewPassword());
if (update(user)) {
return ResultVOUtil.success();
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

@Override
public ResultVO getOwnerMsg() {
User user = getCurrentUser();
UserVO result = userMapper.selectByPhoneNum(user.getPhoneNumber());
if (result != null) {
return ResultVOUtil.success(result);
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

@Override
public ResultVO updateUser(UpdateUserForm form, MultipartFile file) {
User user = getCurrentUser();
BeanUtils.copyProperties(form, user);
if (file != null) {
// 获得文件后缀
String fileName = file.getOriginalFilename();
assert fileName != null;
String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
String filePath = UploadFileUtil.uploadFile(headPicUploadPath + user.getUserId() + "."
+ suffix, file);
log.info("filePath:{}", filePath);
if (!"".equals(filePath)) {
user.setHeadPortrait(filePath);
}
}
if (update(user)) {
UserVO vo = new UserVO();
BeanUtils.copyProperties(user, vo);
return ResultVOUtil.success(vo);
}
return ResultVOUtil.error(ResultEnum.SERVER_ERROR);
}

}

0 comments on commit 01a2e9d

Please sign in to comment.