package com.epmet.controller; import cn.hutool.core.bean.BeanUtil; import com.epmet.commons.tools.constant.AppClientConstant; import com.epmet.commons.tools.constant.ServiceConstant; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.feign.ResultDataResolver; import com.epmet.commons.tools.redis.RedisKeys; import com.epmet.commons.tools.redis.RedisUtils; import com.epmet.commons.tools.security.password.PasswordUtils; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.CustomerStaffDTO; import com.epmet.dto.form.LoginByPassWordFormDTO; import com.epmet.dto.form.RootOrgListByStaffIdFormDTO; import com.epmet.dto.result.StaffOrgsResultDTO; import com.epmet.dto.result.UserTokenResultDTO; import com.epmet.feign.EpmetUserFeignClient; import com.epmet.feign.GovOrgOpenFeignClient; import com.epmet.redis.CaptchaRedis; import com.epmet.redis.IcLoginTicketCacheBean; import com.epmet.service.IcLoginService; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @RestController @RequestMapping("ic") public class IcLoinController implements ResultDataResolver { public static final long IC_LOGIN_TICKET_EXPIRE_SECONDS = 2 * 60l; @Autowired private EpmetUserFeignClient epmetUserFeignClient; @Autowired private GovOrgOpenFeignClient govOrgOpenFeignClient; @Autowired private CaptchaRedis captchaRedis; @Autowired private IcLoginService icLoginService; @Autowired private RedisUtils redisUtils; /** * @description 基层治理赋能平台-根据手机号密码获取组织列表 * * @param input * @return * @author wxz * @date 2021.10.25 09:56:33 */ @PostMapping("getmyorgsbypassword") public Result> getMyOrgsByPassword(@RequestBody LoginByPassWordFormDTO input) { ValidatorUtils.validateEntity(input, LoginByPassWordFormDTO.IcGetOrgsByPwdGroup.class); String captcha = input.getCaptcha(); String mobile = input.getMobile(); String password = input.getPassword(); String uuid = input.getUuid(); // 图片验证码 String captchaInCache = captchaRedis.getIcLoginCaptcha(uuid); if (StringUtils.isBlank(captchaInCache) || !captcha.equals(captchaInCache)) { throw new RenException(EpmetErrorCode.ERR10019.getCode()); } // 获取用户信息 Result> staffResult = epmetUserFeignClient.checkCustomerStaff(mobile); List staffList = getResultDataOrThrowsException(staffResult, ServiceConstant.EPMET_USER_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), "【基层治理平台登录】获取用户信息失败", null); if (CollectionUtils.isEmpty(staffList)) { throw new RenException(EpmetErrorCode.ERR10003.getCode()); } CustomerStaffDTO staffInfo = staffList.get(0); if (!PasswordUtils.matches(password, staffInfo.getPassword())) { throw new RenException(EpmetErrorCode.ERR10004.getCode()); } String staffId = staffInfo.getUserId(); // 查询跟组织列表 RootOrgListByStaffIdFormDTO orgListForm = new RootOrgListByStaffIdFormDTO(); orgListForm.setStaffId(staffId); Result> orgListResult = govOrgOpenFeignClient.getStaffOrgListByStaffId(orgListForm); List orgs = getResultDataOrThrowsException(orgListResult, ServiceConstant.GOV_ORG_SERVER, EpmetErrorCode.SERVER_ERROR.getCode(), "【基层治理平台登录】根据staffId查询所属客户跟组织列表失败", null); // 生成登录票据 String ticket = UUID.randomUUID().toString().replace("-", ""); IcLoginTicketCacheBean ticketCacheBean = new IcLoginTicketCacheBean(); ticketCacheBean.setMobile(mobile); ticketCacheBean.setStaffId(staffId); cacheTicket(ticket, ticketCacheBean); HashMap resultMap = new HashMap<>(); resultMap.put("staffId", staffId); resultMap.put("ticket", ticket); resultMap.put("orgs", orgs); return new Result>().ok(resultMap); } /** * @description IC登录 * * @param input * @return * @author wxz * @date 2021.10.25 21:14:22 */ @PostMapping("login") public Result login(@RequestBody LoginByPassWordFormDTO input) { ValidatorUtils.validateEntity(input, LoginByPassWordFormDTO.IcLoginGroup.class); String ticket = input.getTicket(); String orgId = input.getRootAgencyId(); String staffId = input.getStaffId(); // ticket校验 IcLoginTicketCacheBean ticketCache = getTicketCache(ticket); if (ticketCache == null || !ticketCache.getStaffId().equals(staffId)) { // ticket&userId不对应 throw new RenException(EpmetErrorCode.ERR10008.getCode()); } UserTokenResultDTO tokenInfo = icLoginService.login(staffId, orgId); return new Result().ok(tokenInfo); } private void cacheTicket(String ticket, IcLoginTicketCacheBean cacheBean) { Map stringObjectMap = BeanUtil.beanToMap(cacheBean, false, true); redisUtils.hMSet(RedisKeys.loginTicket(AppClientConstant.APP_IC, ticket), stringObjectMap, IC_LOGIN_TICKET_EXPIRE_SECONDS); } /** * @description 从缓存中取出ticket,并删除 * * @param ticket * @return * @author wxz * @date 2021.10.26 08:58:27 */ private IcLoginTicketCacheBean getTicketCache(String ticket) { String key = RedisKeys.loginTicket(AppClientConstant.APP_IC, ticket); Map map = redisUtils.hGetAll(key); if (CollectionUtils.sizeIsEmpty(map)) { return null; } redisUtils.expire(key, 0); return BeanUtil.mapToBean(map, IcLoginTicketCacheBean.class, false); } }