package me.liaowei.mangdie.controller;

import me.liaowei.mangdie.util.BaseUtil;
import me.liaowei.mangdie.util.Constant;
import me.liaowei.mangdie.util.Json;
import me.liaowei.mangdie.util.SessionHandleUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Random;

/**
 * 简单验证码生成
 * 不考虑图片加干扰线
 * Created by liaowei on 2016/11/18.
 */
@Controller
@RequestMapping("/")
public class CaptchaController {
    private HttpServletRequest request;
    @RequestMapping(value = "getCaptcha", method = RequestMethod.GET)
    public void getCaptcha(HttpServletRequest request,HttpServletResponse response){
        String[] yzmArray = {"A","B","C", "D", "E","F","G","H","I","J", "K", "L","M","N","P","Q","R", "S", "T","U","V","W","X","Y", "Z", "1","2","3","4","5","6","7", "8","9"};
        Random r = new Random();
        String yzmValue = "";
        for (int i=0;i<5;i++){
            int num = r.nextInt(yzmArray.length);
            yzmValue += yzmArray[num];
        }
        SessionHandleUtil.saveSession(request, Constant.SESSION_KEY_CAPTCHA,yzmValue);
        Json json = new Json();
        json.setCode(200);
        json.setMsg("成功");
        json.setObj(yzmValue);
        BaseUtil.renderData(response,json,"CaptchaController");
    }
}
