package com.kidgrow.oauth2.controller;
import com.kidgrow.common.constant.SecurityConstants;
import com.kidgrow.common.model.ResultBody;
import com.kidgrow.oauth2.service.IValidateCodeService;
import com.wf.captcha.GifCaptcha;
import com.wf.captcha.base.Captcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletResponse;
/**
* 石家庄喜高科技有限责任公司 版权所有 © Copyright 2020
*
* @Description: 验证码
* @Project:
* @CreateDate: Created in 2020/2/20 09:19
* @Author: liuke
*/
@Controller
public class ValidateCodeController {
@Autowired
private IValidateCodeService validateCodeService;
/**
* 创建验证码
*
* @throws Exception
*/
@GetMapping(SecurityConstants.DEFAULT_VALIDATE_CODE_URL_PREFIX + "/{deviceId}")
public void createCode(@PathVariable String deviceId, HttpServletResponse response) throws Exception {
Assert.notNull(deviceId, "机器码不能为空");
// 设置请求头为输出图片类型
CaptchaUtil.setHeader(response);
// 三个参数分别为宽、高、位数
GifCaptcha gifCaptcha = new GifCaptcha(100, 35, 4);
// 设置类型:字母数字混合
gifCaptcha.setCharType(Captcha.TYPE_DEFAULT);
// 保存验证码
validateCodeService.saveImageCode(deviceId, gifCaptcha.text().toLowerCase());
// 输出图片流
gifCaptcha.out(response.getOutputStream());
}
/**
* 发送手机验证码
* 后期要加接口限制
*
* @param mobile 手机号
* @return R
*/
@ResponseBody
@GetMapping(SecurityConstants.MOBILE_VALIDATE_CODE_URL_PREFIX + "/{mobile}")
public ResultBody createCode(@PathVariable String mobile) {
Assert.notNull(mobile, "手机号不能为空");
return validateCodeService.sendSmsCode(mobile);
}
}