|
@@ -0,0 +1,84 @@
|
|
|
+package edu.travel.web;
|
|
|
+
|
|
|
+import cn.hutool.captcha.CaptchaUtil;
|
|
|
+import cn.hutool.captcha.CircleCaptcha;
|
|
|
+import edu.travel.dto.CheckImageDto;
|
|
|
+import edu.travel.resp.BaseResponse;
|
|
|
+import edu.travel.resp.PageResponse;
|
|
|
+import edu.travel.utils.SMSUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+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.RequestMapping;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+import javax.servlet.http.HttpSession;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.Random;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * IndexController 类。
|
|
|
+ * <p>
|
|
|
+ * 描述:登录页相关接口
|
|
|
+ *
|
|
|
+ * @author huangwenwen
|
|
|
+ * @date 2025/2/10
|
|
|
+ */
|
|
|
+
|
|
|
+@RestController
|
|
|
+@RequestMapping("/oauth")
|
|
|
+public class IndexController {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private SMSUtils smsUtils;
|
|
|
+ @Autowired
|
|
|
+ private RedisTemplate redisTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取图形验证码
|
|
|
+ * @param session
|
|
|
+ * @param response
|
|
|
+ * @throws IOException
|
|
|
+ */
|
|
|
+ @GetMapping("/createImageCode")
|
|
|
+ public void getCode(HttpSession session, HttpServletResponse response,String phoneNumber) throws IOException {
|
|
|
+
|
|
|
+ CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 100, 4, 5);
|
|
|
+
|
|
|
+ String code = captcha.getCode();
|
|
|
+
|
|
|
+ redisTemplate.opsForValue().set(phoneNumber+"_image_code", code, 5, TimeUnit.MINUTES);
|
|
|
+
|
|
|
+ session.setAttribute("code",code);
|
|
|
+
|
|
|
+ captcha.write(response.getOutputStream());
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 校验图形码,发送短信
|
|
|
+ */
|
|
|
+
|
|
|
+ @GetMapping("/verifyAndSendSms")
|
|
|
+ public BaseResponse verifyAndSendSms(CheckImageDto checkImageDto){
|
|
|
+ if (StringUtils.isBlank(checkImageDto.getPhoneNumber())|| StringUtils.isBlank(checkImageDto.getCode())){
|
|
|
+ return PageResponse.out(500, "手机号或验证码为空");
|
|
|
+ }
|
|
|
+ // 校验图形验证码
|
|
|
+ String imageCode = (String) redisTemplate.opsForValue().get(checkImageDto.getPhoneNumber() + "_image_code");
|
|
|
+ if (StringUtils.isBlank(imageCode)) return PageResponse.out(500, "验证码已失效");
|
|
|
+ if (!checkImageDto.getCode().equals(imageCode)) return PageResponse.out(500, "验证码错误");
|
|
|
+ //发送短信
|
|
|
+ Object o = redisTemplate.opsForValue().get(checkImageDto.getPhoneNumber() + "_user_sms");
|
|
|
+ if (o!=null) return PageResponse.out(500, "验证码已发送,请一分钟后重试");
|
|
|
+ //短信码
|
|
|
+ String smsCode = String.valueOf(new Random().nextInt(9000) + 1000);
|
|
|
+ redisTemplate.opsForValue().set(checkImageDto.getPhoneNumber() + "_user_sms", smsCode, 1, TimeUnit.MINUTES);
|
|
|
+ smsUtils.sendMsg(checkImageDto.getCountryCode(), checkImageDto.getPhoneNumber(), smsCode);
|
|
|
+ return PageResponse.out(200, "发送成功");
|
|
|
+ }
|
|
|
+
|
|
|
+}
|