Detailed explanation of common usage methods of weixin-js-sdk in vue

Detailed explanation of common usage methods of weixin-js-sdk in vue

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:
  • js simple network speed test method complete example
  • Access the javascript code for Baidu and Google speed test
  • JS asynchronous code unit testing magic Promise
  • Native js implements regular validation of the form (submit only after validation)
  • Detailed explanation of reduce fold unfold usage in JS
  • How to use JS WebSocket to implement simple chat
  • How to write elegant JS code
  • Detailed explanation of JS homology strategy and CSRF
  • How to test network speed with JavaScript

<<:  A brief introduction to protobuf and installation tutorial in Ubuntu 16.04 environment

>>:  Ubuntu 16.04 installation tutorial under VMware 12

Recommend

Based on the special characters in the URL escape encoding

Table of contents Special characters in URLs URL ...

Win2008 R2 mysql 5.5 zip format mysql installation and configuration

Win2008 R2 zip format mysql installation and conf...

Some "pitfalls" of MySQL database upgrade

For commercial databases, database upgrade is a h...

HTML+CSS+jQuery imitates the search hot list tab effect with screenshots

Copy code The code is as follows: <!DOCTYPE ht...

How to track users with JS

Table of contents 1. Synchronous AJAX 2. Asynchro...

How to implement load balancing in MySQL

Preface MySQL is a high-speed, high-performance, ...

Explaining immutable values ​​in React

Table of contents What are immutable values? Why ...

Detailed explanation of the difference between JavaScript onclick and click

Table of contents Why is addEventListener needed?...

How to position the header at the top using CSS sticky layout

Application scenarios: One of the new requirement...

Implementation of HTML command line interface

HTML Part Copy code The code is as follows: <!D...

How to set Nginx log printing post request parameters

【Foreword】 The SMS function of our project is to ...

The benefits of div+css and web standard pages

The div element is used to provide structure and b...