增加修改密码接口

master
panqihua 5 years ago
parent 5c942d230c
commit aaeabdad4a
  1. 17
      src/main/java/com/community/pocket/api/android/ForumController.java
  2. 19
      src/main/java/com/community/pocket/api/android/UserController.java
  3. 2
      src/main/java/com/community/pocket/entity/po/android/MyInfo.java
  4. 32
      src/main/java/com/community/pocket/entity/vo/android/InfoResponse.java
  5. 29
      src/main/java/com/community/pocket/entity/vo/android/ModifyPwdForm.java
  6. 32
      src/main/java/com/community/pocket/repository/android/UserDao.java
  7. 12
      src/main/resources/application.yaml
  8. 13
      src/test/java/com/community/pocket/DemoApplicationTests.java

@ -5,6 +5,7 @@ import com.community.pocket.entity.po.ForumContent;
import com.community.pocket.entity.po.Notice; import com.community.pocket.entity.po.Notice;
import com.community.pocket.entity.po.android.Active; import com.community.pocket.entity.po.android.Active;
import com.community.pocket.entity.po.android.Complain; import com.community.pocket.entity.po.android.Complain;
import com.community.pocket.entity.po.android.MyInfo;
import com.community.pocket.entity.po.android.Token; import com.community.pocket.entity.po.android.Token;
import com.community.pocket.entity.vo.android.ActiveForm; import com.community.pocket.entity.vo.android.ActiveForm;
import com.community.pocket.entity.vo.Result; import com.community.pocket.entity.vo.Result;
@ -155,4 +156,20 @@ public class ForumController {
} }
} }
//获取个人信息
@GetMapping("/my/info")
public InfoResponse loadInfo(Token token) {
if (tokenDao.checkToken(token)) {
MyInfo myInfo = userDao.queryUser(token.getUsername());
if (myInfo != null) {
InfoResponse response = new InfoResponse(Result.OK, InfoResponse.Msg.ok);
response.setMyInfo(myInfo);
return response;
} else {
return new InfoResponse(Result.FAIL, InfoResponse.Msg.fail);
}
} else {
return new InfoResponse(Result.FAIL, InfoResponse.Msg.token);
}
}
} }

@ -138,4 +138,23 @@ public class UserController {
queryUserResponse.setPeopleList(userDao.queryUser(userForm)); queryUserResponse.setPeopleList(userDao.queryUser(userForm));
return queryUserResponse; return queryUserResponse;
} }
//修改密码
@PostMapping("/my/info/modifyPwd")
public InfoResponse modifyPwd(ModifyPwdForm modifyPwdForm) {
if(tokenDao.checkToken(modifyPwdForm.getToken())){
if(userDao.login(modifyPwdForm.getUsername(),modifyPwdForm.getOldPassword())){
if(userDao.modifyPwd(modifyPwdForm)){
return new InfoResponse(Result.OK,InfoResponse.Msg.modify_pwd_ok);
}else{
return new InfoResponse(Result.FAIL,InfoResponse.Msg.modify_pwd_fail);
}
}else{
return new InfoResponse(Result.FAIL,InfoResponse.Msg.modify_oldpwd_fail);
}
}else {
return new InfoResponse(Result.FAIL,InfoResponse.Msg.token);
}
}
} }

@ -1,6 +1,7 @@
package com.community.pocket.entity.po.android; package com.community.pocket.entity.po.android;
import com.community.pocket.util.TableName; import com.community.pocket.util.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document; import org.springframework.data.mongodb.core.mapping.Document;
@ -13,6 +14,7 @@ public class MyInfo {
@Id @Id
private String username; private String username;
//密码 //密码
@JsonIgnore
private String password; private String password;
//信用分 //信用分
private Integer creditScore; private Integer creditScore;

@ -0,0 +1,32 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.android.MyInfo;
import com.community.pocket.entity.vo.Result;
/**
* 个人信息响应
*/
public class InfoResponse extends AndroidResponse<InfoResponse.Msg>{
private MyInfo myInfo;
public InfoResponse(Result result, Msg message, Object... args) {
super(result, message, args);
}
public MyInfo getMyInfo() {
return myInfo;
}
public void setMyInfo(MyInfo myInfo) {
this.myInfo = myInfo;
}
public enum Msg implements CustomMessage{
ok,
fail,
modify_pwd_ok,
modify_pwd_fail,
modify_oldpwd_fail,
token
}
}

@ -0,0 +1,29 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.android.Token;
/**
* 修改密码表单
*/
public class ModifyPwdForm extends Token {
//原密码
private String oldPassword;
//新密码
private String newPassword;
public String getOldPassword() {
return oldPassword;
}
public void setOldPassword(String oldPassword) {
this.oldPassword = oldPassword;
}
public String getNewPassword() {
return newPassword;
}
public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}
}

@ -1,13 +1,12 @@
package com.community.pocket.repository.android; package com.community.pocket.repository.android;
import com.community.pocket.entity.po.android.MyInfo; import com.community.pocket.entity.po.android.MyInfo;
import com.community.pocket.entity.vo.android.QueryUserForm; import com.community.pocket.entity.vo.android.*;
import com.community.pocket.entity.vo.android.UserHot;
import com.community.pocket.entity.vo.android.UserRegister;
import com.community.pocket.repository.BaseDao; import com.community.pocket.repository.BaseDao;
import com.mongodb.client.result.UpdateResult; import com.mongodb.client.result.UpdateResult;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria; import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query; import org.springframework.data.mongodb.core.query.Query;
@ -26,6 +25,9 @@ public class UserDao extends BaseDao<MyInfo> {
private static final Logger LOG = LoggerFactory.getLogger(UserDao.class); private static final Logger LOG = LoggerFactory.getLogger(UserDao.class);
@Value("${register.credit-score}")
private int creditScore;
//检查用户名是否已注册 //检查用户名是否已注册
public boolean hasUser(String username){ public boolean hasUser(String username){
if(!StringUtils.isEmpty(username)){ if(!StringUtils.isEmpty(username)){
@ -92,7 +94,10 @@ public class UserDao extends BaseDao<MyInfo> {
myInfo.setPassword(DigestUtils.md5DigestAsHex(userRegister.getPassword().getBytes())); myInfo.setPassword(DigestUtils.md5DigestAsHex(userRegister.getPassword().getBytes()));
myInfo.setMobie(userRegister.getMobie()); myInfo.setMobie(userRegister.getMobie());
myInfo.setEmail(userRegister.getEmail()); myInfo.setEmail(userRegister.getEmail());
myInfo.setCreditScore(0); myInfo.setCreditScore(creditScore);
myInfo.setRecentPosts(0);
myInfo.setRecentVisitors(0);
myInfo.setScoreHistory(new ArrayList<>());
save(myInfo); save(myInfo);
} }
@ -109,16 +114,31 @@ public class UserDao extends BaseDao<MyInfo> {
return userHots; return userHots;
} }
//获取用户 //根据关键字匹配用户
public List<String> queryUser(QueryUserForm userForm){ public List<String> queryUser(QueryUserForm userForm){
List<String> userNames=new ArrayList<>(); List<String> userNames=new ArrayList<>();
List<MyInfo> myInfos=mongoTemplate.find(new Query(Criteria.where("username").ne(userForm.getUsername()).and("username").regex(Pattern.compile(".*"+userForm.getName()+".*"))),entityClass()); List<MyInfo> myInfos=mongoTemplate.find(new Query(new Criteria().andOperator(
Criteria.where("username").regex(Pattern.compile(".*"+userForm.getName()+".*")),
Criteria.where("username").ne(userForm.getUsername())))
,entityClass());
for(MyInfo info:myInfos){ for(MyInfo info:myInfos){
userNames.add(info.getUsername()); userNames.add(info.getUsername());
} }
return userNames; return userNames;
} }
//获取个人信息
public MyInfo queryUser(String username){
return mongoTemplate.findOne(new Query(Criteria.where("username").is(username)),entityClass());
}
//修改密码
public boolean modifyPwd(ModifyPwdForm modifyPwdForm) {
String md5Password= DigestUtils.md5DigestAsHex(modifyPwdForm.getOldPassword().getBytes());
String newMd5password=DigestUtils.md5DigestAsHex(modifyPwdForm.getNewPassword().getBytes());
return mongoTemplate.updateFirst(new Query(Criteria.where("username").is(modifyPwdForm.getUsername()).and("password").is(md5Password)),Update.update("password",newMd5password),entityClass()).wasAcknowledged();
}
@Override @Override
public MyInfo save(MyInfo myInfo) { public MyInfo save(MyInfo myInfo) {
try { try {

@ -15,14 +15,21 @@ spring:
#邮箱配置 #邮箱配置
email: email:
#支持的邮箱类型
support-type: support-type:
- name: 网易 - name: 网易
suffix: '@163.com' suffix: '@163.com'
#邮件标题
subject: 口袋社区 subject: 口袋社区
#发送邮箱
from: 15920722180@163.com from: 15920722180@163.com
#验证码
code: code:
#长度
length: 6 length: 6
#管理员激活邮件模版
manager: 欢迎注册口袋社区后台服务,您获取到的验证码是:%s manager: 欢迎注册口袋社区后台服务,您获取到的验证码是:%s
#用户重置密码邮件模版
user: 重置密码,您获取到的验证码是:%s user: 重置密码,您获取到的验证码是:%s
#令牌配置 #令牌配置
@ -33,3 +40,8 @@ token:
forum: forum:
#帖子正文缩略长度 #帖子正文缩略长度
content-length: 10 content-length: 10
#注册配置
register:
#默认信用分
credit-score: 0

@ -1,13 +1,26 @@
package com.community.pocket; package com.community.pocket;
import com.community.pocket.entity.vo.android.QueryUserForm;
import com.community.pocket.repository.android.UserDao;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest @SpringBootTest
class DemoApplicationTests { class DemoApplicationTests {
@Autowired
private UserDao userDao;
@Test @Test
void contextLoads() { void contextLoads() {
} }
@Test
void queryUser(){
QueryUserForm queryUserForm=new QueryUserForm();
queryUserForm.setName("p");
userDao.queryUser(queryUserForm);
}
} }

Loading…
Cancel
Save