添加上传用户头像接口

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.android.*;
import com.community.pocket.repository.EmailDao;
import com.community.pocket.repository.android.TokenDao;
import com.community.pocket.repository.android.UserDao;
import com.community.pocket.repository.android.VisitorPeopleDao;
import com.community.pocket.repository.android.*;
import com.community.pocket.util.EmailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
@ -30,6 +28,9 @@ public class UserController {
@Autowired
private EmailService emailService;
@Autowired
private TokenService tokenService;
@Autowired
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 password 密码
*/
@PostMapping("/login")
public LoginResponse login(String username, String password) {
if(userDao.login(username, password)){
Token token=tokenDao.save(username);
if (userDao.login(username, password)) {
Token token = tokenDao.save(username);
LoginResponse response;
if(token!=null) {
response=new LoginResponse(Result.OK, LoginResponse.Msg.ok,username);
if (token != null) {
response = new LoginResponse(Result.OK, LoginResponse.Msg.ok, username);
response.setToken(token);
}else{
response=new LoginResponse(Result.FAIL,LoginResponse.Msg.fail);
} else {
response = new LoginResponse(Result.FAIL, LoginResponse.Msg.fail);
}
return response;
}else{
return new LoginResponse(Result.FAIL,LoginResponse.Msg.fail);
} else {
return new LoginResponse(Result.FAIL, LoginResponse.Msg.fail);
}
}
@PostMapping("/login/token")
public LoginResponse check(String token){
if(tokenDao.checkToken(token)){
Token t=tokenDao.get(token);
if(t!=null){
LoginResponse response= new LoginResponse(Result.OK, LoginResponse.Msg.ok,t.getUsername());
response.setToken(t);
public LoginResponse check(Token token) {
return tokenService.checkToken(token, new DoCheck<LoginResponse, Token>() {
@Override
public LoginResponse ok(Token token) {
LoginResponse response = new LoginResponse(Result.OK, LoginResponse.Msg.ok, token.getUsername());
response.setToken(token);
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")
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);
return tokenService.checkToken(modifyPwdForm, new DoCheck<InfoResponse, ModifyPwdForm>() {
@Override
public InfoResponse ok(ModifyPwdForm modifyPwdForm) {
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.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){
return mongoTemplate.exists(new Query(Criteria.where("token").is(token).and("time").gt(System.currentTimeMillis())),entityClass());
private boolean checkToken(String token) {
return mongoTemplate.exists(new Query(Criteria.where("token").is(token).and("time").gt(System.currentTimeMillis())), entityClass());
}
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.util.DigestUtils;
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.List;
import java.util.regex.Pattern;
@ -44,6 +47,12 @@ public class UserDao extends BaseDao<MyInfo> {
@Value("${register.credit-score}")
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) {
if (!StringUtils.isEmpty(username)) {
@ -183,12 +192,34 @@ public class UserDao extends BaseDao<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){
List<UserHot> userHots=new ArrayList<>();
List<MyInfo> myInfos=mongoTemplate.find(new Query().with(Sort.by("creditScore").descending()).limit(num),entityClass());
for(MyInfo info:myInfos){
UserHot userHot=new UserHot();
public List<UserHot> rankUser(int num) {
List<UserHot> userHots = new ArrayList<>();
List<MyInfo> myInfos = mongoTemplate.find(new Query().with(Sort.by("creditScore").descending()).limit(num), entityClass());
for (MyInfo info : myInfos) {
UserHot userHot = new UserHot();
userHot.setUserName(info.getUsername());
userHot.setCreditScore(info.getCreditScore());
userHots.add(userHot);

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

Loading…
Cancel
Save