java 代码
js
public StringwxCard(String cardId){
String url ="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=wx_card".replace("ACCESS_TOKEN", WXUtil.getaccess_token_func());
String str = WXUtil.sendGet(url);
JSONObject obj = JSONObject.fromObject(str);
String api_ticket = obj.get("ticket").toString();
String nonce_str =create_nonce_str();
String timestamp =create_timestamp();
String card_id = cardId;
String signature ="";
ArrayList list=new ArrayList();
list.add(api_ticket);
list.add(nonce_str);
list.add(timestamp);
list.add(card_id);
Collections.sort(list);
String sign =list.get(0) + list.get(1)+list.get(2)+list.get(3);
try
{
MessageDigest crypt = MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(sign.getBytes("UTF-8"));
signature =byteToHex(crypt.digest());
}
catch (Exception e)
{
e.printStackTrace();
}
JSONObject jsonObject =new JSONObject();
jsonObject.put("code","");
jsonObject.put("openid","");
jsonObject.put("card_id",card_id);
jsonObject.put("nonce_str",nonce_str);
jsonObject.put("timestamp",timestamp);
jsonObject.put("signature",signature);
return jsonObject.toString();
}
private static StringbyteToHex(final byte[] hash) {
Formatter formatter =new Formatter();
for (byte b : hash)
{
formatter.format("%02x", b);
}
String result = formatter.toString();
formatter.close();
return result;
}
private static Stringcreate_nonce_str() {
return UUID.randomUUID().toString();
}
private static Stringcreate_timestamp() {
return Long.toString(System.currentTimeMillis() /1000);
}