feat(hjc): 网站企业账号+密码 注册/登录(JWT签发)
- POST /api/hjc/auth/register:创建 common.system.User(用户名=企业名称, BCrypt密码)+ 角色 + hjc_enterprise(待审核),签发 token(LoginResult) - POST /api/hjc/auth/login:校验企业名称+密码,签发 token - 复用 JwtUtil/JwtSubject/userService 与 wx-login 同一套签发;SecurityConfig 放行 /api/hjc/auth/register|login
This commit is contained in:
@@ -80,7 +80,9 @@ public class SecurityConfig {
|
||||
"/api/shop/shop-order/test",
|
||||
"/api/qr-code/**",
|
||||
"/api/shop/order-delivery/notify",
|
||||
"/api/hjc/push/project"
|
||||
"/api/hjc/push/project",
|
||||
"/api/hjc/auth/register",
|
||||
"/api/hjc/auth/login"
|
||||
)
|
||||
.permitAll()
|
||||
.anyRequest()
|
||||
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.gxwebsoft.hjc.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.gxwebsoft.common.core.config.ConfigProperties;
|
||||
import com.gxwebsoft.common.core.security.JwtSubject;
|
||||
import com.gxwebsoft.common.core.security.JwtUtil;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.BaseController;
|
||||
import com.gxwebsoft.common.system.entity.Role;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.entity.UserRole;
|
||||
import com.gxwebsoft.common.system.result.LoginResult;
|
||||
import com.gxwebsoft.common.system.service.RoleService;
|
||||
import com.gxwebsoft.common.system.service.UserRoleService;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import com.gxwebsoft.hjc.dto.HjcAuthRequest;
|
||||
import com.gxwebsoft.hjc.entity.HjcEnterprise;
|
||||
import com.gxwebsoft.hjc.service.HjcEnterpriseService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
/**
|
||||
* 汇吉采 网站企业 注册/登录(企业名称+密码)
|
||||
*/
|
||||
@Tag(name = "汇吉采-企业登录")
|
||||
@RestController
|
||||
@RequestMapping("/api/hjc/auth")
|
||||
public class HjcAuthController extends BaseController {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
@Resource
|
||||
private RoleService roleService;
|
||||
@Resource
|
||||
private UserRoleService userRoleService;
|
||||
@Resource
|
||||
private ConfigProperties configProperties;
|
||||
@Resource
|
||||
private HjcEnterpriseService hjcEnterpriseService;
|
||||
|
||||
@Operation(summary = "企业注册(企业名称+密码,注册后待审核)")
|
||||
@PostMapping("/register")
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public ApiResult<LoginResult> register(@RequestBody HjcAuthRequest request) {
|
||||
if (StrUtil.isBlank(request.getEnterpriseName()) || StrUtil.isBlank(request.getPassword())) {
|
||||
return fail("企业名称和密码不能为空", null);
|
||||
}
|
||||
Integer tenantId = getTenantId();
|
||||
if (tenantId == null) {
|
||||
return fail("租户ID不能为空", null);
|
||||
}
|
||||
String username = request.getEnterpriseName().trim();
|
||||
User exist = userService.getByUsername(username, tenantId);
|
||||
if (exist != null) {
|
||||
return fail("该企业已注册", null);
|
||||
}
|
||||
|
||||
User user = new User();
|
||||
user.setUsername(username);
|
||||
user.setNickname(username);
|
||||
user.setPhone(request.getContactPhone());
|
||||
user.setStatus(0);
|
||||
user.setPlatform("website");
|
||||
user.setGradeId(2);
|
||||
user.setPassword(userService.encodePassword(request.getPassword()));
|
||||
user.setTenantId(tenantId);
|
||||
Role role = roleService.getOne(new QueryWrapper<Role>().eq("role_code", "user"), false);
|
||||
if (role != null) {
|
||||
user.setRoleId(role.getRoleId());
|
||||
}
|
||||
if (!userService.saveUser(user)) {
|
||||
return fail("注册失败", null);
|
||||
}
|
||||
if (role != null) {
|
||||
UserRole userRole = new UserRole();
|
||||
userRole.setUserId(user.getUserId());
|
||||
userRole.setTenantId(tenantId);
|
||||
userRole.setRoleId(role.getRoleId());
|
||||
userRoleService.save(userRole);
|
||||
}
|
||||
|
||||
HjcEnterprise enterprise = new HjcEnterprise();
|
||||
enterprise.setUserId(user.getUserId());
|
||||
enterprise.setName(username);
|
||||
enterprise.setCreditCode(request.getCreditCode());
|
||||
enterprise.setContactPhone(request.getContactPhone());
|
||||
enterprise.setContactEmail(request.getContactEmail());
|
||||
enterprise.setAddress(request.getAddress());
|
||||
enterprise.setAgentName(request.getAgentName());
|
||||
enterprise.setAgentEmail(request.getAgentEmail());
|
||||
enterprise.setAgentPhone(request.getAgentPhone());
|
||||
enterprise.setAuthorizeExpire(request.getAuthorizeExpire());
|
||||
enterprise.setAuthStatus(0);
|
||||
enterprise.setTenantId(tenantId);
|
||||
hjcEnterpriseService.save(enterprise);
|
||||
|
||||
String token = JwtUtil.buildToken(new JwtSubject(user.getUsername(), user.getTenantId()),
|
||||
configProperties.getTokenExpireTime(), configProperties.getTokenKey());
|
||||
return success("注册成功", new LoginResult(token, user));
|
||||
}
|
||||
|
||||
@Operation(summary = "企业登录(企业名称+密码)")
|
||||
@PostMapping("/login")
|
||||
public ApiResult<LoginResult> login(@RequestBody HjcAuthRequest request) {
|
||||
if (StrUtil.isBlank(request.getEnterpriseName()) || StrUtil.isBlank(request.getPassword())) {
|
||||
return fail("企业名称和密码不能为空", null);
|
||||
}
|
||||
Integer tenantId = getTenantId();
|
||||
String username = request.getEnterpriseName().trim();
|
||||
User user = userService.getByUsername(username, tenantId);
|
||||
if (user == null || !userService.comparePassword(user.getPassword(), request.getPassword())) {
|
||||
return fail("企业名称或密码错误", null);
|
||||
}
|
||||
String token = JwtUtil.buildToken(new JwtSubject(user.getUsername(), user.getTenantId()),
|
||||
configProperties.getTokenExpireTime(), configProperties.getTokenKey());
|
||||
return success("登录成功", new LoginResult(token, user));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.gxwebsoft.hjc.dto;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* 汇吉采 网站企业 注册/登录 请求(企业名称+密码)
|
||||
*/
|
||||
@Data
|
||||
@Schema(name = "HjcAuthRequest", description = "汇吉采企业注册/登录请求")
|
||||
public class HjcAuthRequest {
|
||||
|
||||
@Schema(description = "企业名称(登录账号)", required = true)
|
||||
private String enterpriseName;
|
||||
|
||||
@Schema(description = "登录密码", required = true)
|
||||
private String password;
|
||||
|
||||
@Schema(description = "纳税人识别号/统一社会信用代码")
|
||||
private String creditCode;
|
||||
|
||||
@Schema(description = "企业联系电话")
|
||||
private String contactPhone;
|
||||
|
||||
@Schema(description = "企业邮箱")
|
||||
private String contactEmail;
|
||||
|
||||
@Schema(description = "企业地址")
|
||||
private String address;
|
||||
|
||||
@Schema(description = "经办人姓名")
|
||||
private String agentName;
|
||||
|
||||
@Schema(description = "经办人邮箱")
|
||||
private String agentEmail;
|
||||
|
||||
@Schema(description = "经办人手机号")
|
||||
private String agentPhone;
|
||||
|
||||
@Schema(description = "授权委托书到期时间")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime authorizeExpire;
|
||||
}
|
||||
Reference in New Issue
Block a user