WeChat ???? ??jsapi?????? ?? ??? ???? ?????? js ?????? ????? ???? ???.
?? ?????, ?? ????? ???? ?????. ??? JS ?????? ?????
?? jsapi.jsp??
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>微信jsapi測試-V型知識庫</title> <meta name="viewport" content="width=320.1,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"> <script src="http://res.wx.qq.com/open/js/jweixin-1.1.0.js"> </script> </head> <body> <center><h3>歡迎來到微信jsapi測試界面-V型知識庫</h3></center> <br> <p>timestamp:${ timestamp}</p> <p>nonceStr:${ nonceStr}</p> <p>signature:${ signature}</p> <p>appId:${ appId}</p> <!-- <input type="button" value="upload" onclick="uploadImg();"/> <input type="button" value="獲取當(dāng)前位置" onclick="getLocation();"/> --> <p>基礎(chǔ)接口之判斷當(dāng)前客戶端是否支持指定的js接口</p> <input type="button" value="checkJSAPI" id="checkJsApi"> <br> <script type="text/javascript"> wx.config({ debug: true, // 開啟調(diào)試模式,調(diào)用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數(shù),可以在pc端打開,參數(shù)信息會通過log打出,僅在pc端時(shí)才會打印。 appId: '${appId}', // 必填,公眾號的唯一標(biāo)識 timestamp: '${ timestamp}' , // 必填,生成簽名的時(shí)間戳 nonceStr: '${ nonceStr}', // 必填,生成簽名的隨機(jī)串 signature: '${ signature}',// 必填,簽名, jsApiList: ['chooseImage','getLocation','openLocation'] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2 }); wx.ready(function(){ // 1 判斷當(dāng)前版本是否支持指定 JS 接口,支持批量判斷 document.querySelector('#checkJsApi').onclick = function () { wx.checkJsApi({ jsApiList: [ 'getNetworkType', 'previewImage' ], success: function (res) { alert(JSON.stringify(res)); } }); }; }); //初始化jsapi接口 狀態(tài) wx.error(function (res) { alert("調(diào)用微信jsapi返回的狀態(tài):"+res.errMsg); }); </script> </body> </html>
|
4?? ?? ????? ?????? ?????. ?? ?? ?? ????
參數(shù)名 | 說明 |
appId | 必填,公眾號的唯一標(biāo)識 |
timestamp | 必填,生成簽名的時(shí)間戳 |
nonceStr | 必填,生成簽名的隨機(jī)串 |
signature | 必填,簽名 |
? ? ?? ????? ??? ??? ????
??, appId, timestamp, nonceStr, ??
jsapi.jsp ?????? ???? ??, WeChat jsapi ?????? ????? ????? ?? 4? ????? ???? ???. ?, ?? 4? ????? WeChat jsapi ?????? ???? ?? ?? ?????. Appid? WeChat ?? ???? ????? ? ??? ?? ?????? ID???. ??? 3? ????? Get?? ???? ?? ????? ???? ?? ???.
jsapi.jsp? ????? ?? ??? ?????. ????? ???? ?? ??? ?? ??? ???? jsapi.jsp? ?????. ???????? ?? ? ?? ????? wxJsAPIServlet?? ???? ?? ?? ? ???? ???. jsp ?????? ?????. ??? ??? ??? ????:
|
注意用戶點(diǎn)擊的地址必須和簽名算法中的地址保持一致,如果要帶參數(shù)那么參數(shù)也要帶上而且參數(shù)的順序不能改變,否則簽名算法得到的簽名 字符串和用戶請求的地址的簽名字符串不一致導(dǎo)致調(diào)用jsapi失敗。
當(dāng)然我在這里用的servlet跳轉(zhuǎn),讀者也可以把doGET方法中的代碼復(fù)制到spring的一個(gè)普通controller中或者struts中的一個(gè)普通的action方法中。
Sign.java代碼
package com.test.util; 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; public class Sign { public static Map<String, String> sign(String jsapi_ticket, String url) { Map<String, String> ret = new HashMap<String, String>(); String nonce_str = create_nonce_str(); String timestamp = create_timestamp(); String string1; String signature = ""; //注意這里參數(shù)名必須全部小寫,且必須有序 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); return ret; } 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 create_nonce_str() { return UUID.randomUUID().toString(); } private static String create_timestamp() { return Long.toString(System.currentTimeMillis() / 1000); } public static void main(String[] args) { String jsapi_ticket =JsapiTicketUtil.getJSApiTicket(); // 注意 URL 一定要?jiǎng)討B(tài)獲取,不能 hardcode String url = "http://www.vxzsk.com/xx/x.do";//url是你請求的一個(gè)action或者controller地址,并且方法直接跳轉(zhuǎn)到使用jsapi的jsp界面 Map<String, String> ret = sign(jsapi_ticket, url); for (Map.Entry entry : ret.entrySet()) { System.out.println(entry.getKey() + ", " + entry.getValue()); } }; } |
JsapiTicketUtil.java代碼
package com.test.util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import net.sf.json.JSONObject; import com.test.weixin.TestAcessToken; public class JsapiTicketUtil { /*** * 模擬get請求 * @param url * @param charset * @param timeout * @return */ public static String sendGet(String url, String charset, int timeout) { String result = ""; try { URL u = new URL(url); try { URLConnection conn = u.openConnection(); conn.connect(); conn.setConnectTimeout(timeout); BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), charset)); String line=""; while ((line = in.readLine()) != null) { result = result + line; } in.close(); } catch (IOException e) { return result; } } catch (MalformedURLException e) { return result; } return result; } public static String getAccessToken(){ String appid="你公眾號基本設(shè)置里的應(yīng)用id";//應(yīng)用ID String appSecret="你公眾號基本設(shè)置里的應(yīng)用密鑰";//(應(yīng)用密鑰) String url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appid+"&secret="+appSecret+""; String backData=TestAcessToken.sendGet(url, "utf-8", 10000); String accessToken = (String) JSONObject.fromObject(backData).get("access_token"); return accessToken; } public static String getJSApiTicket(){ //獲取token String acess_token= JsapiTicketUtil.getAccessToken(); String urlStr = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+acess_token+"&type=jsapi"; String backData=TestAcessToken.sendGet(urlStr, "utf-8", 10000); String ticket = (String) JSONObject.fromObject(backData).get("ticket"); return ticket; } public static void main(String[] args) { String jsapiTicket = JsapiTicketUtil.getJSApiTicket(); System.out.println("調(diào)用微信jsapi的憑證票為:"+jsapiTicket); } } |
??, ???? ??? ????
?? ? "WeChat jsapi ??? ?????"? ?? ????? ?? ??? ?????
? ??? ?? ?????? ??? js ?????? ????? ???? WeChat ?? ??? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PHP? ? ?? ? ?? ? ?????, ?? WeChat ??? ?? ???? ?? ?? ???? ?????. ??? ?? ? ?? ??? ???? WeChat ??? PHP? ???? ???? ????. PHP? ??? ?? ???? ?? ?? ???? ?????. WeChat ???? ??? ??? ? ???? ??? ??? ???? ?? ??? ?? ??? ?????. ??? ? ??? ??? ?? ???? ?? ??? ?? ???? ??? ? ?? ????? ??? ? ? ????.

WeChat ?? ??? ??? ? ?? ??? ?? ?????. ?? ??? ????? ?? ??? ??? ??? ? ?? ?? ????, ??? ?? ? ?? ??? ?? ??? ????? ???. ? ????? PHP? ???? WeChat ?? ??? ???? ??? ?????. WeChat ?? ?? ??? ???? ?? WeChat ?? ?? ??? ??? ???. WeChat ?? ?????? WeChat ?? ??, ?? ?? ? ?? ??? ???? ??? API ??? ???? ???. PHP ??? ???? ???? ???? WeChat?? ????? ???? PH? ???? ???.

WeChat? ??? ?? ?? ? ?? ??? WeChat? ??? ??? ???? ??????. WeChat ?? ??? ??? ??? WeChat ???? ???? ??? ?? ? ?????. ??? ?? ???? ????? ??? ?????? ?? ??? ?? ??? ?? ?????. ??? WeChat ?? ??? ??? ???? ?? ?? ?????. ? ????? PHP? ???? WeChat ?? ??? ??? ???? ??? ?????. 1. ?? ?? WeChat ?? ??? ??? ????? ?? ?? ??? ???? ???. PHP WeChat ?? ??? ??? ?? ?? ?? ?? ??: Sub

WeChat? ?? ???? ?? ? ??? ??? ??? ?? ??? ? ?????. ??? ???? ??? ?? ?? ? ?? ???? WeChat ???? ???? ??? ??????. WeChat ???? ??? ? ?? ???? ??? ?????. ?? ??? ?? ?? ? ? ???? ?? WeChat ??? PHP ??? ??? ? ????. 1. PHP ?? WeChat ?? PHP? ? ?? ???? ?? ???? ?? ?? ?? ? ???? ?????. WeChat ?? ????? ???? ?? ?????? ???? PHP ??? ???? WeChat? ??? ? ????.

WeChat ?? ?? ???? ??? ?? ??? ???? ???? ? ? ???? ??? ? ??? ?? ?? ??? ?????. ? ????? PHP? ???? WeChat ??? ?? ?? ??? ???? ??? ?????. 1. WeChat ???? openid? ?????. WeChat ??? ?? ?? ??? ???? ?? ?? ???? openid? ???? ???. WeChat ?? ??? ??? ? ??? ??? ?? openid? ?? ?? ???? ?????. ??? ??? ???? ?? ??? ?? ???? ?? ? ????.

WeChat? ???? ??? ?? ? ??? ?????? ??? ???, WeChat? ??? ??? ??? ?? ??? ??? ??? ??? ?? ????. ??? ?? WeChat? ??? ????? ???? ?? ??? ??? ???? WeChat ??? ???? ?? ?? ???? ????. ? ? ?? ?? ??? ?? ?? ?????. ???? PHP ??????? ?? ??? ?? ??? ??? ???? ???? ??? ??? ?????. 1. WeChat ?? ??? ??? ?? ??? ?????. ?? ??? ?? ??? ???? ??? ?????.

PHP? ???? WeChat ?? ??? ???? ?? WeChat ?? ??? ?? ??? ?? ? ?? ??? ?? ??? ??? ????, ????? ???? ? ??? PHP? ???? WeChat ?? ??? ??? ?? ????. ? ????? PHP? ???? WeChat ?? ??? ???? ???? ??? ?????. 1??: WeChat ?? ??? ??? ??? ????. WeChat ?? ?? ??? ???? ?? WeChat ?? ??? ??? ??? ???? ???. ???? ?? ??? WeChat ?? ??? ?? ????? ?????.

???? ??? ??? ??? ???? WeChat? ?? ? ??? ???? ???? ?? ??? ?????. ?? ? ?????? ??? WeChat ??? PHP? ???? ??? ?? ???? ??? ?????. ? ????? ?? WeChat ??? PHP? ???? ??? ?? ?? ?? ???? ?? ? ? ?? ??? ?????. 1. ?? ?? ?? WeChat? ???? ?? ?? ?? ?? ??? ???? ???. ??, PHP ?? ??? WeChat ?? ???? ???? ???.
