员工权限相关接口
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package com.czg.service.account.mapper;
|
||||
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
import com.czg.account.entity.ShopPermission;
|
||||
|
||||
/**
|
||||
* 店铺权限 映射层。
|
||||
*
|
||||
* @author zs
|
||||
* @since 2025-02-18
|
||||
*/
|
||||
public interface ShopPermissionMapper extends BaseMapper<ShopPermission> {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.czg.service.account.mapper;
|
||||
|
||||
import com.czg.account.entity.ShopPermission;
|
||||
import com.mybatisflex.core.BaseMapper;
|
||||
import com.czg.account.entity.ShopStaffPermission;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 店铺员工权限关联表 映射层。
|
||||
*
|
||||
* @author zs
|
||||
* @since 2025-02-18
|
||||
*/
|
||||
public interface ShopStaffPermissionMapper extends BaseMapper<ShopStaffPermission> {
|
||||
|
||||
List<ShopPermission> getPermissionByStaffId(@Param("shopId") Long shopId, @Param("staffId") Long staffId, @Param("userId") Long userId);
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import com.czg.account.vo.LoginVO;
|
||||
import com.czg.config.RedisCst;
|
||||
import com.czg.enums.StatusEnum;
|
||||
import com.czg.exception.ApiNotPrintException;
|
||||
import com.czg.sa.MyStpLogic;
|
||||
import com.czg.sa.StpKit;
|
||||
import com.czg.service.RedisService;
|
||||
import com.czg.service.account.mapper.SysMenuMapper;
|
||||
@@ -18,6 +19,7 @@ import com.wf.captcha.SpecCaptcha;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -35,6 +37,8 @@ public class AuthorizationServiceImpl implements AuthorizationService {
|
||||
@Resource
|
||||
private ShopStaffService shopStaffService;
|
||||
@Resource
|
||||
private ShopStaffPermissionService shopStaffPermissionService;
|
||||
@Resource
|
||||
private ShopInfoService shopInfoService;
|
||||
@Resource
|
||||
private SysRoleService sysRoleService;
|
||||
@@ -90,6 +94,8 @@ public class AuthorizationServiceImpl implements AuthorizationService {
|
||||
}
|
||||
|
||||
ShopInfo shopInfo;
|
||||
boolean isStaff = false;
|
||||
List<String> shopStaffPromissionList = null;
|
||||
// 商户员工登录
|
||||
if (loginDTO.loginType() == 1) {
|
||||
ShopStaff shopStaff = shopStaffService.queryChain().eq(ShopStaff::getStatus, 1)
|
||||
@@ -98,8 +104,11 @@ public class AuthorizationServiceImpl implements AuthorizationService {
|
||||
if (shopStaff == null) {
|
||||
throw new ApiNotPrintException("账户未启用");
|
||||
}
|
||||
|
||||
isStaff = true;
|
||||
shopInfo = shopInfoService.getById(shopStaff.getShopId());
|
||||
|
||||
// 查询员工权限
|
||||
shopStaffPromissionList = shopStaffPermissionService.getPermissionByStaffId(shopInfo.getId(), shopStaff.getId(), user.getId()).stream().map(ShopPermission::getCode).toList();
|
||||
}else {
|
||||
shopInfo = shopInfoService.getById(user.getId());
|
||||
}
|
||||
@@ -114,13 +123,17 @@ public class AuthorizationServiceImpl implements AuthorizationService {
|
||||
}
|
||||
}
|
||||
|
||||
StpKit.USER.login(user.getId(), shopInfo.getId(), true, user.getIsAdmin());
|
||||
StpKit.USER.login(user.getId(), shopInfo.getId(), isStaff ? MyStpLogic.LoginType.STAFF :MyStpLogic.LoginType.MANAGER, user.getIsAdmin());
|
||||
// 查询角色
|
||||
List<SysRole> roleList = sysRoleService.getByUserId(user.getId());
|
||||
List<String> roleNames = roleList.stream().map(SysRole::getName).collect(Collectors.toList());
|
||||
StpKit.USER.addRoleList(roleNames);
|
||||
// 权限赋予
|
||||
List<String> promissionList = sysMenuMapper.selectByUserId(user.getId(), null).stream().map(SysMenu::getPermission).filter(StrUtil::isNotBlank).collect(Collectors.toList());
|
||||
// 加入员工权限
|
||||
if (shopStaffPromissionList != null && !shopStaffPromissionList.isEmpty()) {
|
||||
promissionList.addAll(shopStaffPromissionList);
|
||||
}
|
||||
StpKit.USER.addPermissionList(promissionList);
|
||||
return new LoginVO(StpKit.USER.getTokenInfo(), promissionList, loginDTO.loginType(), shopInfo);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.czg.service.account.service.impl;
|
||||
|
||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
import com.czg.account.entity.ShopPermission;
|
||||
import com.czg.account.service.ShopPermissionService;
|
||||
import com.czg.service.account.mapper.ShopPermissionMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 店铺权限 服务层实现。
|
||||
*
|
||||
* @author zs
|
||||
* @since 2025-02-18
|
||||
*/
|
||||
@Service
|
||||
public class ShopPermissionServiceImpl extends ServiceImpl<ShopPermissionMapper, ShopPermission> implements ShopPermissionService{
|
||||
|
||||
@Override
|
||||
public List<ShopPermission> getPermission() {
|
||||
// 构建 parentId -> Permission 映射
|
||||
List<ShopPermission> permissions = list();
|
||||
Map<Long, List<ShopPermission>> groupByParent = permissions.stream()
|
||||
.collect(Collectors.groupingBy(ShopPermission::getParentId));
|
||||
|
||||
// 通过 stream 递归构建树
|
||||
return permissions.stream()
|
||||
.filter(p -> p.getParentId() == 0)
|
||||
.peek(p -> p.setChildren(getChildren(p.getId(), groupByParent)))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static List<ShopPermission> getChildren(long parentId, Map<Long, List<ShopPermission>> groupByParent) {
|
||||
return groupByParent.getOrDefault(parentId, new ArrayList<>()).stream()
|
||||
.peek(child -> child.setChildren(getChildren(child.getId(), groupByParent)))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.czg.service.account.service.impl;
|
||||
|
||||
import com.czg.account.entity.ShopPermission;
|
||||
import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
import com.czg.account.entity.ShopStaffPermission;
|
||||
import com.czg.account.service.ShopStaffPermissionService;
|
||||
import com.czg.service.account.mapper.ShopStaffPermissionMapper;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 店铺员工权限关联表 服务层实现。
|
||||
*
|
||||
* @author zs
|
||||
* @since 2025-02-18
|
||||
*/
|
||||
@Service
|
||||
public class ShopStaffPermissionServiceImpl extends ServiceImpl<ShopStaffPermissionMapper, ShopStaffPermission> implements ShopStaffPermissionService{
|
||||
|
||||
@Override
|
||||
public List<ShopPermission> getPermissionByStaffId(Long shopId, Long staffId, Long userId) {
|
||||
return mapper.getPermissionByStaffId(shopId, staffId, userId);
|
||||
}
|
||||
}
|
||||
@@ -5,11 +5,11 @@ import cn.hutool.core.util.StrUtil;
|
||||
import com.czg.account.dto.staff.ShopStaffAddDTO;
|
||||
import com.czg.account.dto.staff.ShopStaffEditDTO;
|
||||
import com.czg.account.dto.staff.ShopStaffRemoveDTO;
|
||||
import com.czg.account.entity.ShopPermission;
|
||||
import com.czg.account.entity.ShopStaff;
|
||||
import com.czg.account.entity.ShopStaffPermission;
|
||||
import com.czg.account.entity.SysUser;
|
||||
import com.czg.account.service.ShopStaffService;
|
||||
import com.czg.account.service.SysUserService;
|
||||
import com.czg.account.service.SysUsersRolesService;
|
||||
import com.czg.account.service.*;
|
||||
import com.czg.exception.ApiNotPrintException;
|
||||
import com.czg.sa.StpKit;
|
||||
import com.czg.service.account.mapper.ShopStaffMapper;
|
||||
@@ -20,6 +20,9 @@ import com.mybatisflex.spring.service.impl.ServiceImpl;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 店铺员工 服务层实现。
|
||||
*
|
||||
@@ -32,6 +35,10 @@ public class ShopStaffServiceImpl extends ServiceImpl<ShopStaffMapper, ShopStaff
|
||||
private SysUserService sysUserService;
|
||||
@Resource
|
||||
private SysUsersRolesService sysUsersRolesService;
|
||||
@Resource
|
||||
private ShopPermissionService shopPermissionService;
|
||||
@Resource
|
||||
private ShopStaffPermissionService shopStaffPermissionService;
|
||||
|
||||
@Override
|
||||
public Boolean add(ShopStaffAddDTO shopStaffAddDTO) {
|
||||
@@ -40,7 +47,11 @@ public class ShopStaffServiceImpl extends ServiceImpl<ShopStaffMapper, ShopStaff
|
||||
ShopStaff shopStaff = BeanUtil.copyProperties(shopStaffAddDTO, ShopStaff.class);
|
||||
shopStaff.setShopId(StpKit.USER.getLoginIdAsLong());
|
||||
shopStaff.setId(sysUser.getId());
|
||||
return save(shopStaff);
|
||||
save(shopStaff);
|
||||
|
||||
// 权限添加
|
||||
addPermission(shopStaff, shopStaffAddDTO.getShopPermissionIds());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -53,7 +64,33 @@ public class ShopStaffServiceImpl extends ServiceImpl<ShopStaffMapper, ShopStaff
|
||||
|
||||
sysUserService.updateSysUserPwd(shopStaff.getId(), shopStaffEditDTO.getAccountPwd());
|
||||
BeanUtil.copyProperties(shopStaffEditDTO, shopStaff);
|
||||
return updateById(shopStaff);
|
||||
updateById(shopStaff);
|
||||
|
||||
if (!shopStaffEditDTO.getShopPermissionIds().isEmpty()) {
|
||||
shopStaffPermissionService.updateChain().eq(ShopStaffPermission::getStaffId, shopStaff.getId())
|
||||
.eq(ShopStaffPermission::getShopId, StpKit.USER.getShopId()).eq(ShopStaffPermission::getUserId, shopStaff.getId()).remove();
|
||||
// 权限添加
|
||||
addPermission(shopStaff, shopStaffEditDTO.getShopPermissionIds());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void addPermission(ShopStaff shopStaff, List<Long> shopPermissionIds) {
|
||||
long count = shopPermissionService.queryChain().in(ShopPermission::getId, shopPermissionIds).count();
|
||||
if (count != shopPermissionIds.size()) {
|
||||
throw new ApiNotPrintException("存在不存在的权限");
|
||||
}
|
||||
|
||||
ArrayList<ShopStaffPermission> staffPermissions = new ArrayList<>();
|
||||
shopPermissionIds.forEach(item -> {
|
||||
ShopStaffPermission shopStaffPermission = new ShopStaffPermission();
|
||||
shopStaffPermission.setStaffId(shopStaff.getId());
|
||||
shopStaffPermission.setShopId(StpKit.USER.getShopId());
|
||||
shopStaffPermission.setUserId(shopStaff.getId());
|
||||
shopStaffPermission.setPermissionId(item);
|
||||
staffPermissions.add(shopStaffPermission);
|
||||
});
|
||||
shopStaffPermissionService.saveBatch(staffPermissions);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -82,4 +119,9 @@ public class ShopStaffServiceImpl extends ServiceImpl<ShopStaffMapper, ShopStaff
|
||||
sysUserService.removeUserAndRole(shopStaff.getId());
|
||||
return removeById(shopStaff.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Long> permission(Long id) {
|
||||
return shopStaffPermissionService.getPermissionByStaffId(StpKit.USER.getShopId(), id, id).stream().map(ShopPermission::getId).toList();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.czg.account.service.UserInfoService;
|
||||
import com.czg.enums.StatusEnum;
|
||||
import com.czg.enums.UserAuthSourceEnum;
|
||||
import com.czg.exception.CzgException;
|
||||
import com.czg.sa.MyStpLogic;
|
||||
import com.czg.sa.StpKit;
|
||||
import com.czg.service.account.util.AlipayUtil;
|
||||
import com.czg.service.account.util.WechatAuthUtil;
|
||||
@@ -63,7 +64,7 @@ public class UserAuthorizationServiceImpl implements UserAuthorizationService {
|
||||
userInfo.setLastLoginTime(DateUtil.date().toLocalDateTime());
|
||||
userInfoService.saveOrUpdate(userInfo);
|
||||
// StpKit.USER.login(userInfo.getId());
|
||||
StpKit.USER.login(userInfo.getId(), null, false, false);
|
||||
StpKit.USER.login(userInfo.getId(), null, MyStpLogic.LoginType.USER, false);
|
||||
return new LoginTokenDTO(StpKit.USER.getTokenValue(), userInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.czg.service.account.mapper.ShopPermissionMapper">
|
||||
|
||||
</mapper>
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.czg.service.account.mapper.ShopStaffPermissionMapper">
|
||||
|
||||
<select id="getPermissionByStaffId" resultType="com.czg.account.entity.ShopPermission">
|
||||
select b.* from tb_shop_staff_permission as a left join tb_shop_permission as b on a.permission_id=b.id
|
||||
where a.shop_id=#{shopId} and a.staff_id=#{staffId} and a.user_id=#{userId}
|
||||
</select>
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user