/**
 * Project Name:aiop-web File Name:KaptchaController.java Package Name:com.asiainfo.biapp.aiop.web.controller.common
 * Date:2017年8月29日下午3:13:42 Copyright (c) 2017, chenzhou1025@126.com All Rights Reserved.
 *
 */

package com.ozs.account.controller;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;
import javax.imageio.ImageIO;

import com.ozs.account.util.R;
import com.ozs.account.util.S;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.code.kaptcha.impl.DefaultKaptcha;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import sun.misc.BASE64Encoder;

/* *
 * @Description 获取验证码
 * @Date 2019/3/22
**/
@Api("验证码图片获取接口")
@RestController
public class KaptchaController {

    @Resource
    private DefaultKaptcha defaultKaptcha;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "验证码图片获取", notes = "验证码图片获取")
    @GetMapping(value = "/account/authCode")
    public R<Object> getKaptchaImage(){
        try {
            //生成验证ID，用作验证码校验标识
            String authId = UUID.randomUUID().toString();
            //生成验证码文字
            String authCode = defaultKaptcha.createText();
            //验证ID，验证码存入redis
            redisTemplate.opsForValue().set(authId,authCode,5, TimeUnit.MINUTES);
            //生成图片
            BufferedImage image = defaultKaptcha.createImage(authCode);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", bos);
            //将图片转化成字节
            byte[] bytes = bos.toByteArray();
            bos.close();
            //将字节转化成base64的字符串
            String baseCode = new BASE64Encoder().encodeBuffer(bytes).replaceAll("\\r|\\n","");

            Map<String,Object> map = new HashMap<>();
            map.put("authId",authId);
            map.put("img",baseCode);
            return new R<>(S.OK, S.getMsg(S.OK), map);
        }catch(Exception e){
            e.printStackTrace();
            return new R<>(S.ERROR, S.getMsg(S.ERROR));
        }
    }


    @ApiOperation(value = "判断验证码是否正确", notes = "判断验证码是否正确")
    @ApiImplicitParams({
        @ApiImplicitParam(paramType = "query", name = "authId", value = "验证ID", required = true, dataType = "String"),
        @ApiImplicitParam(paramType = "query", name = "authCode", value = "验证码", required = true, dataType = "String") })
    @GetMapping(value = "/account/ifAuthCodeCorrect")
    public R<Object> ifAuthCodeCorrect(@RequestParam(value = "authId") String authId,@RequestParam("authCode") String authCode){
//        return new R<>(S.OK, S.getMsg(S.OK), true);
        boolean result = false;
        try {
            //得到redis中的验证码
            String storeAuthCode = redisTemplate.opsForValue().get(authId);
            //判断输入的验证码和redis中的文字是否一致
            if(authCode.equalsIgnoreCase(storeAuthCode)){
                result = true;
            }
            return new R<>(S.OK, S.getMsg(S.OK), result);
        }catch(Exception e){
            e.printStackTrace();
            return new R<>(S.ERROR, S.getMsg(S.ERROR));
        }
    }

}

