提交 2c0a7f4a 作者: 沈振路

小程序链接的工厂及处理器

上级 5266853d
package com.yaoyaozw.customer.factory;
import com.yaoyaozw.customer.handler.*;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* 书城推广链接工厂类
*
* @author system
*/
@Component
public class MpReferralFactory {
@Resource
private TomatoMpReferralHandler tomatoMpReferralHandler;
@Resource
private YgMpReferralHandler ygMpReferralHandler;
@Resource
private YwMpReferralHandler ywMpReferralHandler;
/**
* 根据书城类型获取对应的Handler
*
* @param storeType 书城类型
* @return 对应的Handler
*/
public MpReferralHandler getHandler(String storeType) {
if ("TOMATO".equals(storeType)) {
return tomatoMpReferralHandler;
} else if ("YANG_GUANG".equals(storeType)) {
return ygMpReferralHandler;
} else if ("YUE_WEN_1".equals(storeType)) {
return ywMpReferralHandler;
}
throw new IllegalArgumentException("不支持的书城类型: " + storeType);
}
}
package com.yaoyaozw.customer.handler;
import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.yaoyaozw.customer.entity.AuthorizerExpandInfo;
import com.yaoyaozw.customer.entity.AuthorizerInfo;
import com.yaoyaozw.customer.entity.ReferralEntity;
import com.yaoyaozw.customer.feigns.ReferralFeignClient;
import com.yaoyaozw.customer.service.AuthorizerExpandInfoService;
import com.yaoyaozw.customer.service.AuthorizerInfoService;
import org.apache.commons.lang3.StringUtils;
import javax.annotation.Resource;
/**
* 书城推广链接处理器抽象类
*
* @author system
*/
public abstract class MpReferralHandler {
@Resource
protected ReferralFeignClient referralFeignClient;
@Resource
protected AuthorizerInfoService authorizerInfoService;
@Resource
protected AuthorizerExpandInfoService authorizerExpandInfoService;
/**
* 查询推广链接(抽象方法,由子类实现)
*
* @param referralEntity 推广链接实体
* @param authorizerInfo 授权方信息
* @return 推广链接实体
*/
public abstract ReferralEntity queryMpReferral(ReferralEntity referralEntity, AuthorizerInfo authorizerInfo);
/**
* 针对已经获取完成的 mpPath 字段,进行处理,处理之后的小程序链接必然会放置于mpPath字段;
* 如果还需要其它处理,如:小程序链接的http链接,则会放于 referral 字段
* @param referralEntity 原本处理并赋值好链接之后的实体
* @param authorizerInfo 公众号信息
* @param authorizerExpandInfo 公众号绑定信息
* @param responseJson
*/
public abstract void constructMpReferral(ReferralEntity referralEntity, AuthorizerInfo authorizerInfo, AuthorizerExpandInfo authorizerExpandInfo, JSONObject responseJson);
/**
* 查询推广链接(根据infoId查询相关信息后调用抽象方法)
* 传入ReferralEntity不会修改其内部值,会复制新的ReferralEntity并返回
* @param referralEntity 推广链接实体(包含infoId字段)
* @return 推广链接实体
*/
public ReferralEntity queryMpReferral(ReferralEntity referralEntity) {
ReferralEntity neoReferralEntity = new ReferralEntity();
BeanUtil.copyProperties(referralEntity, neoReferralEntity);
// 根据infoId查询authorizer_info表
Long infoId = neoReferralEntity.getInfoId();
if (infoId == null) {
throw new IllegalArgumentException("infoId不能为空");
}
AuthorizerInfo authorizerInfo = authorizerInfoService.getById(infoId);
if (authorizerInfo == null) {
throw new IllegalArgumentException("未找到对应的授权方信息,infoId: " + infoId);
}
// 根据appid查询authorizer_expand_info表(1条)
String appid = authorizerInfo.getAppid();
if (appid == null) {
throw new IllegalArgumentException("公众号appid为空,infoId: " + infoId);
}
// 调用抽象方法
return queryMpReferral(neoReferralEntity, authorizerInfo);
}
protected Boolean isMiniProgramPath(String path) {
return StringUtils.isNotBlank(path) && (path.startsWith("page") || path.startsWith("/page"));
}
protected Boolean isHttpPath(String path) {
return StringUtils.isNotBlank(path) && (path.startsWith("http"));
}
}
package com.yaoyaozw.customer.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yaoyaozw.customer.common.R;
import com.yaoyaozw.customer.constants.ApiResultConstant;
import com.yaoyaozw.customer.constants.CustomerCommonConstant;
import com.yaoyaozw.customer.entity.AuthorizerExpandInfo;
import com.yaoyaozw.customer.entity.AuthorizerInfo;
import com.yaoyaozw.customer.entity.ReferralEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
/**
* 番茄书城推广链接处理器
*
* @author system
*/
@Slf4j
@Component
public class TomatoMpReferralHandler extends MpReferralHandler {
private static final String GET_BOOK_ITEM = "get_book_item_id";
private static final String LANDING_PAGE = "landing_page";
private static final String FROM_APPID = "from_oa_app_id";
private static final String GET_BOOK_ITEM_VAL = "get_book_item_id=true";
private static final String LANDING_PAGE_VAL = "landing_page=reader";
@Override
public ReferralEntity queryMpReferral(ReferralEntity referralEntity, AuthorizerInfo authorizerInfo) {
// 根据appid查询authorizer_expand_info表(1条)
String appid = authorizerInfo.getAppid();
LambdaQueryWrapper<AuthorizerExpandInfo> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(AuthorizerExpandInfo::getAuthorizerAppid, appid);
queryWrapper.last("LIMIT 1");
AuthorizerExpandInfo authorizerExpandInfo = authorizerExpandInfoService.getOne(queryWrapper);
if (authorizerExpandInfo == null) {
throw new IllegalArgumentException("未找到对应的公众号扩展信息,appid: " + appid);
}
referralEntity.setMpAppId(authorizerExpandInfo.getMpAppid());
// 处理参数:设置accountId为mpDistributorId
referralEntity.setInfoId(authorizerInfo.getId());
referralEntity.setAccountName(authorizerInfo.getNickName());
if (CustomerCommonConstant.BOOK_NEWS_TYPE.equals(referralEntity.getNewsType())) {
// 参数设置 mediaSource
referralEntity.setMediaSource("2");
}
if (CustomerCommonConstant.ACTIVITY_NEWS_TYPE.equals(referralEntity.getNewsType())) {
// 活动不使用小程序的分销商Id,使用公众号原分销商Id
referralEntity.setAccountId(authorizerInfo.getAccountId());
if (StringUtils.isBlank(referralEntity.getActivityTitle())) {
// 构造活动标题
String activityTitle = "";
if (referralEntity.getRechargeAmount() != null && BigDecimal.ZERO.compareTo(referralEntity.getRechargeAmount()) != 0) {
activityTitle += "充" + referralEntity.getRechargeAmount().toPlainString();
}
if (referralEntity.getGiftAmount() != null && referralEntity.getGiftAmount() > 0) {
activityTitle += "送" + referralEntity.getGiftAmount() + "书币";
}
referralEntity.setActivityTitle(activityTitle);
}
} else {
// 活动之外的,使用小程序的分销商Id(书籍、常用链接)
referralEntity.setAccountId(authorizerExpandInfo.getMpDistributorId());
}
referralEntity.setMiniProgramQuery(Boolean.TRUE);
// 调用接口获取原生链接
R r = referralFeignClient.productReferral(referralEntity);
if (!r.getCode().equals(ApiResultConstant.SUCCESS_CODE)) {
throw new RuntimeException("获取链接失败: " + r.getMessage());
}
String res = r.getData("storeReferral", new TypeReference<String>() {});
JSONObject jsonObject = JSON.parseObject(res);
// 处理链接
constructMpReferral(referralEntity, authorizerInfo, authorizerExpandInfo, jsonObject);
return referralEntity;
}
@Override
public void constructMpReferral(ReferralEntity referralEntity, AuthorizerInfo authorizerInfo, AuthorizerExpandInfo authorizerExpandInfo, JSONObject responseJson) {
if (referralEntity == null) {
return;
}
// 处理链接:添加番茄小程序所需参数
referralEntity.setPromoteId(responseJson.getString("promoteId"));
String referral = responseJson.getString("referral");
referralEntity.setReferral(referral);
// 判断原始链接是不是http链接
if (StringUtils.isNotBlank(referral) && isHttpPath(referral)) {
referralEntity.setExtraHttpUrl(referral);
}
// 判断是不是活动类型,如果是活动类型,因为创建成功之后接口不返回小程序链接,所以还需要查询链接列表接口,获取小程序链接
if (CustomerCommonConstant.ACTIVITY_NEWS_TYPE.equals(referralEntity.getNewsType())) {
// 根据 promoteId 查询番茄链接详情,番茄活动接口创建链接返回的是http链接,需要查询接口获取其小程序 /page 链接
R r = referralFeignClient.queryActivityInfo(referralEntity.getAccountId(), referralEntity.getPromoteId());
if (!r.getCode().equals(ApiResultConstant.SUCCESS_CODE)) {
throw new RuntimeException("获取活动链接详情失败: " + r.getMessage());
}
String res = r.getData("activityData", new TypeReference<String>() {});
JSONObject jsonObject = JSON.parseObject(res);
referral = jsonObject.getString("url");
}
if (StringUtils.isNotBlank(referral) && isMiniProgramPath(referral)) {
// 添加 get_book_item_id=true 参数
if (!referral.contains(GET_BOOK_ITEM)) {
referral = referral + "&" + GET_BOOK_ITEM_VAL;
}
// 添加 from_oa_app_id={flagId} 参数
if (!referral.contains(FROM_APPID)) {
referral = referral + "&" + FROM_APPID + "=" + authorizerExpandInfo.getFlagId();
}
// 添加 landing_page=reader 参数
if (!referral.contains(LANDING_PAGE) && !CustomerCommonConstant.USUAL_LINK_NEWS_TYPE.equals(referralEntity.getNewsType())) {
referral = referral + "&" + LANDING_PAGE_VAL;
}
// 常用链接换掉scene
if (CustomerCommonConstant.USUAL_LINK_NEWS_TYPE.equals(referralEntity.getNewsType())) {
referral = referral.replace("scene=0", "scene=1");
}
}
referralEntity.setMpAppId(authorizerExpandInfo.getMpAppid());
referralEntity.setMpPath(referral);
// 暂时将小程序地址也赋值到跳转链接地址上
referralEntity.setReferral(referral);
}
}
package com.yaoyaozw.customer.handler;
import com.alibaba.fastjson.JSONObject;
import com.yaoyaozw.customer.entity.AuthorizerExpandInfo;
import com.yaoyaozw.customer.entity.AuthorizerInfo;
import com.yaoyaozw.customer.entity.ReferralEntity;
import org.springframework.stereotype.Component;
/**
* 阳光书城推广链接处理器
*
* @author system
*/
@Component
public class YgMpReferralHandler extends MpReferralHandler {
@Override
public ReferralEntity queryMpReferral(ReferralEntity referralEntity, AuthorizerInfo authorizerInfo) {
// TODO: 实现阳光书城的具体查询逻辑
return referralEntity;
}
@Override
public void constructMpReferral(ReferralEntity referralEntity, AuthorizerInfo authorizerInfo, AuthorizerExpandInfo authorizerExpandInfo, JSONObject responseJson) {
return;
}
}
package com.yaoyaozw.customer.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.yaoyaozw.customer.common.R;
import com.yaoyaozw.customer.constants.ApiResultConstant;
import com.yaoyaozw.customer.entity.AuthorizerExpandInfo;
import com.yaoyaozw.customer.entity.AuthorizerInfo;
import com.yaoyaozw.customer.entity.ReferralEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import java.math.BigDecimal;
import static com.yaoyaozw.customer.constants.CustomerCommonConstant.*;
/**
* 阅文书城推广链接处理器
*
* @author system
*/
@Component
public class YwMpReferralHandler extends MpReferralHandler {
@Override
public ReferralEntity queryMpReferral(ReferralEntity referralEntity, AuthorizerInfo authorizerInfo) {
// 处理参数:设置accountId为mpDistributorId
referralEntity.setInfoId(authorizerInfo.getId());
referralEntity.setAccountId(authorizerInfo.getAccountId());
referralEntity.setAccountName(authorizerInfo.getNickName());
if (ACTIVITY_NEWS_TYPE.equals(referralEntity.getNewsType())) {
if (StringUtils.isBlank(referralEntity.getTemplateId())) {
referralEntity.setTemplateId("1");
}
// 活动不使用小程序的分销商Id,使用公众号原分销商Id
if (StringUtils.isBlank(referralEntity.getActivityTitle())) {
// 构造活动标题
String activityTitle = "";
if (referralEntity.getRechargeAmount() != null && BigDecimal.ZERO.compareTo(referralEntity.getRechargeAmount()) != 0) {
activityTitle += "充" + referralEntity.getRechargeAmount().toPlainString();
}
if (referralEntity.getGiftAmount() != null && referralEntity.getGiftAmount() > 0) {
activityTitle += "送" + referralEntity.getGiftAmount() + "书币";
}
referralEntity.setActivityTitle(activityTitle);
}
}
referralEntity.setMiniProgramQuery(Boolean.TRUE);
// 调用接口获取原生链接
R r = referralFeignClient.productReferral(referralEntity);
if (!r.getCode().equals(ApiResultConstant.SUCCESS_CODE)) {
throw new RuntimeException("获取链接失败: " + r.getMessage());
}
String res = r.getData("storeReferral", new TypeReference<String>() {});
JSONObject jsonObject = JSON.parseObject(res);
// 处理链接
constructMpReferral(referralEntity, authorizerInfo, null, jsonObject);
return referralEntity;
}
@Override
public void constructMpReferral(ReferralEntity referralEntity, AuthorizerInfo authorizerInfo, AuthorizerExpandInfo authorizerExpandInfo, JSONObject responseJson) {
// 原始字段
referralEntity.setPromoteId(responseJson.getString("promoteId"));
String referral = responseJson.getString("referral");
referralEntity.setReferral(referral);
// 小程序字段
String mpPath = responseJson.getString("mpPath");
referralEntity.setMpPath(StringUtils.isNotBlank(mpPath) ? mpPath : (isMiniProgramPath(referral) ? referral : null));
referralEntity.setMpAppId(responseJson.getString("mpAppId"));
referralEntity.setExtraHttpUrl(responseJson.getString("extraHttpUrl"));
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论