CommonController.java
4.61 KB
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
95
96
97
98
99
100
101
102
103
package com.glsx.publicservice.controller;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
import javax.servlet.http.HttpServletRequest;
import com.alibaba.fastjson.JSONObject;
import com.glsx.common.utils.HttpUtils;
import com.glsx.publicservice.util.SmsUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import com.github.pagehelper.util.StringUtil;
import com.glsx.common.annotation.AuthCheck;
import com.glsx.common.constants.ResultCodeEnum;
import com.glsx.common.constants.ResultObject;
import com.glsx.publicservice.service.CommonService;
import com.glsx.publicservice.util.fastdfs.FastDFSClientWrapper;
/**
* @author zhanghj
* @version 1.0.0
* @Title CommonController.java
* @Package com.glsx.publicservice.controller
* @Description TODO
* @date 2019年2月16日 下午5:41:40
*/
@RestController
@RequestMapping(value = "/common")
public class CommonController {
private static final Logger logger = LoggerFactory.getLogger(CommonController.class);
@Autowired
private CommonService commonService;
@RequestMapping(value = "/sendVerificationCode", method = {RequestMethod.GET, RequestMethod.POST})
public ResultObject sendVerificationCode(HttpServletRequest request, String phone) {
Map<String, Object> result = new HashMap<String, Object>();
try {
if (StringUtil.isEmpty(phone)) {
return new ResultObject(ResultCodeEnum.PARAM_ERROR.getCode(), ResultCodeEnum.PARAM_ERROR.getMsg());
}
String verifyCode = commonService.sendVerificationCode(phone);
logger.info("手机号:" + phone + "的短信验证码:" + verifyCode);
//result.put("verifyCode", verifyCode);
return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(), result);
} catch (RedisSystemException e) {
logger.error("获取验证码redis异常" + e.getMessage(), e);
/*
* 测试环境redis连接总被重试异常重试
*/
String verifyCode = commonService.sendVerificationCode(phone);
logger.info("手机号:" + phone + "的短信验证码:" + verifyCode);
result.put("verifyCode", verifyCode);
return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(), result);
} catch (Exception e) {
logger.error("获取验证码失败" + e.getMessage(), e);
return new ResultObject(ResultCodeEnum.ERROR.getCode(), ResultCodeEnum.ERROR.getMsg());
}
}
@AuthCheck
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
public ResultObject uploadImage(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {
String path = FastDFSClientWrapper.uploadFile(file);
logger.info("上传文件路径==" + path);
return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg(), path);
}
@RequestMapping(value = "/upload", method = RequestMethod.GET)
public ModelAndView get(ModelAndView mv) {
mv.setViewName("fileUpload");
return mv;
}
@AuthCheck
@RequestMapping(value = "/sendSms", method = {RequestMethod.GET, RequestMethod.POST})
public ResultObject sendSms(@RequestParam("phone") String phone) throws IOException {
SmsUtil smsUtil = new SmsUtil();
try {
String msgContent = MessageFormat.format("您本次操作验证码为:{0},一分钟内有效。", phone);
smsUtil.send(phone, msgContent);
}catch (Exception e) {
logger.error(phone+"技工注册成功发送短信失败" + e.getMessage(), e);
return new ResultObject(ResultCodeEnum.ERROR.getCode(), ResultCodeEnum.ERROR.getMsg());
}
return new ResultObject(ResultCodeEnum.SUCCESS.getCode(), ResultCodeEnum.SUCCESS.getMsg());
}
}