🎨 重构优化部分代码

This commit is contained in:
Binary Wang
2021-01-27 23:03:42 +08:00
parent 6811bffa18
commit 584c7ac354
15 changed files with 126 additions and 48 deletions

View File

@ -9,7 +9,7 @@ import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.redis.JedisWxRedisOps; import me.chanjar.weixin.common.redis.JedisWxRedisOps;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps; import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.common.redis.WxRedisOps; import me.chanjar.weixin.common.redis.WxRedisOps;
import me.chanjar.weixin.mp.bean.WxMpHostConfig; import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl; import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;

View File

@ -1,7 +1,10 @@
package cn.binarywang.wx.miniapp.api; package cn.binarywang.wx.miniapp.api;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.subscribemsg.CategoryData;
import cn.binarywang.wx.miniapp.bean.subscribemsg.PubTemplateKeyword;
import cn.binarywang.wx.miniapp.bean.subscribemsg.TemplateInfo;
import cn.binarywang.wx.miniapp.bean.template.WxMaPubTemplateTitleListResult; import cn.binarywang.wx.miniapp.bean.template.WxMaPubTemplateTitleListResult;
import lombok.Data;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import java.util.List; import java.util.List;
@ -43,6 +46,11 @@ public interface WxMaSubscribeService {
*/ */
String GET_CATEGORY_URL = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory"; String GET_CATEGORY_URL = "https://api.weixin.qq.com/wxaapi/newtmpl/getcategory";
/**
* 发送订阅消息
*/
String SUBSCRIBE_MSG_SEND_URL = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send";
/** /**
* <pre> * <pre>
* 获取帐号所属类目下的公共模板标题 * 获取帐号所属类目下的公共模板标题
@ -128,26 +136,15 @@ public interface WxMaSubscribeService {
*/ */
List<CategoryData> getCategory() throws WxErrorException; List<CategoryData> getCategory() throws WxErrorException;
@Data /**
class CategoryData { * <pre>
int id; * 发送订阅消息
String name; * https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/subscribe-message/subscribeMessage.send.html
} * </pre>
*
* @param subscribeMessage 订阅消息
* @throws WxErrorException .
*/
void sendSubscribeMsg(WxMaSubscribeMessage subscribeMessage) throws WxErrorException;
@Data
class TemplateInfo {
private String priTmplId;
private String title;
private String content;
private String example;
private int type;
}
@Data
class PubTemplateKeyword {
private int kid;
private String name;
private String example;
private String rule;
}
} }

View File

@ -2,12 +2,20 @@ package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService; import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.bean.subscribemsg.CategoryData;
import cn.binarywang.wx.miniapp.bean.subscribemsg.PubTemplateKeyword;
import cn.binarywang.wx.miniapp.bean.subscribemsg.TemplateInfo;
import cn.binarywang.wx.miniapp.bean.template.WxMaPubTemplateTitleListResult; import cn.binarywang.wx.miniapp.bean.template.WxMaPubTemplateTitleListResult;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder; import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import com.google.common.base.Joiner; import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.enums.WxType;
import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser; import me.chanjar.weixin.common.util.json.GsonParser;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -21,20 +29,20 @@ import java.util.List;
*/ */
@RequiredArgsConstructor @RequiredArgsConstructor
public class WxMaSubscribeServiceImpl implements WxMaSubscribeService { public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
private final WxMaService wxMaService; private final WxMaService service;
@Override @Override
public WxMaPubTemplateTitleListResult getPubTemplateTitleList(String[] ids, int start, int limit) throws WxErrorException { public WxMaPubTemplateTitleListResult getPubTemplateTitleList(String[] ids, int start, int limit) throws WxErrorException {
ImmutableMap<String, ? extends Serializable> params = ImmutableMap.of("ids", StringUtils.join(ids, ","), ImmutableMap<String, ? extends Serializable> params = ImmutableMap.of("ids", StringUtils.join(ids, ","),
"start", start, "limit", limit); "start", start, "limit", limit);
String responseText = this.wxMaService.get(GET_PUB_TEMPLATE_TITLE_LIST_URL, String responseText = this.service.get(GET_PUB_TEMPLATE_TITLE_LIST_URL,
Joiner.on("&").withKeyValueSeparator("=").join(params)); Joiner.on("&").withKeyValueSeparator("=").join(params));
return WxMaPubTemplateTitleListResult.fromJson(responseText); return WxMaPubTemplateTitleListResult.fromJson(responseText);
} }
@Override @Override
public List<PubTemplateKeyword> getPubTemplateKeyWordsById(String id) throws WxErrorException { public List<PubTemplateKeyword> getPubTemplateKeyWordsById(String id) throws WxErrorException {
String responseText = this.wxMaService.get(GET_PUB_TEMPLATE_KEY_WORDS_BY_ID_URL, String responseText = this.service.get(GET_PUB_TEMPLATE_KEY_WORDS_BY_ID_URL,
Joiner.on("&").withKeyValueSeparator("=").join(ImmutableMap.of("tid", id))); Joiner.on("&").withKeyValueSeparator("=").join(ImmutableMap.of("tid", id)));
return WxMaGsonBuilder.create().fromJson(GsonParser.parse(responseText) return WxMaGsonBuilder.create().fromJson(GsonParser.parse(responseText)
.getAsJsonArray("data"), new TypeToken<List<PubTemplateKeyword>>() { .getAsJsonArray("data"), new TypeToken<List<PubTemplateKeyword>>() {
@ -43,7 +51,7 @@ public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
@Override @Override
public String addTemplate(String id, List<Integer> keywordIdList, String sceneDesc) throws WxErrorException { public String addTemplate(String id, List<Integer> keywordIdList, String sceneDesc) throws WxErrorException {
String responseText = this.wxMaService.post(TEMPLATE_ADD_URL, ImmutableMap.of("tid", id, String responseText = this.service.post(TEMPLATE_ADD_URL, ImmutableMap.of("tid", id,
"kidList", keywordIdList.toArray(), "kidList", keywordIdList.toArray(),
"sceneDesc", sceneDesc)); "sceneDesc", sceneDesc));
return GsonParser.parse(responseText).get("priTmplId").getAsString(); return GsonParser.parse(responseText).get("priTmplId").getAsString();
@ -51,7 +59,7 @@ public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
@Override @Override
public List<TemplateInfo> getTemplateList() throws WxErrorException { public List<TemplateInfo> getTemplateList() throws WxErrorException {
String responseText = this.wxMaService.get(TEMPLATE_LIST_URL, null); String responseText = this.service.get(TEMPLATE_LIST_URL, null);
return WxMaGsonBuilder.create().fromJson(GsonParser.parse(responseText) return WxMaGsonBuilder.create().fromJson(GsonParser.parse(responseText)
.getAsJsonArray("data"), new TypeToken<List<TemplateInfo>>() { .getAsJsonArray("data"), new TypeToken<List<TemplateInfo>>() {
}.getType()); }.getType());
@ -59,15 +67,24 @@ public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
@Override @Override
public boolean delTemplate(String templateId) throws WxErrorException { public boolean delTemplate(String templateId) throws WxErrorException {
this.wxMaService.post(TEMPLATE_DEL_URL, ImmutableMap.of("priTmplId", templateId)); this.service.post(TEMPLATE_DEL_URL, ImmutableMap.of("priTmplId", templateId));
return true; return true;
} }
@Override @Override
public List<CategoryData> getCategory() throws WxErrorException { public List<CategoryData> getCategory() throws WxErrorException {
String responseText = this.wxMaService.get(GET_CATEGORY_URL, null); String responseText = this.service.get(GET_CATEGORY_URL, null);
return WxMaGsonBuilder.create().fromJson(GsonParser.parse(responseText) return WxMaGsonBuilder.create().fromJson(GsonParser.parse(responseText)
.getAsJsonArray("data"), new TypeToken<List<CategoryData>>() { .getAsJsonArray("data"), new TypeToken<List<CategoryData>>() {
}.getType()); }.getType());
} }
@Override
public void sendSubscribeMsg(WxMaSubscribeMessage subscribeMessage) throws WxErrorException {
String responseContent = this.service.post(SUBSCRIBE_MSG_SEND_URL, subscribeMessage.toJson());
JsonObject jsonObject = GsonParser.parse(responseContent);
if (jsonObject.get(WxMaConstants.ERRCODE).getAsInt() != 0) {
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
}
}
} }

View File

@ -60,7 +60,7 @@ public class WxMaSubscribeMessage implements Serializable {
* 描述: 模板内容,不填则下发空模板 * 描述: 模板内容,不填则下发空模板
* </pre> * </pre>
*/ */
private List<Data> data; private List<MsgData> data;
/** /**
* 跳转小程序类型developer为开发版trial为体验版formal为正式版默认为正式版 * 跳转小程序类型developer为开发版trial为体验版formal为正式版默认为正式版
@ -72,7 +72,7 @@ public class WxMaSubscribeMessage implements Serializable {
*/ */
private String lang = WxMaConstants.MiniProgramLang.ZH_CN; private String lang = WxMaConstants.MiniProgramLang.ZH_CN;
public WxMaSubscribeMessage addData(Data datum) { public WxMaSubscribeMessage addData(MsgData datum) {
if (this.data == null) { if (this.data == null) {
this.data = new ArrayList<>(); this.data = new ArrayList<>();
} }
@ -86,10 +86,10 @@ public class WxMaSubscribeMessage implements Serializable {
return WxMaGsonBuilder.create().toJson(this); return WxMaGsonBuilder.create().toJson(this);
} }
@lombok.Data @Data
@NoArgsConstructor @NoArgsConstructor
@AllArgsConstructor @AllArgsConstructor
public static class Data implements Serializable { public static class MsgData implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private String name; private String name;

View File

@ -0,0 +1,19 @@
package cn.binarywang.wx.miniapp.bean.subscribemsg;
import lombok.Data;
import java.io.Serializable;
/**
* .
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2021-01-27
*/
@Data
public class CategoryData implements Serializable {
private static final long serialVersionUID = -5935548352317679892L;
private int id;
private String name;
}

View File

@ -0,0 +1,21 @@
package cn.binarywang.wx.miniapp.bean.subscribemsg;
import lombok.Data;
import java.io.Serializable;
/**
* .
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2021-01-27
*/
@Data
public class PubTemplateKeyword implements Serializable {
private static final long serialVersionUID = -1100641668859815647L;
private int kid;
private String name;
private String example;
private String rule;
}

View File

@ -0,0 +1,22 @@
package cn.binarywang.wx.miniapp.bean.subscribemsg;
import lombok.Data;
import java.io.Serializable;
/**
* .
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
* @date 2021-01-27
*/
@Data
public class TemplateInfo implements Serializable {
private static final long serialVersionUID = 6971785763573992264L;
private String priTmplId;
private String title;
private String content;
private String example;
private int type;
}

View File

@ -38,7 +38,7 @@ public class WxMaSubscribeMessageGsonAdapter implements JsonSerializer<WxMaSubsc
return messageJson; return messageJson;
} }
for (WxMaSubscribeMessage.Data datum : message.getData()) { for (WxMaSubscribeMessage.MsgData datum : message.getData()) {
JsonObject dataJson = new JsonObject(); JsonObject dataJson = new JsonObject();
dataJson.addProperty("value", datum.getValue()); dataJson.addProperty("value", datum.getValue());
data.add(datum.getName(), dataJson); data.add(datum.getName(), dataJson);

View File

@ -46,9 +46,9 @@ public class WxMaMsgServiceImplTest {
message.setToUser(config.getOpenid()); message.setToUser(config.getOpenid());
message.setLang(WxMaConstants.MiniProgramLang.ZH_CN); message.setLang(WxMaConstants.MiniProgramLang.ZH_CN);
message.setMiniprogramState(WxMaConstants.MiniProgramState.FORMAL); message.setMiniprogramState(WxMaConstants.MiniProgramState.FORMAL);
message.addData(new WxMaSubscribeMessage.Data("thing1", "苹果到货啦")); message.addData(new WxMaSubscribeMessage.MsgData("thing1", "苹果到货啦"));
message.addData(new WxMaSubscribeMessage.Data("amount3", "¥5")); message.addData(new WxMaSubscribeMessage.MsgData("amount3", "¥5"));
message.addData(new WxMaSubscribeMessage.Data("thing5", "记得领取哦")); message.addData(new WxMaSubscribeMessage.MsgData("thing5", "记得领取哦"));
this.wxService.getMsgService().sendSubscribeMsg(message); this.wxService.getMsgService().sendSubscribeMsg(message);
} }

View File

@ -1,7 +1,9 @@
package cn.binarywang.wx.miniapp.api.impl; package cn.binarywang.wx.miniapp.api.impl;
import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService; import cn.binarywang.wx.miniapp.bean.subscribemsg.CategoryData;
import cn.binarywang.wx.miniapp.bean.subscribemsg.PubTemplateKeyword;
import cn.binarywang.wx.miniapp.bean.subscribemsg.TemplateInfo;
import cn.binarywang.wx.miniapp.bean.template.WxMaPubTemplateTitleListResult; import cn.binarywang.wx.miniapp.bean.template.WxMaPubTemplateTitleListResult;
import cn.binarywang.wx.miniapp.test.ApiTestModule; import cn.binarywang.wx.miniapp.test.ApiTestModule;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
@ -35,7 +37,7 @@ public class WxMaSubscribeServiceImplTest {
@Test @Test
public void testGetPubTemplateKeyWordsById() throws WxErrorException { public void testGetPubTemplateKeyWordsById() throws WxErrorException {
final List<WxMaSubscribeService.PubTemplateKeyword> result = this.wxService.getSubscribeService().getPubTemplateKeyWordsById("99"); final List<PubTemplateKeyword> result = this.wxService.getSubscribeService().getPubTemplateKeyWordsById("99");
System.out.println(result); System.out.println(result);
} }
@ -47,7 +49,7 @@ public class WxMaSubscribeServiceImplTest {
@Test @Test
public void testGetTemplateList() throws WxErrorException { public void testGetTemplateList() throws WxErrorException {
final List<WxMaSubscribeService.TemplateInfo> templateList = this.wxService.getSubscribeService().getTemplateList(); final List<TemplateInfo> templateList = this.wxService.getSubscribeService().getTemplateList();
System.out.println(templateList); System.out.println(templateList);
} }
@ -58,7 +60,7 @@ public class WxMaSubscribeServiceImplTest {
@Test @Test
public void testGetCategory() throws WxErrorException { public void testGetCategory() throws WxErrorException {
final List<WxMaSubscribeService.CategoryData> categoryData = this.wxService.getSubscribeService().getCategory(); final List<CategoryData> categoryData = this.wxService.getSubscribeService().getCategory();
assertThat(categoryData).isNotNull(); assertThat(categoryData).isNotNull();
System.out.println(categoryData); System.out.println(categoryData);
} }

View File

@ -3,7 +3,6 @@ package me.chanjar.weixin.mp.config;
import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.enums.TicketType; import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.mp.bean.WxMpHostConfig;
import java.io.File; import java.io.File;
import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.Lock;

View File

@ -1,4 +1,4 @@
package me.chanjar.weixin.mp.bean; package me.chanjar.weixin.mp.config;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Builder; import lombok.Builder;
@ -29,6 +29,7 @@ public class WxMpHostConfig {
* 对应于https://open.weixin.qq.com * 对应于https://open.weixin.qq.com
*/ */
private String openHost; private String openHost;
/** /**
* 对应于https://mp.weixin.qq.com * 对应于https://mp.weixin.qq.com
*/ */

View File

@ -4,7 +4,7 @@ import lombok.Data;
import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.enums.TicketType; import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.mp.bean.WxMpHostConfig; import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder; import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;

View File

@ -2,10 +2,10 @@ package me.chanjar.weixin.mp.enums;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import me.chanjar.weixin.mp.bean.WxMpHostConfig; import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import static me.chanjar.weixin.mp.bean.WxMpHostConfig.*; import static me.chanjar.weixin.mp.config.WxMpHostConfig.*;
/** /**
* <pre> * <pre>

View File

@ -6,7 +6,7 @@ import lombok.Data;
import me.chanjar.weixin.common.bean.WxAccessToken; import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.enums.TicketType; import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.mp.bean.WxMpHostConfig; import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.open.api.WxOpenConfigStorage; import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken; import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;