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

Detailed tutorial on installing MYSQL under WINDOWS

1. Download the installation package -Choose the ...

Detailed description of shallow copy and deep copy in js

Table of contents 1. js memory 2. Assignment 3. S...

CSS overflow-wrap new property value anywhere usage

1. First, understand the overflow-wrap attribute ...

How to implement h5 input box prompt + normal text box prompt

XML/HTML CodeCopy content to clipboard < input...

Detailed explanation of Promises in JavaScript

Table of contents Basic usage of Promise: 1. Crea...

MySQL compressed package version zip installation configuration method

There are some problems with the compressed versi...

Specific use of Linux man command

01. Command Overview Linux provides a rich help m...

Solution to transparent font problem after turning on ClearType in IE

The solution to the transparent font problem after...

How to use Axios asynchronous request API in Vue

Table of contents Setting up a basic HTTP request...

How to count down the date using bash

Need to know how many days there are before an im...

Example of how to check the capacity of MySQL database table

This article introduces the command statements fo...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...

About Zabbix forget admin login password reset password

The problem of resetting the password for Zabbix ...