账号切换实现

This commit is contained in:
张松 2025-04-08 10:36:28 +08:00
parent dbaa79b471
commit 6bade716cb
3 changed files with 81 additions and 3 deletions

View File

@ -17,6 +17,7 @@ import cn.hutool.core.util.StrUtil;
import com.czg.exception.ApiNotPrintException;
import com.czg.exception.CzgException;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import java.util.List;
import java.util.Map;
@ -25,6 +26,7 @@ import java.util.function.Consumer;
/**
* @author Administrator
*/
@Slf4j
@Getter
public class MyStpLogic {
public static final StpLogic CLIENT_LOGIC = new StpLogic("client");
@ -53,6 +55,29 @@ public class MyStpLogic {
}
}
/**
* 身份切换
* @param id 用户id
* @param account 账户名
* @param shopId 店铺id
* @param shopName 店铺名称
* @param loginType 登录类型
* @param isAdmin 是否管理员
*/
public void switchTo(Long id, String account, Long shopId, String shopName, LoginType loginType, boolean isAdmin) {
StpLogic logic = getLogic();
long headId = logic.getLoginIdAsLong();
SaSession session = logic.getSession();
Object parentId1 = session.get("headId");
if (parentId1 == null) {
session.set("headId", headId);
}
session.set("userId", id).set("isAdmin", isAdmin).set("isManager", loginType.equals(LoginType.MANAGER))
.set("loginType", loginType).set("account", account).set("shopId", shopId).set("shopName", shopName);
logic.getSaTokenDao().set(splicingKeyTokenValue(logic.getTokenValue()), id.toString(), logic.getTokenTimeout());
}
/**
* @param id 登录账号id
* @param shopName 店铺名称
@ -83,6 +108,8 @@ public class MyStpLogic {
SaManager.getSaTokenDao().set(SaManager.getConfig().getTokenName() + ":" + token, String.valueOf(id), SaManager.getConfig().getTimeout());
}
/**
* 获取当前登录账号名称 管理端为用户账号 客户端为openId
*
@ -104,6 +131,15 @@ public class MyStpLogic {
return shopName instanceof String s ? s : null;
}
/**
* 获取主店铺id
* @return id
*/
public Long getHeadId() {
Object headId = getLogic().getSession().get("headId");
return headId == null ? null : Long.parseLong(headId.toString());
}
/**
* 获取店铺id
*
@ -901,9 +937,7 @@ public class MyStpLogic {
}
public void switchTo(Object loginId) {
getLogic().switchTo(loginId);
}
public void endSwitch() {

View File

@ -11,4 +11,10 @@ public interface AuthorizationService {
Object getCaptcha();
LoginVO login(SysLoginDTO loginDTO, String platformType);
/**
* 切换登录用户
* @param sysUserId 系统用户id
*/
void switchTo(Long sysUserId);
}

View File

@ -12,6 +12,7 @@ import com.czg.account.vo.LoginVO;
import com.czg.config.RedisCst;
import com.czg.enums.PlatformTypeEnum;
import com.czg.enums.StatusEnum;
import com.czg.enums.YesNoEnum;
import com.czg.exception.ApiNotPrintException;
import com.czg.sa.MyStpLogic;
import com.czg.sa.StpKit;
@ -173,6 +174,43 @@ public class AuthorizationServiceImpl implements AuthorizationService {
return new LoginVO(StpKit.USER.getTokenInfo(), new ArrayList<>(), loginDTO.loginType(), shopInfo);
}
@Override
public void switchTo(Long sysUserId) {
Long headId = StpKit.USER.getHeadId();
long shopId = StpKit.USER.getLoginIdAsLong();
ShopInfo currentInfo = shopInfoService.getById(shopId);
if (currentInfo.getIsHeadShop() != YesNoEnum.YES.value() && headId == null) {
throw new ApiNotPrintException("登录账号无权限切换");
}
SysUser sysUser = sysUserService.getById(sysUserId);
if (sysUser == null) {
throw new ApiNotPrintException("用户不存在");
}
ShopInfo shopInfo = shopInfoService.getById(sysUser.getId());
if (shopInfo == null) {
throw new ApiNotPrintException("店铺信息不存在");
}
if (!shopInfo.getMainId().equals(headId)) {
throw new ApiNotPrintException("目标店铺非登录账号所有");
}
// 查询角色
List<SysRole> roleList = sysRoleService.getByUserId(sysUser.getId());
List<String> roleNames = roleList.stream().map(SysRole::getName).collect(Collectors.toList());
if (sysUser.getIsAdmin()) {
roleNames.add("admin");
}
StpKit.USER.addRoleList(roleNames);
// 权限赋予
List<String> promissionList = sysMenuMapper.selectByUserId(sysUser.getId(), null).stream().map(SysMenu::getPermission).filter(StrUtil::isNotBlank).toList();
StpKit.USER.switchTo(sysUser.getId(), sysUser.getAccount(), shopInfo.getId(), shopInfo.getShopName(), MyStpLogic.LoginType.MANAGER, sysUser.getIsAdmin());
StpKit.USER.addPermissionList(promissionList);
StpKit.USER.addRoleList(roleNames);
}
@NotNull
private static HandoverRecord getHandoverRecord(boolean isStaff, ShopInfo shopInfo, ShopStaff shopStaff) {
HandoverRecord entity = new HandoverRecord();