Cet article a partagé le code spécifique de numérisation du compte public WeChat pour votre référence. Le contenu spécifique est le suivant
étape
Selon la description du document WeChat JS-JDK, il existe principalement les étapes suivantes pour implémenter la numérisation:
Nom de domaine lié
Remplissez le nom de domaine du nom de domaine de sécurité de l'interface JS, veillez à ne pas apporter http, comme indiqué sur la figure:
Présenter des fichiers JS
<script src = "http://res.wx.qq.com/open/js/jweixin-1.0.0.js"> </ script>
Vérifiez la configuration via l'interface de configuration
$ .ajax ({url: "$ {pagecontext.request.contextpath} / wechat / jsapisign", type: "post", data: {url: location.href.split ('#') [0]}, contenuStype: 'application / x-ww-form-urlencomed; charset = utf-8', async: furet: utforde) {wx.config ({debug: false, appid: data.appid, // requis, générer l'horodatage de signature non-claire: data.noncestr, // requis, générer une signature de chaîne aléatoire Signature: data.signature, // requise, signature, voir l'annexe 1 jsapilist: ["scanqrcode"] // requis, la liste des interfaces js pour être utilisées pour être utilisée, la liste js listes d'interface;}});Vérification du visa WeChat JSAPI
public map <string, string> jsapisign (string url) {map <string, string> ret = new hashmap <string, string> (16); String nonce_str = checkUtil.create_nonce_str (); String TimeStamp = CheckUtil.create_timestamp (); String String1; String Signature = ""; String JSAPI_TICKET = WECHATACCESSTOKENSERVICE.GETJSAPITICKET (); // Notez que les noms de paramètres ici doivent être tous en minuscules et doivent être commandés String1 = "jsapi_ticket =" + jsapi_ticket + "& nonCestr =" + nonce_str + "× tamp =" + himestamp + "& url =" + url; Logger.info ("jSapisign ===" + String1); essayez {messagediGest crypt = messagediGest.getInstance ("sha-1"); crypt.reset (); crypt.update (string1.getBytes ("utf-8")); Signature = CheckUtil.ByteTohex (crypt.digest ()); } catch (NosuchalgorithMexception | UnsupportEncodingException e) {e.printStackTrace (); } ret.put ("appid", appid); ret.put ("URL", URL); ret.put ("jsapi_ticket", jsapi_ticket); ret.put ("noncestr", nonce_str); Ret.put ("horodatage", horodatage); ret.put ("signature", signature); logger.info ("jsapisign === url =" + url + "== jsapi_ticket" + jsapi_ticket + "== nonce_str" + nonce_str + "== horodatamp" + timesstamp + "== signature" + signature); retour retour; } String public getjsapiticket () {agewechataccesstoken wechataccesstokenmapper.selectbyprimarykey (jsapiticketid); Logger.info ("getJsapiticket ===" + wechataccesstoken.getAccessToken ()); if (Strings.isnullOrempty (wechataccesstoken.getAccessToken ()) || wechataccesstoken.getExpiresin () - 100 * 1000 <system.currenttimemillis ()) {// vide ou expiré, re-reresh renvoie RefreshjSapiticket (); } else {return wechataccesstoken.getAccessToken (); }}Code de couche de contrôleur
@RequestMapping (value = "/ jsapisign", méthode = {requestMethod.get, requestMethod.post}, produmes = mediatype_charset_json_utf8) @ResponseBody public String jSapisign (String url) {// ajouter weChat js signature map <String> SignMap = weChingSsice.jSapisign (Url); return JSON.TojSontring (SignMap); }Page de réception JSP Code complet
<% @ page contentType = "text / html; charset = utf-8" linguisse = "java"%> <% string path = request.getContextPath (); String basepath = request.getscheme () + ": //" + request.getServerName () + ":" + request.getServerport () + path + "/";%> <! Doctype html public "- // w3c // dtd html 4.01 transitional // en"> <html lang = "zh-cn"> <méta charset = "utf-8"> <meta http-equiv = "x-ua-compatible" contenu = "ie = edge"> <meta name = "Viewport" contenu = "width = 320.1, initial-scale = 1, minimum-scale = 1, maximum-scale = 1, user-scalable = no"> <read> <base href = "<% = bascath>" externy nofollow "> <ititle> scannez le code pour renvoyer le livre </ title> <link rel =" Stylesheet "href =" http://203.195.235.76/jssdk/css/style.css "/> <script src = "http://res.wx.qq.com/open/js/jweixin-1.1.0.js"> </ script> <script src = "http://libs.baidu.com/jquery/2.0.0/jquery.js"> </cript> <link rel = "Styleheet" type = "Text / CSSS" href = "../../../ ressources / css / bookdetail.css" rel = "external nofollow"> </ head> <body> <div> <img src = "../../../ Resources / images / emprun / return.png"> <div style = "text-aalign: Center; background: # f5f5f5;"> <img src = "../../../ ressources / images / emprunter / scanreturn.png" id = "scanqrcode1"> </ div> </ div> <script type = "text / javascript"> $ .ajax ({url: "$ {pagecontex location.href.split ('#') [0]}, ContentType: 'application / x-www-for-urlencoded; charset = utf-8', async: true, succès: function (data) {wx.config ({debug: false, appid: data.appid, // requis, l'identificateur unique du compte officiel timestamp: data. Timestamp de la signature NONCESTR: DATA.NONCESTR, // requis, générez la chaîne aléatoire de la signature Signature: Data.Signature, // requise, voir l'annexe 1 jsapilist: ["ScanQrcode"] // requis, la liste des interfaces JS à utiliser}); wx.ready (function () {// 9.1.2 Scannez le code QR et renvoyez le document de résultat.QuerySelector ('# scanqrcode1'). onClick = function () {wx.scanqrcode ({NeedResult: 1, desc: 'scanqrcode Desc', Success: Function (Res.Resfr; // Le produit barre du produit, prenez le if (url.indexof (",")> = 0) {var temparray = url.split (','); "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx96668744efc2b2de&redirect_uri=http://cx.ngrok.xiaomiqiu.cn/wechat/toreturndetail?barcode=" + Barcode + "& Response_Type = Code & Scope = SNSAPI_BASE & State = BindFace # WeChat_Redirect"; // Initialisez l'état de l'interface JSAPI wx.error (fonction (res) {alert ("Statut renvoyé en appelant WeChat JSAPI:" + res.errmsg);}); </cript> </ body> </html>Remarque: les erreurs de vérification de signature sont sujettes à se produire pendant le développement. Nous pouvons juger par la cohérence des URL avant et arrière. Deuxièmement, notez que parfois l'erreur est que l'accès à l'accès n'est pas actualisé et doit être actualisé à nouveau.
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.