end: 增加上传(七牛)、信息修改

This commit is contained in:
wangiegie@gmail.com 2017-12-05 22:45:07 +08:00
parent 75029e226c
commit 1c5b53306f
4 changed files with 104 additions and 3 deletions

View File

@ -51,6 +51,12 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--七牛-->
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.2.0, 7.2.99]</version>
</dependency>
</dependencies>

View File

@ -13,13 +13,32 @@ import com.github.pig.common.constant.CommonConstant;
import com.github.pig.common.util.UserUtils;
import com.github.pig.common.vo.UserVo;
import com.github.pig.common.web.BaseController;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.util.RandomUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author lengleng
@ -36,6 +55,7 @@ public class UserController extends BaseController {
@Autowired
private SysMenuService sysMenuService;
/**
* 获取当前用户的用户名
*
@ -43,7 +63,6 @@ public class UserController extends BaseController {
*/
@GetMapping("/info")
public UserInfo user() {
logger.info("hahahahahahahahah------------------->");
SysUser condition = new SysUser();
condition.setUsername(UserUtils.getUserName());
SysUser sysUser = userService.selectOne(new EntityWrapper<>(condition));
@ -146,4 +165,69 @@ public class UserController extends BaseController {
return userService.selectWithRolePage(new Page<>(page, limit), sysUser);
}
/**
* 上传用户头像
* (多机部署有问题建议使用独立的文件服务器)
*
* @param file 资源
* @param request request
* @return filename map
*/
@PostMapping("/upload")
public Map<String, String> upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
String fileExt = FileUtil.extName(file.getOriginalFilename());
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone0());
UploadManager uploadManager = new UploadManager(cfg);
String accessKey = "hM2cBDEM0FTYzpXbigRW90kV12NhhzhFM3jCzurJ";
String secretKey = "g0HJr2Ltrs0k6tJDY6pDI2aVMUCPSWZDTROLcFMs";
String bucket = "pigcloud";
String key = RandomUtil.randomUUID() + "." + fileExt;
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(file.getInputStream(), key, upToken, null, null);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
} catch (IOException e) {
e.printStackTrace();
}
Map<String, String> resultMap = new HashMap<>(1);
resultMap.put("filename", "http://p0hpm86wj.bkt.clouddn.com/" + key);
return resultMap;
}
/**
* 修改个人信息
*
* @param userDto userDto
* @return success/false
*/
@PutMapping("/editInfo")
public Boolean editInfo(@RequestBody UserDto userDto) {
String username = UserUtils.getUserName();
UserVo userVo = userService.findUserByUsername(username);
if (!ENCODER.matches(userDto.getPassword(), userVo.getPassword())) {
return Boolean.FALSE;
}
SysUser sysUser = new SysUser();
sysUser.setUserId(userVo.getUserId());
sysUser.setPassword(ENCODER.encode(userDto.getNewpassword1()));
sysUser.setAvatar(userDto.getAvatar());
return userService.updateById(sysUser);
}
}

View File

@ -12,6 +12,11 @@ public class UserDto extends SysUser {
*/
private Integer role;
/**
* 新密码
*/
private String newpassword1;
public Integer getRole() {
return role;
}
@ -19,4 +24,12 @@ public class UserDto extends SysUser {
public void setRole(Integer role) {
this.role = role;
}
public String getNewpassword1() {
return newpassword1;
}
public void setNewpassword1(String newpassword1) {
this.newpassword1 = newpassword1;
}
}

View File

@ -41,9 +41,7 @@ public class LoggerFilter extends ZuulFilter {
@Override
public Object run() {
MDC.put(KEY_USER, "冷冷");
logSendService.send(RequestContext.getCurrentContext());
MDC.remove(KEY_USER);
return null;
}
}