To view the document

The weixin-jS-SDK is used

Installation:

npm install weixin-js-sdk --save
Copy the code

Introduction:

import wx from 'weixin-js-sdk'
Copy the code

Step 1: Configure wx.config

You need to configure wx.config, which is generally returned by the back-end interface.

Step 2: Call the scan method in Ready

After the config information is validated, execute the ready method.

The relevant interface must be called in the ready function to ensure correct execution.

Step 3: Call sweep

If an error message is displayed, run the config:invalid signature command

If config: invalid signature is present, the config is not paired. The signature is incorrect. The incorrect signature may be due to the incorrect URL.

Note: THE URL must be the URL of the page that is currently calling the sweep method.

There may be situations where Android can pull up and scan and iOS can’t

Url signature failure is highly likely to occur on iOS.

Specific code:

App.vue:

Created (){// Create page URL in app.vue // reason: To combination of iOS in page to sign, the problem of the failure of the signature on the window. The localStorage. SetItem (' scanUrl 'window. The location. The href. Split (' #') [0])},Copy the code

In the specific pull up scan scan page:

Do (){let self = this; let u = navigator.userAgent, app = navigator.appVersion; let isIOS = !! u.match(/\(i[^;] +; ( U;) ? CPU.+Mac OS X/); // let purl = "; if (isIOS) { purl = encodeURIComponent(window.localStorage.getItem('scanUrl')) } else { purl = encodeURIComponent(window.location.href.split('#')[0]) } let params = { url: purl }; getWxConfig(params).then(res => { let {appId, timestamp, nonceStr, signature} = res.data; wx.config({ debug: true, appId, timestamp, nonceStr, signature, jsApiList: [ 'checkJsApi', 'scanQRCode' ] }); Wx.ready (function(){wx.checkJsAPI ({jsApiList: ['scanQRCode'], success: Function (res) {// Wx.scanqrCode must be placed inside the wx.ready callback. }); }}); }); }, err => {}); }}Copy the code

Reference: Call wechat scan function in VUE (compatible with IOS)