提交 873018f4 authored 作者: renjiancai's avatar renjiancai

--no commit message

上级 91749a32
package com.zrqx.sysuser.fg.client.third;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import com.zrqx.core.form.third.sms.SmsForm;
import com.zrqx.core.util.response.CallBack;
/**
* 手机短信
*/
@FeignClient(value="third",fallback=SmsHystric.class)
public interface SmsClient {
/**
* 发送手机短信
* @param smsForm
* @return
*/
@PostMapping("/sms/send")
public CallBack<String> sendSms(@RequestBody SmsForm smsForm);
}
package com.zrqx.sysuser.fg.client.third;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import com.zrqx.core.form.third.sms.SmsForm;
import com.zrqx.core.util.response.CallBack;
@Component
public class SmsHystric implements SmsClient {
private final Logger logger = LoggerFactory.getLogger(SmsHystric.class);
@Override
public CallBack<String> sendSms(@RequestBody SmsForm smsForm){
logger.info(" 发送: "+ smsForm.getPhone() + " 验证码: " + smsForm.getCode() + ",短信接口不可用");
return null;
}
}
package com.zrqx.sysuser.fg.controller.permissions;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
......@@ -18,27 +23,35 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.mysql.fabric.xmlrpc.base.Member;
import com.zrqx.core.constant.sysuser.SysUserRequestPath;
import com.zrqx.core.enums.StatusEnum;
import com.zrqx.core.enums.sysuser.UserTypeEnum;
import com.zrqx.core.exception.BaseException;
import com.zrqx.core.exception.BusinessValidateException;
import com.zrqx.core.form.sysuser.bg.user.UpdateNewPasswordForm;
import com.zrqx.core.form.sysuser.bg.user.UpdatePasswordForm;
import com.zrqx.core.form.sysuser.fg.user.LoginForm;
import com.zrqx.core.form.sysuser.fg.user.LoginUserInfo;
import com.zrqx.core.form.third.sms.SmsForm;
import com.zrqx.core.model.sysuser.role.Role;
import com.zrqx.core.model.sysuser.user.User;
import com.zrqx.core.util.JsonUtil.JsonUtil;
import com.zrqx.core.util.bean.BeanUtils;
import com.zrqx.core.util.datatype.ArrayUtils;
import com.zrqx.core.util.datatype.PasswordUtil;
import com.zrqx.core.util.datatype.UUIDUtil;
import com.zrqx.core.util.datatype.VerifyUtil;
import com.zrqx.core.util.response.CallBack;
import com.zrqx.sysuser.commons.redis.Redis;
import com.zrqx.sysuser.fg.client.third.SmsClient;
import com.zrqx.sysuser.fg.service.role.FgRoleService;
import com.zrqx.sysuser.fg.service.user.FgUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import sun.misc.BASE64Encoder;
import tk.mybatis.mapper.entity.Example;
/**
* 登录Controller
......@@ -56,6 +69,8 @@ public class FgPermissionsController {
@Autowired
StringRedisTemplate stringRedisTemplate;
@Autowired
private SmsClient smsClient;
@Autowired
private Redis redis;
......@@ -173,4 +188,102 @@ public class FgPermissionsController {
userService.updateByPrimaryKeySelective(m);
return CallBack.success("成功");
}
@ApiOperation(value = "重置密码-1 发送手机短信", notes = "0:成功;16:60s内不能再次发送;18:短信验证码发送失败;")
@GetMapping(value = "/password" + 1)
public CallBack<String> resetPassword1(String phone) throws Exception {
if (!userService.isExistByPhone(null, phone)) {
throw new BaseException(99, "手机号未注册");
}
String sendTime = phone + "password";
if (redis.get(sendTime) != null) {
throw new BaseException(16, "60s内不能再次发送");
}
SmsForm ssf = new SmsForm();
ssf.setPhone(phone);
String code = PasswordUtil.createPassword(6, 1);
ssf.setCode(code);
ssf.setType(2); /// 更换手机号验证验证码
CallBack<String> sms = smsClient.sendSms(ssf);
if (sms != null && sms.isStatus()) {
redis.set(phone, code, 60 * 5, TimeUnit.SECONDS);
redis.set(sendTime, "60", 60);
} else {
throw new BaseException(18, "短信验证码发送失败");
}
return CallBack.success("发送成功");
}
@ApiOperation(value = "重置密码-2 验证手机短信", notes = "0:成功;3:验证码不能为空;11:验证码已失效,请重新发送;20:验证码不能为空;21:手机号不能为空;")
@GetMapping(value = "/password" + 2)
public CallBack<String> resetPassword2(String phone, String code) throws Exception {
if (StringUtils.isBlank(phone)) {
throw new BaseException(21, "手机号不能为空。");
}
if (StringUtils.isBlank(code)) {
throw new BaseException(20, "验证码不能为空。");
}
String redisCode = redis.get(phone);
if (redisCode == null) {
throw new BaseException(11, "验证码已失效,请重新发送。");
}
if (!code.toLowerCase().equals(redisCode.toLowerCase())) {
throw new BaseException(3, "验证码有误!");
}
redis.set(phone, phone);
return CallBack.success("验证成功");
}
@ApiOperation(value = "重置密码-3 输入新密码", notes = "0:成功;2:验证码不能为空;3:两次密码不一致;21:手机号不能为空;")
@PostMapping(value = "/password" + 3)
public CallBack<String> resetPassword3(@RequestBody UpdateNewPasswordForm form) throws Exception {
if (StringUtils.isBlank(form.getPhone())) {
throw new BaseException(21, "手机号不能为空");
}
if (StringUtils.isBlank(form.getNewPassword1()) || StringUtils.isBlank(form.getNewPassword2())) {
throw new BaseException(2, "密码不能为空");
}
if (!form.getNewPassword1().equals(form.getNewPassword2())) {
throw new BaseException(3, "两次密码不一致");
}
String redisCode = redis.get(form.getPhone());
if (redisCode == null || !form.getPhone().equals(redisCode)) {
throw new BaseException(99, "请按照流程重置密码");
}
Example example = userService.createExample();
example.createCriteria().andEqualTo("phone", form.getPhone());
User user = new User();
user.setPassword(form.getNewPassword1());
userService.UpdateByExampleSelective(user, example);
redis.delete(form.getPhone());
return CallBack.success("success");
}
@ApiOperation(value = "图片验证码", notes = "验证码")
@GetMapping(value = "/code")
public Map<String, String> code() throws Exception {
// 利用图片工具生成图片
// 第一个参数是生成的验证码,第二个参数是生成的图片
Object[] objs = VerifyUtil.createImage();
// 将验证码存入Session
// session.setAttribute("imageCode",objs[0]);
String uuid = UUIDUtil.getUUID();
redis.set(uuid, objs[0].toString(), 60 * 10, TimeUnit.SECONDS);
// 将图片输出给浏览器
BufferedImage image = (BufferedImage) objs[1];
/*
* response.setContentType("image/png"); OutputStream os =
* response.getOutputStream(); ImageIO.write(image, "png", os);
*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
BASE64Encoder encoder = new BASE64Encoder();
byte[] bytes = baos.toByteArray();// 转换成字节
String png_base64 = encoder.encodeBuffer(bytes).trim();// 转换成base64串
png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");// 删除
// \r\n
Map<String, String> map = new HashMap<String, String>();
map.put("uuid", uuid);
map.put("img", "data:image/png;base64," + png_base64);
return map;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论