Skip to content

Commit

Permalink
🎨 重构优化部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Jan 27, 2021
1 parent 6811bff commit 584c7ac
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
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.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
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 lombok.Data;
import me.chanjar.weixin.common.error.WxErrorException;

import java.util.List;
Expand Down Expand Up @@ -43,6 +46,11 @@ public interface WxMaSubscribeService {
*/
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>
* 获取帐号所属类目下的公共模板标题
Expand Down Expand Up @@ -128,26 +136,15 @@ public interface WxMaSubscribeService {
*/
List<CategoryData> getCategory() throws WxErrorException;

@Data
class CategoryData {
int id;
String name;
}

@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;
}
/**
* <pre>
* 发送订阅消息
* 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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,20 @@

import cn.binarywang.wx.miniapp.api.WxMaService;
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.constant.WxMaConstants;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
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.util.json.GsonParser;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -21,20 +29,20 @@
*/
@RequiredArgsConstructor
public class WxMaSubscribeServiceImpl implements WxMaSubscribeService {
private final WxMaService wxMaService;
private final WxMaService service;

@Override
public WxMaPubTemplateTitleListResult getPubTemplateTitleList(String[] ids, int start, int limit) throws WxErrorException {
ImmutableMap<String, ? extends Serializable> params = ImmutableMap.of("ids", StringUtils.join(ids, ","),
"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));
return WxMaPubTemplateTitleListResult.fromJson(responseText);
}

@Override
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)));
return WxMaGsonBuilder.create().fromJson(GsonParser.parse(responseText)
.getAsJsonArray("data"), new TypeToken<List<PubTemplateKeyword>>() {
Expand All @@ -43,31 +51,40 @@ public List<PubTemplateKeyword> getPubTemplateKeyWordsById(String id) throws WxE

@Override
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(),
"sceneDesc", sceneDesc));
return GsonParser.parse(responseText).get("priTmplId").getAsString();
}

@Override
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)
.getAsJsonArray("data"), new TypeToken<List<TemplateInfo>>() {
}.getType());
}

@Override
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;
}

@Override
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)
.getAsJsonArray("data"), new TypeToken<List<CategoryData>>() {
}.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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class WxMaSubscribeMessage implements Serializable {
* 描述: 模板内容,不填则下发空模板
* </pre>
*/
private List<Data> data;
private List<MsgData> data;

/**
* 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
Expand All @@ -72,7 +72,7 @@ public class WxMaSubscribeMessage implements Serializable {
*/
private String lang = WxMaConstants.MiniProgramLang.ZH_CN;

public WxMaSubscribeMessage addData(Data datum) {
public WxMaSubscribeMessage addData(MsgData datum) {
if (this.data == null) {
this.data = new ArrayList<>();
}
Expand All @@ -86,10 +86,10 @@ public String toJson() {
return WxMaGsonBuilder.create().toJson(this);
}

@lombok.Data
@Data
@NoArgsConstructor
@AllArgsConstructor
public static class Data implements Serializable {
public static class MsgData implements Serializable {
private static final long serialVersionUID = 1L;

private String name;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public JsonElement serialize(WxMaSubscribeMessage message, Type typeOfSrc, JsonS
return messageJson;
}

for (WxMaSubscribeMessage.Data datum : message.getData()) {
for (WxMaSubscribeMessage.MsgData datum : message.getData()) {
JsonObject dataJson = new JsonObject();
dataJson.addProperty("value", datum.getValue());
data.add(datum.getName(), dataJson);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void testSendSubscribeMsg() throws WxErrorException {
message.setToUser(config.getOpenid());
message.setLang(WxMaConstants.MiniProgramLang.ZH_CN);
message.setMiniprogramState(WxMaConstants.MiniProgramState.FORMAL);
message.addData(new WxMaSubscribeMessage.Data("thing1", "苹果到货啦"));
message.addData(new WxMaSubscribeMessage.Data("amount3", "¥5"));
message.addData(new WxMaSubscribeMessage.Data("thing5", "记得领取哦"));
message.addData(new WxMaSubscribeMessage.MsgData("thing1", "苹果到货啦"));
message.addData(new WxMaSubscribeMessage.MsgData("amount3", "¥5"));
message.addData(new WxMaSubscribeMessage.MsgData("thing5", "记得领取哦"));
this.wxService.getMsgService().sendSubscribeMsg(message);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package cn.binarywang.wx.miniapp.api.impl;

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.test.ApiTestModule;
import com.google.common.collect.Lists;
Expand Down Expand Up @@ -35,7 +37,7 @@ public void testGetPubTemplateTitleList() throws WxErrorException {

@Test
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);
}

Expand All @@ -47,7 +49,7 @@ public void testAddTemplate() throws WxErrorException {

@Test
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);
}

Expand All @@ -58,7 +60,7 @@ public void testDelTemplate() throws WxErrorException {

@Test
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();
System.out.println(categoryData);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.enums.TicketType;
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
import me.chanjar.weixin.mp.bean.WxMpHostConfig;

import java.io.File;
import java.util.concurrent.locks.Lock;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.chanjar.weixin.mp.bean;
package me.chanjar.weixin.mp.config;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down Expand Up @@ -29,6 +29,7 @@ public class WxMpHostConfig {
* 对应于:https://open.weixin.qq.com
*/
private String openHost;

/**
* 对应于:https://mp.weixin.qq.com
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.enums.TicketType;
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.util.json.WxMpGsonBuilder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import lombok.AllArgsConstructor;
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 static me.chanjar.weixin.mp.bean.WxMpHostConfig.*;
import static me.chanjar.weixin.mp.config.WxMpHostConfig.*;

/**
* <pre>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import me.chanjar.weixin.common.bean.WxAccessToken;
import me.chanjar.weixin.common.enums.TicketType;
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.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.bean.WxOpenAuthorizerAccessToken;
Expand Down

0 comments on commit 584c7ac

Please sign in to comment.