>  기사  >  웹 프론트엔드  >  WeChat JS 인터페이스 서명의 특정 코드 구현 예를 공유하세요.

WeChat JS 인터페이스 서명의 특정 코드 구현 예를 공유하세요.

零下一度
零下一度원래의
2018-05-25 10:02:302657검색

이 글에서는 주로 WeChat JSAPI 티켓인터페이스서명을 자세히 소개하며, 관심 있는 친구들이 참고할 수 있습니다.

이 글의 예는 모든 사람을 위해 WeChat JS 인터페이스 서명의 특정 코드를 공유합니다. 구체적인 내용은 다음과 같습니다

1. WeChat JS 인터페이스 서명 확인 도구

2. 특정 개발

2.1 access_token을 얻은 다음 jsapi_ticket

/**
  * 获取access_token,然后jsapi_ticket
  */
 private String getAccessToken_ticket(String path) {
  String access_token = null; // access_token
  String atime = null;// 获取时间
  String a_expires_in = null;// 有效时间(s)
  String ticket = null;// jsapi_ticket
  String ttime = null;// 得到时间
  String t_expires_in = null;// 有效时间(s)
  String access_tokenStr = TUtils.getAccessToken(APPID,
    API_KEY);
  if (access_tokenStr != null
    && access_tokenStr.indexOf("access_token") != -1) {
   try {
    JSONObject jsonObject = new JSONObject(access_tokenStr);
    access_token = jsonObject.getString("access_token");
    a_expires_in = jsonObject.getString("expires_in");
    atime = getCurrentDateStr();
   } catch (JSONException e) {
    // e.printStackTrace();
   }
  }
  if (access_token != null && !access_token.equals("")) {
   String ticketStr = TicketUtils.getJSAPITicket(access_token);
   // System.out.println("ticketStr:" + ticketStr);
   if (ticketStr != null && ticketStr.indexOf("ticket") != -1) {
    try {
     JSONObject jsonObject = new JSONObject(ticketStr);
     ticket = jsonObject.getString("ticket");
     t_expires_in = jsonObject.getString("expires_in");
     ttime = getCurrentDateStr();
    } catch (JSONException e) {
     // e.printStackTrace();
    }
   }
  }
  String result = null;
  if (ticket != null && !ticket.equals("")) {
   result = "{\"access_token\":\"" + access_token
     + "\",\"a_expires_in\":\"" + a_expires_in
     + "\",\"atime\":\"" + atime + "\",\"ticket\":\"" + ticket
     + "\",\"t_expires_in\":\"" + t_expires_in
     + "\",\"ttime\":\"" + ttime + "\"}";
   if (MyFileUtils.writeIntoText(path, result)) {
    // System.out.println("写入文件成功");
    // System.out.println(result);
   } else {
    System.out.println("写入微信签名文件失败");
   }
  }
  return result;
 }
public static String getAccessToken(String APPID, String APPSECRET) {

String url = "https://api.weixin.qq.com/cgi-bin/token";
  String params = "grant_type=client_credential&appid=" + APPID
    + "&secret=" + APPSECRET;
String resultStr = HttpRequest.sendGet(url, params);
// sendGet:用get方法获取数据 ,具体请参考之间的关于微信的文章 http://www.cnblogs.com/jiduoduo/p/5749363.html

return resultStr;

}


/**
  * 根据access_token获取ticket { "errcode":0, "errmsg":"ok", "ticket":
  * "bxLdikRXVbTPdHSM05e5u5sUoXNKd8-41ZO3MhKoyN5OfkWITDGgnr2fwJ0m9E8NYzWKVZvdVtaUgWvsdshFKA"
  * , "expires_in":7200 }
  * 
  * @param access_token
  * @return
  */
 public static String getJSAPITicket(String access_token) {
  String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
  String params = "type=jsapi&access_token=" + access_token;
  String resultStr = HttpRequest.sendGet(url, params);

  return resultStr;
 }

2.2 서명 서명을 구체적으로 생성합니다

public String Wx_Signature() {
  String path = ServletActionContext.getServletContext().getRealPath(
    "/wx/");
  // System.out.println(path);
  try {
   String tokenJSON = MyFileUtils.readText(path);
   // String access_token = null; // access_token
   String atime = null;// 获取时间
   String a_expires_in = null;// 有效时间(s)
   String ticket = null;// jsapi_ticket
   // String ttime = null;// 得到时间
   // String t_expires_in = null;// 有效时间(s)
   String result = tokenJSON;
   if (result == null || result.equals("")) {
    tokenJSON = getAccessToken_ticket(path);
   }
   // System.out.println(result);
   if (tokenJSON != null && !tokenJSON.equals("")
     && tokenJSON.indexOf("access_token") != -1) {
    try {
     JSONObject jsonObject = new JSONObject(tokenJSON);
     // access_token = jsonObject.getString("access_token");//
     // access_token
     atime = jsonObject.getString("atime");// 开始时间
     a_expires_in = jsonObject.getString("a_expires_in");// 有效时间
     ticket = jsonObject.getString("ticket");// jsapi_ticket
     // System.out.println(ticket);
     // ttime = jsonObject.getString("ttime");// 开始时间
     // t_expires_in = jsonObject.getString("t_expires_in");//
     // 有效时间
     String t1 = getCurrentDateStr();
     String t2 = atime;
     // System.out.println(atime);
     // System.out.println(a_expires_in);
     // System.out.println(TimeInterval.getInterval(t2, t1));
     long end_time = Long.parseLong(a_expires_in) - 60;
     if (TimeInterval.getInterval(t2, t1) > end_time) {
      ticket = getAccessToken_ticket(path);
     }
    } catch (JSONException e) {
     msg = e.getMessage();
    }
   } else {

   }
   // System.out.println(ticket);
   String url = getParameter("url");
   String noncestr = TUtils.getRandomString(16);
   String timestamp = System.currentTimeMillis() + "";
   timestamp = timestamp.substring(0, 10);
   String data = "jsapi_ticket=" + ticket + "&noncestr=" + noncestr
     + "&timestamp=" + timestamp + "&url=" + url;
   String digest = new SHA1().getDigestOfString(data.getBytes());
   String signature = digest.toLowerCase();// signature
   result = "{\"noncestr\":\"" + noncestr + "\",\"timestamp\":\""
     + timestamp + "\",\"url\":\"" + url + "\",\"signature\":\""
     + signature + "\" ,\"ticket\":\"" + ticket + "\"}";
        msg = result;
  } catch (IOException e) {
   msg = e.getMessage();
  }
  return msg 
 }

지침 : 서명에는 여러 호출이 있으므로 서버 파일에 캐시되어야 합니다.

위 내용은 WeChat JS 인터페이스 서명의 특정 코드 구현 예를 공유하세요.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.