この記事は、参照のためにWeChatパブリックアカウントをスキャンする特定のコードを共有しています。特定のコンテンツは次のとおりです
ステップ
WeChat JS-JDKドキュメントの説明によると、主にスキャンを実装するための次の手順があります。
バインドドメイン名
JSインターフェイスセキュリティドメイン名にドメイン名を入力してください。図に示すように、HTTPを持たないように注意してください。
JSファイルを紹介します
<スクリプトsrc = "http://res.wx.qq.com/open/js/jweixin-1.0.0.js"> </script>
構成インターフェイスを介して構成を確認します
$ .ajax({url: "$ {pagecontext.request.contextpath}/wechat/jsapisign"、type: "post"、data:{url:location.href.split( '#')[0]}、contenttype: 'applice/x-ww-form-form-formsed; { wx.config({ debug: false, appId: data.appid, // Required, generate the signature timestamp nonceStr: data.nonceStr, // Required, generate a random string signature signature: data.signature,// Required, signature, see Appendix 1 jsApiList: ["scanQRCode"] // Required, list of JS interfaces to be used, see Appendix 2 for all JSインターフェイスリスト;wechat jsapiビザの確認
public Map <string、string> jsapisign(string url){map <string、string> ret = new hashmap <string、string>(16);文字列nonce_str = checkutil.create_nonce_str();文字列タイムスタンプ= checkutil.create_timestamp();文字列string1;文字列署名= ""; string jsapi_ticket = wechataccesstokenservice.getjsapiticket(); //ここのパラメーター名はすべて小文字でなければならず、string1 = "jsapi_ticket =" + jsapi_ticket + "&noncestr =" + nonce_str + "×tamp =" +タイムスタンプ + "&url =" + url; logger.info( "jsapisign ===" + string1); try {MessageDigest Crypt = MessagedGest.getInstance( "SHA-1"); crypt.reset(); crypt.update(string1.getBytes( "utf-8")); signature = checkutil.bytetohex(crypt.digest()); } catch(nosuchalgorithmexception | unsupportedencodingexception e){e.printstacktrace(); } ret.put( "appid"、appid); Ret.put( "url"、url); ret.put( "jsapi_ticket"、jsapi_ticket); Ret.put( "noncestr"、nonce_str); Ret.put( "Timestamp"、Timestamp); Ret.put( "署名"、署名); logger.info( "jsapisign === url =" + url + "== jsapi_ticket" + jsapi_ticket + "== nonce_str" + nonce_str + "==タイムスタンプ" + "==署名" +署名);返品; } public String getJsapiticket(){augewechataccestoken wechataccestokenmapper.selectbyprimarykey(jsapiticketid); logger.info( "getjsapiticket ===" + wechataccestoken.getaccestoken()); if(strings.isnullorempty(wechataccestoken.getaccestoken())|| wechataccestoken.getExpiresin() - 100 * 1000 <System.CurrentTimeMillis()){// } else {return wechatacstoken.getaccestoken(); }}コントローラーレイヤーコード
@RequestMapping(value = "/jsapisign"、method = {requestmethod.get、requestmethod.post}、生成= mediatype_charset_json_utf8)@responsebody public string jsapisign(string url){// json.tojsonstring(signmap)を返します。 }フロントデスクJSPページ完全コード
<%@ page contentType = "text/html; charset = utf-8"言語= "java"%> <%string path = request.getContextPath(); string basepath = request.getscheme() + "://" + request.getServername() + ":" + request.getServerport() + path + "/";%> <!<!<!<!<! charset = "utf-8"> <meta http-equiv = "x-ua-compatible" content = "ie = edge"> <meta name = "viewport" content = "width = 320.1、1、最小スケール= 1、最大スケール= 1、ユーザースケーラブル=" > <title>本を返すようにコードをスキャン</title> <link rel = "styleSheet" href = "http://203.195.235.76/jssdk/css/style.css"/> <script src = "http://res.wx.q.q.com/open/js/jweix. src = "http://libs.baidu.com/jquery/2.0.0/jquery.js"> </script> <link rel = "styleSheet" type = "text/css" href = "..///../ Resources/css/bookdetail.css src = "../../../リソース/画像/brover/return.png"> <div style = "text-align:center; background-color:#f5f5f5;"> <img src = "../../ ... resources/resource/resuster/scanreturn.png" id = "scanqrcode1"> </div div </div <> div <> <> <> <> <> <> <> <> <> <> <> <> <> </javas $ .ajax({url: "$ {pagecontext.request.contextpath}/wechat/jsapisign"、type: "post"、data:{url:location.href.split( '#')[0]}、contenttype: 'applice/x-ww-form-form-formsed; {wx.config({debug:false、appid:data.appid、//必須、公式アカウントの一意の識別子:data.timestamp、// execrese signature noncestr:data.noncestr、// signature signature signature see see see sees signature signature signature signature signature seesuation signature seesutis signature seesinationの["scanqrcode"] //使用するJSインターフェイスのリスト}}}; wx.ready(function(){// 9.1.2 qrコードをスキャンして結果document.queryselector( '#scanqrcode1')。 Res.ResultStr; // if( "、")> = 0) "https://open.weixin.qqc.com/connect/oauth2/authorize?appid=wx966874444442de&redirect_uri=htttp://cx.ngrok.xiaomiqiu.cn/wechat/toreturndetail?barcode=" + 「&response = code&scope = snsapi_base = bindface」 // JSAPIインターフェイスステータスwx.error(function(res){alert( "wechat jsapi:" + res.errmsg);}); </script> </body> </html>注:署名検証エラーは、開発中に発生する傾向があります。フロントエンドURLの一貫性を判断できます。第二に、AccessTokenが更新されず、再度更新する必要があるため、エラーが時々あることに注意してください。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。