Introduction to how to use WeChat jssdk logic in vue (code example)
This article brings you an introduction to the use of WeChat jssdk logic in vue (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
Simple use of WeChat jssdk in vue
import wx from 'weixin-js-sdk'; wx.config({ debug: true, appId: '', timestamp: , nonceStr: '', signature: '', jsApiList: [] }); wx.ready(() => { // do something... }); wx.error((err) => { // do something... });
The above is the example code given by WeChat official, but for actual project use, it is still The code needs to be further encapsulated. This article demonstrates based on vue, and the same applies to other frameworks.
It has been pointed out in the official document of the WeChat public platform that due to security considerations, the signature logic needs to be processed on the backend, so the principle of signature will not be described in detail here. It mainly talks about how to use the signature returned by the backend. Call jssdk. At the logical level, since the wx.config method is required before calling any interface, we can extract it as much as possible and place it separately.
# utils/ . ├── common.js # 通用函数 └── lib └── wechat # 微信相关代码 ├── auth # 微信用户登陆获取信息相关代码 │ ├── auth.js │ └── index.js ├── config # jssdk 初始化相关代码 │ └── index.js ├── helper.js # 微信相关操作 └── share # 分享接口相关代码 └── index.js
import sdk from 'weixin-js-sdk'; export function initSdk({ appid, timestamp, noncestr, signature, jsApiList }) { // 从后端获取 sdk.config({ debug: process.env.VUE_APP_ENV !== 'production', appId: appid, timestamp: timestamp, nonceStr: noncestr, signature: signature, jsApiList: jsApiList }); }
In this way, the initialization of jssdk
can be completed, and then the sharing interface can be initialized. At the beginning, I wanted to share that since it is possible to correspond to every url
page (view
in the SPA
application), then it should be in view
was written using mixin
mixed in, so the first version of the implementation was produced.
// example.vue export default { name: 'example', wechatShareConfig() { return { title: 'example', desc: 'example desc', imgUrl: 'http://xxx/example.png', link: window.location.href.split('#')[0] }; } }
// wechatMixin.js import { share } from '@/utils/lib/wechat/share'; // 获取 wechat 分享接口配置 function getWechatShareConfig(vm) { const { wechatShareConfig } = vm.$options; if (wechatShareConfig) { return typeof wechatShareConfig === 'function' ? wechatShareConfig.call(vm) : wechatShareConfig; } } const wechatShareMixin = { created() { const wechatShareConfig = getWechatShareConfig(this); if (wechatShareConfig) { share({ ...wechatShareConfig }); } } }; export default wechatShareMixin;
// utils/lib/wechat/share import { getTicket } from '@/utils/lib/wechat/helper'; // 签名接口 import { initSdk } from '@/utils/lib/wechat/config'; import sdk from 'weixin-js-sdk'; // 接口清单 const JS_API_LIST = ['onMenuShareAppMessage', 'onMenuShareTimeline']; // 消息分享 function onMenuShareAppMessage(config) { const { title, desc, link, imgUrl } = config; sdk.onMenuShareAppMessage({ title, desc, link, imgUrl }); } // 朋友圈分享 function onMenuShareTimeline(config) { const { title, link, imgUrl } = config; sdk.onMenuShareTimeline({ title, link, imgUrl }); } export function share(wechatShareConfig) { if (!wechatShareConfig.link) return false; // 签名验证 getTicket(wechatShareConfig.link).then(res => { // 初始化 `jssdk` initSdk({ appid: res.appid, timestamp: res.timestamp, noncestr: res.noncestr, signature: res.signature, jsApiList: JS_API_LIST }); sdk.ready(() => { // 初始化目标接口 onMenuShareAppMessage(wechatShareConfig); onMenuShareTimeline(wechatShareConfig); }); }); }
After writing it, there seems to be nothing wrong at first glance, but each view
folder has a WeChat configuration for .vue
which seems very bloated, so The second version of the implementation places the jssdk
initialization in the beforeEach
hook of vue-router
, so that unified configuration of the shared configuration can be achieved, which is more intuitive. .
// router.js //... routes: [ { path: '/', component: Example, meta: { wechat: { share: { title: 'example', desc: 'example desc', imgUrl: 'https://xxx/example.png' } } } } ] //... // 初始化分享接口 function initWechatShare (config) { if (config) { share(config); } } router.beforeEach((to, from, next) => { const { shareConfig } = to.meta && to.meta.wechat; const link = window.location.href; if (!shareConfig) next(); initWechatShare({ ...shareConfig, link }); switchTitle(shareConfig.title); // 切换标题 next(); });
In this way, .vue will look much cleaner and there will not be too much code other than business logic.
The above is the detailed content of Introduction to how to use WeChat jssdk logic in vue (code example). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data
