phi_platform_common/pom.xml
@@ -40,6 +40,12 @@ <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>com.hx.gitee</groupId> <artifactId>platform-common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies> <build> phi_platform_common/src/main/java/com/hx/phip/common/wx/corp/WeiXinCorpMpUtil.java
New file @@ -0,0 +1,213 @@ package com.hx.phip.common.wx.corp; import com.hx.common.dao.CommonDao; import com.hx.common.service.CommonService; import com.hx.mp.util.CorpMpUtil; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.model.BaseEntity; import com.hx.phip.dao.mapper.SysParameterMapper; import com.hx.phip.model.SysParameter; import com.hx.util.StringUtils; import com.hx.util.corp.entity.WeiXinInfo; import com.platform.exception.PlatTipsException; import com.platform.resultTool.PlatformCode; import net.sf.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Map; public class WeiXinCorpMpUtil { private static Logger logger = LoggerFactory.getLogger(WeiXinCorpMpUtil.class); /** * 获取企业微信应用access_token * @param commonService * @param appId * @param appSecret * @return */ public static SysParameter getApplicationAccessToken(CommonService commonService, String appId, String appSecret) { SqlSentence ss = new SqlSentence(); Map<String, Object> map = new HashMap<String, Object>(); map.put("isDel", SysParameter.NO); map.put("paramName",appSecret); ss.sqlSentence("select * from pla_sys_parameter where isDel = #{m.isDel} and paramName = #{m.paramName}",map); SysParameter sp = commonService.selectOne(SysParameterMapper.class,ss); int needGetAgain = 1; if(sp != null) { //需要判断一下是否过期 Calendar ca = Calendar.getInstance(); long curTime = ca.getTimeInMillis(); ca.setTime(sp.getCreateTime()); ca.add(Calendar.SECOND, Integer.parseInt(sp.getParamValue1())); if(curTime < ca.getTimeInMillis()) { needGetAgain = 0; } } if(needGetAgain == 1) { //需要重新获取 JSONObject reObj = CorpMpUtil.getApplicationAccessToken(appId, appSecret); if(reObj != null) { if(reObj.optInt("errcode", -1) == 0) { if(sp == null) { sp = new SysParameter(); } sp.setCreateTime(new Date()); sp.setParamName(appSecret); sp.setParamValue(reObj.optString("access_token")); sp.setParamValue1(reObj.optInt("expires_in",7200) + ""); if(StringUtils.isEmpty(sp.getId())) { commonService.insert(SysParameterMapper.class,sp); }else{ commonService.updateAll(SysParameterMapper.class,sp); } }else{ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"获取token错误:"+reObj.optString("errmsg")); } }else{ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"获取token错误:"+reObj.optString("errmsg")); } }else{ sp = verificationToken(sp,appId, appSecret,commonService); } return sp; } /** * 获取企业微信应用access_token * @param commonDao * @param appId * @param appSecret * @return */ public static SysParameter getApplicationAccessToken(CommonDao commonDao, String appId, String appSecret) { SqlSentence ss = new SqlSentence(); Map<String, Object> map = new HashMap<String, Object>(); map.put("isDel", BaseEntity.NO); map.put("paramName",appSecret); ss.sqlSentence("select * from pla_sys_parameter where isDel = #{m.isDel} and paramName = #{m.paramName}",map); SysParameter sp = commonDao.selectOne(SysParameterMapper.class,ss); int needGetAgain = 1; if(sp != null) { //需要判断一下是否过期 Calendar ca = Calendar.getInstance(); long curTime = ca.getTimeInMillis(); ca.setTime(sp.getCreateTime()); ca.add(Calendar.SECOND, Integer.parseInt(sp.getParamValue1())); if(curTime < ca.getTimeInMillis()) { needGetAgain = 0; } } if(needGetAgain == 1) { //需要重新获取 JSONObject reObj = CorpMpUtil.getApplicationAccessToken(appId, appSecret); if(reObj != null) { if(reObj.optInt("errcode", -1) == 0) { if(sp == null) { sp = new SysParameter(); } sp.setCreateTime(new Date()); sp.setParamName(appSecret); sp.setParamValue(reObj.optString("access_token")); sp.setParamValue1(reObj.optInt("expires_in",7200) + ""); if(StringUtils.isEmpty(sp.getId())) { commonDao.insert(SysParameterMapper.class,sp); }else{ commonDao.updateAll(SysParameterMapper.class,sp); } }else{ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"获取token错误:"+reObj.optString("errmsg")); } }else{ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"获取token错误:"+reObj.optString("errmsg")); } }else{ sp = verificationToken(sp,appId, appSecret,commonDao); } return sp; } /** * 校验token并重获取token保存 * @return */ public static SysParameter verificationToken(SysParameter sp,String appId,String appSecret,CommonService commonService){ //校验TOKEN是否有效 WeiXinInfo weiXinInfo = com.hx.util.corp.CorpMpUtil.userInfo("123456",sp.getParamValue()); if(weiXinInfo.getErrcode() == 42001){ //需要重新获取 JSONObject reObj = CorpMpUtil.getApplicationAccessToken(appId, appSecret); if(reObj != null) { if(reObj.optInt("errcode", -1) == 0) { if(sp == null) { sp = new SysParameter(); } sp.setCreateTime(new Date()); sp.setParamName(appSecret); sp.setParamValue(reObj.optString("access_token")); sp.setParamValue1(reObj.optInt("expires_in",7200) + ""); if(StringUtils.isEmpty(sp.getId())) { commonService.insert(SysParameterMapper.class,sp); }else{ commonService.updateAll(SysParameterMapper.class,sp); } }else{ logger.error("获取企业应用at失败:" + reObj.toString()); } }else{ logger.error("获取企业应用at失败,没返回"); } } return sp; } /** * 校验token并重获取token保存 * @return */ public static SysParameter verificationToken(SysParameter sp,String appId,String appSecret,CommonDao commonDao){ //校验TOKEN是否有效 WeiXinInfo weiXinInfo = com.hx.util.corp.CorpMpUtil.userInfo("123456",sp.getParamValue()); if(weiXinInfo.getErrcode() == 42001){ //需要重新获取 JSONObject reObj = CorpMpUtil.getApplicationAccessToken(appId, appSecret); if(reObj != null) { if(reObj.optInt("errcode", -1) == 0) { if(sp == null) { sp = new SysParameter(); } sp.setCreateTime(new Date()); sp.setParamName(appSecret); sp.setParamValue(reObj.optString("access_token")); sp.setParamValue1(reObj.optInt("expires_in",7200) + ""); if(StringUtils.isEmpty(sp.getId())) { commonDao.insert(SysParameterMapper.class,sp); }else{ commonDao.updateAll(SysParameterMapper.class,sp); } }else{ logger.error("校验token失败:" + reObj.toString()); } }else{ logger.error("校验token失败,没返回"); } } return sp; } } phi_platform_common/src/main/java/com/hx/phip/dao/mapper/SystemParameterMapper.java
File was deleted phi_platform_model/src/main/java/com/hx/phip/model/SysParameter.java
New file @@ -0,0 +1,61 @@ package com.hx.phip.model; import com.gitee.sunchenbin.mybatis.actable.annotation.Column; import com.gitee.sunchenbin.mybatis.actable.annotation.Table; import com.gitee.sunchenbin.mybatis.actable.annotation.Unique; import com.gitee.sunchenbin.mybatis.actable.constants.MySqlTypeConstant; import com.hx.phiappt.model.BaseEntity; /** * 系统参数 */ @Table(name = "pla_sys_parameter") public class SysParameter extends BaseEntity { @Unique @Column(comment = "key", length = 64, type = MySqlTypeConstant.VARCHAR) private String paramName; @Column(comment = "值", type = MySqlTypeConstant.TEXT) private String paramValue; @Column(comment = "辅助的值") private String paramValue1; public SysParameter() { } public SysParameter(String paramName, String paramValue, String paramValue1) { this.paramName = paramName; this.paramValue = paramValue; this.paramValue1 = paramValue1; } ////////////////// public String getParamName() { return paramName; } public void setParamName(String paramName) { this.paramName = paramName; } public String getParamValue() { return paramValue; } public void setParamValue(String paramValue) { this.paramValue = paramValue; } public String getParamValue1() { return paramValue1; } public void setParamValue1(String paramValue1) { this.paramValue1 = paramValue1; } } phi_platform_user/src/main/java/com/hx/phip/AutoDomeUtil.java
@@ -4,6 +4,7 @@ import com.hx.auto.common.UrlData; import com.hx.phiappt.model.*; import com.hx.phip.model.ApplyParameter; import com.hx.phip.model.SysParameter; /** * 自动生成工具 @@ -35,7 +36,7 @@ //生成全部的xml和mapper //GeneratorUtil.generatorXmlAndMapper("com.hx.medical.model",urlData); Class<?> clas = EmployeeRoleType.class; Class<?> clas = SysParameter.class; //dao GeneratorUtil.generatorDao(clas,urlData); //mapper phi_platform_user/src/main/java/com/hx/phip/Rsa.java
@@ -7,14 +7,14 @@ public class Rsa { public static void main(String[] args) { //System.out.println("AES:"+AesUtil.aesEncryp("8d6899f042059a83531c6cfed9a6b38f")); System.out.println("AES:"+AesUtil.aesEncryp("dfa4acbdea008099231db51ed49ff454")); //配置文件数据加密 BasicTextEncryptor encryptor = new BasicTextEncryptor(); /*BasicTextEncryptor encryptor = new BasicTextEncryptor(); encryptor.setPassword(AesUtil.SECRET); String encrypted = encryptor.encrypt("pis_rw:UYWoCz#4^@w5Soj*"); System.out.println(encrypted); System.out.println(encrypted);*/ //解密 //String decrypt = encryptor.decrypt("uFESkQwpAXSxg0SUR6BOlTujZqFC3pfIi3eTfuP0Lmrd0fqxG5XC4X0ho0Sp2FEV"); //System.out.println("decrypt:"+decrypt); phi_platform_user/src/main/java/com/hx/phip/common/corp/mp/WeiXinCorpMpUtil.java
File was deleted phi_platform_user/src/main/java/com/hx/phip/controller/AccountTokenController.java
@@ -1,40 +1,30 @@ package com.hx.phip.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hx.common.BaseController; import com.hx.mp.util.CorpMpUtil; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.model.*; import com.hx.phip.common.corp.mp.WeiXinCorpMpUtil; import com.hx.phip.dao.mapper.*; import com.hx.phip.common.wx.corp.WeiXinCorpMpUtil; import com.hx.phip.entity.AccountToken; import com.hx.phip.entity.EmployeeQrLogin; import com.hx.phip.entity.EmployeeRoleLogin; import com.hx.phip.model.ApplyParameter; import com.hx.phip.model.SysParameter; import com.hx.phip.tool.ApplyParameterTool; import com.hx.redis.RedisUtil; import com.hx.util.AesUtil; import com.hx.util.DateUtil; import com.hx.util.HttpServletRequestUtil; import com.hx.util.JwtTool; import com.hx.util.StringUtils; 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.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; /** @@ -47,9 +37,9 @@ @RequestMapping("/account_token") public class AccountTokenController extends BaseController { /**企业员工-扫码登录*/ /**获取企业通讯录token*/ @RequestMapping(value = "/communi/get_token",method = RequestMethod.POST) public PlatformResult loginQr(HttpServletRequest request) { public PlatformResult communitionToken(HttpServletRequest request) { //获取返回参数 String bodydata = HttpServletRequestUtil.getBody(request); @@ -77,13 +67,96 @@ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"corpId is error"); } net.sf.json.JSONObject reObj = CorpMpUtil.getApplicationAccessToken(applyParameter.getCorpId(), AesUtil.aesDecryp(applyParameter.getCommuniSecretKey())); SysParameter sysParameter = WeiXinCorpMpUtil.getApplicationAccessToken(commonService,applyParameter.getCorpId(),AesUtil.aesDecryp(applyParameter.getCommuniSecretKey())); if(sysParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_SYSTEM,"服务错误"+ SystemCode.ERROR_GET_CORP_TOKEN); } Map<String,Object> data = new HashMap<>(); data.put("data",reObj); data.put("access_token",sysParameter.getParamValue()); data.put("create_time",DateUtil.formatDate(sysParameter.getCreateTime(),"yyyy-MM-dd HH:mm:ss")); data.put("expires_in",Integer.parseInt(sysParameter.getParamValue1())); data.put("expires_surplus",secondNum(sysParameter.getCreateTime(),Integer.parseInt(sysParameter.getParamValue1()))); return PlatformResult.success(data); } /**获取企业联系我token*/ @RequestMapping(value = "/contact/get_token",method = RequestMethod.POST) public PlatformResult contactToken(HttpServletRequest request) { //获取返回参数 String bodydata = HttpServletRequestUtil.getBody(request); if(StringUtils.isEmpty(bodydata)){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } AccountToken accountToken = null; try{ accountToken = JSONObject.parseObject(bodydata,AccountToken.class); }catch (Exception e){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } if(accountToken == null){ throw new PlatTipsException(PlatformCode.ERROR_BODY_DATA,"数据格式错误"); } if (StringUtils.isEmpty(accountToken.getCorpId())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"corpId is required"); } if (StringUtils.isEmpty(accountToken.getMpId())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"mpId is required"); } //获取应用数据 ApplyParameter applyParameter = ApplyParameterTool.getApplyParameter(accountToken.getCorpId(),accountToken.getMpId(),commonService); if(applyParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"corpId is error"); } 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); } Map<String,Object> data = new HashMap<>(); data.put("access_token",sysParameter.getParamValue()); data.put("create_time",DateUtil.formatDate(sysParameter.getCreateTime(),"yyyy-MM-dd HH:mm:ss")); data.put("expires_in",Integer.parseInt(sysParameter.getParamValue1())); data.put("expires_surplus",secondNum(sysParameter.getCreateTime(),Integer.parseInt(sysParameter.getParamValue1()))); return PlatformResult.success(data); } /** * token剩余时间 * @param createTime 获取时间 * @param second 有效时间(秒) * @return */ public static long secondNum(Date createTime, int second) { //需要判断一下是否过期 Date newDate = new Date(); long secondNum = (newDate.getTime() - createTime.getTime())/1000; secondNum = second - secondNum; if(secondNum < 0){ secondNum = 0; } return secondNum; } public static void main(String[] args) { /*net.sf.json.JSONObject data = CorpMpUtil.getApplicationAccessToken("wx23a7c266dcd048aa", "Ged7KSLS2d-0y-aCI0LzIMnILbFA6M5HXBcOwV4fBIA"); System.out.println("应用token:"+data.toString()); WeiXinInfo weiXinInfo = com.hx.util.corp.CorpMpUtil.userInfo("123456",data.optString("access_token")); System.out.println("应用weiXinInfo:"+weiXinInfo.toString()); data = CorpMpUtil.getApplicationAccessToken("wx23a7c266dcd048aa", "uwr3G0y_IaB0MtrOqFYdROxf457-Gvp9s9rXexyutho"); System.out.println("通讯录token:"+data.toString()); weiXinInfo = com.hx.util.corp.CorpMpUtil.userInfo("123456",data.optString("access_token")); System.out.println("通讯录weiXinInfo:"+weiXinInfo.toString());*/ } } phi_platform_user/src/main/java/com/hx/phip/controller/EmployeeLoginController.java
@@ -5,11 +5,12 @@ import com.hx.common.BaseController; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.model.*; import com.hx.phip.common.corp.mp.WeiXinCorpMpUtil; import com.hx.phip.common.wx.corp.WeiXinCorpMpUtil; import com.hx.phip.dao.mapper.*; import com.hx.phip.entity.EmployeeQrLogin; import com.hx.phip.entity.EmployeeRoleLogin; import com.hx.phip.model.ApplyParameter; import com.hx.phip.model.SysParameter; import com.hx.phip.tool.ApplyParameterTool; import com.hx.redis.RedisUtil; import com.hx.util.AesUtil; @@ -94,13 +95,13 @@ } // 解析code String accessToken = WeiXinCorpMpUtil.getApplicationAccessToken(commonService,applyParameter.getCorpId(), AesUtil.aesDecryp(applyParameter.getApplySecretKey())); if(StringUtils.isEmpty(accessToken)){ 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); } //net.sf.json.JSONObject json = WeiXinCorpMpUtil.getLoginUserInfo(accessToken, employeeQrLogin.getCode()); PersonnelQr personnelQr = PersonnelTool.qrLoginInfo(accessToken,employeeQrLogin.getCode()); PersonnelQr personnelQr = PersonnelTool.qrLoginInfo(sysParameter.getParamValue(),employeeQrLogin.getCode()); logger.info("扫码登录:"+personnelQr.toString()); @@ -118,7 +119,7 @@ } // 查询企业微信员工是否离职 WeiXinInfo weiXinInfo = CorpMpUtil.userInfo(userId, accessToken); WeiXinInfo weiXinInfo = CorpMpUtil.userInfo(userId, sysParameter.getParamValue()); if (weiXinInfo == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该员工不存在企业微信"); } @@ -212,10 +213,14 @@ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"corpId or mpId is error"); } String accessToken = WeiXinCorpMpUtil.getApplicationAccessToken(commonService,applyParameter.getCorpId(),AesUtil.aesDecryp(applyParameter.getApplySecretKey())); 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(accessToken,employeeQrLogin.getCode()); 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", ""); @@ -228,7 +233,7 @@ } // 查询企业微信员工是否离职 WeiXinInfo weiXinInfo = CorpMpUtil.userInfo(userId, accessToken); WeiXinInfo weiXinInfo = CorpMpUtil.userInfo(userId, sysParameter.getParamValue()); if (weiXinInfo == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"该员工不存在企业微信"); } @@ -334,10 +339,9 @@ arrAuth.add(objAuth); } JSONObject data = new JSONObject(); data.put("loginToken", JwtTool.createJWT(employeeRole.getRoleTypeId(), null, ttlMillis)); data.put("id", employeeRole.getRoleTypeId()); data.put("id", employeeRole.getId()); data.put("uniqueStr", employeeRole.getRoleUniqueStr()); data.put("roleName", employeeRole.getRoleName()); data.put("shopId", employeeRole.getShopId()); phi_platform_user/src/main/java/com/hx/phip/controller/RoleController.java
@@ -1,29 +1,11 @@ package com.hx.phip.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hx.common.BaseController; import com.hx.mybatisTool.SqlSentence; import com.hx.phiappt.model.*; import com.hx.phip.common.corp.mp.WeiXinCorpMpUtil; import com.hx.phip.dao.mapper.*; import com.hx.phip.entity.EmployeeQrLogin; import com.hx.phip.entity.EmployeeRoleLogin; import com.hx.phip.model.ApplyParameter; import com.hx.phip.tool.ApplyParameterTool; import com.hx.phiappt.model.EmployeeRoleType; import com.hx.phip.dao.mapper.EmployeeRoleTypeMapper; 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.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; phi_platform_user/src/main/java/com/hx/phip/service/SystemParameterService.java
File was deleted phi_platform_user/src/main/java/com/hx/phip/service/impl/SystemParameterServiceImpl.java
File was deleted