CommonController.java 3.32 KB
package com.glsx.publicservice.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.RedisSystemException;
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;

/**
 * 
 * @Title CommonController.java
 * @Package com.glsx.publicservice.controller
 * @Description TODO
 * @author zhanghj
 * @date 2019年2月16日 下午5:41:40
 * @version 1.0.0
 */
@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;
	}
}