package com.unionpay.acp.sdk;
import java.util.UUID;
import java.util.Map;
import java.util.HashMap;
import java.util.Formatter;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.io.UnsupportedEncodingException;
import net.sf.json.JSONObject;
import com.xx.common.config.Global;
import com.unionpay.acp.sdk.HttpClient;/**
* 微信分享签名
**/
public classSign {public static voidmain(String[] args) {//String jsapi_ticket = "kgt8ON7yVITDhtdwci0qecRXz9HyXuyXgIslzmd3ri0s5EnLrVZZfrPJotRfKWQ-TTuQ2ZMAK86GUlWF1PHhcg";
};public static MapgetResult(){//String jsapi_ticket = getTicket();//注意 URL 一定要动态获取,不能 hardcode
String url = "http://www.xx.com/operation/mybusi/barcode";//String url = Global.getConfig("wx.shareUrl");
Map ret =sign(getTicket(), url);
ret.put("appId", getAppId());returnret;
}private staticString getAppId(){return Global.getConfig("wx.appid");
}private staticString getToken(){
String accessToken= "";
String url= "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+getAppId()+"&secret="+Global.getConfig("wx.secret");
HttpClient hc= new HttpClient(url, 30000, 30000);try{int status = hc.sendGet("utf-8");if (200 ==status) {
String resultString=hc.getResult();if (null != resultString && !"".equals(resultString)) {
JSONObject json=JSONObject.fromObject(resultString);if (null==json.get("errcode")){
accessToken= json.get("access_token").toString();
}else{
System.out.println(resultString);
}
}
}else{
System.out.println("返回http状态码["+status+"],请检查请求报文或者请求地址是否正确");
}
}catch(Exception e) {
e.printStackTrace();
}returnaccessToken;
}private staticString getTicket(){
String ticket= "";
String url= "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+getToken()+"&type=jsapi";
HttpClient hc= new HttpClient(url, 30000, 30000);try{int status = hc.sendGet("utf-8");if (200 ==status) {
String resultString=hc.getResult();if (null != resultString && !"".equals(resultString)) {
JSONObject json=JSONObject.fromObject(resultString);if ("0".equals(json.getString("errcode"))){
ticket= json.getString("ticket");
}else{
System.out.println(resultString);
}
}
}else{
System.out.println("返回http状态码["+status+"],请检查请求报文或者请求地址是否正确");
}
}catch(Exception e) {
e.printStackTrace();
}returnticket;
}public static Mapsign(String jsapi_ticket, String url) {
Map ret = new HashMap();
String nonce_str=create_nonce_str();
String timestamp=create_timestamp();
String string1;
String signature= "";//注意这里参数名必须全部小写,且必须有序
string1 = "jsapi_ticket=" + jsapi_ticket +
"&noncestr=" + nonce_str +
"×tamp=" + timestamp +
"&url=" +url;
System.out.println(string1);try{
MessageDigest crypt= MessageDigest.getInstance("SHA-1");
crypt.reset();
crypt.update(string1.getBytes("UTF-8"));
signature=byteToHex(crypt.digest());
}catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
ret.put("url", url);
ret.put("jsapi_ticket", jsapi_ticket);
ret.put("nonceStr", nonce_str);
ret.put("timestamp", timestamp);
ret.put("signature", signature);returnret;
}private static String byteToHex(final byte[] hash) {
Formatter formatter= newFormatter();for (byteb : hash)
{
formatter.format("%02x", b);
}
String result=formatter.toString();
formatter.close();returnresult;
}private staticString create_nonce_str() {returnUUID.randomUUID().toString();
}private staticString create_timestamp() {return Long.toString(System.currentTimeMillis() / 1000);
}
}