package dynamic.sys; import com.alibaba.fastjson.JSONObject; import dynamic.model.Sm_Md213; import dynamic.model.Sm_Md214; import microbee.http.annotation.Controller; import microbee.http.annotation.ExcludeL; import microbee.http.annotation.ModelResources; import microbee.http.apps.dbnet.ConditionPJ; import microbee.http.apps.dynamic.HoContext; import microbee.http.utills.GlobalData; import microbee.http.utills.SlideCircus; import microbee.http.utills.SlideCircusUtil; import org.apache.commons.lang.StringUtils; import javax.imageio.ImageIO; import java.io.File; import java.io.FileInputStream; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; @Controller public class SlideCircusController { @ModelResources Sm_Md214 sm_md214; @ModelResources Sm_Md213 sm_md213; //获取文件存储地址 String rootPath = GlobalData.server_conf_dom4j.getUploaddir(); /** * 生成滑块拼图验证码 * * @param hoContext - 本地照片 */ @ExcludeL public String ResImage(HoContext hoContext) throws Exception { //随机查询出一个图片 List> list = sm_md214.randName(new HashMap<>()); String name = "/uploads/20241204/114337722.png"; if (list.size() > 0) { name = StringUtils.isEmpty(list.get(0).get("name").toString()) ? name : list.get(0).get("name").toString(); } File file = new File(rootPath + name); //根据图片文件生成滑块数据 SlideCircus slideCircus = SlideCircusUtil.createImage(new FileInputStream(file)); if (slideCircus == null) { System.out.println("图片验证码生成失败"); return JSONObject.toJSONString(slideCircus); } File file1 = new File(rootPath + "/uploads/png/demo2BigImage.png"); File file2 = new File(rootPath + "/uploads/png/demo2SmallImage.png"); ImageIO.write(slideCircus.getBigImage(), "png", file1); ImageIO.write(slideCircus.getSmallImage(), "png", file2); //保存到session中 hoContext.httpRequest.setSession("posX", slideCircus.getPosX()); slideCircus.setBigImage(null); slideCircus.setSmallImage(null); slideCircus.setPosX(0); return JSONObject.toJSONString(slideCircus); } /** * 校验滑块拼图移动距离 * * @param hoContext */ @ExcludeL public String verify(HoContext hoContext) { Map params = hoContext.httpRequest.getParams(); String movePosX = params.get("movePosX"); Map resultMap = new HashMap<>(); try { if (movePosX == null) { resultMap.put("errcode", 1); resultMap.put("errmsg", "未提交参数!"); return JSONObject.toJSONString(resultMap); } if (Objects.isNull(hoContext.httpRequest.getSession("posX"))) { resultMap.put("errcode", 1); resultMap.put("errmsg", "验证码不同域请联系管理员"); return JSONObject.toJSONString(resultMap); } Integer posX = Integer.parseInt(hoContext.httpRequest.getSession("posX").toString()); if (posX == null) { resultMap.put("errcode", 1); resultMap.put("errmsg", "验证过期,请重试!"); return JSONObject.toJSONString(resultMap); } if (Math.abs(posX - Integer.parseInt(movePosX)) > 5) { resultMap.put("errcode", 1); resultMap.put("errmsg", "验证不通过!"); } else { resultMap.put("errcode", 0); resultMap.put("errmsg", "验证通过!"); } } catch (Exception e) { throw new RuntimeException(e.getMessage()); } finally { //设置session为空 hoContext.httpRequest.setSession("posX", ""); } return JSONObject.toJSONString(resultMap); } /** * 验证手机号码是否存在 * * @param hoContext */ @ExcludeL public String verifyPhone(HoContext hoContext) { String res = "false"; Map params = hoContext.httpRequest.getParams(); String phone = params.get("phone"); Object session = hoContext.httpRequest.getSession(phone); if (StringUtils.isEmpty(phone)) { return res; } List conditionPJS = ConditionPJ.inits(1, "phone", "mcb_eq", phone); Map map = sm_md213.gainOne(conditionPJS); if (map != null && Objects.nonNull(map.get("phone")) && StringUtils.isNotEmpty(map.get("phone").toString())) { if (Objects.nonNull(map.get("code")) && map.get("code").equals(session)) { res = "true"; } } return res; } /** * 验证手机号码是否存在 * * @param hoContext */ @ExcludeL public String verifyPhoneCode(HoContext hoContext) { String res = "2";//校验不通过、包含电话号为空验证码为空,验证码不通过 Map params = hoContext.httpRequest.getParams(); String phone = params.get("phone"); String code = params.get("code"); Object session = hoContext.httpRequest.getSession("phoneCode"); if (StringUtils.isEmpty(phone)) { return res; } if (StringUtils.isEmpty(code)) { return res; } List conditionPJS = ConditionPJ.inits(1, "phone", "mcb_eq", phone); Map map = sm_md213.gainOne(conditionPJS); if (map == null || Objects.isNull(map.get("phone"))) { res ="3";//不存在此手机号 return res; } if (Objects.nonNull(session) && code.equals(session.toString())) { res = "1";//通过 Map hashMap = new HashMap<>(); hashMap.put("code",session); Boolean id = sm_md213.updateByIdbo(hashMap, map.get("id")); if(id){ hoContext.httpRequest.setSession(phone,code); return res;//更新成功 } } return res; } }