1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
package com.mesasoft.cn.web.controller;
import com.alibaba.fastjson.JSONObject;
import com.mesasoft.cn.modules.constant.DefaultValues;
import com.mesasoft.cn.annotation.AuthInterceptor;
import com.mesasoft.cn.config.SettingConfig;
import com.mesasoft.cn.enums.InterceptorLevel;
import com.mesasoft.cn.service.ICommonService;
import com.mesasoft.cn.util.ControllerUtils;
import com.zhazhapan.modules.constant.ValueConsts;
import com.zhazhapan.util.Checker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
* @author pantao
* @since 2018/1/23
*/
@RestController
@RequestMapping("/common")
@Api(value = "/common", description = "公共接口")
public class CommonController {
private static Logger logger = LoggerFactory.getLogger(ConfigController.class);
private final ICommonService commonService;
private final HttpServletRequest request;
private final JSONObject jsonObject;
@Autowired
public CommonController(ICommonService commonService, HttpServletRequest request, JSONObject jsonObject) {
this.commonService = commonService;
this.request = request;
this.jsonObject = jsonObject;
}
@ApiOperation(value = "获取头像资源")
@AuthInterceptor(InterceptorLevel.NONE)
@RequestMapping(value = "/avatar/{name}", method = RequestMethod.GET)
public void getAvatar(HttpServletResponse response, @PathVariable("name") String name) throws IOException {
String path = SettingConfig.getAvatarStoragePath() + ValueConsts.SEPARATOR + name;
ControllerUtils.loadResource(response, path, ValueConsts.FALSE);
}
@ApiOperation(value = "上传头像")
@ApiImplicitParam(name = "multipartFile", value = "头像", required = true)
@AuthInterceptor(InterceptorLevel.USER)
@RequestMapping(value = "/avatar", method = RequestMethod.POST)
public String avatarUpload(@RequestParam("file") MultipartFile multipartFile) {
String name = commonService.uploadAvatar(multipartFile);
if (Checker.isEmpty(name)) {
jsonObject.put("error", "文件格式不合法");
} else {
jsonObject.put("success", "/common/avatar/" + name);
}
return jsonObject.toString();
}
@ApiOperation(value = "发送验证码")
@AuthInterceptor(InterceptorLevel.NONE)
@RequestMapping(value = "/{email}/code", method = RequestMethod.POST)
public String sendVerifyCode(@PathVariable("email") String email) {
int code = commonService.sendVerifyCode(email);
if (code > 0) {
request.getSession().setAttribute(DefaultValues.CODE_STRING, code);
logger.info("verify code: " + code);
jsonObject.put("status", "success");
} else {
jsonObject.put("status", "error");
}
return jsonObject.toString();
}
@ApiOperation(value = "验证验证码是否正确")
@AuthInterceptor(InterceptorLevel.NONE)
@RequestMapping(value = "/{code}/verification", method = RequestMethod.PUT)
public String verifyCode(@PathVariable("code") String code) {
boolean isSuccess = Checker.checkNull(code).equals(String.valueOf(request.getSession().getAttribute
(DefaultValues.CODE_STRING)));
return ControllerUtils.getResponse(isSuccess);
}
}
|