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

How to use html table (to show the visual effect of web page)

We know that when using HTML on NetEase Blog, we ...

How to get the contents of .txt file through FileReader in JS

Table of contents JS obtains the .txt file conten...

Introducing multiple custom fonts in CSS3

Today I found a problem in HTML. There are many d...

An article explains Tomcat's class loading mechanism

Table of contents - Preface - - JVM Class Loader ...

Summary of various uses of JSON.stringify

Preface Anyone who has used json should know that...

Three common uses of openlayers6 map overlay (popup window marker text)

Table of contents 1. Write in front 2. Overlay to...

A MySQL migration plan and practical record of pitfalls

Table of contents background Solution 1: Back up ...

Difference between querySelector and getElementById methods in JS

Table of contents 1. Overview 1.1 Usage of queryS...

How to change MySQL character set utf8 to utf8mb4

For MySQL 5.5, if the character set is not set, t...

Summary of MySQL lock related knowledge

Locks in MySQL Locks are a means to resolve resou...

mysql data insert, update and delete details

Table of contents 1. Insert 2. Update 3. Delete 1...

Detailed summary of mysql sql statements to create tables

mysql create table sql statement Common SQL state...

CSS implements six adaptive two-column layout methods

HTML structure <body> <div class="w...

Sample code for deploying Spring-boot project with Docker

1. Basic Spring-boot Quick Start 1.1 Quick start ...