Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add JSON format support for WeChat Mini Program message push #3723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Copilot wants to merge 2 commits into develop
base: develop
Choose a base branch
Loading
from copilot/fix-7cdffe6f-47dd-430c-9832-5bb69314dad6
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package cn.binarywang.wx.miniapp.message;

import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder;
import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

/**
* 微信小程序输出给微信服务器的JSON格式消息.
*
* @author <a href="https://github.com/binarywang">Binary Wang</a>
*/
@Data
@Accessors(chain = true)
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class WxMaJsonOutMessage implements Serializable {
private static final long serialVersionUID = 4241135225946919154L;

protected String toUserName;
protected String fromUserName;
protected Long createTime;
protected String msgType;

/**
* 转换成JSON格式.
*/
public String toJson() {
return WxMaGsonBuilder.create().toJson(this);
}

/**
* 转换成加密的JSON格式.
*/
public String toEncryptedJson(WxMaConfig config) {
String plainJson = toJson();
WxMaCryptUtils pc = new WxMaCryptUtils(config);
return pc.encrypt(plainJson);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package cn.binarywang.wx.miniapp.message;

import me.chanjar.weixin.common.api.WxConsts;
import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class WxMaJsonOutMessageTest {

@Test
public void testToJson() {
WxMaJsonOutMessage message = WxMaJsonOutMessage.builder()
.fromUserName("test_from_user")
.toUserName("test_to_user")
.msgType(WxConsts.XmlMsgType.TRANSFER_CUSTOMER_SERVICE)
.createTime(System.currentTimeMillis() / 1000)
.build();

String jsonResult = message.toJson();
assertThat(jsonResult).isNotEmpty();
assertThat(jsonResult).contains("test_from_user");
assertThat(jsonResult).contains("test_to_user");
assertThat(jsonResult).contains(WxConsts.XmlMsgType.TRANSFER_CUSTOMER_SERVICE);

System.out.println("JSON Output:");
System.out.println(jsonResult);
}

@Test
public void testEmptyMessage() {
WxMaJsonOutMessage message = new WxMaJsonOutMessage();
String jsonResult = message.toJson();
assertThat(jsonResult).isNotEmpty();
System.out.println("Empty message JSON:");
System.out.println(jsonResult);
}
}

AltStyle によって変換されたページ (->オリジナル) /