|  |  | 
 |  |  |  | 
 |  |  | import com.dy.common.aop.SsoVo; | 
 |  |  | import com.dy.common.multiDataSource.DataSourceContext; | 
 |  |  | import com.dy.common.util.MD5; | 
 |  |  | import com.dy.common.webUtil.BaseResponse; | 
 |  |  | import com.dy.common.webUtil.BaseResponseUtils; | 
 |  |  | import com.dy.common.webUtil.ResultCodeMsg; | 
 |  |  | import com.dy.pipIrrGlobal.pojoBa.BaUser; | 
 |  |  | import com.dy.pipIrrGlobal.util.DistrictLevel; | 
 |  |  | import com.dy.pipIrrGlobal.util.Org; | 
 |  |  | import com.mysql.cj.util.StringUtils; | 
 |  |  | import io.swagger.v3.oas.annotations.Hidden; | 
 |  |  | import io.swagger.v3.oas.annotations.Operation; | 
 |  |  | import io.swagger.v3.oas.annotations.Parameter; | 
 |  |  | 
 |  |  | import org.springframework.validation.BindingResult; | 
 |  |  | import org.springframework.web.bind.annotation.*; | 
 |  |  |  | 
 |  |  | import java.util.Arrays; | 
 |  |  | import java.util.List; | 
 |  |  | import java.util.Objects; | 
 |  |  | import java.util.UUID; | 
 |  |  |  | 
 |  |  | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 客户端请求得到所有组织机构 | 
 |  |  |      * @return 所有组织机构数据 | 
 |  |  |      */ | 
 |  |  |     @Operation(summary = "所有组织机构", description = "返回所有所有组织机构数据") | 
 |  |  |     @ApiResponses(value = { | 
 |  |  |             @ApiResponse( | 
 |  |  |                     responseCode = ResultCodeMsg.RsCode.SUCCESS_CODE, | 
 |  |  |                     description = "返回所有组织机构数据(BaseResponse.content:[ { \"tag\":\"ym\", \"name\":\"元谋\" }, { \"tag\":\"片角\", \"name\":\"片角镇\" }])", | 
 |  |  |                     content = {@Content(mediaType = MediaType.APPLICATION_JSON_VALUE, | 
 |  |  |                             schema = @Schema(implementation = Org.class))} | 
 |  |  |             ) | 
 |  |  |     }) | 
 |  |  |     @GetMapping(path = "allOrg") | 
 |  |  |     public BaseResponse<List<Org>> allOrg(){ | 
 |  |  |         //List<Org> list = Arrays.asList(Org.Ym, Org.Pj) ; | 
 |  |  |         return BaseResponseUtils.buildSuccess(Org.OrgList); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 客户端请求用户登录,客户端提交Json数据 | 
 |  |  |      * @param vo 用户登录值对象 | 
 |  |  |      * @param bindingResult 输入验证 | 
 |  |  | 
 |  |  |             ) | 
 |  |  |     }) | 
 |  |  |     @PostMapping(path = "loginJson", consumes = MediaType.APPLICATION_JSON_VALUE) | 
 |  |  |     public BaseResponse<UserVo> loginJson(@Valid @RequestBody LoginVo vo,  @Parameter(hidden = true) BindingResult bindingResult) { | 
 |  |  |     public BaseResponse<UserVo> loginJson(@RequestBody @Parameter(description = "登录json数据", required = true) @Valid LoginVo vo,  @Parameter(hidden = true) BindingResult bindingResult) { | 
 |  |  |         if(bindingResult != null && bindingResult.hasErrors()){ | 
 |  |  |             return BaseResponseUtils.buildFail(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage()); | 
 |  |  |         } | 
 |  |  |         if(!vo.phone.equals("admin")){ | 
 |  |  |             if(vo.phone.length() != 11){ | 
 |  |  |                 return BaseResponseUtils.buildFail("手机号(长度不是11位)不正确"); | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         if(vo.orgTag == null || vo.orgTag.trim().length() == 0){ | 
 |  |  |             return BaseResponseUtils.buildFail("未选择组织单位"); | 
 |  |  |         } | 
 |  |  |         //把组织单位标签作为数据源名称 | 
 |  |  |         DataSourceContext.set(vo.orgTag); | 
 |  |  |  | 
 |  |  |         String uuid ; | 
 |  |  |         BaUser userPo ; | 
 |  |  |         try { | 
 |  |  |             //Boolean flag = cacheManager.getCacheNames().isEmpty() ; | 
 |  |  |             uuid = UUID.randomUUID().toString(); | 
 |  |  |             userPo = this.sv.login(uuid, vo.phone, vo.password); | 
 |  |  |             if(!StringUtils.isNullOrEmpty(vo.password)){ | 
 |  |  |                 /* | 
 |  |  |                 如果前端进行了base64加密 | 
 |  |  |                 po.password = new String(Base64.getDecoder().decode(po.password)) ; | 
 |  |  |                 */ | 
 |  |  |                 vo.password = MD5.encrypt(vo.password) ; | 
 |  |  |             } | 
 |  |  |             userPo = this.sv.loginWithMapperXml(uuid, vo.phone, vo.password); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("用户登录异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()) ; | 
 |  |  | 
 |  |  |         try { | 
 |  |  |             //Boolean flag = cacheManager.getCacheNames().isEmpty() ; | 
 |  |  |             uuid = UUID.randomUUID().toString(); | 
 |  |  |             userPo = this.sv.login(uuid, vo.phone, vo.password); | 
 |  |  |             if(!StringUtils.isNullOrEmpty(vo.password)){ | 
 |  |  |                 /* | 
 |  |  |                 如果前端进行了base64加密 | 
 |  |  |                 po.password = new String(Base64.getDecoder().decode(po.password)) ; | 
 |  |  |                 */ | 
 |  |  |                 vo.password = MD5.encrypt(vo.password) ; | 
 |  |  |             } | 
 |  |  |             userPo = this.sv.loginWithMapperXml(uuid, vo.phone, vo.password); | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("用户登录异常", e); | 
 |  |  |             return BaseResponseUtils.buildException(e.getMessage()) ; | 
 |  |  | 
 |  |  |         BaUser userPo = this.sv.getByUuid(token) ; | 
 |  |  |         SsoVo vo = new SsoVo(); | 
 |  |  |         if(userPo != null){ | 
 |  |  |             vo.dataSourceName = userPo.orgTag ; | 
 |  |  |             vo.logined = true ; | 
 |  |  |             vo.hasPower = false ;//默认是无权限 | 
 |  |  |             if(userPo.supperAdmin != null && userPo.supperAdmin == 1){ | 
 |  |  |                 vo.hasPower = true ; | 
 |  |  |             }else{ | 
 |  |  |                 if(userPo.privileges != null && userPo.privileges.size() > 0){ | 
 |  |  |                     if(privilege != null && !privilege.trim().equals("")){ | 
 |  |  |                         int intPri = Integer.parseInt(privilege) ; | 
 |  |  |                         for(Integer pri : userPo.privileges){ | 
 |  |  |                             if(pri == intPri){ | 
 |  |  |                                 vo.hasPower = true ; | 
 |  |  |                                 break ; | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                     }else{ | 
 |  |  |                         if(allPrivilege != null && allPrivilege.length > 0){ | 
 |  |  |                             int intPri ; | 
 |  |  |                             boolean ok = false ; | 
 |  |  |                             boolean allOk = true ; | 
 |  |  |                             for(String strPri : allPrivilege){ | 
 |  |  |                                 intPri = Integer.parseInt(strPri) ; | 
 |  |  |                                 for(Integer pri : userPo.privileges){ | 
 |  |  |                                     if(pri == intPri){ | 
 |  |  |                                         ok = true ; | 
 |  |  |                                         break ; | 
 |  |  |                                     } | 
 |  |  |                                 } | 
 |  |  |                                 if(!ok){ | 
 |  |  |                                     allOk = false ; | 
 |  |  |                                     break ; | 
 |  |  |                                 } | 
 |  |  |                             } | 
 |  |  |                             if(allOk){ | 
 |  |  |                                 vo.hasPower = true ; | 
 |  |  |                             } | 
 |  |  |                         }else{ | 
 |  |  |                             int intPri ; | 
 |  |  |                             if(anyPrivilege != null && anyPrivilege.length > 0){ | 
 |  |  |                                 for(String strPri : anyPrivilege){ | 
 |  |  |                                     intPri = Integer.parseInt(strPri) ; | 
 |  |  |                                     for(Integer pri : userPo.privileges){ | 
 |  |  |                                         if(pri == intPri){ | 
 |  |  |                                             vo.hasPower = true ; | 
 |  |  |                                             break ; | 
 |  |  |                                         } | 
 |  |  |                                     } | 
 |  |  |                                     if(vo.hasPower){ | 
 |  |  |                                         break ; | 
 |  |  |                                     } | 
 |  |  |                                 } | 
 |  |  |                 if(privilege.equals("-1")){ | 
 |  |  |                     //无需权限验证 | 
 |  |  |                     vo.hasPower = true ; | 
 |  |  |                 }else{ | 
 |  |  |                     if(userPo.privileges != null && userPo.privileges.size() > 0){ | 
 |  |  |                         vo.hasPower = this.hasOnePrivilege(privilege, userPo) ; | 
 |  |  |                         if(!vo.hasPower){ | 
 |  |  |                             vo.hasPower = this.hasAllPrivilege(allPrivilege, userPo) ; | 
 |  |  |                             if(!vo.hasPower){ | 
 |  |  |                                 vo.hasPower = this.hasAnyPrivilege(anyPrivilege, userPo) ; | 
 |  |  |                             } | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             vo.dataSourceName = userPo.getOrgTag() ; | 
 |  |  |         }else{ | 
 |  |  |             vo.logined = false ; | 
 |  |  |             vo.hasPower = false ; | 
 |  |  | 
 |  |  |         return vo ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 获得当前登录用户 | 
 |  |  |      * @param token 登录用户token | 
 |  |  |      * @return SsoVo | 
 |  |  |      */ | 
 |  |  |     @Hidden | 
 |  |  |     @GetMapping(path = "ssoCurUser") | 
 |  |  |     public CurUserVo ssoCurUser(String token){ | 
 |  |  |         BaUser userPo = this.sv.getByUuid(token) ; | 
 |  |  |         CurUserVo vo = new CurUserVo(); | 
 |  |  |         if(userPo != null){ | 
 |  |  |             vo.id = userPo.id ; | 
 |  |  |             vo.name = userPo.name ; | 
 |  |  |         } | 
 |  |  |         return vo ; | 
 |  |  |     } | 
 |  |  |     ///////////////////////////////////////////////////////////////// | 
 |  |  |     // | 
 |  |  |     // 以下私有方法 | 
 |  |  |     // | 
 |  |  |     ///////////////////////////////////////////////////////////////// | 
 |  |  |     /** | 
 |  |  |      * 判断登录用户是否拥有指定的一个权限 | 
 |  |  |      * @param privilege 指定的一个权限 | 
 |  |  |      * @param userPo 当前登录用户 | 
 |  |  |      * @return 是否有权限 | 
 |  |  |      */ | 
 |  |  |     private boolean hasOnePrivilege(String privilege, BaUser userPo){ | 
 |  |  |         boolean hasPrivilege  = false ; | 
 |  |  |         if (privilege != null && !privilege.trim().equals("")) { | 
 |  |  |             int intPri = Integer.parseInt(privilege); | 
 |  |  |             for (Integer pri : userPo.privileges) { | 
 |  |  |                 if (pri == intPri) { | 
 |  |  |                     hasPrivilege = true; | 
 |  |  |                     break; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return hasPrivilege ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 判断登录用户是否拥有指定的多个权限 | 
 |  |  |      * @param allPrivilege 指定的多个权限 | 
 |  |  |      * @param userPo 当前登录用户 | 
 |  |  |      * @return 是否有权限 | 
 |  |  |      */ | 
 |  |  |     private boolean hasAllPrivilege(String[] allPrivilege, BaUser userPo){ | 
 |  |  |         boolean hasPrivilege  = false ; | 
 |  |  |         if(allPrivilege != null && allPrivilege.length > 0){ | 
 |  |  |             int intPri ; | 
 |  |  |             int count = 0 ; | 
 |  |  |             for(String strPri : allPrivilege){ | 
 |  |  |                 intPri = Integer.parseInt(strPri) ; | 
 |  |  |                 for(Integer pri : userPo.privileges){ | 
 |  |  |                     if(pri == intPri){ | 
 |  |  |                         count++ ; | 
 |  |  |                         break ; | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |             if(count  == allPrivilege.length){ | 
 |  |  |                 hasPrivilege = true ; | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return hasPrivilege ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * 判断登录用户是否拥有指定的某个权限 | 
 |  |  |      * @param anyPrivilege 指定的多个权限 | 
 |  |  |      * @param userPo 当前登录用户 | 
 |  |  |      * @return 是否有权限 | 
 |  |  |      */ | 
 |  |  |     private boolean hasAnyPrivilege(String[] anyPrivilege, BaUser userPo){ | 
 |  |  |         boolean hasPrivilege  = false ; | 
 |  |  |         int intPri ; | 
 |  |  |         if(anyPrivilege != null && anyPrivilege.length > 0){ | 
 |  |  |             for(String strPri : anyPrivilege){ | 
 |  |  |                 intPri = Integer.parseInt(strPri) ; | 
 |  |  |                 for(Integer pri : userPo.privileges){ | 
 |  |  |                     if(pri == intPri){ | 
 |  |  |                         hasPrivilege = true ; | 
 |  |  |                         break ; | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |                 if(hasPrivilege){ | 
 |  |  |                     break ; | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         return hasPrivilege ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  | } |