添加上传用户头像接口

master
panqihua 5 years ago
parent 6a19ea0ce2
commit e6a823b93f
  1. 91
      src/main/java/com/community/pocket/api/android/UserController.java
  2. 19
      src/main/java/com/community/pocket/entity/vo/android/UserHeadImg.java
  3. 4
      src/main/java/com/community/pocket/repository/android/TokenDao.java
  4. 41
      src/main/java/com/community/pocket/repository/android/UserDao.java
  5. 5
      src/main/resources/application.yaml

@ -4,9 +4,7 @@ import com.community.pocket.entity.po.android.Token;
import com.community.pocket.entity.vo.Result; import com.community.pocket.entity.vo.Result;
import com.community.pocket.entity.vo.android.*; import com.community.pocket.entity.vo.android.*;
import com.community.pocket.repository.EmailDao; import com.community.pocket.repository.EmailDao;
import com.community.pocket.repository.android.TokenDao; import com.community.pocket.repository.android.*;
import com.community.pocket.repository.android.UserDao;
import com.community.pocket.repository.android.VisitorPeopleDao;
import com.community.pocket.util.EmailService; import com.community.pocket.util.EmailService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
@ -30,6 +28,9 @@ public class UserController {
@Autowired @Autowired
private EmailService emailService; private EmailService emailService;
@Autowired
private TokenService tokenService;
@Autowired @Autowired
private TokenDao tokenDao; private TokenDao tokenDao;
@ -111,39 +112,63 @@ public class UserController {
} }
@PostMapping("/uploadHeadImg")
public InfoResponse upload(UserHeadImg userHeadImg) {
return tokenService.checkToken(userHeadImg, new DoCheck<InfoResponse, UserHeadImg>() {
@Override
public InfoResponse ok(UserHeadImg userHeadImg) {
if (userDao.uploadImg(userHeadImg)) {
return new InfoResponse(Result.OK, InfoResponse.Msg.ok);
} else {
return new InfoResponse(Result.FAIL, InfoResponse.Msg.fail);
}
}
@Override
public InfoResponse fail() {
return new InfoResponse(Result.FAIL, InfoResponse.Msg.fail, InfoResponse.Msg.token);
}
});
}
/** /**
* 用户登录 * 用户登录
*
* @param username 用户 * @param username 用户
* @param password 密码 * @param password 密码
*/ */
@PostMapping("/login") @PostMapping("/login")
public LoginResponse login(String username, String password) { public LoginResponse login(String username, String password) {
if(userDao.login(username, password)){ if (userDao.login(username, password)) {
Token token=tokenDao.save(username); Token token = tokenDao.save(username);
LoginResponse response; LoginResponse response;
if(token!=null) { if (token != null) {
response=new LoginResponse(Result.OK, LoginResponse.Msg.ok,username); response = new LoginResponse(Result.OK, LoginResponse.Msg.ok, username);
response.setToken(token); response.setToken(token);
}else{ } else {
response=new LoginResponse(Result.FAIL,LoginResponse.Msg.fail); response = new LoginResponse(Result.FAIL, LoginResponse.Msg.fail);
} }
return response; return response;
}else{ } else {
return new LoginResponse(Result.FAIL,LoginResponse.Msg.fail); return new LoginResponse(Result.FAIL, LoginResponse.Msg.fail);
} }
} }
@PostMapping("/login/token") @PostMapping("/login/token")
public LoginResponse check(String token){ public LoginResponse check(Token token) {
if(tokenDao.checkToken(token)){ return tokenService.checkToken(token, new DoCheck<LoginResponse, Token>() {
Token t=tokenDao.get(token); @Override
if(t!=null){ public LoginResponse ok(Token token) {
LoginResponse response= new LoginResponse(Result.OK, LoginResponse.Msg.ok,t.getUsername()); LoginResponse response = new LoginResponse(Result.OK, LoginResponse.Msg.ok, token.getUsername());
response.setToken(t); response.setToken(token);
return response; return response;
} }
}
return new LoginResponse(Result.FAIL, LoginResponse.Msg.fail); @Override
public LoginResponse fail() {
return new LoginResponse(Result.FAIL, LoginResponse.Msg.fail);
}
});
} }
//检索投诉人 //检索投诉人
@ -163,19 +188,25 @@ public class UserController {
//修改密码 //修改密码
@PostMapping("/my/info/modifyPwd") @PostMapping("/my/info/modifyPwd")
public InfoResponse modifyPwd(ModifyPwdForm modifyPwdForm) { public InfoResponse modifyPwd(ModifyPwdForm modifyPwdForm) {
if(tokenDao.checkToken(modifyPwdForm.getToken())){ return tokenService.checkToken(modifyPwdForm, new DoCheck<InfoResponse, ModifyPwdForm>() {
if(userDao.login(modifyPwdForm.getUsername(),modifyPwdForm.getOldPassword())){ @Override
if(userDao.modifyPwd(modifyPwdForm)){ public InfoResponse ok(ModifyPwdForm modifyPwdForm) {
return new InfoResponse(Result.OK,InfoResponse.Msg.modify_pwd_ok); if (userDao.login(modifyPwdForm.getUsername(), modifyPwdForm.getOldPassword())) {
}else{ if (userDao.modifyPwd(modifyPwdForm)) {
return new InfoResponse(Result.FAIL,InfoResponse.Msg.modify_pwd_fail); 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.modify_oldpwd_fail);
} }
}else {
return new InfoResponse(Result.FAIL,InfoResponse.Msg.token); @Override
} public InfoResponse fail() {
return new InfoResponse(Result.FAIL, InfoResponse.Msg.token);
}
});
} }
} }

@ -0,0 +1,19 @@
package com.community.pocket.entity.vo.android;
import com.community.pocket.entity.po.android.Token;
import org.springframework.web.multipart.MultipartFile;
/**
* 上传头像表单
*/
public class UserHeadImg extends Token {
private MultipartFile file;
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}

@ -58,8 +58,8 @@ public class TokenDao extends BaseDao<Token> {
} }
//判断令牌是否存在 //判断令牌是否存在
public boolean checkToken(String token){ private boolean checkToken(String token) {
return mongoTemplate.exists(new Query(Criteria.where("token").is(token).and("time").gt(System.currentTimeMillis())),entityClass()); return mongoTemplate.exists(new Query(Criteria.where("token").is(token).and("time").gt(System.currentTimeMillis())), entityClass());
} }
public Token get(String token){ public Token get(String token){

@ -24,7 +24,10 @@ import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import org.springframework.util.DigestUtils; import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@ -44,6 +47,12 @@ public class UserDao extends BaseDao<MyInfo> {
@Value("${register.credit-score}") @Value("${register.credit-score}")
private int creditScore; private int creditScore;
@Value("${spring.resources.static-locations}")
private String imgPath;
@Value("${spring.mvc.static-path-pattern}")
private String mvcPath;
//检查用户名是否已注册 //检查用户名是否已注册
public boolean hasUser(String username) { public boolean hasUser(String username) {
if (!StringUtils.isEmpty(username)) { if (!StringUtils.isEmpty(username)) {
@ -183,12 +192,34 @@ public class UserDao extends BaseDao<MyInfo> {
save(myInfo); save(myInfo);
} }
//上传头像
public boolean uploadImg(UserHeadImg userHeadImg) {
MultipartFile file = userHeadImg.getFile();
if (file != null) {
try {
String fileName = file.getOriginalFilename() == null ? System.currentTimeMillis() + ".png" : file.getOriginalFilename();
File loadFile = new File(getClass().getResource("/").getFile() + imgPath.replace("classpath:/", ""), fileName);
if (!loadFile.exists() && !loadFile.mkdirs()) {
throw new RuntimeException("无法建立目录" + loadFile.getAbsolutePath());
}
file.transferTo(loadFile);
return mongoTemplate.updateFirst(new Query(Criteria.where(LookupOperationUtil.idKey).is(userHeadImg.getUsername())), Update.update("headImg", mvcPath.replace("**", loadFile.getName())), entityClass()).wasAcknowledged();
} catch (IOException e) {
e.printStackTrace();
LOG.error(e.toString());
return false;
}
} else {
return false;
}
}
//获取活跃用户 //获取活跃用户
public List<UserHot> rankUser(int num){ public List<UserHot> rankUser(int num) {
List<UserHot> userHots=new ArrayList<>(); List<UserHot> userHots = new ArrayList<>();
List<MyInfo> myInfos=mongoTemplate.find(new Query().with(Sort.by("creditScore").descending()).limit(num),entityClass()); List<MyInfo> myInfos = mongoTemplate.find(new Query().with(Sort.by("creditScore").descending()).limit(num), entityClass());
for(MyInfo info:myInfos){ for (MyInfo info : myInfos) {
UserHot userHot=new UserHot(); UserHot userHot = new UserHot();
userHot.setUserName(info.getUsername()); userHot.setUserName(info.getUsername());
userHot.setCreditScore(info.getCreditScore()); userHot.setCreditScore(info.getCreditScore());
userHots.add(userHot); userHots.add(userHot);

@ -15,6 +15,11 @@ spring:
#国际化配置 #国际化配置
messages: messages:
basename: static/i18n/messages #相对路径 开头请勿添加斜杠 basename: static/i18n/messages #相对路径 开头请勿添加斜杠
#静态资源目录
mvc:
static-path-pattern: /image/**
resources:
static-locations: classpath:/resources/static/image
#邮箱配置 #邮箱配置
email: email:

Loading…
Cancel
Save