|
|
@ -1,7 +1,9 @@ |
|
|
|
package com.epmet.service.impl; |
|
|
|
|
|
|
|
import cn.hutool.core.convert.Convert; |
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.alibaba.fastjson.TypeReference; |
|
|
|
import com.alibaba.nacos.client.config.utils.IOUtils; |
|
|
|
import com.epmet.commons.tools.constant.NumConstant; |
|
|
|
import com.epmet.commons.tools.exception.RenException; |
|
|
@ -12,10 +14,8 @@ import com.epmet.constant.ThirdRedisKeyConstant; |
|
|
|
import com.epmet.constant.ThirdRunTimeInfoConstant; |
|
|
|
import com.epmet.dao.*; |
|
|
|
import com.epmet.dto.form.*; |
|
|
|
import com.epmet.dto.result.AuthCodeResultDTO; |
|
|
|
import com.epmet.dto.result.AuthorizationInfoResultDTO; |
|
|
|
import com.epmet.dto.result.CreateOpenResultDTO; |
|
|
|
import com.epmet.dto.result.WillOverDueResultDTO; |
|
|
|
import com.epmet.dto.result.*; |
|
|
|
import com.epmet.feign.EpmetUserOpenFeignClient; |
|
|
|
import com.epmet.mpaes.WXBizMsgCrypt; |
|
|
|
import com.epmet.mpaes.WXXmlToMapUtil; |
|
|
|
import com.epmet.redis.RedisThird; |
|
|
@ -32,9 +32,7 @@ import org.springframework.web.bind.annotation.RequestBody; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.IOException; |
|
|
|
import java.io.InputStream; |
|
|
|
import java.io.UnsupportedEncodingException; |
|
|
|
import java.lang.reflect.Field; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
@ -79,6 +77,10 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
private MiniCategoryInfoDao miniCategoryInfoDao; |
|
|
|
@Autowired |
|
|
|
private BusinessInfoDao businessInfoDao; |
|
|
|
@Autowired |
|
|
|
private EpmetUserOpenFeignClient epmetUserOpenFeignClient; |
|
|
|
@Autowired |
|
|
|
private AuthResultRecordDao authResultRecordDao; |
|
|
|
|
|
|
|
@Value("${third.platform.appId}") |
|
|
|
private String componentAppId; |
|
|
@ -109,20 +111,17 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
String nonce = request.getParameter(ModuleConstant.NONCE); |
|
|
|
String encryptType = request.getParameter(ModuleConstant.ENCRYPT_TYPE); |
|
|
|
String signature = request.getParameter(ModuleConstant.SIGNATURE); |
|
|
|
log.info(String.format(ThirdRunTimeInfoConstant.VERIFY_TICKET,msgSignature,timeStamp,nonce,encryptType,signature)); |
|
|
|
// 从请求中读取整个post数据
|
|
|
|
InputStream inputStream; |
|
|
|
String postData = null; |
|
|
|
inputStream = request.getInputStream(); |
|
|
|
postData= IOUtils.toString(inputStream,ModuleConstant.UTF8); |
|
|
|
log.info("postData = "+postData); |
|
|
|
WXBizMsgCrypt wxBizMsgCrypt = new WXBizMsgCrypt(token,aesKey,componentAppId); |
|
|
|
String msg = wxBizMsgCrypt.decryptMsg(msgSignature, timeStamp, nonce, postData); |
|
|
|
log.info(String.format(ThirdRunTimeInfoConstant.MSG,msg)); |
|
|
|
// 将xml转为map
|
|
|
|
Map<String, String> result = WXXmlToMapUtil.xmlToMap(msg); |
|
|
|
String infotype = result.get(ModuleConstant.INFO_TYPE); //获取infotype,注:微信开放平台文档中标明固定为:"component_verify_ticket",但参考其他代码,还包含authorized???
|
|
|
|
log.info(infotype); |
|
|
|
String infotype = result.get(ModuleConstant.INFO_TYPE); |
|
|
|
switch (infotype){ |
|
|
|
case ModuleConstant.TICKET_UNDERLINE_KEY: //接收票据 【component_verify_ticket】
|
|
|
|
String ComponentVerifyTicket = result.get(ModuleConstant.TICKET_KEY); |
|
|
@ -133,29 +132,22 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
ticketFormDTO.setComponentAppId(componentAppId); |
|
|
|
ticketFormDTO.setComponentVerifyTicket(ComponentVerifyTicket); |
|
|
|
//先逻辑删
|
|
|
|
ticketDao.updateOldComponentVerifyTicket(); |
|
|
|
ticketDao.deleteOldComponentVerifyTicket(); |
|
|
|
ticketDao.insertComponentVerifyTicket(ticketFormDTO); |
|
|
|
log.info(ModuleConstant.TICKET_UNDERLINE_KEY+":"+ComponentVerifyTicket); |
|
|
|
break; |
|
|
|
case ModuleConstant.AUTHORIZED: //授权成功
|
|
|
|
// 更改customer_mp 授权信息,appId,并绑定
|
|
|
|
// String authAppId = result.get(ModuleConstant.APP_ID);
|
|
|
|
String authCode = result.get(ModuleConstant.AUTHORIZATION_CODE_HUMP); |
|
|
|
log.info("=============================="+authCode); |
|
|
|
AuthCodeResultDTO authCodeResultDTO = authCodeDao.selectCustomerIdByAuthCode(authCode); |
|
|
|
String clientType = authCodeResultDTO.getClientType(); |
|
|
|
String customerId = authCodeResultDTO.getCustomerId(); |
|
|
|
Map<String, Object> authorizerRefreshToken = redisThird.getAuthorizerRefreshToken(customerId + ThirdRedisKeyConstant.COLON + clientType); |
|
|
|
String authAppId = authorizerRefreshToken.get("authorizerAppid").toString(); |
|
|
|
this.updateCustomerMpAppIdAndCreateOpenPlatform(customerId,authAppId,clientType); |
|
|
|
// this.authInfoByAuthCode(authCode, customerId,clientType);
|
|
|
|
authCodeDao.updateAppId(customerId,clientType,authAppId); |
|
|
|
this.saveAuthAccountInfo(customerId,authAppId,clientType); |
|
|
|
// customerMpDao.updateAuthorizationFlag(authAppId);
|
|
|
|
this.disposeAuthResult(result); |
|
|
|
break; |
|
|
|
case ModuleConstant.UNAUTHORIZED://用户取消授权
|
|
|
|
//todo 取消授权
|
|
|
|
String authorizerAppId = result.get(ModuleConstant.AUTHORIZER_APP_ID_HUMP); |
|
|
|
customerMpDao.updateAuthorizationFlag(authorizerAppId); |
|
|
|
this.disposeAuthResult(result); |
|
|
|
break; |
|
|
|
case ModuleConstant.UPDATE_AUTHORIZED://授权变更
|
|
|
|
this.disposeAuthResult(result); |
|
|
|
break; |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
log.error(e.getMessage()); |
|
|
@ -170,6 +162,24 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
return ModuleConstant.SUCCESS; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 授权结果插入 |
|
|
|
* @param result |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void disposeAuthResult(Map<String,String> result){ |
|
|
|
Map tempMap = result; |
|
|
|
Map<String,Object> data = tempMap; |
|
|
|
AuthResultRecordFormDTO authResultRecord = mapToEntity(data, AuthResultRecordFormDTO.class); |
|
|
|
authResultRecord.setWechatCreateTime(this.sToDate(result.get(ModuleConstant.CREATE_TIME))); |
|
|
|
if (result.containsKey(ModuleConstant.AUTHORIZATION_CODE_EXPIRED_TIME)) { |
|
|
|
authResultRecord.setExpiredTime(this.sToDate(result.get(ModuleConstant.AUTHORIZATION_CODE_EXPIRED_TIME))); |
|
|
|
} |
|
|
|
authResultRecord.setComponentAppId(componentAppId); |
|
|
|
authResultRecordDao.insertAuthResultRecord(authResultRecord); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 定时获取 (令牌,component_access_token) 第三方与微信交互使用的component_access_token |
|
|
|
* 每十分钟执行一次,判断是否有马上超时的(15分钟以内算马上超时) |
|
|
@ -283,28 +293,13 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
authInfoDTO.setExpiresInTime(expiresInTime); |
|
|
|
authInfoDTO.setCustomerId(customerId); |
|
|
|
authInfoDTO.setClientType(clientType); |
|
|
|
//先逻辑删除,在插入
|
|
|
|
authorizationInfoDao.updateOldAuthorizationInfo(customerId,clientType); |
|
|
|
authorizationInfoDao.insertAuthorizationInfo(authInfoDTO); |
|
|
|
// 2. 权限列表
|
|
|
|
log.info(START_INSERT_FUNC_INFO); |
|
|
|
List<FuncInfoFormDTO> funcInfos = new ArrayList<>(); |
|
|
|
List<Map> func_info = authorizationInfoResultDTO.getFunc_info(); |
|
|
|
log.info("权限列表信息:"+func_info); |
|
|
|
/*func_info.forEach(func -> { |
|
|
|
func.forEach((key,value) -> { |
|
|
|
FuncInfoFormDTO fu = new FuncInfoFormDTO(); |
|
|
|
fu.setFuncscopeCategory((String) key); |
|
|
|
Map funcScope = (Map) value; |
|
|
|
fu.setFuncscopeId(funcScope.get(ModuleConstant.ID).toString()); |
|
|
|
fu.setAuthorizationInfoAppid(authAppId); |
|
|
|
fu.setCustomerId(customerId); |
|
|
|
funcInfos.add(fu); |
|
|
|
}); |
|
|
|
});*/ |
|
|
|
// todo 先逻辑删除,在插入
|
|
|
|
/*funcInfoDao.updateOldFuncInfo(customerId,authAppId); |
|
|
|
funcInfoDao.insertFuncInfo(funcInfos);*/ |
|
|
|
String funcInfo = JSON.toJSONString(func_info); |
|
|
|
authInfoDTO.setFuncInfo(funcInfo); |
|
|
|
//先逻辑删除,在插入
|
|
|
|
authorizationInfoDao.updateOldAuthorizationInfo(authAppId); |
|
|
|
authorizationInfoDao.insertAuthorizationInfo(authInfoDTO); |
|
|
|
// 授权信息放入缓存
|
|
|
|
redisThird.setAuthInfo(authorizationInfoResultDTO,customerId,clientType); |
|
|
|
//authorizer_refresh_token 放入缓存
|
|
|
@ -338,27 +333,29 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
String componentAccessToken = redisThird.getComponentAccessToken(); |
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_AUTHORIZER_TOKEN_URL + componentAccessToken, JSON.toJSONString(jsonObject)).getData(); |
|
|
|
Map map = JSON.parseObject(data, HashMap.class); |
|
|
|
//authorizer_access_token
|
|
|
|
String authorizerAccessToken = map.get(ModuleConstant.AUTHORIZER_ACCESS_TOKEN).toString(); |
|
|
|
String expiresIn = map.get(ModuleConstant.EXPIRES_IN).toString(); |
|
|
|
String authorizerRefreshToken = map.get(ModuleConstant.AUTHORIZER_REFRESH_TOKEN).toString(); |
|
|
|
Date expiresInTime = this.countExpirationTime(expiresIn); |
|
|
|
//更新DB
|
|
|
|
AuthorizerAccessTokenFormDTO formDTO = new AuthorizerAccessTokenFormDTO(); |
|
|
|
AuthorizationInfoFormDTO authorizationInfo = new AuthorizationInfoFormDTO(); |
|
|
|
BeanUtils.copyProperties(formDTO,authorizationInfo); |
|
|
|
authorizationInfo.setAuthorizerAppid(authAppId); |
|
|
|
formDTO.setAuthorizerAccessToken(authorizerAccessToken); |
|
|
|
formDTO.setAuthorizerRefreshToken(authorizerRefreshToken); |
|
|
|
formDTO.setExpiresInTime(expiresInTime); |
|
|
|
formDTO.setCustomerId(customerId); |
|
|
|
formDTO.setAuthAppid(authAppId); |
|
|
|
//先逻辑删除,在插入
|
|
|
|
authorizationInfoDao.updateOldAuthorizerAccessToken(willOverDueDTO.getCustomerId(),clientType); |
|
|
|
authorizationInfoDao.insertAuthorizerAccessToken(formDTO); |
|
|
|
|
|
|
|
//缓存 refreshAuthorizerAccessToken
|
|
|
|
redisThird.setAuthorizerRefreshToken(authorizationInfo); |
|
|
|
if (!map.containsKey(ModuleConstant.ERR_CODE)) { |
|
|
|
log.info(REFRESH_AUTH_ACCESS_TOKEN); |
|
|
|
String authorizerAccessToken = map.get(ModuleConstant.AUTHORIZER_ACCESS_TOKEN).toString(); |
|
|
|
String expiresIn = map.get(ModuleConstant.EXPIRES_IN).toString(); |
|
|
|
String authorizerRefreshToken = map.get(ModuleConstant.AUTHORIZER_REFRESH_TOKEN).toString(); |
|
|
|
Date expiresInTime = this.countExpirationTime(expiresIn); |
|
|
|
//更新DB
|
|
|
|
AuthorizerAccessTokenFormDTO formDTO = new AuthorizerAccessTokenFormDTO(); |
|
|
|
AuthorizationInfoFormDTO authorizationInfo = new AuthorizationInfoFormDTO(); |
|
|
|
BeanUtils.copyProperties(formDTO, authorizationInfo); |
|
|
|
authorizationInfo.setAuthorizerAppid(authAppId); |
|
|
|
formDTO.setAuthorizerAccessToken(authorizerAccessToken); |
|
|
|
formDTO.setAuthorizerRefreshToken(authorizerRefreshToken); |
|
|
|
formDTO.setExpiresInTime(expiresInTime); |
|
|
|
formDTO.setCustomerId(customerId); |
|
|
|
formDTO.setAuthAppid(authAppId); |
|
|
|
formDTO.setClientType(clientType); |
|
|
|
//先逻辑删除,在插入
|
|
|
|
authorizationInfoDao.updateOldAuthorizerAccessToken(customerId, clientType); |
|
|
|
authorizationInfoDao.insertAuthorizerAccessToken(formDTO); |
|
|
|
//缓存 refreshAuthorizerAccessToken
|
|
|
|
redisThird.setAuthorizerRefreshToken(authorizationInfo); |
|
|
|
} |
|
|
|
}); |
|
|
|
log.info("更新authorizer_access_token成功"); |
|
|
|
} |
|
|
@ -375,27 +372,31 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
public void redirectUri(TokenDto tokenDto, @RequestBody AuthCodeAndTimeFromDTO authCodeAndTime){ |
|
|
|
log.info("开始执行回调URL"); |
|
|
|
String authCode = authCodeAndTime.getAuthCode(); |
|
|
|
String client = authCodeAndTime.getClientType(); |
|
|
|
String clientType = authCodeAndTime.getClientType(); |
|
|
|
String expiresIn = authCodeAndTime.getExpiresIn(); |
|
|
|
String customerId = tokenDto.getCustomerId(); |
|
|
|
customerId = "f530774b31e0609a3c7f0f83794cda0c"; |
|
|
|
String customerId = this.getLoginUserCustomerId(tokenDto); |
|
|
|
Date expiresInTime = this.countExpirationTime(expiresIn); |
|
|
|
if (StringUtils.isBlank(customerId)||StringUtils.isBlank(client)||StringUtils.isBlank(authCode)||StringUtils.isBlank(expiresIn)){ |
|
|
|
log.info("客户ID = "+customerId+", 客户端类型为 = "+client+", 授权码为 = "+authCode+", 有效期 = "+expiresIn); |
|
|
|
} |
|
|
|
//authCode存数据库
|
|
|
|
AuthCodeFormDTO formDTO = new AuthCodeFormDTO(); |
|
|
|
formDTO.setAuthCode(authCode); |
|
|
|
formDTO.setExpiresInTime(expiresInTime); |
|
|
|
formDTO.setClientType(client); |
|
|
|
//授权方AppId
|
|
|
|
formDTO.setClientType(clientType); |
|
|
|
formDTO.setCustomerId(customerId); |
|
|
|
log.info(formDTO.toString()); |
|
|
|
authCodeDao.deleteCustomerAuthCode(customerId,client); |
|
|
|
authCodeDao.deleteCustomerAuthCode(customerId,clientType); |
|
|
|
authCodeDao.insertRedirectAuthCode(formDTO); |
|
|
|
//authCode存缓存
|
|
|
|
redisThird.setAuthCode(formDTO); |
|
|
|
this.authInfoByAuthCode(authCode, customerId,client); |
|
|
|
// 1. 开始获取授权信息(使用授权码)
|
|
|
|
AuthorizationInfoResultDTO authorizationInfoResultDTO = this.authInfoByAuthCode(authCode, customerId, clientType); |
|
|
|
String authAppId = authorizationInfoResultDTO.getAuthorizer_appid(); |
|
|
|
// 2. 创建开放平台账号并绑定 或者 直接绑定开放平台
|
|
|
|
this.createAndBindOpenAccount(customerId,authAppId,clientType); |
|
|
|
// 3. 更新 customer_mp 表授权状态和AuthAppId
|
|
|
|
this.updateCustomerMpAppId(customerId,authAppId,clientType); |
|
|
|
authCodeDao.updateAppId(customerId,clientType,authAppId); |
|
|
|
// 4. 保存授权方账户信息
|
|
|
|
this.saveAuthAccountInfo(customerId,authAppId,clientType); |
|
|
|
log.info("回调结束"); |
|
|
|
} |
|
|
|
|
|
|
@ -456,18 +457,20 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
//todo 小程序配置的类目信息插入
|
|
|
|
List<MiniCategoryInfoFormDTO> categoryInfoList = new ArrayList<>(); |
|
|
|
List<Map> categories = (List<Map>) miniProgramInfo.get(ModuleConstant.CATEGORIES); |
|
|
|
categories.forEach(category -> { |
|
|
|
category.forEach((key,value) -> { |
|
|
|
MiniCategoryInfoFormDTO categoryInfo = new MiniCategoryInfoFormDTO(); |
|
|
|
categoryInfo.setCustomerId(customerId); |
|
|
|
categoryInfo.setClientType(clientType); |
|
|
|
categoryInfo.setCategorySort(key.toString()); |
|
|
|
categoryInfo.setCategoryName(value.toString()); |
|
|
|
categoryInfo.setPrimaryId(primaryId); |
|
|
|
categoryInfoList.add(categoryInfo); |
|
|
|
if (categories.size() != NumConstant.ZERO) { |
|
|
|
categories.forEach(category -> { |
|
|
|
category.forEach((key, value) -> { |
|
|
|
MiniCategoryInfoFormDTO categoryInfo = new MiniCategoryInfoFormDTO(); |
|
|
|
categoryInfo.setCustomerId(customerId); |
|
|
|
categoryInfo.setClientType(clientType); |
|
|
|
categoryInfo.setCategorySort(key.toString()); |
|
|
|
categoryInfo.setCategoryName(value.toString()); |
|
|
|
categoryInfo.setPrimaryId(primaryId); |
|
|
|
categoryInfoList.add(categoryInfo); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
miniCategoryInfoDao.insertCategoryInfo(categoryInfoList); |
|
|
|
miniCategoryInfoDao.insertCategoryInfo(categoryInfoList); |
|
|
|
} |
|
|
|
}else { |
|
|
|
log.info("授权方为公众号 并 开始插入信息"); |
|
|
|
PaInfoFormDTO paInfoFormDTO = this.mapToEntity(authorizerInfo, PaInfoFormDTO.class); |
|
|
@ -493,89 +496,107 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 1.创建开放平台账号并绑定 2.回填 customer_mp appId |
|
|
|
* @Description 回填 customer_mp appId , 更改授权状态 |
|
|
|
* @param |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
@Transactional(rollbackFor = Exception.class) |
|
|
|
public void updateCustomerMpAppIdAndCreateOpenPlatform(String customerId,String authAppId,String clientType){ |
|
|
|
log.info("开始创建开放平台账号并绑定"); |
|
|
|
Integer authCount = customerMpDao.selectAuthCount(customerId); |
|
|
|
String openPlatformId = null; |
|
|
|
public void updateCustomerMpAppId(String customerId,String authAppId,String clientType){ |
|
|
|
log.info("==========回填customer_mp开始=========="); |
|
|
|
Integer checkBindCount = customerMpDao.checkBind(authAppId, clientType); |
|
|
|
if (checkBindCount == NumConstant.ZERO) { |
|
|
|
AuthCodeFormDTO formDTO = new AuthCodeFormDTO(); |
|
|
|
formDTO.setClientType(clientType); |
|
|
|
formDTO.setAuthAppId(authAppId); |
|
|
|
formDTO.setCustomerId(customerId); |
|
|
|
//回填customer_mp的appId 只需以上三个字段
|
|
|
|
customerMpDao.updateAppIDByCustomerIdAndClient(formDTO); |
|
|
|
} |
|
|
|
log.info("==========回填customer_mp结束=========="); |
|
|
|
} |
|
|
|
|
|
|
|
public void createAndBindOpenAccount(String customerId,String authAppId,String clientType){ |
|
|
|
Map<String, Object> authorizerRefreshToken = redisThird.getAuthorizerRefreshToken(customerId + ThirdRedisKeyConstant.COLON + clientType); |
|
|
|
String authorizerAccessToken = authorizerRefreshToken.get("authorizerAccessToken").toString(); |
|
|
|
if (authCount==NumConstant.ZERO){ |
|
|
|
log.info("未查询到该客户授权信息,先创建开放平台账号,再绑定"); |
|
|
|
//没有任何一个小程序/公众号授权,【先创建,再绑定】
|
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
jsonObject.put(ModuleConstant.LOW_APP_ID,authAppId); |
|
|
|
// 此处的 access_token 为 【authorizer_access_token】
|
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_CREATE_OPEN + authorizerAccessToken, JSON.toJSONString(jsonObject)).getData(); |
|
|
|
Map<String,String> map = JSON.parseObject(data, Map.class); |
|
|
|
CreateOpenResultDTO createOpen = new CreateOpenResultDTO(); |
|
|
|
createOpen.setErrCode(Integer.valueOf(map.get(ModuleConstant.ERR_CODE))); |
|
|
|
createOpen.setErrMsg(map.get(ModuleConstant.ERR_MSG)); |
|
|
|
createOpen.setOpenAppId(map.get(ModuleConstant.OPEN_APP_ID)); |
|
|
|
switch (createOpen.getErrCode()){ |
|
|
|
case NumConstant.ONE: |
|
|
|
log.info(CREATE_AND_BIND_SUCCESS); |
|
|
|
break; |
|
|
|
case NumConstant.ONE_NEG: |
|
|
|
throw new RenException(SYSTEM_ERROR); |
|
|
|
case ModuleConstant.FORTY_THOUSAND_AND_THIRTEEN: |
|
|
|
throw new RenException(INVALID_APP_ID); |
|
|
|
case ModuleConstant.EIGHTY_NINE_THOUSAND: |
|
|
|
throw new RenException(ACCOUNT_HAS_BOUND_OPEN); |
|
|
|
JSONObject bindInfoForm = new JSONObject(); |
|
|
|
bindInfoForm.put(ModuleConstant.LOW_APP_ID,authAppId); |
|
|
|
String bindResult = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_OPEN_GET + authorizerAccessToken, JSON.toJSONString(bindInfoForm)).getData(); |
|
|
|
Map<String,Object> bindInfo = JSON.parseObject(bindResult, Map.class); |
|
|
|
boolean bindStatus = bindInfo.containsKey(ModuleConstant.OPEN_APP_ID); |
|
|
|
if (bindStatus != true) { |
|
|
|
log.info(CREATE_AND_BIND_SUCCESS); |
|
|
|
List<String> authCount = customerMpDao.selectAuthCount(customerId); |
|
|
|
String openPlatformId = null; |
|
|
|
if (authCount.size() > 0) { |
|
|
|
openPlatformId = openPlatformAccountDao.selectOpenAppIdByCustomerId(customerId); |
|
|
|
} |
|
|
|
CreateOpenFormDTO coForm = new CreateOpenFormDTO(); |
|
|
|
coForm.setOpenid(map.get(ModuleConstant.OPEN_APP_ID)); |
|
|
|
coForm.setCustomerId(customerId); |
|
|
|
//插入 open_platform_account 表
|
|
|
|
openPlatformAccountDao.insertOpenPlatFormAccount(coForm); |
|
|
|
openPlatformId = coForm.getId(); |
|
|
|
}else if (authCount>NumConstant.ZERO){ |
|
|
|
log.info("该客户已创建过开放平台账号,直接绑定"); |
|
|
|
String openAppId = openPlatformAccountDao.selectOpenAppIdByCustomerId(customerId); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
jsonObject.put(ModuleConstant.LOW_APP_ID,componentAppId); |
|
|
|
jsonObject.put(ModuleConstant.OPEN_APP_ID,openAppId); |
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_BIND_OPEN + authorizerRefreshToken, JSON.toJSONString(jsonObject)).getData(); |
|
|
|
Map<String,String> map = JSON.parseObject(data, Map.class); |
|
|
|
CreateOpenResultDTO createOpen = new CreateOpenResultDTO(); |
|
|
|
createOpen.setErrCode(Integer.valueOf(map.get(ModuleConstant.ERR_CODE))); |
|
|
|
createOpen.setErrMsg(map.get(ModuleConstant.ERR_MSG)); |
|
|
|
switch (createOpen.getErrCode()){ |
|
|
|
case NumConstant.ONE: |
|
|
|
log.info(BIND_SUCCESS); |
|
|
|
break; |
|
|
|
case NumConstant.ONE_NEG: |
|
|
|
throw new RenException(SYSTEM_ERROR); |
|
|
|
case ModuleConstant.FORTY_THOUSAND_AND_THIRTEEN: |
|
|
|
throw new RenException(INVALID_APP_ID); |
|
|
|
case ModuleConstant.EIGHTY_NINE_THOUSAND: |
|
|
|
throw new RenException(ACCOUNT_HAS_BOUND_OPEN); |
|
|
|
case ModuleConstant.EIGHTY_NINE_THOUSAND_AND_ONE: |
|
|
|
throw new RenException(NOT_SAME_CONTRACTOR); |
|
|
|
case ModuleConstant.EIGHTY_NINE_THOUSAND_AND_THREE: |
|
|
|
throw new RenException(NOT_ALLOWED_OPERATE); |
|
|
|
case ModuleConstant.EIGHTY_NINE_THOUSAND_AND_FOUR: |
|
|
|
throw new RenException(TO_LIMIT); |
|
|
|
if (authCount.size() == NumConstant.ZERO) { |
|
|
|
log.info("未查询到该客户授权信息,先创建开放平台账号,再绑定"); |
|
|
|
//没有任何一个小程序/公众号授权,【先创建,再绑定】
|
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
jsonObject.put(ModuleConstant.LOW_APP_ID, authAppId); |
|
|
|
// 此处的 access_token 为 【authorizer_access_token】
|
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_CREATE_OPEN + authorizerAccessToken, JSON.toJSONString(jsonObject)).getData(); |
|
|
|
Map<String, Object> map = JSON.parseObject(data, Map.class); |
|
|
|
openPlatformId = map.get(ModuleConstant.OPEN_APP_ID).toString(); |
|
|
|
CreateOpenResultDTO createOpen = new CreateOpenResultDTO(); |
|
|
|
createOpen.setErrCode((Integer) map.get(ModuleConstant.ERR_CODE)); |
|
|
|
createOpen.setErrMsg(map.get(ModuleConstant.ERR_MSG).toString()); |
|
|
|
createOpen.setOpenAppId(map.get(ModuleConstant.OPEN_APP_ID).toString()); |
|
|
|
switch (createOpen.getErrCode()) { |
|
|
|
case NumConstant.ZERO: |
|
|
|
log.info(CREATE_AND_BIND_SUCCESS); |
|
|
|
CreateOpenFormDTO coForm = new CreateOpenFormDTO(); |
|
|
|
coForm.setOpenid(map.get(ModuleConstant.OPEN_APP_ID).toString()); |
|
|
|
coForm.setCustomerId(customerId); |
|
|
|
//插入 open_platform_account 表
|
|
|
|
openPlatformAccountDao.deleteOldOpenPlatFprmAccount(customerId); |
|
|
|
openPlatformAccountDao.insertOpenPlatFormAccount(coForm); |
|
|
|
break; |
|
|
|
case NumConstant.ONE_NEG: |
|
|
|
throw new RenException(SYSTEM_ERROR); |
|
|
|
case ModuleConstant.FORTY_THOUSAND_AND_THIRTEEN: |
|
|
|
throw new RenException(INVALID_APP_ID); |
|
|
|
case ModuleConstant.EIGHTY_NINE_THOUSAND: |
|
|
|
throw new RenException(ACCOUNT_HAS_BOUND_OPEN); |
|
|
|
} |
|
|
|
} else if (authCount.size() > NumConstant.ZERO && !authCount.contains(authAppId)) { |
|
|
|
log.info("该客户已创建过开放平台账号,直接绑定"); |
|
|
|
JSONObject jsonObject = new JSONObject(); |
|
|
|
jsonObject.put(ModuleConstant.LOW_APP_ID, authAppId); |
|
|
|
jsonObject.put(ModuleConstant.OPEN_APP_ID, openPlatformId); |
|
|
|
String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.API_BIND_OPEN + authorizerAccessToken, JSON.toJSONString(jsonObject)).getData(); |
|
|
|
Map<String, Object> map = JSON.parseObject(data, Map.class); |
|
|
|
CreateOpenResultDTO createOpen = new CreateOpenResultDTO(); |
|
|
|
createOpen.setErrCode((Integer) map.get(ModuleConstant.ERR_CODE)); |
|
|
|
createOpen.setErrMsg(map.get(ModuleConstant.ERR_MSG).toString()); |
|
|
|
switch (createOpen.getErrCode()) { |
|
|
|
case NumConstant.ZERO: |
|
|
|
log.info(BIND_SUCCESS); |
|
|
|
break; |
|
|
|
case NumConstant.ONE_NEG: |
|
|
|
throw new RenException(SYSTEM_ERROR); |
|
|
|
case ModuleConstant.FORTY_THOUSAND_AND_THIRTEEN: |
|
|
|
throw new RenException(INVALID_APP_ID); |
|
|
|
case ModuleConstant.EIGHTY_NINE_THOUSAND: |
|
|
|
throw new RenException(ACCOUNT_HAS_BOUND_OPEN); |
|
|
|
case ModuleConstant.EIGHTY_NINE_THOUSAND_AND_ONE: |
|
|
|
throw new RenException(NOT_SAME_CONTRACTOR); |
|
|
|
case ModuleConstant.EIGHTY_NINE_THOUSAND_AND_THREE: |
|
|
|
throw new RenException(NOT_ALLOWED_OPERATE); |
|
|
|
case ModuleConstant.EIGHTY_NINE_THOUSAND_AND_FOUR: |
|
|
|
throw new RenException(TO_LIMIT); |
|
|
|
} |
|
|
|
} |
|
|
|
//插入 binding_account
|
|
|
|
BindingAccountFormDTO bindingAccount = new BindingAccountFormDTO(); |
|
|
|
bindingAccount.setOpenPlatformAccountId(openPlatformId); |
|
|
|
bindingAccount.setAuthAppId(authAppId); |
|
|
|
bindingAccount.setClientType(clientType); |
|
|
|
bindingAccount.setCustomerId(customerId); |
|
|
|
bindingAccountDao.insertBindingAccount(bindingAccount); |
|
|
|
log.info("创建绑定账号结束"); |
|
|
|
} |
|
|
|
AuthCodeFormDTO formDTO = new AuthCodeFormDTO(); |
|
|
|
formDTO.setClientType(clientType); |
|
|
|
formDTO.setAuthAppId(authAppId); |
|
|
|
formDTO.setCustomerId(customerId); |
|
|
|
//回填customer_mp的appId 只需以上三个字段
|
|
|
|
customerMpDao.updateAppIDByCustomerIdAndClient(formDTO); |
|
|
|
//插入 binding_account
|
|
|
|
BindingAccountFormDTO bindingAccount = new BindingAccountFormDTO(); |
|
|
|
bindingAccount.setOpenPlatformAccountId(openPlatformId); |
|
|
|
bindingAccount.setAuthAppId(authAppId); |
|
|
|
bindingAccount.setClientType(clientType); |
|
|
|
bindingAccount.setCustomerId(customerId); |
|
|
|
bindingAccountDao.insertBindingAccount(bindingAccount); |
|
|
|
log.info("创建绑定账号结束"); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
@ -620,4 +641,28 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe |
|
|
|
date.setTime(l); |
|
|
|
return date; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 查询客户Id |
|
|
|
* @param tokenDto |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
public String getLoginUserCustomerId(TokenDto tokenDto){ |
|
|
|
LoginUserDetailsFormDTO dto = new LoginUserDetailsFormDTO(); |
|
|
|
BeanUtils.copyProperties(tokenDto,dto); |
|
|
|
LoginUserDetailsResultDTO data = epmetUserOpenFeignClient.getLoginUserDetails(dto).getData(); |
|
|
|
return data.getCustomerId(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* @Description 时间戳(秒级)转换 Date |
|
|
|
* @param t |
|
|
|
* @author zxc |
|
|
|
*/ |
|
|
|
public Date sToDate(String t){ |
|
|
|
Long aLong = Long.valueOf(t + "000"); |
|
|
|
Date date = new Date(); |
|
|
|
date.setTime(aLong); |
|
|
|
return date; |
|
|
|
} |
|
|
|
} |
|
|
|