init
This commit is contained in:
105
ruoyi-cai/src/main/java/com/ruoyi/cai/auth/CaiLoginManager.java
Normal file
105
ruoyi-cai/src/main/java/com/ruoyi/cai/auth/CaiLoginManager.java
Normal file
@@ -0,0 +1,105 @@
|
||||
package com.ruoyi.cai.auth;
|
||||
|
||||
import cn.dev33.satoken.exception.NotLoginException;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import com.ruoyi.cai.domain.CaiUser;
|
||||
import com.ruoyi.cai.domain.CaiUserInfo;
|
||||
import com.ruoyi.cai.executor.ExecutorConstant;
|
||||
import com.ruoyi.cai.service.CaiUserInfoService;
|
||||
import com.ruoyi.cai.service.CaiUserService;
|
||||
import com.ruoyi.common.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.enums.UserType;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import com.ruoyi.common.helper.LoginHelper;
|
||||
import com.ruoyi.common.utils.MessageUtils;
|
||||
import com.ruoyi.common.utils.ServletUtils;
|
||||
import com.ruoyi.common.utils.ip.AddressUtils;
|
||||
import com.ruoyi.system.service.SysLoginService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Collections;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class CaiLoginManager {
|
||||
|
||||
@Autowired
|
||||
private CaiUserService caiUserService;
|
||||
@Autowired
|
||||
private SysLoginService sysLoginService;
|
||||
|
||||
|
||||
public String login(String username,String password){
|
||||
CaiUser caiUser = caiUserService.getByUsername(username);
|
||||
if(caiUser == null){
|
||||
throw new ServiceException("用户不存在或密码错误");
|
||||
}
|
||||
if(caiUser.getStatus() != 0){
|
||||
throw new ServiceException("用户已封禁,请联系客服");
|
||||
}
|
||||
/*if(!BCrypt.checkpw(password, caiUser.getPassword())){
|
||||
throw new ServiceException("用户不存在或密码错误");
|
||||
}*/
|
||||
LoginUser loginUser = new LoginUser();
|
||||
loginUser.setDeptId(null);
|
||||
loginUser.setDeptName(null);
|
||||
// loginUser.setToken(caiUser.getToken());
|
||||
// loginUser.setLoginTime();
|
||||
// loginUser.setExpireTime();
|
||||
// loginUser.setIpaddr();
|
||||
// loginUser.setLoginLocation();
|
||||
// loginUser.setBrowser();
|
||||
// loginUser.setOs();
|
||||
loginUser.setMenuPermission(Collections.emptySet());
|
||||
loginUser.setRolePermission(Collections.emptySet());
|
||||
loginUser.setUsername(caiUser.getMobile());
|
||||
loginUser.setRoles(Collections.emptyList());
|
||||
loginUser.setRoleId(null);
|
||||
loginUser.setUserId(caiUser.getId());
|
||||
loginUser.setUserType(UserType.APP_USER.getUserType());
|
||||
LoginHelper.login(loginUser);
|
||||
sysLoginService.recordLogininfor(loginUser.getUsername(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success"));
|
||||
String clientIP = ServletUtils.getClientIP();
|
||||
ExecutorConstant.COMMON_EXECUTOR.execute(() -> recordLoginInfo(caiUser,clientIP));
|
||||
return StpUtil.getTokenValue();
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private CaiUserInfoService caiUserInfoService;
|
||||
|
||||
public void recordLoginInfo(CaiUser caiUser,String ip) {
|
||||
String address = AddressUtils.getRealAddressByIP(ip);
|
||||
CaiUserInfo caiUserInfo = caiUserInfoService.getById(caiUser.getId());
|
||||
CaiUserInfo update = new CaiUserInfo();
|
||||
update.setUserId(caiUser.getId());
|
||||
update.setLoginCount(caiUserInfo.getLoginCount()+1);
|
||||
update.setPrevLoginTime(caiUserInfo.getLastLoginTime());
|
||||
update.setLastLoginIp(ip);
|
||||
update.setLastLoginTime(LocalDateTime.now());
|
||||
update.setLastLocation(address);
|
||||
caiUserInfoService.updateById(update);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
public void logout() {
|
||||
try {
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
sysLoginService.recordLogininfor(loginUser.getUsername(), Constants.LOGOUT, MessageUtils.message("user.logout.success"));
|
||||
} catch (NotLoginException ignored) {
|
||||
} finally {
|
||||
try {
|
||||
StpUtil.logout();
|
||||
} catch (NotLoginException ignored) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package com.ruoyi.cai.auth;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class LoginCaiUser {
|
||||
private String username;
|
||||
private String password;
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.ruoyi.cai.controller.app;
|
||||
|
||||
import cn.dev33.satoken.annotation.SaIgnore;
|
||||
import com.ruoyi.cai.auth.CaiLoginManager;
|
||||
import com.ruoyi.cai.auth.LoginCaiUser;
|
||||
import com.ruoyi.cai.service.CaiUserService;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.domain.model.LoginBody;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
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 java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/cai/auth")
|
||||
@SaIgnore
|
||||
public class CaiAppAuthController {
|
||||
|
||||
@Autowired
|
||||
private CaiLoginManager caiLoginManager;
|
||||
@Autowired
|
||||
private CaiUserService caiUserService;
|
||||
|
||||
@PostMapping("/register")
|
||||
public R<Void> register(@RequestBody LoginCaiUser caiUser){
|
||||
caiUserService.register(caiUser);
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
@PostMapping("/login")
|
||||
public R<Map<String,Object>> login(@RequestBody LoginBody loginBody){
|
||||
Map<String, Object> ajax = new HashMap<>();
|
||||
String token = caiLoginManager.login(loginBody.getUsername(), loginBody.getPassword());
|
||||
ajax.put("token",token);
|
||||
return R.ok(ajax);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.ruoyi.cai.controller.app;
|
||||
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.helper.LoginHelper;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/cai/user")
|
||||
public class CaiAppUserController {
|
||||
|
||||
@GetMapping("/info")
|
||||
public R<LoginUser> info(String user){
|
||||
LoginUser loginUser = LoginHelper.getLoginUser();
|
||||
return R.ok(loginUser);
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public class CaiUser implements Serializable {
|
||||
*
|
||||
*/
|
||||
@TableId(value = "id")
|
||||
private Integer id;
|
||||
private Long id;
|
||||
/**
|
||||
* 用户号/ID号
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.ruoyi.cai.domain;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.*;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import com.ruoyi.common.core.domain.BaseEntity;
|
||||
|
||||
/**
|
||||
* 用户信息对象 cai_user_info
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2023-12-20
|
||||
*/
|
||||
@Data
|
||||
@TableName("cai_user_info")
|
||||
public class CaiUserInfo {
|
||||
|
||||
private static final long serialVersionUID=1L;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@TableId(value = "user_id",type = IdType.INPUT)
|
||||
private Long userId;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private String realname;
|
||||
/**
|
||||
* 星座
|
||||
*/
|
||||
private Integer signs;
|
||||
/**
|
||||
* 用户评分
|
||||
*/
|
||||
private BigDecimal giveScore;
|
||||
/**
|
||||
* 奖励好友支出的比率
|
||||
*/
|
||||
private BigDecimal rewardPayRate;
|
||||
/**
|
||||
* 奖励好友收入的比率
|
||||
*/
|
||||
private BigDecimal rewardIncomeRate;
|
||||
/**
|
||||
* 奖励好友充值的比率
|
||||
*/
|
||||
private BigDecimal rewardRechargeRate;
|
||||
/**
|
||||
* 登录次数
|
||||
*/
|
||||
private Integer loginCount;
|
||||
/**
|
||||
* 连续登录天数
|
||||
*/
|
||||
private Long continueLoginCount;
|
||||
/**
|
||||
* 最大连续登录天数
|
||||
*/
|
||||
private Long maxLoginCount;
|
||||
/**
|
||||
* 上次登录时间
|
||||
*/
|
||||
private LocalDateTime prevLoginTime;
|
||||
/**
|
||||
* 最后登录IP
|
||||
*/
|
||||
private String lastLoginIp;
|
||||
/**
|
||||
* 最后登录时间
|
||||
*/
|
||||
private LocalDateTime lastLoginTime;
|
||||
/**
|
||||
* 最后登录位置
|
||||
*/
|
||||
private String lastLocation;
|
||||
/**
|
||||
* 注册IP
|
||||
*/
|
||||
private String regIp;
|
||||
/**
|
||||
* 注册时间
|
||||
*/
|
||||
private LocalDateTime regTime;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.ruoyi.cai.executor;
|
||||
|
||||
import com.alibaba.ttl.threadpool.TtlExecutors;
|
||||
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
||||
|
||||
import java.util.concurrent.*;
|
||||
|
||||
/**
|
||||
* 线程变量定义
|
||||
* <p>created on 2023/3/3 11:16</p>
|
||||
* @author ZL
|
||||
*/
|
||||
public class ExecutorConstant {
|
||||
|
||||
private final static int CPU_NUM = Runtime.getRuntime().availableProcessors();
|
||||
|
||||
public static Executor SYNC_EXECUTOR;
|
||||
|
||||
public static Executor COMMON_EXECUTOR;
|
||||
|
||||
static {
|
||||
ThreadPoolExecutor syncExecutor = initExecutor(CPU_NUM,
|
||||
CPU_NUM << 2,
|
||||
5,
|
||||
TimeUnit.SECONDS,
|
||||
new SynchronousQueue<>(),
|
||||
"syncThreadPool-%d");
|
||||
SYNC_EXECUTOR = TtlExecutors.getTtlExecutor(syncExecutor);
|
||||
|
||||
ThreadPoolExecutor commonExecutor = new ThreadPoolExecutor(CPU_NUM,
|
||||
CPU_NUM << 2,
|
||||
5,
|
||||
TimeUnit.SECONDS,
|
||||
new ArrayBlockingQueue<>(50),
|
||||
init("commonThreadPool-%d"),
|
||||
new ThreadPoolExecutor.CallerRunsPolicy());
|
||||
COMMON_EXECUTOR = TtlExecutors.getTtlExecutor(commonExecutor);
|
||||
|
||||
}
|
||||
|
||||
private static ThreadFactory init(String nameFormat){
|
||||
return new ThreadFactoryBuilder().setNameFormat(nameFormat).build();
|
||||
}
|
||||
|
||||
private static ThreadPoolExecutor initExecutor(int corePoolSize, int maxPoolSize, int keepAliveTime, TimeUnit timeUnit,
|
||||
BlockingQueue<Runnable> workQueue, String nameFormat){
|
||||
return new ThreadPoolExecutor(corePoolSize, maxPoolSize, keepAliveTime, timeUnit, workQueue,
|
||||
init(nameFormat));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.ruoyi.cai.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.ruoyi.cai.domain.CaiUserInfo;
|
||||
|
||||
public interface CaiUserInfoMapper extends BaseMapper<CaiUserInfo> {
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package com.ruoyi.cai.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.ruoyi.cai.domain.CaiUserInfo;
|
||||
|
||||
public interface CaiUserInfoService extends IService<CaiUserInfo> {
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.ruoyi.cai.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.ruoyi.cai.auth.LoginCaiUser;
|
||||
import com.ruoyi.cai.domain.CaiUser;
|
||||
|
||||
/**
|
||||
@@ -11,4 +12,7 @@ import com.ruoyi.cai.domain.CaiUser;
|
||||
*/
|
||||
public interface CaiUserService extends IService<CaiUser> {
|
||||
|
||||
CaiUser getByUsername(String username);
|
||||
|
||||
void register(LoginCaiUser caiUser);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.ruoyi.cai.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ruoyi.cai.domain.CaiUserInfo;
|
||||
import com.ruoyi.cai.mapper.CaiUserInfoMapper;
|
||||
import com.ruoyi.cai.service.CaiUserInfoService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class CaiUserInfoServiceImpl extends ServiceImpl<CaiUserInfoMapper, CaiUserInfo> implements CaiUserInfoService {
|
||||
|
||||
}
|
||||
@@ -1,11 +1,11 @@
|
||||
package com.ruoyi.cai.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.ruoyi.cai.domain.CaiUser;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import com.ruoyi.cai.mapper.CaiUserMapper;
|
||||
import com.ruoyi.cai.service.CaiUserService;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/**
|
||||
* 用户Service业务层处理
|
||||
@@ -13,10 +13,12 @@ import com.ruoyi.cai.service.CaiUserService;
|
||||
* @author 77
|
||||
* @date 2023-12-19
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Service
|
||||
public class CaiUserServiceImpl extends ServiceImpl<CaiUserMapper, CaiUser> implements CaiUserService {
|
||||
|
||||
private final CaiUserMapper baseMapper;
|
||||
|
||||
@Override
|
||||
public CaiUser getByUsername(String username) {
|
||||
return this.getOne(Wrappers.lambdaQuery(CaiUser.class)
|
||||
.eq(CaiUser::getMobile,username));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user