新增消费机设备SN码登录代码

This commit is contained in:
liux 2025-11-28 09:15:10 +08:00
parent 52cc0709aa
commit efbc063a2f
15 changed files with 222 additions and 11 deletions

View File

@ -55,6 +55,9 @@ public interface RemoteUserService {
public R<LoginUser> getKitchenStaffInfoByPhone(@PathVariable("phone") String phone, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
@GetMapping("/user/deviceStaff/{deviceSn}")
public R<LoginUser> getDeviceStaffInfoBySn(@PathVariable("deviceSn") String deviceSn, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
* 通过用户名查询用户信息
*

View File

@ -201,6 +201,13 @@ public class SysUser extends BaseEntity {
@ApiModelProperty("查询类型")
private boolean bingStatus = false;
@Getter
private String features;
public void setFeatures(String features) {
this.features = features;
}
public void setBingStatus(boolean bingStatus) {
this.bingStatus = bingStatus;
}

View File

@ -62,6 +62,12 @@ public class RemoteUserFallbackFactory implements FallbackFactory<RemoteUserServ
return R.fail("获取用户失败:" + throwable.getMessage());
}
@Override
public R<LoginUser> getDeviceStaffInfoBySn(String deviceSn, String source) {
return R.fail("获取用户失败:" + throwable.getMessage());
}
/**
* 通过用户名查询用户信息
*

View File

@ -26,7 +26,9 @@ public enum LoginType {
/**
* 邮箱验证码
*/
KITCHEN_PHONE_PASSWORD;
KITCHEN_PHONE_PASSWORD,
DEVICE_PASSWORD;
@JsonCreator
public static LoginType fromString(String key) {

View File

@ -136,6 +136,7 @@ public class TokenController {
public R<?> login(@RequestBody LoginBody form) {
// 获取相应的登录策略
LoginStrategy strategy = loginStrategyFactory.getStrategy(form.getLoginType());
System.err.println("strategy=="+strategy);
if (strategy == null) {
return R.fail("不支持的登录方式");
}
@ -158,12 +159,19 @@ public class TokenController {
String username = Sm4Utils.decrypt(form.getUsername());
String password = Sm4Utils.decrypt(form.getPassword());
LoginUser login = strategy.login(username, password);
logService.saveLogin(username, "登录", "登录成功", null, "成功");
if (LoginType.KITCHEN_PHONE_PASSWORD.equals(form.getLoginType())) {
return R.ok(tokenService.createKitchenStaffToken(login));
} else {
//
if (LoginType.USERNAME_PASSWORD.equals(form.getLoginType())) {
LoginUser login = strategy.login(username, password);
logService.saveLogin(username, "登录", "登录成功", null, "成功");
return R.ok(tokenService.createToken(login));
} else if(LoginType.KITCHEN_PHONE_PASSWORD.equals(form.getLoginType())){
LoginUser login = strategy.login(username, password);
logService.saveLogin(username, "登录", "登录成功", null, "成功");
return R.ok(tokenService.createKitchenStaffToken(login));
}else{
LoginUser login = strategy.login(username, password);
logService.saveLogin(username, "登录", "登录成功", null, "成功");
return R.ok(tokenService.createKitchenStaffToken(login));
}
}

View File

@ -33,6 +33,8 @@ public class LoginStrategyFactory {
strategyMap.put(LoginType.EMAIL_OTP, strategy);
} else if (strategy instanceof KitchenPhonePasswordLoginStrategy) {
strategyMap.put(LoginType.KITCHEN_PHONE_PASSWORD, strategy);
}else if(strategy instanceof DevicePasswordLoginStrategy){
strategyMap.put(LoginType.DEVICE_PASSWORD, strategy);
}
// 继续添加其他策略
});

View File

@ -0,0 +1,79 @@
package com.bonus.auth.service;
import com.bonus.common.core.constant.SecurityConstants;
import com.bonus.common.core.domain.R;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.config.SystemConfig;
import com.bonus.system.api.RemoteUserService;
import com.bonus.system.api.domain.SysUser;
import com.bonus.system.api.model.LoginUser;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
/**
* @author xliu
* @date 2025/11/20 13:29
*/
@Service
public class DevicePasswordLoginStrategy implements LoginStrategy{
@Resource
private SystemConfig systemConfig;
@Resource
private RemoteUserService remoteUserService;
@Resource
private PasswordValidatorService passwordValidatorService;
@Resource
private SysPasswordService passwordService;
@Override
public LoginUser login(String phone, String password) {
System.err.println("phone========"+phone+", password="+password);
if (!systemConfig.getLoginConfig().isPhonePassword()) {
throw new ServiceException("用户不存在/密码错误");
}
if(phone == null){
throw new ServiceException("请输入账户");
}
String[] str = phone.split("_");
if(str.length<2){
throw new ServiceException("输入的sn码不正确");
}
LocalDateTime now = LocalDateTime.now();
// 方式1直接格式化
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
String formattedDate = now.format(formatter);
System.out.println("格式化日期: " + formattedDate);
if(formattedDate.equals(str[1])==false){
throw new ServiceException("输入的sn码出错");
}
//通过手机号获取用户信息
R<LoginUser> userResult = remoteUserService.getDeviceStaffInfoBySn(str[0], SecurityConstants.INNER);
//验证用户是否存在
passwordValidatorService.validateUserResult(str[0], userResult);
//获取用户信息
LoginUser userInfo = userResult.getData();
SysUser user = userInfo.getSysUser();
//校验用户审批状态
//passwordValidatorService.validateApprovalStatus(user.getUserName(), user);
// 处理IP校验
//passwordValidatorService.validateIpBlacklist(user.getUserName());
// 验证密码
passwordService.validateSn(user, str[0], System.currentTimeMillis());
//校验用户启用状态
//passwordValidatorService.validateUserStatus(user.getUserName(), user);
//passwordValidatorService.processLoginBlackList(user);
//返回信息
return userInfo;
}
}

View File

@ -7,6 +7,7 @@ import com.bonus.common.core.exception.CaptchaException;
import com.bonus.common.core.exception.ServiceException;
import com.bonus.common.core.utils.StringUtils;
import com.bonus.common.core.utils.VerificationCodeUtils;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.utils.sms.SmsUtils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.redis.service.RedisService;
@ -83,6 +84,19 @@ public class SysPasswordService {
clearLoginRecordCache(username);
}
}
public void validateSn(SysUser user, String sn, long l) {
String username = user.getUserName();
boolean tf = SecurityUtils.matchesPassword(sn, Sm4Utils.encrypt(user.getPhonenumber()));
if(!tf){
throw new ServiceException("验证失败!");
}else{
clearLoginRecordCache(username);
}
}
public boolean matches(SysUser user, String rawPassword) {
return SecurityUtils.matchesPassword(rawPassword, user.getPassword());
}
@ -120,4 +134,5 @@ public class SysPasswordService {
return R.ok();
}
}

View File

@ -87,6 +87,7 @@ public class TokenService {
rspMap.put("expires_in", EXPIRETIME);
rspMap.put("isLogin", isLogin(String.valueOf(userId)));
long tokenTime = getTokenTime();
System.err.println("tokenTime="+tokenTime);
//对token进行存储
redisService.setCacheObject(LOGIN_USER_KEY + userId, token, tokenTime, TimeUnit.MINUTES);
SysUser sysUser = new SysUser();
@ -121,9 +122,14 @@ public class TokenService {
rspMap.put("staffId", loginUser.getUserid());
rspMap.put("staffName", loginUser.getSysUser().getNickName());
rspMap.put("mobile", loginUser.getSysUser().getPhonenumber());
long tokenTime = getTokenTime();
long tokenTime = getKitchenTokenTime();
System.err.println("tokenTime="+tokenTime);
//对token进行存储
redisService.setCacheObject(LOGIN_STAFF_KEY + userId, token, tokenTime, TimeUnit.MINUTES);
// 验证设置
Long actualExpire = redisService.getExpire(LOGIN_STAFF_KEY + userId);
log.info("期望过期时间: {}分钟, 实际过期时间: {}秒",
tokenTime*60, actualExpire);
return rspMap;
}
@ -253,4 +259,22 @@ public class TokenService {
}
return tokenTime;
}
private Long getKitchenTokenTime(){
long tokenTime = 20L;
String redisResult = redisService.getCacheObject("sys_config:"+ "sys.visit.device.tokentime");
if(StringUtils.isNotEmpty(redisResult)) {
try {
tokenTime = Long.parseLong(redisResult);
} catch (Exception e) {
tokenTime = 43200L; //MINUTES
}
}else {
Long result = systemConfig.getTokenTime();
if (!ObjectUtil.isEmpty(result)){
tokenTime = result;
}
}
return tokenTime;
}
}

View File

@ -13,7 +13,7 @@ import javax.servlet.http.HttpServletRequest;
/**
* 权限获取工具类
*
*
* @author bonus
*/
public class SecurityUtils
@ -83,7 +83,7 @@ public class SecurityUtils
/**
* 是否为管理员
*
*
* @param userId 用户ID
* @return 结果
*/

View File

@ -202,6 +202,21 @@ public class SysUserController extends BaseController {
return R.ok(sysUserVo);
}
@InnerAuth
@GetMapping("/deviceStaff/{deviceSn}")
public R<LoginUser> getDeviceStaffInfoBySn(@PathVariable("deviceSn") String deviceSn) {
SysUser sysUser = userService.getDeviceStaffInfoBySn(deviceSn);
if (StringUtils.isNull(sysUser)) {
return R.fail("用户名或密码错误");
}
// 权限集合
Set<String> permissions = permissionService.getDevicePermission(sysUser);
LoginUser sysUserVo = new LoginUser();
sysUserVo.setSysUser(sysUser);
sysUserVo.setPermissions(permissions);
return R.ok(sysUserVo);
}
/**
* 获取当前用户信息
*/

View File

@ -160,4 +160,6 @@ public interface SysUserMapper {
int systemUpdateUser(SysUser user);
List<Long> selectBindUserIds();
SysUser getDeviceStaffInfoBySn(String encrypt);
}

View File

@ -245,4 +245,5 @@ public interface ISysUserService {
public AjaxResult systemUpdateUser(SysUser user);
SysUser getDeviceStaffInfoBySn(String photoNumber);
}

View File

@ -206,6 +206,15 @@ public class SysUserServiceImpl implements ISysUserService {
return sysUser;
}
@Override
public SysUser getDeviceStaffInfoBySn(String deviceSn) {
SysUser sysUser = userMapper.getDeviceStaffInfoBySn(Sm4Utils.encrypt(deviceSn));
if (Objects.isNull(sysUser)) {
sysUser = userMapper.getDeviceStaffInfoBySn(deviceSn);
}
return sysUser;
}
/**
* 通过用户名查询用户
*
@ -420,7 +429,28 @@ public class SysUserServiceImpl implements ISysUserService {
insertUserPost(user);
// 同步更新user face数据
try {
uploadUserFace(user);
SysUser su = userMapper.selectUserById(user.getUserId());
String oldPhoto = su.getPhotoUrl();
String newPhoto = user.getPhotoUrl();
if(Objects.nonNull(oldPhoto) && Objects.nonNull(newPhoto)){
int oldPhotoIndex = oldPhoto.indexOf("uploads");
int newPhotoIndex = newPhoto.indexOf("uploads");
System.err.println("oldPhotoIndex="+oldPhotoIndex);
System.err.println("newPhotoIndex="+newPhotoIndex);
//都有值可能人脸已经换了
String oldPhotoUrl = oldPhoto.substring(oldPhotoIndex);
String newPhotoUrl = newPhoto.substring(newPhotoIndex);
if(!oldPhotoUrl.equals(newPhotoUrl)){
System.err.println("人脸更新喽~~~~~~");
uploadUserFace(user);
}else{
System.err.println("人脸没更新!!!!!");
}
}else{
System.err.println("变更人脸信息");
//一个有数据 一个无数据 必定更新
uploadUserFace(user);
}
} catch (Exception e) {
log.error("同步更新user face数据失败", e.getMessage());
}

View File

@ -4,7 +4,7 @@
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bonus.system.mapper.SysUserMapper">
<resultMap type="SysUser" id="SysUserResult">
<resultMap type="com.bonus.system.api.domain.SysUser" id="SysUserResult">
<id property="userId" column="user_id"/>
<id property="userCode" column="user_code"/>
<result property="deptId" column="dept_id"/>
@ -35,6 +35,8 @@
<result property="isBuiltIn" column="is_built_in"/>
<result property="effectiveStartDay" column="effective_start_day"/>
<result property="effectiveEndDay" column="effective_end_day"/>
<result property="features" column="features"/>
<association property="dept" javaType="SysDept" resultMap="deptResult"/>
<collection property="roles" javaType="java.util.List" resultMap="RoleResult"/>
@ -129,6 +131,10 @@
r.data_scope,
r.status as role_status,
uf.photo_url,
CASE
WHEN uf.features IS NOT NULL AND uf.features != '' THEN '1'
ELSE '0'
END AS features,
CASE
WHEN uf.photo_url is not null THEN '1'
WHEN u.photo_url is null THEN '0'
@ -435,4 +441,15 @@
select supplier_user_id from ims_supplier WHERE del_flag = '0'
group by supplier_id
</select>
<select id="getDeviceStaffInfoBySn" resultType="com.bonus.system.api.domain.SysUser">
select device_id as user_id,
device_sn as phonenumber,
device_name as user_name,
device_type as nick_name,
device_state as sex, device_pwd
from device_info
where device_sn = #{deviceSn}
</select>
</mapper>