|
|
@@ -22,6 +22,8 @@ import com.YuyeTech.TPlat.enums.WxInfoEnum;
|
|
|
import com.YuyeTech.TPlat.exception.UserException;
|
|
|
import com.YuyeTech.TPlat.exception.WxInfoException;
|
|
|
|
|
|
+import freemarker.template.Template;
|
|
|
+import freemarker.template.TemplateException;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import me.chanjar.weixin.common.error.WxErrorException;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
@@ -32,13 +34,19 @@ import org.springframework.data.redis.core.StringRedisTemplate;
|
|
|
import org.springframework.mail.MailException;
|
|
|
import org.springframework.mail.SimpleMailMessage;
|
|
|
import org.springframework.mail.javamail.JavaMailSender;
|
|
|
+import org.springframework.mail.javamail.MimeMessageHelper;
|
|
|
+import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
+import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
|
|
|
|
|
|
+import javax.mail.MessagingException;
|
|
|
+import javax.mail.internet.MimeMessage;
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
import javax.validation.Valid;
|
|
|
+import java.io.IOException;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@@ -83,6 +91,10 @@ public class LoginController {
|
|
|
|
|
|
@Autowired
|
|
|
private JavaMailSender mailSender;
|
|
|
+ @Autowired
|
|
|
+ private FreeMarkerConfigurer freeMarkerConfigurer;
|
|
|
+
|
|
|
+
|
|
|
|
|
|
|
|
|
@GetMapping("/getSessionToken")
|
|
|
@@ -345,47 +357,65 @@ public class LoginController {
|
|
|
* 2021-09-18
|
|
|
* 在手机号或者邮件中获取验证码。通用
|
|
|
*
|
|
|
- * @param count
|
|
|
+ * @param account
|
|
|
* @param type 0 默认手机,1默认邮件
|
|
|
* @return
|
|
|
*/
|
|
|
@GetMapping("/getCodeAccordingType")
|
|
|
- public ResultVO getCodeAccordingType(@RequestParam(value = "count") String count,
|
|
|
+ public ResultVO getCodeAccordingType(@RequestParam(value = "account") String account,
|
|
|
@RequestParam(value = "type") Integer type) {
|
|
|
Integer expire = RedisConstant.CODE_EXPIRE;//过期时间
|
|
|
String code = String.valueOf((int) ((Math.random() * 9 + 1) * 1000));
|
|
|
- if (count != null) {
|
|
|
+ if (account != null) {
|
|
|
|
|
|
if(type.equals(0)){
|
|
|
//发送验证码给 手机
|
|
|
- aliyunSMSUtil.sendSms(count, code);
|
|
|
+ aliyunSMSUtil.sendSms(account, code);
|
|
|
}else if(type.equals(1)){
|
|
|
//发送验证码给 邮件
|
|
|
//创建简单邮件消息
|
|
|
- SimpleMailMessage message = new SimpleMailMessage();
|
|
|
- //谁发的
|
|
|
- message.setFrom(mailUsername);
|
|
|
- //谁要接收
|
|
|
- message.setTo(count);
|
|
|
- //邮件标题
|
|
|
- message.setSubject("哔蹦平台登录验证码");
|
|
|
+// SimpleMailMessage message = new SimpleMailMessage();
|
|
|
+
|
|
|
//邮件内容
|
|
|
- message.setText("哔蹦平台登录的验证码:"+code+",请勿泄露给其他人!");
|
|
|
+// message.setText("哔蹦平台登录的验证码:"+code+",请勿泄露给其他人!");
|
|
|
+ //freemarker
|
|
|
try {
|
|
|
+ MimeMessage message = mailSender.createMimeMessage();
|
|
|
+ MimeMessageHelper helper = new MimeMessageHelper(message);
|
|
|
+ //谁发的
|
|
|
+ helper.setFrom(mailUsername);
|
|
|
+ //谁要接收
|
|
|
+ helper.setTo(account);
|
|
|
+ //邮件标题
|
|
|
+ helper.setSubject("哔蹦平台验证码");
|
|
|
+ //freemarker
|
|
|
+ Template template = freeMarkerConfigurer.getConfiguration().getTemplate("/mail/sendSMS.ftl");
|
|
|
+ Map<String, String> emailMap = new HashMap<>();
|
|
|
+ emailMap.put("emailCode",code);
|
|
|
+ String s = FreeMarkerTemplateUtils.processTemplateIntoString(template, emailMap);
|
|
|
+ helper.setText(s,true);
|
|
|
mailSender.send(message);
|
|
|
- Map map = new HashMap();
|
|
|
- map.put("receiver",count);
|
|
|
- map.put("msg","发送普通邮件成功");
|
|
|
- return ResultVOUtil.success(map);
|
|
|
+
|
|
|
} catch (MailException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ return ResultVOUtil.error(200, "发送普通邮件方失败!");
|
|
|
+ }catch (MessagingException e) {
|
|
|
+ return ResultVOUtil.error(200, "发送普通邮件方失败!");
|
|
|
+ } catch (IOException e) {
|
|
|
+ return ResultVOUtil.error(200, "发送普通邮件方失败!");
|
|
|
+ } catch (TemplateException e) {
|
|
|
return ResultVOUtil.error(200, "发送普通邮件方失败!");
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
//成功后保存code到redis
|
|
|
- redisTemplate.opsForValue().set(String.format(RedisConstant.SMS_PREFIX, count), code, expire, TimeUnit.SECONDS);
|
|
|
+ redisTemplate.opsForValue().set(String.format(RedisConstant.SMS_PREFIX, account), code, expire, TimeUnit.SECONDS);
|
|
|
log.info("getCode:" + code);
|
|
|
- return ResultVOUtil.success();
|
|
|
+ Map map = new HashMap();
|
|
|
+ map.put("receiver",account);
|
|
|
+ map.put("tip","发送验证码成功");
|
|
|
+ map.put("type",type);
|
|
|
+ return ResultVOUtil.success(map);
|
|
|
} else {
|
|
|
return ResultVOUtil.error(200, "获取code失败");
|
|
|
}
|
|
|
@@ -393,12 +423,13 @@ public class LoginController {
|
|
|
|
|
|
/**
|
|
|
* 验证码绑定号码
|
|
|
- *
|
|
|
+ * 2.4.12 版本后弃用
|
|
|
* @param phoneNumber
|
|
|
* @param code
|
|
|
* @return
|
|
|
*/
|
|
|
@GetMapping("/SMS_bind_phone")
|
|
|
+ @Deprecated
|
|
|
public ResultVO userBindPhoneNumber(@RequestParam("phoneNumber") String phoneNumber,
|
|
|
@RequestParam("code") String code,
|
|
|
@RequestParam("userId") String userId) {
|
|
|
@@ -440,6 +471,67 @@ public class LoginController {
|
|
|
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 验证码绑定号码或者邮箱
|
|
|
+ *
|
|
|
+ * @param account
|
|
|
+ * @param code
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/SMS_bind_account")
|
|
|
+ public ResultVO userBindAccount(@RequestParam("account") String account,
|
|
|
+ @RequestParam("code") String code,
|
|
|
+ @RequestParam("type") Integer type,
|
|
|
+ @RequestParam("userId") String userId) {
|
|
|
+
|
|
|
+ String codeValue = redisTemplate.opsForValue().get(String.format(RedisConstant.SMS_PREFIX, account));
|
|
|
+ if (!StringUtils.equals(codeValue, code)) {
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_CODE_ERROR.getCode(), UserEnum.USER_CODE_ERROR.getMessage());
|
|
|
+ }
|
|
|
+ log.info("codeValue:" + codeValue);
|
|
|
+
|
|
|
+ try {
|
|
|
+ MainInfo mainInfo = null;
|
|
|
+ if(type.equals(0)){
|
|
|
+ mainInfo = mainInfoService.findMainInfoByPhoneNumber(account);
|
|
|
+ if (mainInfo != null) {
|
|
|
+ //如果存在,此手机号提示已注册
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_HAS_REGISTER_PHONE.getCode(), UserEnum.USER_HAS_REGISTER_PHONE.getMessage());
|
|
|
+ }
|
|
|
+ }else if(type.equals(1)){
|
|
|
+ mainInfo = mainInfoService.findMainInfoByEmail(account);
|
|
|
+ if (mainInfo != null) {
|
|
|
+ //如果存在,此手机号提示已注册
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_HAS_REGISTER_MAILBOX.getCode(), UserEnum.USER_HAS_REGISTER_MAILBOX.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ mainInfo = mainInfoService.findMainInfoById(userId);
|
|
|
+ //TODO 验证码登录流程
|
|
|
+ //如果不存在用户
|
|
|
+ if (mainInfo == null) {
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_REGISTER_ERROR.getCode(), UserEnum.USER_REGISTER_ERROR.getMessage());
|
|
|
+ }
|
|
|
+ if(type.equals(0)){
|
|
|
+ mainInfo.setTelephoneNumber(account);
|
|
|
+ }else if(type.equals(1)){
|
|
|
+ mainInfo.setEmail(account);
|
|
|
+ }
|
|
|
+ mainInfoService.addMainInfo(mainInfo);
|
|
|
+ //TODO 写入成功后,删除redis里面保存的code
|
|
|
+ Boolean bSuccess = redisTemplate.delete(String.format(RedisConstant.SMS_PREFIX, account));
|
|
|
+ if (!bSuccess) {
|
|
|
+ //删除不成功,redis不存在
|
|
|
+ log.info("redis 没有对应的token");
|
|
|
+ }
|
|
|
+ return ResultVOUtil.success();
|
|
|
+
|
|
|
+ } catch (UserException e) {
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_REGISTER_ERROR.getCode(), UserEnum.USER_REGISTER_ERROR.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
@GetMapping("/delete_phone")
|
|
|
public ResultVO userDeletePhoneNumber(@RequestParam("userId") String userId) {
|
|
|
@@ -550,12 +642,13 @@ public class LoginController {
|
|
|
|
|
|
/**
|
|
|
* 密码登录
|
|
|
- *
|
|
|
+ * 2.4.10 版本后弃用
|
|
|
* @param phoneNumber
|
|
|
* @param password
|
|
|
* @return
|
|
|
*/
|
|
|
@GetMapping("/password_login")
|
|
|
+ @Deprecated
|
|
|
public ResultVO userPasswordLogin(String phoneNumber, String password) {
|
|
|
try {
|
|
|
MainInfo mainInfo = mainInfoService.findMainInfoByPhoneNumberAndPassword(phoneNumber, password);
|
|
|
@@ -592,6 +685,50 @@ public class LoginController {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 密码登录
|
|
|
+ *
|
|
|
+ * @param account
|
|
|
+ * @param password
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/password_login_type")
|
|
|
+ public ResultVO userPasswordLoginOnTheType(String account, String password,Integer type) {
|
|
|
+ try {
|
|
|
+ MainInfo mainInfo = null;
|
|
|
+ if(type.equals(0)){
|
|
|
+ mainInfo = mainInfoService.findMainInfoByPhoneNumberAndPassword(account, password);
|
|
|
+ }else if(type.equals(1)){
|
|
|
+ mainInfo = mainInfoService.findMainInfoByEmailAndPassword(account, password);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (mainInfo == null) {
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_PASSWORD_ERROR.getCode(), UserEnum.USER_PASSWORD_ERROR.getMessage());
|
|
|
+ }
|
|
|
+ //token
|
|
|
+ String token = UUID.randomUUID().toString();
|
|
|
+ Integer expire = RedisConstant.EXPIRE;//过期时间
|
|
|
+ Map<String, String> tokenMap = new HashMap<>();
|
|
|
+
|
|
|
+ //TODO 登录成功,返回对应的字段
|
|
|
+ tokenMap.put("userId", mainInfo.getUserId());
|
|
|
+ redisTemplate.opsForHash().putAll(String.format(RedisConstant.TOKEN_PREFIX, token), tokenMap);
|
|
|
+ redisTemplate.expire(String.format(RedisConstant.TOKEN_PREFIX, token), expire, TimeUnit.SECONDS);
|
|
|
+
|
|
|
+ //返回一个token
|
|
|
+ LoginVO loginVO = new LoginVO();
|
|
|
+ loginVO.setToken(token);
|
|
|
+ loginVO.setNewUser(bUserInfoByUserId(mainInfo.getUserId()));
|
|
|
+ return ResultVOUtil.success(loginVO);
|
|
|
+ } catch (UserException e) {
|
|
|
+
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_REGISTER_ERROR.getCode(), UserEnum.USER_REGISTER_ERROR.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -776,4 +913,89 @@ public class LoginController {
|
|
|
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据手机号或者邮箱获取验证码
|
|
|
+ * 用验证码修改密码
|
|
|
+ *
|
|
|
+ * @param account
|
|
|
+ * @param code
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping("/SMS_modify_password")
|
|
|
+ public ResultVO userModifyPassword(@RequestParam("account") String account,
|
|
|
+ @RequestParam("code") String code,
|
|
|
+ @RequestParam("type") Integer type,
|
|
|
+ @RequestParam("password") String password,
|
|
|
+ @RequestParam("userId") String userId) {
|
|
|
+
|
|
|
+ String codeValue = redisTemplate.opsForValue().get(String.format(RedisConstant.SMS_PREFIX, account));
|
|
|
+ if (!StringUtils.equals(codeValue, code)) {
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_CODE_ERROR.getCode(), UserEnum.USER_CODE_ERROR.getMessage());
|
|
|
+ }
|
|
|
+ log.info("userModifyPassword codeValue:" + codeValue);
|
|
|
+ try {
|
|
|
+ MainInfo mainInfo = null;
|
|
|
+ if(type.equals(0)){
|
|
|
+ mainInfo = mainInfoService.findMainInfoByPhoneNumber(account);
|
|
|
+ }else if(type.equals(1)){
|
|
|
+ mainInfo = mainInfoService.findMainInfoByEmail(account);
|
|
|
+ }
|
|
|
+ //如果不存在用户
|
|
|
+ if (mainInfo == null) {
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_REGISTER_ERROR.getCode(), UserEnum.USER_REGISTER_ERROR.getMessage());
|
|
|
+ }
|
|
|
+ //设置密码
|
|
|
+ mainInfo.setPassword(password);
|
|
|
+ mainInfoService.addMainInfo(mainInfo);
|
|
|
+ //TODO 写入成功后,删除redis里面保存的code
|
|
|
+ Boolean bSuccess = redisTemplate.delete(String.format(RedisConstant.SMS_PREFIX, account));
|
|
|
+ if (!bSuccess) {
|
|
|
+ //删除不成功,redis不存在
|
|
|
+ log.info("redis 没有对应的token");
|
|
|
+ }
|
|
|
+ return ResultVOUtil.success();
|
|
|
+
|
|
|
+ } catch (UserException e) {
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_REGISTER_ERROR.getCode(), UserEnum.USER_REGISTER_ERROR.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据手机号或者邮箱,加旧密码,修改成新密码
|
|
|
+ *
|
|
|
+ */
|
|
|
+ @GetMapping("/modify_password_from_account")
|
|
|
+ public ResultVO userModifyPasswordFromAccount(@RequestParam("account") String account,
|
|
|
+ @RequestParam("type") Integer type,
|
|
|
+ @RequestParam("oldPassword") String oldPassword,
|
|
|
+ @RequestParam("newPassword") String newPassword,
|
|
|
+ @RequestParam("userId") String userId) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ MainInfo mainInfo = null;
|
|
|
+ if(type.equals(0)){
|
|
|
+ mainInfo = mainInfoService.findMainInfoByPhoneNumber(account);
|
|
|
+ }else if(type.equals(1)){
|
|
|
+ mainInfo = mainInfoService.findMainInfoByEmail(account);
|
|
|
+ }
|
|
|
+ //如果不存在用户
|
|
|
+ if (mainInfo == null) {
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_MODIFY_ACCOUNT_ERROR.getCode(), UserEnum.USER_MODIFY_ACCOUNT_ERROR.getMessage());
|
|
|
+ }
|
|
|
+ if(!mainInfo.getPassword().equals(oldPassword))
|
|
|
+ {
|
|
|
+ //如果旧密码不对,提示
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_OLD_PASSWORD_ERROR.getCode(), UserEnum.USER_OLD_PASSWORD_ERROR.getMessage());
|
|
|
+ }
|
|
|
+ //设置密码
|
|
|
+ mainInfo.setPassword(newPassword);
|
|
|
+ mainInfoService.addMainInfo(mainInfo);
|
|
|
+ return ResultVOUtil.success();
|
|
|
+ } catch (UserException e) {
|
|
|
+ return ResultVOUtil.error(UserEnum.USER_MODIFY_ERROR.getCode(), UserEnum.USER_MODIFY_ERROR.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
}
|