Skip to content

Commit

Permalink
Merge pull request #325 from Jzow/master
Browse files Browse the repository at this point in the history
Add email login type
  • Loading branch information
Jzow authored May 7, 2024
2 parents 88f689c + 702e9ae commit 6c36417
Show file tree
Hide file tree
Showing 51 changed files with 10,684 additions and 10,923 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,21 @@ public Response<UserInfoVO> mobileLogin(@RequestBody MobileLoginDTO mobileLoginD
return userService.mobileLogin(mobileLoginDto);
}

@PostMapping(value = "emailLogin")
public Response<UserInfoVO> emailLogin(@RequestBody EmailLoginDTO emailLoginDTO) {
return userService.emailLogin(emailLoginDTO);
}

@PostMapping(value = "updatePassword")
public Response<String> updatePassword(@RequestBody UpdatePasswordDto updatePasswordDto) {
public Response<String> updatePasswordByPhone(@RequestBody UpdatePasswordDto updatePasswordDto) {
return userService.updatePassword(updatePasswordDto);
}

@PostMapping(value = "updatePasswordByEmail")
public Response<String> updatePasswordByEmail(@RequestBody UpdatePasswordByEmailDto updatePasswordByEmailDto) {
return userService.updatePasswordByEmail(updatePasswordByEmailDto);
}

@PutMapping(value = "userUpdatePassword")
public Response<String> userUpdatePassword(@RequestBody ResetPasswordDTO resetPasswordDTO) {
return userService.resetPassword(resetPasswordDTO);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,6 @@ public class AccountRegisterDTO {
String phoneNumber;

String sms;

String email;
}
13 changes: 13 additions & 0 deletions core/domain/src/main/java/com/wansenai/dto/user/EmailLoginDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.wansenai.dto.user;

import lombok.Data;

@Data
public class EmailLoginDTO {

private String email;

private Integer type;

private String emailCode;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.wansenai.dto.user;

import lombok.Data;

@Data
public class UpdatePasswordByEmailDto {

String password;

String email;

String emailCode;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
@Data
public class UpdatePasswordDto {

String username;

String password;

String phoneNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ public void addInterceptors(InterceptorRegistry registry) {
.excludePathPatterns("/user/login")
.excludePathPatterns("/user/mobileLogin")
.excludePathPatterns("/user/updatePassword")
.excludePathPatterns("/user/updatePasswordByEmail")
.excludePathPatterns("/user/emailLogin")
.excludePathPatterns("/v2/common/sms/{type}/{phoneNumber}")
.excludePathPatterns("/v2/common/email/{type}/{email}")
.excludePathPatterns("/v2/common/nextId/{type}")
.excludePathPatterns("/sys/config/getCompanyInfo");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,23 +243,44 @@ public Response<String> sendEmailCode(Integer type, String email) {
if (!StringUtils.hasLength(email)) {
return Response.responseMsg(BaseCodeEnum.PARAMETER_NULL);
}
String resultCode = "";
if (redisUtil.hasKey(SecurityConstants.EMAIL_RESET_VERIFY_CODE_CACHE_PREFIX + email)) {
resultCode = redisUtil.getString(email + ":forget_code");
} else {
resultCode = getForgetCode();
redisUtil.set(SecurityConstants.EMAIL_RESET_VERIFY_CODE_CACHE_PREFIX + email, resultCode);
redisUtil.expire(SecurityConstants.EMAIL_RESET_VERIFY_CODE_CACHE_PREFIX + email, 180);
}

try {
switch (type)
{
case 0:
EmailUtils.forgetPasswordEmailNotice(resultCode, email);
String resultCode = "";
if (redisUtil.hasKey(SecurityConstants.EMAIL_RESET_PASSWORD_LOGIN_VERIFY_CODE_CACHE_PREFIX + email)) {
resultCode = redisUtil.getString(SecurityConstants.EMAIL_RESET_PASSWORD_LOGIN_VERIFY_CODE_CACHE_PREFIX + email);
EmailUtils.forgetPasswordEmailNotice(resultCode, email);
} else {
resultCode = getForgetCode();
redisUtil.set(SecurityConstants.EMAIL_RESET_PASSWORD_LOGIN_VERIFY_CODE_CACHE_PREFIX + email, resultCode);
redisUtil.expire(SecurityConstants.EMAIL_RESET_PASSWORD_LOGIN_VERIFY_CODE_CACHE_PREFIX + email, 180);
EmailUtils.forgetPasswordEmailNotice(resultCode, email);
}
break;
case 1:
EmailUtils.resetEmailNotice(resultCode, email);
String resultCode2 = "";
if (redisUtil.hasKey(SecurityConstants.EMAIL_RESET_VERIFY_CODE_CACHE_PREFIX + email)) {
resultCode2 = redisUtil.getString(SecurityConstants.EMAIL_RESET_VERIFY_CODE_CACHE_PREFIX + email);
EmailUtils.resetEmailNotice(resultCode2, email);
} else {
resultCode2 = getForgetCode();
redisUtil.set(SecurityConstants.EMAIL_RESET_VERIFY_CODE_CACHE_PREFIX + email, resultCode2);
redisUtil.expire(SecurityConstants.EMAIL_RESET_VERIFY_CODE_CACHE_PREFIX + email, 180);
EmailUtils.resetEmailNotice(resultCode2, email);
}
break;
case 2:
String resultCode3 = "";
if (redisUtil.hasKey(SecurityConstants.EMAIL_LOGIN_VERIFY_CODE_CACHE_PREFIX + email)) {
resultCode3 = redisUtil.getString(SecurityConstants.EMAIL_LOGIN_VERIFY_CODE_CACHE_PREFIX + email);
EmailUtils.loginEmailNotice(resultCode3, email);
} else {
resultCode3 = getForgetCode();
redisUtil.set(SecurityConstants.EMAIL_LOGIN_VERIFY_CODE_CACHE_PREFIX + email, resultCode3);
redisUtil.expire(SecurityConstants.EMAIL_LOGIN_VERIFY_CODE_CACHE_PREFIX + email, 180);
EmailUtils.loginEmailNotice(resultCode3, email);
}
break;
default:
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ public interface ISysUserService extends IService<SysUser> {

Response<UserInfoVO> mobileLogin(MobileLoginDTO mobileLoginDto);

Response<UserInfoVO> emailLogin(EmailLoginDTO emailLoginDTO);

Response<String> updatePassword(UpdatePasswordDto updatePasswordDto);

Response<String> updatePasswordByEmail(UpdatePasswordByEmailDto updatePasswordByEmailDto);

Response<String> resetPassword(ResetPasswordDTO resetPasswordDto);

Response<UserInfoVO> userInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ public Response<String> accountRegister(AccountRegisterDTO accountRegisterDto) {
.id(userId)
.userName(accountRegisterDto.getUsername())
.name("测试租户")
.email(accountRegisterDto.getEmail())
.password(CommonTools.md5Encryp(password))
.phoneNumber(accountRegisterDto.getPhoneNumber())
.tenantId(userId)
Expand Down Expand Up @@ -348,6 +349,57 @@ public Response<UserInfoVO> mobileLogin(MobileLoginDTO mobileLoginDto) {
.build());
}

@Override
public Response<UserInfoVO> emailLogin(EmailLoginDTO emailLoginDTO) {
if (emailLoginDTO == null) {
return Response.responseMsg(BaseCodeEnum.PARAMETER_NULL);
}
var verifyCode = redisUtil.getString(SecurityConstants.EMAIL_LOGIN_VERIFY_CODE_CACHE_PREFIX + emailLoginDTO.getEmail());
if (ObjectUtils.isEmpty(verifyCode)) {
return Response.responseMsg(BaseCodeEnum.VERIFY_CODE_EXPIRE);
}
if (!verifyCode.equals(emailLoginDTO.getEmailCode())) {
return Response.responseMsg(BaseCodeEnum.VERIFY_CODE_ERROR);
}

var user = lambdaQuery()
.eq(SysUser::getEmail, emailLoginDTO.getEmail())
.one();

if (user == null) {
return Response.responseMsg(UserCodeEnum.USER_NOT_EXISTS);
}

if (user.getStatus() == UserConstants.USER_STATUS_DISABLE) {
return Response.responseMsg(UserCodeEnum.USER_ACCOUNT_FREEZE);
}

if (user.getDeleteFlag() == CommonConstants.DELETED) {
return Response.responseMsg(UserCodeEnum.USER_ACCOUNT_INVALID);
}

var token = "";
if (redisUtil.hasKey(user.getUserName() + ":token")) {
token = String.valueOf(redisUtil.get(user.getUserName() + ":token"));
} else {
// 生成JWT的令牌
token = jwtUtil.createToken(user.getUserName());
redisUtil.set(user.getUserName() + ":token", token);
redisUtil.expire(user.getUserName() + ":token", 86400);
var userId = String.valueOf(user.getId());
var tenantId = String.valueOf(user.getTenantId());
redisUtil.set(token + ":userName", user.getUserName(), 86400);
redisUtil.set(token + ":userId", userId, 86400);
redisUtil.set(token + ":tenantId", tenantId, 86400);
}

return Response.responseData(UserInfoVO.builder()
.id(user.getId())
.token(token)
.expire(1694757956L)
.build());
}

@Override
public Response<String> updatePassword(UpdatePasswordDto updatePasswordDto) {
if (updatePasswordDto == null) {
Expand All @@ -364,7 +416,6 @@ public Response<String> updatePassword(UpdatePasswordDto updatePasswordDto) {
}

var isExists = lambdaQuery()
.eq(SysUser::getUserName, updatePasswordDto.getUsername())
.eq(SysUser::getPhoneNumber, updatePasswordDto.getPhoneNumber())
.one();

Expand All @@ -373,7 +424,7 @@ public Response<String> updatePassword(UpdatePasswordDto updatePasswordDto) {
}

var result = lambdaUpdate()
.eq(SysUser::getUserName, updatePasswordDto.getUsername())
.eq(SysUser::getUserName, isExists.getUserName())
.eq(SysUser::getPhoneNumber, updatePasswordDto.getPhoneNumber())
.set(SysUser::getPassword, CommonTools.md5Encryp(updatePasswordDto.getPassword()))
.update();
Expand All @@ -385,6 +436,42 @@ public Response<String> updatePassword(UpdatePasswordDto updatePasswordDto) {
return Response.responseMsg(UserCodeEnum.UPDATE_PASSWORD_SUCCESS);
}

@Override
public Response<String> updatePasswordByEmail(UpdatePasswordByEmailDto updatePasswordByEmailDto) {
if (updatePasswordByEmailDto == null) {
return Response.responseMsg(BaseCodeEnum.PARAMETER_NULL);
}

var verifyCode = redisUtil.getString(SecurityConstants.EMAIL_RESET_PASSWORD_LOGIN_VERIFY_CODE_CACHE_PREFIX + updatePasswordByEmailDto.getEmail());
if (ObjectUtils.isEmpty(verifyCode)) {
return Response.responseMsg(BaseCodeEnum.VERIFY_CODE_EXPIRE);
}

if (!verifyCode.equals(updatePasswordByEmailDto.getEmailCode())) {
return Response.responseMsg(BaseCodeEnum.VERIFY_CODE_ERROR);
}

var isExists = lambdaQuery()
.eq(SysUser::getEmail, updatePasswordByEmailDto.getEmail())
.one();

if (isExists == null) {
return Response.responseMsg(UserCodeEnum.USER_NOT_EXISTS);
}

var result = lambdaUpdate()
.eq(SysUser::getUserName, isExists.getUserName())
.eq(SysUser::getEmail, updatePasswordByEmailDto.getEmail())
.set(SysUser::getPassword, CommonTools.md5Encryp(updatePasswordByEmailDto.getPassword()))
.update();

if (!result) {
return Response.responseMsg(UserCodeEnum.UPDATE_PASSWORD_ERROR);
}

return Response.responseMsg(UserCodeEnum.UPDATE_PASSWORD_SUCCESS);
}

@Override
public Response<String> resetPassword(ResetPasswordDTO resetPasswordDto) {
if(!StringUtils.hasLength(resetPasswordDto.getNewPassword()) || !StringUtils.hasLength(resetPasswordDto.getPassword())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface EmailConstant {
String EMAIL_USER_NAME = "[email protected]";

/** 邮箱授权码 */
String EMAIL_PASSWORD = "YSWXSMYYQSVDSBYL";
String EMAIL_PASSWORD = "ICGUDLCLYOEZOAJF";

/** 邮件HMAC256加密算法头字符串 */
String EMAIL_HMAC256_TOKEN_HEAD = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public interface SecurityConstants {

String EMAIL_RESET_VERIFY_CODE_CACHE_PREFIX = "AUTH:VERIFY_CODE:EMAIL_RESET:";

String EMAIL_LOGIN_VERIFY_CODE_CACHE_PREFIX = "AUTH:VERIFY_CODE:EMAIL_LOGIN:";

String EMAIL_RESET_PASSWORD_LOGIN_VERIFY_CODE_CACHE_PREFIX = "AUTH:VERIFY_CODE:EMAIL_RESET_PASSWORD:";

String LOGIN_SECURITY_KEY = "QsCdA/3d8CkxZ6k5c6eA61==";

String REGISTER_SECURITY_KEY = "7Fd2u4qF/3k0z6O1c9AeC7==";
Expand Down
16 changes: 16 additions & 0 deletions core/utils/src/main/java/com/wansenai/utils/email/EmailUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,22 @@ public static void resetEmailNotice(String code, String to) throws Exception {
emailUtils.send();
}

public static void loginEmailNotice(String code, String to) throws Exception {
String subject = "【万森ERP系统】: 邮箱登录验证码"; // 主题

String body = "<pre>\n" +
"您好,\n" +
"\n" +
" 您正在登录与此电子邮件地址 ("+ to + ")关联的 万森ERP系统帐户.\n\n" +
" 本次验证码是:<b>" + code + "</b>, 该验证码3分钟有效\n\n" +
" 如果您没有发起此请求,请忽略此电子邮件, \n" +
" 谢谢, \n\n\n" +
" 万森AI团队." +
"</pre>";
EmailUtils emailUtils = EmailUtils.entity(EmailConstant.EMAIL_HOST, EmailConstant.EMAIL_USER_NAME, EmailConstant.EMAIL_PASSWORD, to, null, subject, body, null);
emailUtils.send();
}

/**
* 项目合同通知邮件模板
*
Expand Down
Binary file added images/login-page-zh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 7 additions & 4 deletions web/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
FROM node:18.18.0 as build-stage
MAINTAINER WanSen AI<[email protected]>
WORKDIR app
WORKDIR /app

COPY . ./

# 设置 node 阿里镜像
RUN npm config set registry https://registry.npmmirror.com

ENV NODE_OPTIONS --max-old-space-size=16384

RUN npm install pnpm -g
RUN pnpm install --frozen-lockfile
RUN pnpm build:docker
RUN npm install pnpm -g && \
pnpm install --frozen-lockfile && \
pnpm build:docker

RUN echo "Build Success"

Expand Down
2 changes: 1 addition & 1 deletion web/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM nginx:1.23.4-alpine
FROM nginx:stable-alpine3.17-slim

COPY dist/ /usr/share/nginx/html/
COPY ../deploy/default.conf /etc/nginx/conf.d/
Expand Down
23 changes: 11 additions & 12 deletions web/internal/stylelint-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,18 @@
"stub": "pnpm unbuild --stub"
},
"devDependencies": {
"postcss": "^8.4.29",
"postcss-html": "^1.5.0",
"postcss": "^8.4.38",
"postcss-html": "^1.6.0",
"postcss-less": "^6.0.0",
"postcss-scss": "^4.0.7",
"prettier": "^2.8.8",
"stylelint": "^15.10.3",
"stylelint-config-property-sort-order-smacss": "^9.1.0",
"stylelint-config-recommended": "^12.0.0",
"stylelint-config-recommended-scss": "^11.0.0",
"postcss-scss": "^4.0.9",
"prettier": "^3.2.5",
"stylelint": "^16.4.0",
"stylelint-config-property-sort-order-smacss": "^10.0.0",
"stylelint-config-recommended-scss": "^14.0.0",
"stylelint-config-recommended-vue": "^1.5.0",
"stylelint-config-standard": "^33.0.0",
"stylelint-config-standard-scss": "^9.0.0",
"stylelint-order": "^6.0.3",
"stylelint-prettier": "^3.0.0"
"stylelint-config-standard": "^36.0.0",
"stylelint-config-standard-scss": "^13.1.0",
"stylelint-order": "^6.0.4",
"stylelint-prettier": "^5.0.0"
}
}
5 changes: 2 additions & 3 deletions web/internal/ts-config/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
"node-server.json"
],
"dependencies": {
"@types/node": "^18.17.12",
"unplugin-vue-define-options": "^1.3.17",
"vite": "^4.4.9"
"@types/node": "^20.12.7",
"vite": "^5.2.10"
}
}
Loading

0 comments on commit 6c36417

Please sign in to comment.