1. 程式人生 > >微信掃一掃功能呼叫

微信掃一掃功能呼叫

後臺獲取簽名程式碼:

package com.epoch.wap.common;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Formatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import
org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.epoch.common.utils.JsonUtil; /** * 官方給的使用js的驗證工具 * @author Administrator * */
public class JsSignUtil { public static String accessToken = null; public static Map<String, Object> sign(String url) { String sendUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=**appid**&secret= **secret**"; String result = getHttpResult(sendUrl); Object accessResult = JsonUtil.getValueFromJson(result, "access_token"
); String signUrl = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+accessResult.toString()+"&type=jsapi"; String resultSign = getHttpResult(signUrl); Object ticket = JsonUtil.getValueFromJson(resultSign, "ticket"); Map<String, Object> ret = new HashMap<String, Object>(); String nonce_str = create_nonce_str(); String timestamp = create_timestamp(); String string1; String signature = ""; //注意這裡引數名必須全部小寫,且必須有序 string1 = "jsapi_ticket=" + ticket + "&noncestr=" + nonce_str + "&timestamp=" + timestamp + "&url=" + url; 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("nonceStr", nonce_str); ret.put("timestamp", timestamp); ret.put("signature", signature); ret.put("jsapi_ticket", ticket); ret.put("appId", "wx154f74654ae04891"); return ret; } /** * 隨機加密 * @param hash * @return */ private static String byteToHex(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 String getHttpResult(String url) { String result = ""; HttpGet httpRequest = new HttpGet(url); try { HttpResponse httpResponse = HttpClients.createDefault().execute(httpRequest); if (httpResponse.getStatusLine().getStatusCode() == 200) { result = EntityUtils.toString(httpResponse.getEntity()); } } catch (ClientProtocolException e) { e.printStackTrace(); result = e.getMessage().toString(); } catch (IOException e) { e.printStackTrace(); result = e.getMessage().toString(); } return result; } /** * 產生隨機串--由程式自己隨機產生 * @return */ private static String create_nonce_str() { return UUID.randomUUID().toString(); } /** * 由程式自己獲取當前時間 * @return */ private static String create_timestamp() { return Long.toString(System.currentTimeMillis() / 1000); } }

官方文件示例:

參考以下文件獲取access_token(有效期7200秒,開發者必須在自己的服務全域性快取access_token):../15/54ce45d8d30b6bf6758f68d2e95bc627.html
用第一步拿到的access_token 採用http GET方式請求獲得jsapi_ticket(有效期7200秒,開發者必須在自己的服務全域性快取jsapi_ticket):https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi

4 第四步: 前臺進入頁面:`

 @RequestMapping(value = "/tiaoma")
public String tiaoma( HttpServletRequest request,GoodsVO goods) throws Exception {

    String Url=request.getRequestURL()+"?"+request.getQueryString().toString();;
    Map<String, Object> resMap = new HashMap<String, Object>();
    resMap = JsSignUtil.sign(Url);
    request.setAttribute("nonceStr", resMap.get("nonceStr"));
    request.setAttribute("timestamp", resMap.get("timestamp"));
    request.setAttribute("signature", resMap.get("signature"));
    request.setAttribute("appId", resMap.get("appId"));
    request.setAttribute("url", resMap.get("url"));
    request.setAttribute("jsapi_ticket",                                  resMap.get("jsapi_ticket"));
    return "index/tiaoma";
}`