Link: https://qydev.weixin.qq.com/wiki/index.php?title=%E5%BE%AE%E4%BF%A1JS-SDK%E6%8E%A5%E5%8F%A3#.E6.AD.A5.E9.AA.A4.E4.B8.80.EF.BC.9A.E5.BC.95.E5.85.A5JS.E6.96.87.E4.BB.B6 1. Import dependent packages npm install weixin-js-sdk 2. Determine whether it is in the WeChat browser env.js <script> var ua = navigator.userAgent.toLowerCase(); var isWeixin = ua.indexOf('micromessenger') != -1; var isAndroid = ua.indexOf('android') != -1; var isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1); if(!isWeixin) { document.head.innerHTML = '<title>Sorry, an error occurred</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=0"><link rel="stylesheet" type="text/css" href="https://res.wx.qq.com/open/libs/weui/0.4.1/weui.css" rel="external nofollow" >'; document.body.innerHTML = '<div class="weui_msg"><div class="weui_icon_area"><i class="weui_icon_info weui_icon_msg"></i></div><div class="weui_text_area"><h4 class="weui_msg_title">Please open the link in the WeChat client</h4></div></div>'; } Reference in main.js: import env from "./env"; //Running environment Log in with WeChat, exchange the code for openid, and use this method on the start page: <script> methods:{ // WeChat login wxLogin() { var that = this; axios .get("/common/loginAuth") .then(function(res) { console.log("The link address returned by the background", res.data); window.location.href = res.data; // Jump to the link address returned by the background}) .catch(function(error) {}); }, //Exchange user information postCode(res) { var that = this; axios .post("/common/getUserInfo", { code:res }) .then(function(res) { cookie.set("openid", res.data.openid); //code exchanges openid for backend and stores it}) .catch(function(error) { console.log(error); }); }}, created() { var r = window.location.href; //Get the current link and split the current link //The current link address is the parameter returned by the background. If there is a split, get the code in the link, use the postCode() method to get the openid. If there is no openid, use the wxLogin() method to start WeChat login if (r.indexOf("?") != -1) { r = r.split("?"); r = r[1].split("&"); r = r[0].split("="); r = r[1]; } else { this.wxLogin(); } if (r) { this.postCode(r); } else { this.wxLogin(); } }, </script> 3. Front-end page usage import wx from 'weixin-js-sdk' this.axios({ method: 'post', url: 'url', data:{ url:location.href.split('#')[0] } // Provide the authorization url parameter to the server, and the part after # is not needed}).then((res)=>{ wx.config({ debug: true, // Enable debug mode, appId: res.appId, // Required, unique ID of the enterprise number, enter the enterprise number corpid here timestamp: res.timestamp, // Required, generates the timestamp of the signature nonceStr: res.nonceStr, // Required, generates the random string of the signature signature: res.signature, // Required, signature, see Appendix 1 jsApiList: ['scanQRCode'] // Required, list of JS interfaces to be used, all JS interfaces are listed}); }) This is the end of this article about the common usage methods of weixin-js-sdk in vue. For more related vue weixin-js-sdk content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A brief introduction to protobuf and installation tutorial in Ubuntu 16.04 environment
>>: Ubuntu 16.04 installation tutorial under VMware 12
This article records the installation and configu...
Table of contents Special characters in URLs URL ...
Today I have nothing to do, so I listed some tool...
Win2008 R2 zip format mysql installation and conf...
For commercial databases, database upgrade is a h...
Copy code The code is as follows: <!DOCTYPE ht...
Table of contents 1. Synchronous AJAX 2. Asynchro...
MySQL is a database that I like very much. Today,...
Preface MySQL is a high-speed, high-performance, ...
Table of contents What are immutable values? Why ...
Table of contents Why is addEventListener needed?...
Application scenarios: One of the new requirement...
HTML Part Copy code The code is as follows: <!D...
【Foreword】 The SMS function of our project is to ...
The div element is used to provide structure and b...