From a01057d543ae3138de9de4c585a7474e7db6c7d8 Mon Sep 17 00:00:00 2001 From: zxc <954985706@qq.com> Date: Fri, 24 Jul 2020 16:59:41 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=AC=AC=E4=B8=89=E6=96=B9?= =?UTF-8?q?=E5=B9=B3=E5=8F=B0-=E6=8E=88=E6=9D=83=E5=9B=9E=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/epmet/dto/form/OpenAppIdFormDTO.java | 20 +++++ .../com/epmet/dto/form/RemoveBindFormDTO.java | 26 +++++++ .../AppLetAuthorizationController.java | 27 +++++++ .../java/com/epmet/dao/BindingAccountDao.java | 9 +++ .../java/com/epmet/dao/CustomerMpDao.java | 10 ++- .../main/java/com/epmet/redis/RedisThird.java | 11 +++ .../service/AppLetAuthorizationService.java | 18 +++++ .../impl/AppLetAuthorizationServiceImpl.java | 78 +++++++++++++++++++ .../ComponentVerifyTicketServiceImpl.java | 8 +- .../wxapi/constant/WxMaCodeConstant.java | 10 +++ .../resources/mapper/BindingAccountDao.xml | 9 +++ .../main/resources/mapper/CustomerMpDao.xml | 19 ++++- 12 files changed, 236 insertions(+), 9 deletions(-) create mode 100644 epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/OpenAppIdFormDTO.java create mode 100644 epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/RemoveBindFormDTO.java diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/OpenAppIdFormDTO.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/OpenAppIdFormDTO.java new file mode 100644 index 0000000000..ddd36a2376 --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/OpenAppIdFormDTO.java @@ -0,0 +1,20 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import java.io.Serializable; + +/** + * @Author zxc + * @CreateTime 2020/7/24 16:31 + */ +@Data +public class OpenAppIdFormDTO implements Serializable { + + private static final long serialVersionUID = 4181403694668558506L; + + /** + * 客户端类型 + */ + private String clientType; +} diff --git a/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/RemoveBindFormDTO.java b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/RemoveBindFormDTO.java new file mode 100644 index 0000000000..c83dbcc97b --- /dev/null +++ b/epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/dto/form/RemoveBindFormDTO.java @@ -0,0 +1,26 @@ +package com.epmet.dto.form; + +import com.epmet.commons.tools.validator.group.CustomerClientShowGroup; +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * @Author zxc + * @CreateTime 2020/7/23 9:09 + */ +@Data +public class RemoveBindFormDTO implements Serializable { + + private static final long serialVersionUID = 1920195626466407047L; + + public interface RemoveBind extends CustomerClientShowGroup {} + + /** + * 客户端类型 resi:居民端 , work:工作端 + */ + @NotBlank(message = "客户端类型不能为空",groups = {RemoveBindFormDTO.RemoveBind.class}) + private String clientType; + +} diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/AppLetAuthorizationController.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/AppLetAuthorizationController.java index 45c8a65cb4..34d3d87cf2 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/AppLetAuthorizationController.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/AppLetAuthorizationController.java @@ -5,6 +5,8 @@ import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; import com.epmet.dto.form.GoToAuthFormDTO; +import com.epmet.dto.form.OpenAppIdFormDTO; +import com.epmet.dto.form.RemoveBindFormDTO; import com.epmet.dto.result.GoToAuthResultDTO; import com.epmet.service.AppLetAuthorizationService; import org.springframework.beans.factory.annotation.Autowired; @@ -36,4 +38,29 @@ public class AppLetAuthorizationController { return new Result().ok(goToAuthResultDTO); } + /** + * @Description 将公众号/小程序从开放平台帐号下解绑 + * @param tokenDto + * @param formDTO + * @author zxc + */ + @PostMapping("removeBind") + public Result removeBind(@LoginUser TokenDto tokenDto , @RequestBody RemoveBindFormDTO formDTO){ + ValidatorUtils.validateEntity(formDTO,RemoveBindFormDTO.RemoveBind.class); + appLetAuthorizationService.removeBind(tokenDto,formDTO); + return new Result(); + } + + /** + * @Description 查看小程序绑定的开放平台账号 + * @param tokenDto + * @param formDTO + * @author zxc + */ + @PostMapping("getopenappidbyauthappid") + public Result getOpenAppId(@LoginUser TokenDto tokenDto , @RequestBody OpenAppIdFormDTO formDTO){ + String openAppId = appLetAuthorizationService.getOpenAppId(tokenDto, formDTO); + return new Result().ok(openAppId); + } + } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/BindingAccountDao.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/BindingAccountDao.java index 4ea9771d2c..2d6b624400 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/BindingAccountDao.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/BindingAccountDao.java @@ -21,6 +21,7 @@ import com.epmet.commons.mybatis.dao.BaseDao; import com.epmet.dto.form.BindingAccountFormDTO; import com.epmet.entity.BindingAccountEntity; import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; /** * 公众号/小程序绑定开放平台帐号表 @@ -37,5 +38,13 @@ public interface BindingAccountDao extends BaseDao { * @author zxc */ void insertBindingAccount(BindingAccountFormDTO formDTO); + + /** + * @Description 更新绑定状态 + * @param customerId + * @param authAppId + * @author zxc + */ + void updateBindStatus(@Param("customerId")String customerId,@Param("authAppId")String authAppId); } \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/CustomerMpDao.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/CustomerMpDao.java index ae1fb6ebf7..3b3a4ee9b8 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/CustomerMpDao.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/CustomerMpDao.java @@ -58,7 +58,7 @@ public interface CustomerMpDao extends BaseDao { * @param customerId * @author zxc */ - List selectAuthCount(@Param("customerId")String customerId); + Integer selectAuthCount(@Param("customerId")String customerId); /** * @Description 回填customer_mp的appId @@ -115,4 +115,12 @@ public interface CustomerMpDao extends BaseDao { * @author zxc */ Integer checkBind(String authAppId,String clientType); + + /** + * @Description 根据客户Id和客户端类型查询AppId + * @param customerId + * @param clientType + * @author zxc + */ + String getAppId(@Param("customerId")String customerId,@Param("clientType")String clientType); } \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/redis/RedisThird.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/redis/RedisThird.java index e367e13227..50de85c556 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/redis/RedisThird.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/redis/RedisThird.java @@ -126,4 +126,15 @@ public class RedisThird { redisUtils.hMSet(key, map,NOT_EXPIRE); } + /** + * @Description 获取授权信息 + * @param customerId + * @param clientType + * @author zxc + */ + public Map getAuthInfo(String customerId,String clientType){ + String key = ThirdRedisKeyConstant.AUTH_INFO_REDIS_KEY + customerId + ThirdRedisKeyConstant.COLON + clientType; + return redisUtils.hGetAll(key); + } + } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/AppLetAuthorizationService.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/AppLetAuthorizationService.java index 30fa445a54..abb8f4bfbd 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/AppLetAuthorizationService.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/AppLetAuthorizationService.java @@ -2,6 +2,8 @@ package com.epmet.service; import com.epmet.commons.tools.security.dto.TokenDto; import com.epmet.dto.form.GoToAuthFormDTO; +import com.epmet.dto.form.OpenAppIdFormDTO; +import com.epmet.dto.form.RemoveBindFormDTO; import com.epmet.dto.result.GoToAuthResultDTO; /** @@ -17,4 +19,20 @@ public interface AppLetAuthorizationService { */ GoToAuthResultDTO goToAuth(TokenDto tokenDto, GoToAuthFormDTO formDTO); + /** + * @Description 将公众号/小程序从开放平台帐号下解绑 + * @param tokenDto + * @param formDTO + * @author zxc + */ + void removeBind(TokenDto tokenDto, RemoveBindFormDTO formDTO); + + /** + * @Description 查看小程序绑定的开放平台账号 + * @param tokenDto + * @param formDTO + * @author zxc + */ + String getOpenAppId(TokenDto tokenDto, OpenAppIdFormDTO formDTO); + } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/AppLetAuthorizationServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/AppLetAuthorizationServiceImpl.java index a80fe2b76a..db41634212 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/AppLetAuthorizationServiceImpl.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/AppLetAuthorizationServiceImpl.java @@ -1,9 +1,22 @@ package com.epmet.service.impl; +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; +import com.epmet.commons.tools.constant.NumConstant; +import com.epmet.commons.tools.exception.RenException; import com.epmet.commons.tools.security.dto.TokenDto; +import com.epmet.commons.tools.utils.HttpClientManager; +import com.epmet.constant.ModuleConstant; +import com.epmet.dao.BindingAccountDao; +import com.epmet.dao.CustomerMpDao; +import com.epmet.dao.OpenPlatformAccountDao; import com.epmet.dao.PaCustomerAgencyDao; import com.epmet.dto.form.GoToAuthFormDTO; +import com.epmet.dto.form.OpenAppIdFormDTO; +import com.epmet.dto.form.RemoveBindFormDTO; +import com.epmet.dto.result.AuthorizationInfoResultDTO; import com.epmet.dto.result.GoToAuthResultDTO; +import com.epmet.redis.RedisThird; import com.epmet.service.AppLetAuthorizationService; import com.epmet.service.ComponentVerifyTicketService; import com.epmet.wxapi.constant.WxMaCodeConstant; @@ -12,6 +25,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import java.util.Map; + /** * @Author zxc * @CreateTime 2020/7/10 15:52 @@ -24,6 +39,17 @@ public class AppLetAuthorizationServiceImpl implements AppLetAuthorizationServic private PaCustomerAgencyDao paCustomerAgencyDao; @Autowired private ComponentVerifyTicketService componentVerifyTicketService; + @Autowired + private ComponentVerifyTicketServiceImpl componentVerifyTicketServiceImpl; + @Autowired + private OpenPlatformAccountDao openPlatformAccountDao; + @Autowired + private CustomerMpDao customerMpDao; + @Autowired + private RedisThird redisThird; + @Autowired + private BindingAccountDao bindingAccountDao; + @Value("${third.platform.appId}") private String componentAppId; @@ -44,4 +70,56 @@ public class AppLetAuthorizationServiceImpl implements AppLetAuthorizationServic result.setUrl(authUrl); return result; } + + /** + * @Description 将公众号/小程序从开放平台帐号下解绑 + * @param tokenDto + * @param formDTO + * @author zxc + */ + @Override + public void removeBind(TokenDto tokenDto, RemoveBindFormDTO formDTO) { + String customerId = componentVerifyTicketServiceImpl.getLoginUserCustomerId(tokenDto); + String clientType = formDTO.getClientType(); + //查询openAppId + String openAppId = openPlatformAccountDao.selectOpenAppIdByCustomerId(customerId); + //查询appId + String authAppId = customerMpDao.getAppId(customerId, clientType); + Map authInfo = redisThird.getAuthInfo(customerId, clientType); + AuthorizationInfoResultDTO authorizationInfo = componentVerifyTicketServiceImpl.mapToEntity(authInfo, AuthorizationInfoResultDTO.class); + JSONObject jsonObject = new JSONObject(); + jsonObject.put(ModuleConstant.LOW_APP_ID,authAppId); + jsonObject.put(ModuleConstant.OPEN_APP_ID,openAppId); + String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.UN_BIND + authorizationInfo.getAuthorizer_access_token(), JSON.toJSONString(jsonObject)).getData(); + Map map = JSON.parseObject(data, Map.class); + if (!map.get(ModuleConstant.ERR_CODE).equals(NumConstant.ZERO_STR)){ + log.error("解绑失败" + data); + throw new RenException("解绑失败" + data); + } + bindingAccountDao.updateBindStatus(customerId,authAppId); + + } + + /** + * @Description 查看小程序绑定的开放平台账号 + * @param tokenDto + * @param formDTO + * @author zxc + */ + @Override + public String getOpenAppId(TokenDto tokenDto, OpenAppIdFormDTO formDTO) { + String customerId = componentVerifyTicketServiceImpl.getLoginUserCustomerId(tokenDto); + String clientType = formDTO.getClientType(); + String authAppId = customerMpDao.getAppId(customerId, clientType); + Map authInfo = redisThird.getAuthInfo(customerId, clientType); + AuthorizationInfoResultDTO authorizationInfo = componentVerifyTicketServiceImpl.mapToEntity(authInfo, AuthorizationInfoResultDTO.class); + JSONObject jsonObject = new JSONObject(); + jsonObject.put(ModuleConstant.LOW_APP_ID,authAppId); + String data = HttpClientManager.getInstance().sendPostByJSON(WxMaCodeConstant.UN_BIND + authorizationInfo.getAuthorizer_access_token(), JSON.toJSONString(jsonObject)).getData(); + Map map = JSON.parseObject(data, Map.class); + if (map.containsKey(ModuleConstant.OPEN_APP_ID)){ + return map.get(ModuleConstant.OPEN_APP_ID).toString(); + } + return map.get(ModuleConstant.ERR_MSG).toString(); + } } diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java index 69302821e7..4bcf98a9e3 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/ComponentVerifyTicketServiceImpl.java @@ -525,12 +525,12 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe boolean bindStatus = bindInfo.containsKey(ModuleConstant.OPEN_APP_ID); if (bindStatus != true) { log.info(CREATE_AND_BIND_SUCCESS); - List authCount = customerMpDao.selectAuthCount(customerId); + Integer authCount = customerMpDao.selectAuthCount(customerId); String openPlatformId = null; - if (authCount.size() > 0) { + if (authCount > 0) { openPlatformId = openPlatformAccountDao.selectOpenAppIdByCustomerId(customerId); } - if (authCount.size() == NumConstant.ZERO) { + if (authCount == NumConstant.ZERO) { log.info("未查询到该客户授权信息,先创建开放平台账号,再绑定"); //没有任何一个小程序/公众号授权,【先创建,再绑定】 JSONObject jsonObject = new JSONObject(); @@ -560,7 +560,7 @@ public class ComponentVerifyTicketServiceImpl implements ComponentVerifyTicketSe case ModuleConstant.EIGHTY_NINE_THOUSAND: throw new RenException(ACCOUNT_HAS_BOUND_OPEN); } - } else if (authCount.size() > NumConstant.ZERO && !authCount.contains(authAppId)) { + } else if (authCount > NumConstant.ZERO) { log.info("该客户已创建过开放平台账号,直接绑定"); JSONObject jsonObject = new JSONObject(); jsonObject.put(ModuleConstant.LOW_APP_ID, authAppId); diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java index 8ec83ac350..9302f876ff 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java +++ b/epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/wxapi/constant/WxMaCodeConstant.java @@ -56,6 +56,16 @@ public interface WxMaCodeConstant { */ String API_BIND_OPEN = "https://api.weixin.qq.com/cgi-bin/open/bind?access_token="; + /** + *公众号/小程序从开放平台帐号下解绑 + */ + String UN_BIND = "https://api.weixin.qq.com/cgi-bin/open/unbind?access_token="; + + /** + * 获取公众号/小程序所绑定的开放平台帐号 + */ + String OPEN_GET = "https://api.weixin.qq.com/cgi-bin/open/get?access_token="; + /** * 获取授权方的帐号基本信息 */ diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/BindingAccountDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/BindingAccountDao.xml index d0d3d49815..c745f40578 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/BindingAccountDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/BindingAccountDao.xml @@ -21,4 +21,13 @@ NOW() ) + + + + UPDATE binding_account + SET del_flag = 1 + WHERE + customer_id = #{customerId} + AND client_type = #{clientType} + \ No newline at end of file diff --git a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml index f4e6b6403a..cd54689027 100644 --- a/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml +++ b/epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/CustomerMpDao.xml @@ -19,15 +19,14 @@ client ASC - SELECT app_id AS authAppId FROM - customer_mp + open_platform_account WHERE - del_flag = '0' + del_flag = 0 AND customer_id = #{customerId} - AND authorization_flag = 1 @@ -127,4 +126,16 @@ AND client != #{clientType} + + + \ No newline at end of file