package com.hx.phip.controller; import com.alibaba.fastjson.JSONObject; import com.hx.common.BaseController; import com.hx.phip.common.wx.corp.WeiXinCorpMpUtil; import com.hx.phip.common.wx.corp.WeiXinMpUtil; import com.hx.phip.config.CustomParameter; import com.hx.phip.entity.AccountToken; import com.hx.phip.model.ApplyParameter; import com.hx.phip.model.SysParameter; import com.hx.phip.tool.ApplyParameterTool; import com.hx.util.AesUtil; import com.hx.util.DateUtil; import com.hx.util.HttpServletRequestUtil; import com.hx.util.StringUtils; import com.platform.constants.PlatformPattern; import com.platform.exception.PlatTipsException; import com.platform.resultTool.PlatformCode; import com.platform.resultTool.PlatformResult; import com.platform.resultTool.SystemCode; 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 javax.servlet.http.HttpServletRequest; import java.util.Date; import java.util.HashMap; import java.util.Map; /** * 公共平台获取账号token * * @USER: chenjiahe * @DATE: 2021/11/01 **/ @RestController @RequestMapping("/account_token") public class MpTokenController extends BaseController { @Resource private CustomParameter customParameter; /**获取企业通讯录token*/ @RequestMapping(value = "/communi/get_token",method = RequestMethod.POST) public PlatformResult communitionToken(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(!PlatformPattern.PROD.equals(customParameter.getPlatformPattern())){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"正式环境获取TOKEN"); } //获取应用数据 ApplyParameter applyParameter = ApplyParameterTool.getCommunition(accountToken.getCorpId(),commonService); if(applyParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"corpId is error"); } 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 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",secondNum(sysParameter.getCreateTime(),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(!PlatformPattern.PROD.equals(customParameter.getPlatformPattern())){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"正式环境获取TOKEN"); } //获取应用数据 ApplyParameter applyParameter = ApplyParameterTool.getContact(accountToken.getCorpId(),commonService); if(applyParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"corpId is error"); } SysParameter sysParameter = WeiXinCorpMpUtil.getApplicationAccessToken(commonService,applyParameter.getCorpId(),AesUtil.aesDecryp(applyParameter.getContactSecretKey())); if(sysParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_SYSTEM,"服务错误"+ SystemCode.ERROR_GET_CORP_TOKEN); } Map 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",secondNum(sysParameter.getCreateTime(),Integer.parseInt(sysParameter.getParamValue1()))); data.put("expires_surplus",secondNum(sysParameter.getCreateTime(),Integer.parseInt(sysParameter.getParamValue1()))); return PlatformResult.success(data); } /**获取企业应用token*/ @RequestMapping(value = "/apply/get_token",method = RequestMethod.POST) public PlatformResult applyToken(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"); } if(!PlatformPattern.PROD.equals(customParameter.getPlatformPattern())){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"正式环境获取TOKEN"); } //获取应用数据 ApplyParameter applyParameter = ApplyParameterTool.getApplyParameter(accountToken.getCorpId(),accountToken.getMpId(),commonService); if(applyParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"corpId is error or mpId 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 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",secondNum(sysParameter.getCreateTime(),Integer.parseInt(sysParameter.getParamValue1()))); data.put("expires_surplus",secondNum(sysParameter.getCreateTime(),Integer.parseInt(sysParameter.getParamValue1()))); return PlatformResult.success(data); } /**获取小程序普通token*/ @RequestMapping(value = "/mp/get_token",method = RequestMethod.POST) public PlatformResult mpToken(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.getMpId())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"mpId is required"); } if(!PlatformPattern.PROD.equals(customParameter.getPlatformPattern())){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"正式环境获取TOKEN"); } //获取应用数据 ApplyParameter applyParameter = ApplyParameterTool.getMpParameter(accountToken.getMpId(),commonService); if(applyParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"mpId is error"); } SysParameter sysParameter = WeiXinMpUtil.getAccessToken(commonService,applyParameter.getMpAppId(),AesUtil.aesDecryp(applyParameter.getMpSecretKey())); if(sysParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_SYSTEM,"服务错误"+ SystemCode.ERROR_GET_CORP_TOKEN); } Map 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",secondNum(sysParameter.getCreateTime(),Integer.parseInt(sysParameter.getParamValue1()))); data.put("expires_surplus",secondNum(sysParameter.getCreateTime(),Integer.parseInt(sysParameter.getParamValue1()))); return PlatformResult.success(data); } /**获取公众号普通token*/ @RequestMapping(value = "/weChat/get_token",method = RequestMethod.POST) public PlatformResult WeChatToken(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.getWeChatAppId())) { throw new PlatTipsException(PlatformCode.ERROR_PARAMETER_NULL,"weChatAppId is required"); } if(!PlatformPattern.PROD.equals(customParameter.getPlatformPattern())){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"正式环境获取TOKEN"); } //获取应用数据 ApplyParameter applyParameter = ApplyParameterTool.getWeChatParameter(accountToken.getMpId(),commonService); if(applyParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_TIPS,"weChatAppId is error"); } SysParameter sysParameter = WeiXinMpUtil.getAccessToken(commonService,applyParameter.getWeChatAppId(),AesUtil.aesDecryp(applyParameter.getWeChatSecretKey())); if(sysParameter == null){ throw new PlatTipsException(PlatformCode.ERROR_SYSTEM,"服务错误"+ SystemCode.ERROR_GET_CORP_TOKEN); } Map 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",secondNum(sysParameter.getCreateTime(),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());*/ } }