1 package com.foxinmy.weixin4j.model.qr;
2
3 import java.io.Serializable;
4
5 import com.alibaba.fastjson.JSONObject;
6 import com.alibaba.fastjson.annotation.JSONField;
7 import com.foxinmy.weixin4j.model.card.CardQR;
8 import com.foxinmy.weixin4j.type.QRType;
9 import com.foxinmy.weixin4j.util.Consts;
10
11
12
13
14
15
16
17
18
19
20
21
22
23 public class QRParameter implements Serializable {
24
25 private static final long serialVersionUID = 6611187606558274253L;
26
27
28
29
30
31
32 @JSONField(name = "action_name")
33 private QRType qrType;
34
35
36
37 @JSONField(name = "expire_seconds")
38 private Integer expireSeconds;
39
40
41
42 @JSONField(serialize = false)
43 private String sceneValue;
44
45
46
47 @JSONField(name = "action_info")
48 private JSONObject sceneContent;
49
50 private QRParameter(QRType qrType, Integer expireSeconds,
51 String sceneValue, JSONObject sceneContent) {
52 this.qrType = qrType;
53 this.expireSeconds = expireSeconds;
54 this.sceneValue = sceneValue;
55 this.sceneContent = sceneContent;
56 }
57
58 public Integer getExpireSeconds() {
59 return expireSeconds;
60 }
61
62 public QRType getQrType() {
63 return qrType;
64 }
65
66 public String getSceneValue() {
67 return sceneValue;
68 }
69
70 public JSONObject getSceneContent() {
71 return sceneContent;
72 }
73
74
75
76
77
78
79
80
81
82
83 public static QRParameter createTemporaryQR(int expireSeconds,
84 long sceneValue) {
85 JSONObject sceneContent = new JSONObject();
86 JSONObject scene = new JSONObject();
87 scene.put("scene_id", sceneValue);
88 sceneContent.put("scene", scene);
89 return new QRParameter(QRType.QR_SCENE, expireSeconds,
90 Long.toString(sceneValue), sceneContent);
91 }
92
93
94
95
96
97
98
99
100
101
102 public static QRParameter createTemporaryQR(int expireSeconds,
103 String sceneValue) {
104 JSONObject sceneContent = new JSONObject();
105 JSONObject scene = new JSONObject();
106 scene.put("scene_str", sceneValue);
107 sceneContent.put("scene", scene);
108 return new QRParameter(QRType.QR_STR_SCENE, expireSeconds, sceneValue,
109 sceneContent);
110 }
111
112
113
114
115
116
117
118 public static QRParameter createPermanenceQR(int sceneValue) {
119 JSONObject sceneContent = new JSONObject();
120 JSONObject scene = new JSONObject();
121 scene.put("scene_id", sceneValue);
122 sceneContent.put("scene", scene);
123 return new QRParameter(QRType.QR_LIMIT_SCENE, null,
124 Integer.toString(sceneValue), sceneContent);
125 }
126
127
128
129
130
131
132
133 public static QRParameter createPermanenceQR(String sceneValue) {
134 JSONObject sceneContent = new JSONObject();
135 JSONObject scene = new JSONObject();
136 scene.put("scene_str", sceneValue);
137 sceneContent.put("scene", scene);
138 return new QRParameter(QRType.QR_LIMIT_STR_SCENE, null, sceneValue,
139 sceneContent);
140 }
141
142
143
144
145
146
147
148
149
150 public static QRParameter createCardCouponQR(Integer expireSeconds,
151 CardQR... cardQRs) {
152 QRType qrType = QRType.QR_CARD;
153 JSONObject sceneContent = new JSONObject();
154 StringBuilder sceneValue = new StringBuilder();
155 sceneValue.append(cardQRs[0].getSceneValue());
156 if (cardQRs.length > 1) {
157 qrType = QRType.QR_MULTIPLE_CARD;
158 JSONObject multipleCard = new JSONObject();
159 multipleCard.put("card_list", cardQRs);
160 sceneContent.put("multiple_card", multipleCard);
161 for (int i = 1; i < cardQRs.length; i++) {
162 sceneValue.append(Consts.SEPARATOR).append(
163 cardQRs[i].getSceneValue());
164 }
165 } else {
166 sceneContent.put("card", cardQRs[0]);
167 }
168 return new QRParameter(qrType, expireSeconds, sceneValue.toString(),
169 sceneContent);
170 }
171
172 @Override
173 public int hashCode() {
174 final int prime = 31;
175 int result = 1;
176 result = prime * result
177 + ((expireSeconds == null) ? 0 : expireSeconds.hashCode());
178 result = prime * result + ((qrType == null) ? 0 : qrType.hashCode());
179 result = prime * result
180 + ((sceneContent == null) ? 0 : sceneContent.hashCode());
181 return result;
182 }
183
184 @Override
185 public boolean equals(Object obj) {
186 if (this == obj)
187 return true;
188 if (obj == null)
189 return false;
190 if (getClass() != obj.getClass())
191 return false;
192 QRParameter other = (QRParameter) obj;
193 if (expireSeconds == null) {
194 if (other.expireSeconds != null)
195 return false;
196 } else if (!expireSeconds.equals(other.expireSeconds))
197 return false;
198 if (qrType != other.qrType)
199 return false;
200 if (sceneContent == null) {
201 if (other.sceneContent != null)
202 return false;
203 } else if (!sceneContent.equals(other.sceneContent))
204 return false;
205 return true;
206 }
207
208 @Override
209 public String toString() {
210 return "QRParameter [qrType=" + qrType + ", expireSeconds="
211 + expireSeconds + ", sceneContent=" + sceneContent + "]";
212 }
213 }