File tree Expand file tree Collapse file tree 2 files changed +84
-0
lines changed
main/java/cn/binarywang/wx/miniapp/message
test/java/cn/binarywang/wx/miniapp/message Expand file tree Collapse file tree 2 files changed +84
-0
lines changed Original file line number Diff line number Diff line change 1+ package cn .binarywang .wx .miniapp .message ;
2+
3+ import cn .binarywang .wx .miniapp .config .WxMaConfig ;
4+ import cn .binarywang .wx .miniapp .json .WxMaGsonBuilder ;
5+ import cn .binarywang .wx .miniapp .util .crypt .WxMaCryptUtils ;
6+ import lombok .AllArgsConstructor ;
7+ import lombok .Builder ;
8+ import lombok .Data ;
9+ import lombok .NoArgsConstructor ;
10+ import lombok .experimental .Accessors ;
11+
12+ import java .io .Serializable ;
13+
14+ /**
15+ * 微信小程序输出给微信服务器的JSON格式消息.
16+ *
17+ * @author <a href="https://github.com/binarywang">Binary Wang</a>
18+ */
19+ @ Data
20+ @ Accessors (chain = true )
21+ @ Builder
22+ @ AllArgsConstructor
23+ @ NoArgsConstructor
24+ public class WxMaJsonOutMessage implements Serializable {
25+ private static final long serialVersionUID = 4241135225946919154L ;
26+
27+ protected String toUserName ;
28+ protected String fromUserName ;
29+ protected Long createTime ;
30+ protected String msgType ;
31+
32+ /**
33+ * 转换成JSON格式.
34+ */
35+ public String toJson () {
36+ return WxMaGsonBuilder .create ().toJson (this );
37+ }
38+
39+ /**
40+ * 转换成加密的JSON格式.
41+ */
42+ public String toEncryptedJson (WxMaConfig config ) {
43+ String plainJson = toJson ();
44+ WxMaCryptUtils pc = new WxMaCryptUtils (config );
45+ return pc .encrypt (plainJson );
46+ }
47+ }
Original file line number Diff line number Diff line change 1+ package cn .binarywang .wx .miniapp .message ;
2+
3+ import me .chanjar .weixin .common .api .WxConsts ;
4+ import org .testng .annotations .Test ;
5+
6+ import static org .assertj .core .api .Assertions .assertThat ;
7+
8+ public class WxMaJsonOutMessageTest {
9+
10+ @ Test
11+ public void testToJson () {
12+ WxMaJsonOutMessage message = WxMaJsonOutMessage .builder ()
13+ .fromUserName ("test_from_user" )
14+ .toUserName ("test_to_user" )
15+ .msgType (WxConsts .XmlMsgType .TRANSFER_CUSTOMER_SERVICE )
16+ .createTime (System .currentTimeMillis () / 1000 )
17+ .build ();
18+
19+ String jsonResult = message .toJson ();
20+ assertThat (jsonResult ).isNotEmpty ();
21+ assertThat (jsonResult ).contains ("test_from_user" );
22+ assertThat (jsonResult ).contains ("test_to_user" );
23+ assertThat (jsonResult ).contains (WxConsts .XmlMsgType .TRANSFER_CUSTOMER_SERVICE );
24+
25+ System .out .println ("JSON Output:" );
26+ System .out .println (jsonResult );
27+ }
28+
29+ @ Test
30+ public void testEmptyMessage () {
31+ WxMaJsonOutMessage message = new WxMaJsonOutMessage ();
32+ String jsonResult = message .toJson ();
33+ assertThat (jsonResult ).isNotEmpty ();
34+ System .out .println ("Empty message JSON:" );
35+ System .out .println (jsonResult );
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments