内存优化
This commit is contained in:
@@ -258,4 +258,26 @@ public class SysLoginController
|
||||
return loginService.loginOrRegister(loginBody);
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业用户登录注册二合一接口
|
||||
* 登录:传手机号(username)、密码(password)
|
||||
* 注册:传企业信用代码(companyCode)、企业名称(companyName)、手机号(username)、密码(password)
|
||||
* @return 结果
|
||||
*/
|
||||
@ApiOperation("企业用户登录注册二合一")
|
||||
@PostMapping("/app/company/loginOrRegister")
|
||||
public AjaxResult companyLoginOrRegister(@RequestBody LoginBody loginBody)
|
||||
{
|
||||
if (loginBody == null) {
|
||||
return AjaxResult.error("参数不能为空!");
|
||||
}
|
||||
if (StringUtils.isBlank(loginBody.getUsername())) {
|
||||
return AjaxResult.error("手机号不能为空!");
|
||||
}
|
||||
if (StringUtils.isBlank(loginBody.getPassword())) {
|
||||
return AjaxResult.error("密码不能为空!");
|
||||
}
|
||||
return loginService.companyLoginOrRegister(loginBody);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -49,6 +49,16 @@ public class LoginBody
|
||||
*/
|
||||
private String name;
|
||||
|
||||
/**
|
||||
* 企业信用代码
|
||||
*/
|
||||
private String companyCode;
|
||||
|
||||
/**
|
||||
* 企业名称
|
||||
*/
|
||||
private String companyName;
|
||||
|
||||
public String getUsername()
|
||||
{
|
||||
return username;
|
||||
@@ -128,4 +138,20 @@ public class LoginBody
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getCompanyCode() {
|
||||
return companyCode;
|
||||
}
|
||||
|
||||
public void setCompanyCode(String companyCode) {
|
||||
this.companyCode = companyCode;
|
||||
}
|
||||
|
||||
public String getCompanyName() {
|
||||
return companyName;
|
||||
}
|
||||
|
||||
public void setCompanyName(String companyName) {
|
||||
this.companyName = companyName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,11 +5,14 @@ import javax.annotation.Resource;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.ruoyi.cms.domain.vo.WechatAuthVO;
|
||||
import com.ruoyi.cms.mapper.CompanyMapper;
|
||||
import com.ruoyi.cms.service.IAppUserService;
|
||||
import com.ruoyi.cms.service.ICompanyService;
|
||||
import com.ruoyi.cms.util.StringUtil;
|
||||
import com.ruoyi.cms.util.WechatUtil;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.domain.entity.AppUser;
|
||||
import com.ruoyi.common.core.domain.entity.Company;
|
||||
import com.ruoyi.common.core.domain.model.LoginBody;
|
||||
import com.ruoyi.common.core.domain.model.LoginSiteUser;
|
||||
import com.ruoyi.common.core.domain.model.RegisterBody;
|
||||
@@ -76,6 +79,10 @@ public class SysLoginService
|
||||
private IAppUserService appUserService;
|
||||
@Autowired
|
||||
private DistributedLockUtil distributedLockUtil;
|
||||
@Autowired
|
||||
private ICompanyService companyService;
|
||||
@Autowired
|
||||
private CompanyMapper companyMapper;
|
||||
|
||||
/**
|
||||
* 登录验证
|
||||
@@ -801,4 +808,103 @@ public class SysLoginService
|
||||
ajax.put("idCard", appUser.getIdCard());
|
||||
return ajax;
|
||||
}
|
||||
|
||||
/**
|
||||
* 企业用户登录注册二合一
|
||||
* 逻辑:
|
||||
* 1. 根据手机号查询企业用户,存在则登录
|
||||
* 2. 不存在则检查是否传了企业信息,有则注册
|
||||
* 3. 注册时根据信用代码判断企业是否已注册
|
||||
*/
|
||||
@Transactional(rollbackFor = Exception.class)
|
||||
public AjaxResult companyLoginOrRegister(LoginBody loginBody) {
|
||||
String phone = loginBody.getUsername();
|
||||
String password = loginBody.getPassword();
|
||||
String companyCode = loginBody.getCompanyCode();
|
||||
String companyName = loginBody.getCompanyName();
|
||||
|
||||
// 1. 根据手机号查询企业用户
|
||||
AppUser appUser = appUserService.getPhoneAndUserType(phone, StringUtil.IS_COMPANY_USER);
|
||||
|
||||
if (appUser != null) {
|
||||
// 用户存在,执行登录
|
||||
if (StringUtils.isBlank(appUser.getYtjPassword())) {
|
||||
return AjaxResult.error("账号密码未设置,请联系管理员!");
|
||||
}
|
||||
if (!SiteSecurityUtils.matchesPassword(password, appUser.getYtjPassword())) {
|
||||
return AjaxResult.error("密码错误,请重新输入!");
|
||||
}
|
||||
|
||||
// 更新登录时间
|
||||
AppUser updateParam = new AppUser();
|
||||
updateParam.setUserId(appUser.getUserId());
|
||||
updateParam.setLoginDate(new Date());
|
||||
appUserService.updateAppUser(updateParam);
|
||||
|
||||
// 生成token并返回
|
||||
String token = loginUserIdApp(appUser);
|
||||
AjaxResult ajax = AjaxResult.success();
|
||||
ajax.put(Constants.TOKEN, token);
|
||||
ajax.put("isNewUser", false);
|
||||
ajax.put("companyCode", appUser.getIdCard());
|
||||
ajax.put("companyName", appUser.getName());
|
||||
return ajax;
|
||||
}
|
||||
|
||||
// 2. 用户不存在,执行注册
|
||||
// 校验注册必填参数
|
||||
if (StringUtils.isBlank(companyCode)) {
|
||||
return AjaxResult.error("企业信用代码不能为空!");
|
||||
}
|
||||
if (StringUtils.isBlank(companyName)) {
|
||||
return AjaxResult.error("企业名称不能为空!");
|
||||
}
|
||||
|
||||
// 使用分布式锁防止并发注册
|
||||
String lockKey = "company_register_" + companyCode;
|
||||
try (DistributedLockUtil.AutoReleaseLock lock = distributedLockUtil.tryLock(lockKey, 3, TimeUnit.SECONDS)) {
|
||||
if (!lock.isLocked()) {
|
||||
return AjaxResult.error("注册请求过于频繁,请稍后重试!");
|
||||
}
|
||||
|
||||
// 检查企业是否已注册
|
||||
Company existCompany = companyService.queryCodeCompany(companyCode);
|
||||
if (existCompany != null) {
|
||||
return AjaxResult.error("该企业已注册,请联系企业管理员加入组织!");
|
||||
}
|
||||
|
||||
// 创建企业信息
|
||||
Company company = new Company();
|
||||
company.setCode(companyCode);
|
||||
company.setName(companyName);
|
||||
company.setContactPersonPhone(phone);
|
||||
company.setStatus(0); // 审核中
|
||||
companyMapper.insert(company);
|
||||
|
||||
// 创建企业用户
|
||||
AppUser newUser = new AppUser();
|
||||
newUser.setPhone(phone);
|
||||
newUser.setName(companyName);
|
||||
newUser.setIdCard(companyCode);
|
||||
newUser.setIsCompanyUser(StringUtil.IS_COMPANY_USER);
|
||||
newUser.setYtjPassword(SiteSecurityUtils.encryptPassword(password));
|
||||
newUser.setLoginDate(new Date());
|
||||
appUserService.insertAppUser(newUser);
|
||||
|
||||
// 关联企业和用户
|
||||
Company updateCompany = new Company();
|
||||
updateCompany.setCompanyId(company.getCompanyId());
|
||||
updateCompany.setUserId(newUser.getUserId());
|
||||
companyMapper.updateById(updateCompany);
|
||||
|
||||
// 生成token并返回
|
||||
String token = loginUserIdApp(newUser);
|
||||
AjaxResult ajax = AjaxResult.success("注册成功!");
|
||||
ajax.put(Constants.TOKEN, token);
|
||||
ajax.put("isNewUser", true);
|
||||
ajax.put("companyCode", companyCode);
|
||||
ajax.put("companyName", companyName);
|
||||
return ajax;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user