goods_detail.html
function getMiaoshaPath(){}
MiaoshaController
@RequestMapping(value="/generate_path", method=RequestMethod.GET)
@ResponseBody
public Result
@RequestParam("goodsId")long goodsId,
@RequestParam("verifyCode")int verifyCode) {
if(user == null){
return Result.error(CodeMsg.SESSION_ERROR);
}
//这里判断验证码是否输入正确,如果不正确,则不产生秒杀接口的地址
boolean pass = miaoshaService.passVerifyCode(user,goodsId,verifyCode);
if(!pass){
return Result.error(CodeMsg.VERIFY_CODE_ERROR);
}
MiaoshaService
@RequestMapping(value="/verifyCode", method=RequestMethod.GET)
@ResponseBody
public Result
if(user == null){
return Result.error(CodeMsg.SESSION_ERROR);
}
BufferedImage image = miaoshaService.
try{
OutputStream out = response.getOutputStream();//输出流就相当于返回的数据
ImageIO.write(image, "JPEG", out);
out.flush();
out.close();
return null;
}catch(Exception e){
e.printStackTrace();
return Result.error(CodeMsg.MIAOSHA_FAIL);
}
}
public BufferedImage createVerifyCode(MiaoshaUser user, long goodsId) {
if(user == null || goodsId < 0){
return null;
}
int width = 80;
int height = 32;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);//创建内存缓存区
Graphics g = image.getGraphics();//创建画笔
//set the background color
g.setColor(new Color(0xDCDCDC));
g.fillRect(0, 0, width, height);
//draw the border
g.setColor(Color.black);
g.drawRect(0, 0, width - 1, height - 1);
//create a random instance to generate the codes
Random rdm = new Random();
//make some confusion
for(int i = 0; i < 60; i++){//画一些干扰的小点点
int x = rdm.nextInt(width);
int y = rdm.nextInt(height);
g.drawOval(x, y, 0, 0);
}
//generate a random code
String verifyCode = generateVerifyCode(rdm);//表达式
g.setColor(new Color(0, 100, 0));
g.setFont(new Font("Candara", Font.BOLD, 24));
g.drawString(verifyCode, 8, 24);
g.dispose();
//把验证码(计算结果)存到redis中
int md = calc(verifyCode);//对表达式进行计算
redisService.set(MiaoshaKey.getVerifyCode, "" + user.getId() + "_" + goodsId, md);//计算结果存入redis
return image;
}
@SuppressWarnings("restriction")
private Integer calc(String exp) {//返回计算结果
try{
ScriptEngineManager manager = new javax.script.ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
Object obj = engine.eval(exp);
String str = obj.toString();
Double dou = Double.parseDouble(str.toString());
int in = dou.intValue();
return in;
}catch(Exception e){
e.printStackTrace();
}
return 0;
}
private static char[] ops = new char[]{'+', '-', '*'};
private String generateVerifyCode(Random rdm) {//产生算术表达式
int num1 = rdm.nextInt(10);
int num2 = rdm.nextInt(10);
int num3 = rdm.nextInt(10);
char op1 = ops[rdm.nextInt(3)];
char op2 = ops[rdm.nextInt(3)];
String exp = "" + num1 + op1 + num2 + op2 + num3;
return exp;
}
public boolean passVerifyCode(MiaoshaUser user, long goodsId, int verifyCode) {//表单输入的验证
Integer vc = redisService.get(MiaoshaKey.getVerifyCode,"" + user.getId() + "_" + goodsId, Integer.class);
if(vc!=null && (vc - verifyCode == 0)){
return true;
}
//这里删除redis中的结果
redisService.delete(MiaoshaKey.getVerifyCode,"" + user.getId() + "_" + goodsId);
return false;
}