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

Solution to mysql login warning problem

1. Introduction When we log in to MySQL, we often...

Example of how to enable Slow query in MySQL

Preface Slow query log is a very important functi...

WeChat applet uses canvas to draw clocks

This article shares the specific code of using ca...

Summary of essential knowledge points for MySQL query optimization

Preface Query optimization is not something that ...

Implementation of installing and uninstalling CUDA and CUDNN in Ubuntu

Table of contents Preface Install the graphics dr...

Analysis of permissions required to run docker

Running Docker requires root privileges. To solve...

MySQL kill command usage guide

KILL [CONNECTION | QUERY] processlist_id In MySQL...

In-depth analysis of the diff algorithm in React

Understanding of diff algorithm in React diff alg...

Implementation of Nginx configuration of multi-port and multi-domain name access

To deploy multiple sites on a server, you need to...

Summary of commonly used SQL in MySQL operation tables

1. View the types of fields in the table describe...

Specific use of MySQL binlog_ignore_db parameter

Preface: After studying the previous article, we ...

Detailed Analysis of Event Bubbling Mechanism in JavaScript

What is bubbling? There are three stages in DOM e...

About nginx to implement jira reverse proxy

Summary: Configure nginx reverse proxy jira and i...

A Brief Analysis of Patroni in Docker Containers

Table of contents Create an image File Structure ...