package com.crm.auth.controller; import cn.hutool.core.util.StrUtil; import com.crm.auth.domain.dto.ModuleScopeDTO; import com.crm.auth.domain.dto.RoleDTO; import com.crm.auth.domain.entity.SysRole; import com.crm.auth.domain.param.RoleParam; import com.crm.auth.service.ISysRoleService; import com.crm.base.domain.result.PageResult; import com.crm.base.domain.result.Result; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import io.swagger.v3.oas.annotations.Operation; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.stream.Collectors; /** * 瑙掕壊绠$悊绔帴鍙o紙ADR-0012锛? *

閬靛惊鍏ㄥ眬鎺ュ彛濂戠害锛氶潪涓ユ牸 RESTful锛屽啓鎿嶄綔 POST + 鍔ㄤ綔鍚庣紑锛岃〃鍗曞瓧娈垫敹鍙傦紙ADR-0017锛? *

鏉冮檺鎺у埗锛氱函 hasAuthority锛屾潈闄愮爜鐢辨暟鎹垵濮嬪寲鍣ㄧ瀛愬寲鍒版潈闄愯祫婧愭爲

*

鍒嗘ā鍧楁敼閫犲悗锛?7 鍐崇瓥锛夛細saveOrUpdate 鐢?moduleScopes JSON 瀛楃涓茶〃鍗曞瓧娈垫浛浠?dataScope锛? * detail 杩斿洖 moduleScopes 鏁扮粍锛沺age 鍘绘帀 dataScope

*/ @Slf4j @RestController @RequestMapping("/api/roles") @RequiredArgsConstructor public class RoleController { private final ISysRoleService sysRoleService; private final ObjectMapper objectMapper; @PostMapping("/page") @PreAuthorize("hasAuthority('crm:role:list')") @Operation(summary = "鍒嗛〉鏌ヨ瑙掕壊", tags = {"绯荤粺绠$悊/鏉冮檺绠$悊/瑙掕壊绠$悊"}) public Result> page( @RequestParam(defaultValue = "1") Integer current, @RequestParam(defaultValue = "10") Integer size, @RequestParam(required = false) String keyword) { RoleParam param = new RoleParam(); param.setCurrent(current); param.setSize(size); param.setKeyword(keyword); return Result.success(sysRoleService.pageRoles(param)); } @PostMapping("/saveOrUpdate") @PreAuthorize("hasAuthority('crm:role:save')") @Operation(summary = "鏂板鎴栫紪杈戣鑹?, tags = {"绯荤粺绠$悊/鏉冮檺绠$悊/瑙掕壊绠$悊"}) public Result saveOrUpdate( @RequestParam(required = false) Long id, @RequestParam String roleName, @RequestParam String roleCode, @RequestParam(required = false) String moduleScopes, @RequestParam(defaultValue = "0") Integer sort, @RequestParam(required = false) String remark) { SysRole role = new SysRole(); role.setId(id); role.setRoleName(roleName); role.setRoleCode(roleCode); role.setSort(sort); role.setRemark(remark); // 瑙f瀽 moduleScopes JSON 瀛楃涓? List scopes = parseModuleScopes(moduleScopes); // 鍘熷瓙浜嬪姟锛氳鑹蹭繚瀛?+ 鑼冨洿鍏ㄩ噺鏇挎崲鍚屾彁浜ゅ悓鍥炴粴 sysRoleService.saveRoleWithScopes(role, scopes); return Result.success(); } @GetMapping("/detail") @PreAuthorize("hasAuthority('crm:role:detail')") @Operation(summary = "瑙掕壊璇︽儏锛堝惈鎺堟潈璧勬簮闆嗗悎 + 姣忔ā鍧楁。浣嶏級", tags = {"绯荤粺绠$悊/鏉冮檺绠$悊/瑙掕壊绠$悊"}) public Result detail(@RequestParam Long roleId) { return Result.success(sysRoleService.getRoleDetail(roleId)); } @PostMapping("/assign-resources") @PreAuthorize("hasAuthority('crm:role:assign')") @Operation(summary = "鍒嗛厤瑙掕壊鏉冮檺璧勬簮锛堢鍏堣ˉ鍏?鍏ㄩ噺鏇挎崲锛?, tags = {"绯荤粺绠$悊/鏉冮檺绠$悊/瑙掕壊绠$悊"}) public Result assignResources( @RequestParam Long roleId, @RequestParam String resourceIds) { List menuIdList = StrUtil.isBlank(resourceIds) ? Collections.emptyList() : Arrays.stream(resourceIds.split(",")) .filter(StrUtil::isNotBlank) .map(idStr -> Long.valueOf(idStr.trim())) .collect(Collectors.toList()); sysRoleService.assignResources(roleId, menuIdList); return Result.success(); } @PostMapping("/delete") @PreAuthorize("hasAuthority('crm:role:delete')") @Operation(summary = "鍒犻櫎瑙掕壊锛堢骇鑱旀竻鐞嗗叧鑱旇〃锛?, tags = {"绯荤粺绠$悊/鏉冮檺绠$悊/瑙掕壊绠$悊"}) public Result delete(@RequestParam Long id) { sysRoleService.deleteRoleCascade(id); return Result.success(); } private List parseModuleScopes(String moduleScopes) { if (StrUtil.isBlank(moduleScopes)) { return Collections.emptyList(); } try { return objectMapper.readValue(moduleScopes, new TypeReference<>() {}); } catch (Exception e) { log.warn("moduleScopes JSON 瑙f瀽澶辫触锛歿}", e.getMessage()); throw new IllegalArgumentException("moduleScopes 鏍煎紡涓嶅悎娉曪紝闇€涓?JSON 鏁扮粍锛歔{\"moduleCode\":\"...\",\"dataScope\":1}]"); } } }