View Javadoc
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   * @className QRParameter
15   * @author jinyu(foxinmy@gmail.com)
16   * @date 2014年4月8日
17   * @since JDK 1.6
18   * @see #createTemporaryQR(int, long) 创建整型临时二维码
19   * @see #createPermanenceQR(int) 创建整型永久二维码
20   * @see #createPermanenceQR(String) 创建字符串型永久二维码
21   * @see #createCardCouponQR(Integer, CardQR...) 创建卡券二维码
22   */
23  public class QRParameter implements Serializable {
24  
25  	private static final long serialVersionUID = 6611187606558274253L;
26  
27  	/**
28  	 * 二维码的类型
29  	 * 
30  	 * @see com.foxinmy.weixin4j.type.QRType
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  	 * 创建临时二维码(场景值为int)
76  	 * 
77  	 * @param expireSeconds
78  	 *            二维码有效时间,以秒为单位。 最大不超过2592000(即30天)
79  	 * @param sceneValue
80  	 *            二维码的场景值 <font color="red">临时二维码最大值为无符号32位非0整型</font>
81  	 * @return 二维码参数
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  	 * 创建临时二维码(场景值为string)
95  	 * 
96  	 * @param expireSeconds
97  	 *            二维码有效时间,以秒为单位。 最大不超过2592000(即30天)
98  	 * @param sceneValue
99  	 *            二维码的场景值
100 	 * @return 二维码参数
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 	 * 创建永久二维码(场景值为int)
114 	 * 
115 	 * @param sceneValue
116 	 *            场景值 最大值为100000 (目前参数只支持1--100000)
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 	 * 创建永久二维码(场景值为string)
129 	 * 
130 	 * @param sceneValue
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 	 * @param expireSeconds
146 	 *            指定二维码的有效时间,范围是60 ~ 1800秒。不填默认为365天有效
147 	 * @param cardQRs
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 }