package cn.jsonlu.passguard.model; import cn.jsonlu.passguard.utils.MD5Util;
import com.fasterxml.jackson.databind.annotation.JsonSerialize; import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Locale; /**
* @author JsonLu
* @email [email protected]
* @since 2016/2/22 19:35
*/
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class BaseModel { private String time;
private String device;
private String sign;
private String token;
private String service; public String getService() {
return service;
} public void setService(String service) {
this.service = service;
} public String getTime() {
return time;
} public void setTime(String time) {
this.time = time;
} public String getDevice() {
return device;
} public void setDevice(String device) {
this.device = device;
} public String getSign() {
return sign;
} public void setSign(String sign) {
this.sign = sign;
} public String getToken() {
return token;
} public void setToken(String token) {
this.token = token;
} public void makeSign() {
setSign(MD5Util.MD5(toString()));
} /**
* 验签
*
* @return
*/
public boolean verifSign() {
String trueSign = MD5Util.MD5(toString());
if (trueSign.equals(sign)) {
return true;
}
System.out.println("签名:" + trueSign);
return false;
} /**
* 遍历清空属性值
*/
public void clear() {
String[] fields = invokeFieldsName();
for (int i = ; i < fields.length; i++) {
try {
invokeMethod(this, fields[i]);
} catch (Exception e) {
e.printStackTrace();
}
}
} /**
* 清空属性值
*
* @param owner
* @param fieldName
* @throws Exception
*/
public void invokeMethod(Object owner, String fieldName) throws Exception {
Class<? extends Object> ownerClass = owner.getClass();
String methodName = fieldName.substring(, ).toUpperCase(Locale.getDefault()) + fieldName.substring();
Method method = ownerClass.getMethod("set" + methodName, String.class);
String str = new String();
str = null;
//此处不能直接放null,否则报错
method.invoke(owner, str);
} /**
* 获取属性名称列表
*
* @return
*/
public String[] invokeFieldsName() {
Field[] superFields = getClass().getSuperclass().getDeclaredFields();
Field[] fields = getClass().getDeclaredFields();
String[] fieldsName = new String[superFields.length + fields.length];
for (int i = ; i < superFields.length; i++) {
fieldsName[i] = superFields[i].getName();
}
for (int i = ; i < fields.length; i++) {
fieldsName[superFields.length + i] = fields[i].getName();
}
Arrays.sort(fieldsName);
return fieldsName;
} /**
* 获取属性值
*
* @param owner
* @param fieldName
* @param args
* @return
* @throws Exception
*/
public Object invokeMethod(Object owner, String fieldName, Object[] args) throws Exception {
Class<? extends Object> ownerClass = owner.getClass();
String methodName = fieldName.substring(, ).toUpperCase(Locale.getDefault()) + fieldName.substring();
Method method = ownerClass.getMethod("get" + methodName);
return method.invoke(owner);
} /**
* 转换为device=00-787-88855-6644&sign=45555格式
*
* @return
*/
@Override
public String toString() {
String[] fields = invokeFieldsName();
StringBuffer buffer = new StringBuffer();
for (int i = ; i < fields.length; i++) {
if ("sign".equals(fields[i])) {
continue;
}
Object value = null;
try {
value = invokeMethod(this, fields[i], null);
} catch (Exception e) {
e.printStackTrace();
}
if (null != value && !"".equals(value.toString())) {
buffer.append(fields[i] + "=" + value + "&");
}
}
if (buffer.length() == )
return null;
if (buffer.lastIndexOf("&") == (buffer.length() - )) {
buffer.setLength(buffer.length() - );
}
System.out.println("params source is : " + buffer.toString());
return buffer.toString();
}
}