-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8e9d9d
commit e237f0b
Showing
12 changed files
with
429 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaTemplateData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package cn.binarywang.wx.miniapp.bean; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* <pre> | ||
* | ||
* Created by Binary Wang on 2018/9/23. | ||
* </pre> | ||
* | ||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
public class WxMaTemplateData { | ||
private String name; | ||
private String value; | ||
private String color; | ||
|
||
public WxMaTemplateData(String name, String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
public WxMaTemplateData(String name, String value, String color) { | ||
this.name = name; | ||
this.value = value; | ||
this.color = color; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaUniformMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package cn.binarywang.wx.miniapp.bean; | ||
|
||
import java.io.Serializable; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
/** | ||
* 模板消息. | ||
* 参考 https://mp.weixin.qq.com/debug/wxadoc/dev/api/notice.html#接口说明 模板消息部分 | ||
* | ||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | ||
*/ | ||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public class WxMaUniformMessage implements Serializable { | ||
private static final long serialVersionUID = 5063374783759519418L; | ||
|
||
/** | ||
* 是否发送公众号模版消息,否则发送小程序模版消息. | ||
*/ | ||
private boolean isMpTemplateMsg; | ||
|
||
/** | ||
* 用户openid. | ||
* 可以是小程序的openid,也可以是mp_template_msg.appid对应的公众号的openid | ||
*/ | ||
private String toUser; | ||
|
||
/** | ||
* 公众号appid,要求与小程序有绑定且同主体. | ||
*/ | ||
private String appid; | ||
|
||
/** | ||
* 公众号或小程序模板ID. | ||
*/ | ||
private String templateId; | ||
|
||
/** | ||
* 公众号模板消息所要跳转的url. | ||
*/ | ||
private String url; | ||
|
||
/** | ||
* 小程序页面路径. | ||
*/ | ||
private String page; | ||
|
||
/** | ||
* 小程序模板消息formid. | ||
*/ | ||
private String formId; | ||
|
||
/** | ||
* 公众号模板消息所要跳转的小程序,小程序的必须与公众号具有绑定关系. | ||
*/ | ||
private MiniProgram miniProgram; | ||
|
||
/** | ||
* 小程序模板数据. | ||
*/ | ||
private List<WxMaTemplateData> data; | ||
|
||
/** | ||
* 模板需要放大的关键词,不填则默认无放大. | ||
*/ | ||
private String emphasisKeyword; | ||
|
||
public WxMaUniformMessage addData(WxMaTemplateData datum) { | ||
if (this.data == null) { | ||
this.data = new ArrayList<>(); | ||
} | ||
this.data.add(datum); | ||
|
||
return this; | ||
} | ||
|
||
public String toJson() { | ||
return WxMaGsonBuilder.create().toJson(this); | ||
} | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class MiniProgram implements Serializable { | ||
private static final long serialVersionUID = -7945254706501974849L; | ||
|
||
private String appid; | ||
private String pagePath; | ||
|
||
/** | ||
* 是否使用path,否则使用pagepath. | ||
* 加入此字段是基于微信官方接口变化多端的考虑 | ||
*/ | ||
private boolean usePath = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...niapp/src/main/java/cn/binarywang/wx/miniapp/util/json/WxMaUniformMessageGsonAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package cn.binarywang.wx.miniapp.util.json; | ||
|
||
import java.lang.reflect.Type; | ||
|
||
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData; | ||
import cn.binarywang.wx.miniapp.bean.WxMaUniformMessage; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonSerializationContext; | ||
import com.google.gson.JsonSerializer; | ||
|
||
/** | ||
* @author <a href="https://github.com/binarywang">Binary Wang</a> | ||
*/ | ||
public class WxMaUniformMessageGsonAdapter implements JsonSerializer<WxMaUniformMessage> { | ||
|
||
@Override | ||
public JsonElement serialize(WxMaUniformMessage message, Type typeOfSrc, JsonSerializationContext context) { | ||
JsonObject messageJson = new JsonObject(); | ||
messageJson.addProperty("touser", message.getToUser()); | ||
if (message.isMpTemplateMsg()) { | ||
JsonObject msg = new JsonObject(); | ||
if (message.getAppid() != null) { | ||
msg.addProperty("appid", message.getAppid()); | ||
} | ||
|
||
msg.addProperty("template_id", message.getTemplateId()); | ||
|
||
if (message.getUrl() != null) { | ||
msg.addProperty("url", message.getUrl()); | ||
} | ||
|
||
final WxMaUniformMessage.MiniProgram miniProgram = message.getMiniProgram(); | ||
if (miniProgram != null) { | ||
JsonObject miniProgramJson = new JsonObject(); | ||
miniProgramJson.addProperty("appid", miniProgram.getAppid()); | ||
if (miniProgram.isUsePath()) { | ||
miniProgramJson.addProperty("path", miniProgram.getPagePath()); | ||
} else { | ||
miniProgramJson.addProperty("pagepath", miniProgram.getPagePath()); | ||
} | ||
msg.add("miniprogram", miniProgramJson); | ||
} | ||
|
||
if (message.getData() != null) { | ||
JsonObject data = new JsonObject(); | ||
for (WxMaTemplateData templateData : message.getData()) { | ||
JsonObject dataJson = new JsonObject(); | ||
dataJson.addProperty("value", templateData.getValue()); | ||
if (templateData.getColor() != null) { | ||
dataJson.addProperty("color", templateData.getColor()); | ||
} | ||
data.add(templateData.getName(), dataJson); | ||
} | ||
msg.add("data", data); | ||
} | ||
|
||
|
||
messageJson.add("mp_template_msg", msg); | ||
return messageJson; | ||
} | ||
|
||
//小程序模版消息 | ||
JsonObject msg = new JsonObject(); | ||
msg.addProperty("template_id", message.getTemplateId()); | ||
|
||
if (message.getPage() != null) { | ||
msg.addProperty("page", message.getPage()); | ||
} | ||
|
||
if (message.getFormId() != null) { | ||
msg.addProperty("form_id", message.getFormId()); | ||
} | ||
|
||
JsonObject data = new JsonObject(); | ||
msg.add("data", data); | ||
|
||
if (message.getData() != null) { | ||
for (WxMaTemplateData templateData : message.getData()) { | ||
JsonObject dataJson = new JsonObject(); | ||
dataJson.addProperty("value", templateData.getValue()); | ||
if (templateData.getColor() != null) { | ||
dataJson.addProperty("color", templateData.getColor()); | ||
} | ||
data.add(templateData.getName(), dataJson); | ||
} | ||
} | ||
|
||
if (message.getEmphasisKeyword() != null) { | ||
msg.addProperty("emphasis_keyword", message.getEmphasisKeyword()); | ||
} | ||
|
||
messageJson.add("weapp_template_msg", msg); | ||
|
||
return messageJson; | ||
} | ||
|
||
} |
Oops, something went wrong.