1   package com.foxinmy.weixin4j.mp.message;
2   
3   import java.io.Serializable;
4   import java.util.HashMap;
5   import java.util.Map;
6   
7   import com.alibaba.fastjson.annotation.JSONCreator;
8   import com.alibaba.fastjson.annotation.JSONField;
9   import com.foxinmy.weixin4j.util.NameValue;
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  public class TemplateMessage implements Serializable {
22  
23      private static final long serialVersionUID = 7950608393821661436L;
24  
25      
26  
27  
28      @JSONField(name = "touser")
29      private String toUser;
30      
31  
32  
33      @JSONField(name = "template_id")
34      private String templateId;
35      
36  
37  
38      private String url;
39      
40  
41  
42      @JSONField(serialize = false)
43      private NameValue head;
44      
45  
46  
47      @JSONField(serialize = false)
48      private NameValue tail;
49  
50      
51  
52  
53      @JSONField(name = "miniprogram")
54      private Miniprogram miniprogram;
55      
56  
57  
58      @JSONField(name = "data")
59      private Map<String, NameValue> content;
60  
61      private final static String HEAD_KEY = "first";
62      private final static String TAIL_KEY = "remark";
63      private final static String DEFAULT_COLOR = "#173177";
64  
65      @JSONCreator
66      public TemplateMessage(@JSONField(name = "toUser") String toUser, @JSONField(name = "templateId") String templateId,
67              @JSONField(name = "url") String url) {
68          this.toUser = toUser;
69          this.templateId = templateId;
70          this.url = url;
71          this.content = new HashMap<String, NameValue>();
72      }
73  
74      public String getToUser() {
75          return toUser;
76      }
77  
78      public String getTemplateId() {
79          return templateId;
80      }
81  
82      public String getUrl() {
83          return url;
84      }
85  
86      public NameValue getHead() {
87          return head == null ? content.get(HEAD_KEY) : head;
88      }
89  
90      public NameValue getTail() {
91          return tail == null ? content.get(TAIL_KEY) : tail;
92      }
93  
94      public Map<String, NameValue> getContent() {
95          return content;
96      }
97  
98      
99  
100 
101 
102 
103 
104 
105     public TemplateMessage pushHead(String text) {
106         return pushHead("#FF0000", text);
107     }
108 
109     
110 
111 
112 
113 
114 
115 
116 
117 
118     public TemplateMessage pushHead(String color, String text) {
119         head = new NameValue(color, text);
120         content.put(HEAD_KEY, head);
121         return this;
122     }
123 
124     
125 
126 
127 
128 
129 
130 
131     public TemplateMessage pushTail(String text) {
132         return pushTail(DEFAULT_COLOR, text);
133     }
134 
135     
136 
137 
138 
139 
140 
141 
142 
143 
144     public TemplateMessage pushTail(String color, String text) {
145         tail = new NameValue(color, text);
146         content.put(TAIL_KEY, tail);
147         return this;
148     }
149 
150     
151 
152 
153 
154 
155 
156 
157 
158 
159     public TemplateMessage pushItem(String key, String text) {
160         return pushItem(key, DEFAULT_COLOR, text);
161     }
162 
163     
164 
165 
166 
167 
168 
169 
170 
171 
172 
173 
174     public TemplateMessage pushItem(String key, String color, String text) {
175         content.put(key, new NameValue(color, text));
176         return this;
177     }
178 
179     
180 
181 
182 
183 
184     public void setItems(Map<String, NameValue> items) {
185         this.content = items;
186     }
187 
188     public Miniprogram getMiniprogram() {
189 		return miniprogram;
190 	}
191 
192 	public void setMiniprogram(String appid,String pagepath) {
193         this.miniprogram = new Miniprogram(appid, pagepath);
194     }
195 
196     @Override
197     public String toString() {
198         return "TemplateMessage [toUser=" + toUser + ", templateId=" + templateId + ", url=" + url + ", head="
199                 + getHead() + ", tail=" + getTail() + ", content=" + content + "]";
200     }
201     
202 
203 
204 
205 
206 
207 
208 
209     static class Miniprogram{
210     	  
211 
212 
213         private String appid;
214         
215 
216 
217         private String pagepath;
218 		public Miniprogram(String appid, String pagepath) {
219 			this.appid = appid;
220 			this.pagepath = pagepath;
221 		}
222 		public String getAppid() {
223 			return appid;
224 		}
225 		public String getPagepath() {
226 			return pagepath;
227 		}
228     }
229 }