package com.hx.phip.controller.login; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hx.common.BaseController; import com.hx.mp.util.MPWeixinBaseUtil; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.model.*; import com.hx.phip.common.wx.corp.WeiXinCorpMpUtil; import com.hx.phip.dao.mapper.*; import com.hx.phip.entity.employee.EmployeeQrLogin; import com.hx.phip.entity.employee.EmployeeRoleLogin; import com.hx.phip.model.ApplyParameter; import com.hx.phip.model.SysParameter; import com.hx.phip.service.EmployeeBindService; import com.hx.phip.tool.ApplyParameterTool; import com.hx.redis.RedisUtil; import com.hx.util.AesUtil; import com.hx.util.HttpServletRequestUtil; import com.hx.util.JwtTool; import com.hx.util.StringUtils; import com.hx.util.corp.CorpMpUtil; import com.hx.util.corp.entity.OpenIdAUserId; import com.hx.util.corp.entity.WeiXinInfo; import com.platform.exception.PlatTipsException; import com.platform.resultTool.PlatformCode; import com.platform.resultTool.PlatformResult; import com.platform.resultTool.SystemCode; import com.platform.util.corp.PersonnelTool; import com.platform.util.corp.entity.PersonnelQr; import org.apache.catalina.servlet4preview.http.HttpServletRequest; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 公共平台登录接口 * * @USER: wangrenhuang * @DATE: 2021/10/14 **/ @RestController @RequestMapping("/employee") public class EmployeeLoginController extends BaseController { //log4j日志 private static Logger logger = LoggerFactory.getLogger(EmployeeLoginController.class.getName()); @Resource private RedisUtil redisUtil; @Resource private EmployeeBindService employeeBindService; /**有效时间(秒)*/ private static final long ttlMillis = 42300; /**企业员工-扫码登录 */ @RequestMapping(value = "/login/qr",method = RequestMethod.POST) public PlatformResult loginQr(HttpServletRequest request) { //获取返回参数 String bodydata = HttpServletRequestUtil.getBody(request); if(StringUtils.isEmpty(bodydata)){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } EmployeeQrLogin employeeQrLogin = null; try{ employeeQrLogin = JSONObject.parseObject(bodydata,EmployeeQrLogin.class); }catch (Exception e){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } if(employeeQrLogin == null){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } if (StringUtils.isEmpty(employeeQrLogin.getCode())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"code is required"); } if (StringUtils.isEmpty(employeeQrLogin.getCorpId())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"corpId is required"); } if (StringUtils.isEmpty(employeeQrLogin.getMpId())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"mpId is required"); } //获取应用数据 ApplyParameter applyParameter = ApplyParameterTool.getApplyParameter(employeeQrLogin.getCorpId(),employeeQrLogin.getMpId(),commonService); if(applyParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"corpId or mpId is error"); } //获取token SysParameter sysParameter = WeiXinCorpMpUtil.getApplicationAccessToken(commonService,applyParameter.getCorpId(), AesUtil.aesDecryp(applyParameter.getApplySecretKey())); if(sysParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_SYSTEM,"服务错误"+ SystemCode.ERROR_GET_CORP_TOKEN); } //解析code PersonnelQr personnelQr = PersonnelTool.qrLoginInfo(sysParameter.getParamValue(),employeeQrLogin.getCode()); logger.info("扫码登录:"+personnelQr.toString()); if(personnelQr == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"人员信息不存在"); } if(!"0".equals(personnelQr.getErrcode())){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"人员信息信息异常"); } String userId = personnelQr.getUserId(); if (StringUtils.isEmpty(userId)) { throw new PlatTipsException(PlatformCode.ERROR_TIPS,"人员没有权限登录"); } // 查询企业微信员工是否离职 WeiXinInfo weiXinInfo = CorpMpUtil.userInfo(userId, sysParameter.getParamValue()); if (weiXinInfo == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该员工不存在企业微信"); } if (1 != weiXinInfo.getStatus()){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该员工在企业微信中已离职"); } SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); sqlSentence.setM(values); values.put("userId", userId); sqlSentence.setSqlSentence(" select * from employee where userId = #{m.userId} and isDel = 0 "); List employees = commonService.selectList(EmployeeMapper.class,sqlSentence); if(employees.size() > 1){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该人员信息存在多个"); }else if(employees.size() <= 0){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该人员不存在"); } Employee employee = employees.get(0); if(employee.getIsJob() != Employee.YES){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该人员已离职"); } values.clear(); values.put("employeeId",employee.getId()); values.put("isDel",EmployeeRole.NO); sqlSentence.setSqlSentence(" select o.id,ert.id AS roleTypeId,ert.name,ert.uniqueStr,s.id AS shopId,s.name AS shopName from employee_role o JOIN employee_role_type ert ON ert.isDel = 0" + " AND ert.id = o.roleTypeId LEFT JOIN shop s ON s.id = o.shopId WHERE o.employeeId = #{m.employeeId} AND o.isDel = #{m.isDel}"); List> roleList = commonService.selectListMap(EmployeeRoleTypeMapper.class,sqlSentence); Map data = new HashMap<>(); data.put("roleList", roleList); data.put("preId", employee.getId()); data.put("cnName", employee.getCnName()); data.put("enName", employee.getEnName()); data.put("tel", employee.getTel()); data.put("email", employee.getEmail()); data.put("imgUrl", employee.getImgUrl()); data.put("gender", employee.getGender()); data.put("loginToken", JwtTool.createJWT(employee.getId(), null, ttlMillis)); JSONObject redisObj = new JSONObject(); redisObj.put("id",employee.getId()); redisObj.put("cnName",employee.getCnName()); redisObj.put("enName",employee.getEnName()); redisObj.put("userId",employee.getUserId()); redisUtil.set(employee.getId(),redisObj,ttlMillis); return PlatformResult.success(data); } /**企业员工-网页登录(企业小程序) * @param request * @return */ @RequestMapping(value = "/login/webpage",method = RequestMethod.POST) public PlatformResult loginWebpage(HttpServletRequest request) { //获取返回参数 String bodydata = HttpServletRequestUtil.getBody(request); if(StringUtils.isEmpty(bodydata)){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } EmployeeQrLogin employeeQrLogin = null; try{ employeeQrLogin = JSONObject.parseObject(bodydata,EmployeeQrLogin.class); }catch (Exception e){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } if(employeeQrLogin == null){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } if (StringUtils.isEmpty(employeeQrLogin.getCode())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"code is required"); } if (StringUtils.isEmpty(employeeQrLogin.getCorpId())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"corpId is required"); } if (StringUtils.isEmpty(employeeQrLogin.getMpId())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"mpId is required"); } //获取应用数据 ApplyParameter applyParameter = ApplyParameterTool.getApplyParameter(employeeQrLogin.getCorpId(),employeeQrLogin.getMpId(),commonService); if(applyParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"corpId or mpId is error"); } //获取token SysParameter sysParameter = WeiXinCorpMpUtil.getApplicationAccessToken(commonService,applyParameter.getCorpId(),AesUtil.aesDecryp(applyParameter.getApplySecretKey())); if(sysParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_SYSTEM,"服务错误"+ SystemCode.ERROR_GET_CORP_TOKEN); } //获取userId net.sf.json.JSONObject oauthObj = com.hx.mp.util.CorpMpUtil.code2Session(sysParameter.getParamValue(),employeeQrLogin.getCode()); //String errcode = oauthObj.optString("errcode", "");// 有错误的时候才有 // String corpId = oauthObj.optString("corpid", "");// 有错误的时候才有 String userId = oauthObj.optString("userid", ""); // String sessionKey = oauthObj.optString("session_key"); logger.info("网页登录:"+oauthObj.toString()); if (StringUtils.isEmpty(userId)) { throw new PlatTipsException(PlatformCode.ERROR_TIPS,"人员没有权限登录"); } // 查询企业微信员工是否离职 WeiXinInfo weiXinInfo = CorpMpUtil.userInfo(userId, sysParameter.getParamValue()); if (weiXinInfo == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该员工不存在企业微信"); } if (1 != weiXinInfo.getStatus()){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该员工在企业微信中已离职"); } SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); sqlSentence.setM(values); values.put("userId", userId); sqlSentence.setSqlSentence(" select * from employee where userId = #{m.userId} and isDel = 0 "); List employees = commonService.selectList(EmployeeMapper.class,sqlSentence); if(employees.size() > 1){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该人员信息存在多个"); }else if(employees.size() <= 0){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该人员不存在"); } Employee employee = employees.get(0); if(employee.getIsJob() != Employee.YES){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该人员已离职"); } values.clear(); values.put("employeeId",employee.getId()); values.put("isDel",EmployeeRole.NO); sqlSentence.setSqlSentence(" select o.id,ert.id AS roleTypeId,ert.name,ert.uniqueStr,s.id AS shopId,s.name AS shopName from employee_role o JOIN employee_role_type ert ON ert.isDel = 0" + " AND ert.id = o.roleTypeId LEFT JOIN shop s ON s.id = o.shopId WHERE o.employeeId = #{m.employeeId} AND o.isDel = #{m.isDel}"); List> roleList = commonService.selectListMap(EmployeeRoleTypeMapper.class,sqlSentence); Map data = new HashMap<>(); data.put("roleList", roleList); data.put("preId", employee.getId()); data.put("cnName", employee.getCnName()); data.put("enName", employee.getEnName()); data.put("tel", employee.getTel()); data.put("email", employee.getEmail()); data.put("imgUrl", employee.getImgUrl()); data.put("gender", employee.getGender()); data.put("loginToken", JwtTool.createJWT(employee.getId(), null, ttlMillis)); JSONObject redisObj = new JSONObject(); redisObj.put("id",employee.getId()); redisObj.put("cnName",employee.getCnName()); redisObj.put("enName",employee.getEnName()); redisObj.put("userId",employee.getUserId()); redisUtil.set(employee.getId(),redisObj,ttlMillis); return PlatformResult.success(data); } /**企业员工-网页登录(普通小程序) * 普通小程序登录 * @param request * @return */ @RequestMapping(value = "/login/mp/webpage",method = RequestMethod.POST) public PlatformResult loginMpWebpage(HttpServletRequest request) { //获取返回参数 String bodydata = HttpServletRequestUtil.getBody(request); if(StringUtils.isEmpty(bodydata)){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } EmployeeQrLogin employeeQrLogin = null; try{ employeeQrLogin = JSONObject.parseObject(bodydata,EmployeeQrLogin.class); }catch (Exception e){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } if(employeeQrLogin == null){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } if (StringUtils.isEmpty(employeeQrLogin.getCode())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"code is required"); } if (StringUtils.isEmpty(employeeQrLogin.getCorpId())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"corpId is required"); } if (StringUtils.isEmpty(employeeQrLogin.getMpId())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"mpId is required"); } //获取应用数据 ApplyParameter applyParameter = ApplyParameterTool.getApplyParameter(employeeQrLogin.getCorpId(),employeeQrLogin.getMpId(),commonService); if(applyParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"corpId or mpId is error"); } //获取openId net.sf.json.JSONObject oauthObj = MPWeixinBaseUtil.getJscode2session(applyParameter.getMpAppId(),AesUtil.aesDecryp(applyParameter.getMpSecretKey()),employeeQrLogin.getCode()); String errcode = oauthObj.optString("errcode", "");// 有错误的时候才有 String openId = oauthObj.optString("openid", ""); //String session_key = oauthObj.optString("session_key"); String unionid = oauthObj.optString("unionid", ""); if(StringUtils.noNull(errcode)){ logger.error("企业员工-网页登录(普通小程序):"+oauthObj.toString()); throw new PlatTipsException(PlatformCode.ERROR_TIPS,"解析code获取用户信息失败"); } //获取token SysParameter sysParameter = WeiXinCorpMpUtil.getApplicationAccessToken(commonService,applyParameter.getCorpId(),AesUtil.aesDecryp(applyParameter.getApplySecretKey())); if(sysParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_SYSTEM,"服务错误"+ SystemCode.ERROR_GET_CORP_TOKEN); } //openId转userId OpenIdAUserId openIdAUserId = CorpMpUtil.openIdToUserId(openId,sysParameter.getParamValue()); if(openIdAUserId.getErrcode() != 0){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,openIdAUserId.getErrmsg()); } if (StringUtils.isEmpty(openIdAUserId.getUserid())) { throw new PlatTipsException(PlatformCode.ERROR_TIPS,"人员没有权限登录"); } // 查询企业微信员工是否离职 WeiXinInfo weiXinInfo = CorpMpUtil.userInfo(openIdAUserId.getUserid(), sysParameter.getParamValue()); if (weiXinInfo == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该员工不存在企业微信"); } if (1 != weiXinInfo.getStatus()){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该员工在企业微信中已离职"); } SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); sqlSentence.setM(values); values.put("userId", openIdAUserId.getUserid()); sqlSentence.setSqlSentence(" select * from employee where userId = #{m.userId} and isDel = 0 "); List employees = commonService.selectList(EmployeeMapper.class,sqlSentence); if(employees.size() > 1){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该人员信息存在多个"); }else if(employees.size() <= 0){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该人员不存在"); } Employee employee = employees.get(0); if(employee.getIsJob() != Employee.YES){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该人员已离职"); } values.clear(); values.put("employeeId",employee.getId()); values.put("isDel",EmployeeRole.NO); sqlSentence.setSqlSentence(" select o.id,ert.id AS roleTypeId,ert.name,ert.uniqueStr,s.id AS shopId,s.name AS shopName from employee_role o JOIN employee_role_type ert ON ert.isDel = 0" + " AND ert.id = o.roleTypeId LEFT JOIN shop s ON s.id = o.shopId WHERE o.employeeId = #{m.employeeId} AND o.isDel = #{m.isDel}"); List> roleList = commonService.selectListMap(EmployeeRoleTypeMapper.class,sqlSentence); employeeBindService.addBind(applyParameter,employee,openId,unionid); Map data = new HashMap<>(); data.put("roleList", roleList); data.put("preId", employee.getId()); data.put("cnName", employee.getCnName()); data.put("enName", employee.getEnName()); data.put("tel", employee.getTel()); data.put("email", employee.getEmail()); data.put("imgUrl", employee.getImgUrl()); data.put("gender", employee.getGender()); data.put("loginToken", JwtTool.createJWT(employee.getId(), null, ttlMillis)); JSONObject redisObj = new JSONObject(); redisObj.put("id",employee.getId()); redisObj.put("cnName",employee.getCnName()); redisObj.put("enName",employee.getEnName()); redisObj.put("userId",employee.getUserId()); redisUtil.set(employee.getId(),redisObj,ttlMillis); return PlatformResult.success(data); } /**选择登录角色*/ @RequestMapping(value ="/login/role",method = RequestMethod.POST) public PlatformResult selectLoginRole(HttpServletRequest request) { String bodyData = HttpServletRequestUtil.getBody(request); if(StringUtils.isEmpty(bodyData)){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } EmployeeRoleLogin employeeRoleLogin = null; try { employeeRoleLogin = JSONObject.parseObject(bodyData,EmployeeRoleLogin.class); }catch (Exception e){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } if(employeeRoleLogin == null){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } if(StringUtils.isEmpty(employeeRoleLogin.getRoleId())){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"Role ID is required"); } if(StringUtils.isEmpty(employeeRoleLogin.getPreId())){ throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"Pre ID is required"); } SqlSentence sqlSentence = new SqlSentence(); Map values = new HashMap<>(); values.put("roleId", employeeRoleLogin.getRoleId()); values.put("employeeId", employeeRoleLogin.getPreId()); sqlSentence.sqlSentence(" SELECT o.*,ert.name AS roleName FROM employee_role o JOIN employee_role_type ert ON ert.isDel = 0" + " AND ert.id = o.roleTypeId WHERE o.id = #{m.roleId} AND o.isDel = 0 AND o.employeeId = #{m.employeeId}",values); EmployeeRole employeeRole = commonService.selectOne(EmployeeRoleMapper.class,sqlSentence); if(employeeRole == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"角色不存在"); } EmployeeRoleType employeeRoleType = commonService.selectOneByKey(EmployeeRoleTypeMapper.class,employeeRole.getRoleTypeId()); if(employeeRoleType == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"角色信息有误!"); } if(employeeRoleType.getIsUp() == EmployeeRoleType.NO){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"角色权限!"); } values.clear(); values.put("roleTypeId",employeeRole.getRoleTypeId()); values.put("isDel", EmployeeRoleAdminAuthority.NO); sqlSentence.sqlSentence("SELECT saa.tag FROM emloyee_role_admin_authority o JOIN sys_admin_authority saa ON saa.isDel = #{m.isDel} AND saa.id = o.sysAdAuId WHERE o.roleTypeId = #{m.roleTypeId} AND o.isDel = #{m.isDel}",values); List authList = commonService.selectList(SysAdminAuthorityMapper.class,sqlSentence); JSONArray arrAuth = new JSONArray(); for(SysAdminAuthority sysAdminAuthority:authList){ JSONObject objAuth = new JSONObject(); objAuth.put("tag",sysAdminAuthority.getTag()); arrAuth.add(objAuth); } JSONObject data = new JSONObject(); data.put("loginToken", JwtTool.createJWT(employeeRole.getRoleTypeId(), null, ttlMillis)); data.put("id", employeeRole.getId()); data.put("preId", employeeRole.getEmployeeId()); data.put("uniqueStr", employeeRole.getRoleUniqueStr()); data.put("roleName", employeeRole.getRoleName()); data.put("shopId", employeeRole.getShopId()); Shop shop = commonService.selectOneByKey(ShopMapper.class,employeeRole.getShopId()); if(shop != null){ data.put("shopName",shop.getName()); } data.put("authList", arrAuth); JSONObject redisObj = new JSONObject(); redisObj.put("id",employeeRole.getId()); redisObj.put("roleTypeId",employeeRole.getRoleTypeId()); redisObj.put("roleName",employeeRole.getRoleName()); redisObj.put("uniqueStr",employeeRole.getRoleUniqueStr()); redisObj.put("preId",employeeRole.getEmployeeId()); redisObj.put("arrAuth",arrAuth); redisObj.put("shopId",employeeRole.getShopId()); if(shop != null){ redisObj.put("shopName", shop.getName()); } redisUtil.set(employeeRole.getId(),redisObj,ttlMillis); return PlatformResult.success(data); } @RequestMapping(value ="/test",method = RequestMethod.POST) public PlatformResult test(HttpServletRequest request){ //redisUtil.set("222","44444444444"); System.out.println(redisUtil.get("222")); return PlatformResult.success("2222222"); } }