package top.imwork.window.silos.controller.generator;

import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.imwork.commons.core.constants.Constants;
import top.imwork.window.silos.utils.ImageCodeUtils;
import top.imwork.window.silos.utils.RedisUtils;

import java.io.IOException;

/**
 * Copyright (C), 2015-2021
 * FileName: ValidateCodeController
 * Author(作者姓名):   jiangxiaowei
 * CreateDate(创建时间):     2021/8/6 15:49
 * UpdateDate(修改时间):     2021/8/6 15:49
 * Description(功能描述):    验证码控制器
 * Since(版本号)  1.0.0
 * History:
 */
@RequestMapping("/ValidateCode")
@RestController
public class ValidateCodeController {
    @Resource
    private RedisUtils redisUtils;

    @GetMapping(value = "/imageCode")
    public void image(HttpServletRequest request, HttpServletResponse response) throws IOException {
        redisUtils.hdel(Constants.SESSION_KEY,request.getSession().getId());
        response.setContentType("image/jpeg");
        response.setHeader("Pragma","no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        ImageCodeUtils imageCode = new ImageCodeUtils();
        redisUtils.hset(Constants.SESSION_KEY,request.getSession().getId(),imageCode.getCode(),60);
        imageCode.write(response.getOutputStream());
    }
}
