|  | @ -0,0 +1,184 @@
 | 
												
													
														
															|  | 
 |  | package com.yihu.wlyy.web.patient.questionnaire;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import com.google.code.kaptcha.impl.DefaultKaptcha;
 | 
												
													
														
															|  | 
 |  | import com.google.code.kaptcha.util.Config;
 | 
												
													
														
															|  | 
 |  | import com.yihu.wlyy.web.BaseController;
 | 
												
													
														
															|  | 
 |  | import io.swagger.annotations.Api;
 | 
												
													
														
															|  | 
 |  | import io.swagger.annotations.ApiOperation;
 | 
												
													
														
															|  | 
 |  | import org.springframework.beans.factory.annotation.Autowired;
 | 
												
													
														
															|  | 
 |  | import org.springframework.data.redis.core.StringRedisTemplate;
 | 
												
													
														
															|  | 
 |  | import org.springframework.http.MediaType;
 | 
												
													
														
															|  | 
 |  | import org.springframework.web.bind.annotation.*;
 | 
												
													
														
															|  | 
 |  | import sun.misc.BASE64Encoder;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | import javax.imageio.ImageIO;
 | 
												
													
														
															|  | 
 |  | import java.awt.image.BufferedImage;
 | 
												
													
														
															|  | 
 |  | import java.io.ByteArrayOutputStream;
 | 
												
													
														
															|  | 
 |  | import java.io.IOException;
 | 
												
													
														
															|  | 
 |  | import java.util.HashMap;
 | 
												
													
														
															|  | 
 |  | import java.util.Map;
 | 
												
													
														
															|  | 
 |  | import java.util.Properties;
 | 
												
													
														
															|  | 
 |  | import java.util.UUID;
 | 
												
													
														
															|  | 
 |  | import java.util.concurrent.ConcurrentHashMap;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  | /**
 | 
												
													
														
															|  | 
 |  |  * 微信端非app用户可参与活动,获取验证码
 | 
												
													
														
															|  | 
 |  |  *
 | 
												
													
														
															|  | 
 |  |  * @author Sand
 | 
												
													
														
															|  | 
 |  |  * @created 2016/09/28
 | 
												
													
														
															|  | 
 |  |  */
 | 
												
													
														
															|  | 
 |  | @Api(description = "验证码")
 | 
												
													
														
															|  | 
 |  | @RestController
 | 
												
													
														
															|  | 
 |  | @RequestMapping(value = "/questionnaire/captcha", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
 | 
												
													
														
															|  | 
 |  | public class QuestionnaireCaptchaController extends BaseController {
 | 
												
													
														
															|  | 
 |  |     @Autowired
 | 
												
													
														
															|  | 
 |  |     private StringRedisTemplate redisTemplate;
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     private Map<String, String> captchaCache = new ConcurrentHashMap<>();
 | 
												
													
														
															|  | 
 |  |     private DefaultKaptcha captchaProducer = new DefaultKaptcha();
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     public QuestionnaireCaptchaController() {
 | 
												
													
														
															|  | 
 |  |         Properties properties = new Properties();
 | 
												
													
														
															|  | 
 |  |         properties.put("kaptcha.textproducer.font.color", "blue");
 | 
												
													
														
															|  | 
 |  |         properties.put("kaptcha.textproducer.font.size", "45");
 | 
												
													
														
															|  | 
 |  |         properties.put("kaptcha.textproducer.char.length", "4");
 | 
												
													
														
															|  | 
 |  |         properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         Config config = new Config(properties);
 | 
												
													
														
															|  | 
 |  |         captchaProducer.setConfig(config);
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     @RequestMapping(method = RequestMethod.GET)
 | 
												
													
														
															|  | 
 |  |     @ApiOperation("生成验证码,用于第一次请求")
 | 
												
													
														
															|  | 
 |  |     public String createCaptcha(){
 | 
												
													
														
															|  | 
 |  |         try{
 | 
												
													
														
															|  | 
 |  |             return generateCaptcha();
 | 
												
													
														
															|  | 
 |  |         } catch (Exception e){
 | 
												
													
														
															|  | 
 |  |             error(e);
 | 
												
													
														
															|  | 
 |  |             return error(500, e.getMessage());
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     @RequestMapping(value = "/{legacy_key}", method = RequestMethod.POST)
 | 
												
													
														
															|  | 
 |  |     @ApiOperation("刷新验证码,需提供第一次生成验证码返回的key")
 | 
												
													
														
															|  | 
 |  |     public String refreshCaptcha(@PathVariable("legacy_key") String legacyKey){
 | 
												
													
														
															|  | 
 |  |         try{
 | 
												
													
														
															|  | 
 |  |             cleanCaptcha(legacyKey);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             return generateCaptcha();
 | 
												
													
														
															|  | 
 |  |         } catch (Exception e){
 | 
												
													
														
															|  | 
 |  |             error(e);
 | 
												
													
														
															|  | 
 |  |             return error(500, e.getMessage());
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     @RequestMapping(value = "/{key}", method = RequestMethod.GET)
 | 
												
													
														
															|  | 
 |  |     @ApiOperation("校验证码,提供key及用户输入的验证码")
 | 
												
													
														
															|  | 
 |  |     public String verifyCaptcha(@PathVariable("key") String key,
 | 
												
													
														
															|  | 
 |  |                                 @RequestParam("text") String text){
 | 
												
													
														
															|  | 
 |  |         try{
 | 
												
													
														
															|  | 
 |  |             boolean pass = false;
 | 
												
													
														
															|  | 
 |  |             String captcha = captchaCache.get(key);
 | 
												
													
														
															|  | 
 |  |             if (captcha != null && captcha.equals(text.toLowerCase())){
 | 
												
													
														
															|  | 
 |  |                 pass = true;
 | 
												
													
														
															|  | 
 |  |                 cleanCaptcha(key);
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             return write(200, "ok", "pass", pass);
 | 
												
													
														
															|  | 
 |  |         } catch (Exception e){
 | 
												
													
														
															|  | 
 |  |             error(e);
 | 
												
													
														
															|  | 
 |  |             return error(500, e.getMessage());
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     private String generateCaptcha() throws IOException {
 | 
												
													
														
															|  | 
 |  |         String captchaText = captchaProducer.createText();
 | 
												
													
														
															|  | 
 |  |         BufferedImage image = captchaProducer.createImage(captchaText);
 | 
												
													
														
															|  | 
 |  |         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 | 
												
													
														
															|  | 
 |  |         ImageIO.write(image, "png", outputStream);
 | 
												
													
														
															|  | 
 |  |         String base64Img = new BASE64Encoder().encode(outputStream.toByteArray());
 | 
												
													
														
															|  | 
 |  |         String key = "captcha:" + UUID.randomUUID().toString() + ":text";
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         Map<String, String> data = new HashMap<>();
 | 
												
													
														
															|  | 
 |  |         data.put("key", key);
 | 
												
													
														
															|  | 
 |  |         data.put("image", base64Img);
 | 
												
													
														
															|  | 
 |  |         data.put("format", "png");
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         captchaCache.put(key, captchaText.toLowerCase());
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         return write(200, "ok", "data", data);
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     private void cleanCaptcha(String key){
 | 
												
													
														
															|  | 
 |  |         captchaCache.remove(key);
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     //=======================抽奖活动要结合openid生成验证码和校验验证码==========================//
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     @RequestMapping(value = "/withOpenId/{openId}",method = RequestMethod.POST)
 | 
												
													
														
															|  | 
 |  |     @ApiOperation("结合openid生成验证码,用于第一次请求")
 | 
												
													
														
															|  | 
 |  |     public String createCaptchaOpenId(@PathVariable("openId") String openId){
 | 
												
													
														
															|  | 
 |  |         try{
 | 
												
													
														
															|  | 
 |  |             return generatiCaptchaOpenId(openId);
 | 
												
													
														
															|  | 
 |  |         } catch (Exception e){
 | 
												
													
														
															|  | 
 |  |             error(e);
 | 
												
													
														
															|  | 
 |  |             return error(500, e.getMessage());
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     private String generatiCaptchaOpenId(String openId)throws Exception{
 | 
												
													
														
															|  | 
 |  |         String captchaText = captchaProducer.createText();
 | 
												
													
														
															|  | 
 |  |         BufferedImage image = captchaProducer.createImage(captchaText);
 | 
												
													
														
															|  | 
 |  |         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 | 
												
													
														
															|  | 
 |  |         ImageIO.write(image, "png", outputStream);
 | 
												
													
														
															|  | 
 |  |         String base64Img = new BASE64Encoder().encode(outputStream.toByteArray());
 | 
												
													
														
															|  | 
 |  |         String uuid = UUID.randomUUID().toString();
 | 
												
													
														
															|  | 
 |  |         String key = "captcha:" + openId +"_"+uuid + ":text";
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         Map<String, String> data = new HashMap<>();
 | 
												
													
														
															|  | 
 |  |         data.put("key", "captcha:" +uuid + ":text");
 | 
												
													
														
															|  | 
 |  |         data.put("image", base64Img);
 | 
												
													
														
															|  | 
 |  |         data.put("format", "png");
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         redisTemplate.opsForValue().set(key,captchaText.toLowerCase());
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |         return write(200, "ok", "data", data);
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     @RequestMapping(value = "/refresh/{legacy_key}", method = RequestMethod.POST)
 | 
												
													
														
															|  | 
 |  |     @ApiOperation("刷新验证码,需提供第一次生成验证码返回的key")
 | 
												
													
														
															|  | 
 |  |     public String refreshCaptchaOpenId(@PathVariable("legacy_key") String legacyKey,
 | 
												
													
														
															|  | 
 |  |                                        @RequestParam("openId") String openId){
 | 
												
													
														
															|  | 
 |  |         try{
 | 
												
													
														
															|  | 
 |  |             legacyKey = legacyKey.replaceFirst(":",":"+openId+"_");
 | 
												
													
														
															|  | 
 |  |             redisTemplate.delete(legacyKey);
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             return generatiCaptchaOpenId(openId);
 | 
												
													
														
															|  | 
 |  |         } catch (Exception e){
 | 
												
													
														
															|  | 
 |  |             error(e);
 | 
												
													
														
															|  | 
 |  |             return error(500, e.getMessage());
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |     @RequestMapping(value = "/verifyWithOpenId/{key}", method = RequestMethod.POST)
 | 
												
													
														
															|  | 
 |  |     @ApiOperation("校验证码,提供key及用户输入的验证码")
 | 
												
													
														
															|  | 
 |  |     public String verifyCaptcha(@PathVariable("key") String key,
 | 
												
													
														
															|  | 
 |  |                                 @RequestParam("openId") String openId,
 | 
												
													
														
															|  | 
 |  |                                 @RequestParam("text") String text){
 | 
												
													
														
															|  | 
 |  |         try{
 | 
												
													
														
															|  | 
 |  |             boolean pass = false;
 | 
												
													
														
															|  | 
 |  |             key = key.replaceFirst(":",":"+openId+"_");
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             String captcha = redisTemplate.opsForValue().get(key);
 | 
												
													
														
															|  | 
 |  |             if (captcha != null && captcha.equals(text.toLowerCase())){
 | 
												
													
														
															|  | 
 |  |                 pass = true;
 | 
												
													
														
															|  | 
 |  |                 redisTemplate.delete(key);
 | 
												
													
														
															|  | 
 |  |             }
 | 
												
													
														
															|  | 
 |  | 
 | 
												
													
														
															|  | 
 |  |             return write(200, "ok", "pass", pass);
 | 
												
													
														
															|  | 
 |  |         } catch (Exception e){
 | 
												
													
														
															|  | 
 |  |             error(e);
 | 
												
													
														
															|  | 
 |  |             return error(500, e.getMessage());
 | 
												
													
														
															|  | 
 |  |         }
 | 
												
													
														
															|  | 
 |  |     }
 | 
												
													
														
															|  | 
 |  | }
 |