How does uniapp obtain shared parameters?
With the gradual increase in social traffic, the sharing function has become one of the essential functions of modern APPs. In the process of developing an APP, how to obtain sharing parameters is a crucial step in the development of sharing functions. For uniapp developers, there are two ways to obtain shared parameters, which we will introduce in detail below.
1. Obtain parameters through the sharing function of uni-app mpx (mini program)
In the development of uniapp, mpx comes with many functions necessary for the development of mini programs. It also includes the sharing function that comes with the mini program. Developers of uniapp can use this function to quickly and easily obtain shared parameters.
- Add the page that needs to be shared to the pages.json file
Add the path of the page that needs to be shared in the pages.json file, as follows:
"pages": [ { "path": "pages/index/index", "style": {} }, { "path": "pages/share/share", "style": {}, "navigationBarTitleText": "分享页面" } ],
- Set the mini program sharing configuration
In each page, you can set the sharing parameters of the mini program through the onShareAppMessage function. The sample code is as follows:
onShareAppMessage: function () { return{ title:"分享标题", path:"/pages/share/share", imageUrl:"分享图片地址", success:function(res){ console.log('分享成功') }, fail:function(res){ console.log('分享失败') } } }
Through this sample code, we can see that we can set the shared title, shared path, and shared image address, and can also perform corresponding operations after successful or failed sharing.
- Get sharing parameters in the sharing page
In the sharing page, we can get the sharing parameters through the api of uni.mp. The sample code is as follows:
import uni from 'uni.mp' export default { created() { uni.getShareInfo(function(res) { console.log(res) }) } }
In this sample code, we can see that we use the uni.mp.getShareInfo() API to obtain the sharing parameters.
2. Obtain shared parameters through the wx object of the mini program
In uniapp, you can directly use the API of the mini program to obtain the shared parameters. Similarly, we can also use the wx object of the mini program to obtain the shared parameters. Here is a detailed introduction on how to use it.
- Add the path of the page that needs to be shared to the pages.json file
Similar to the above method, you need to add the path of the page that needs to be shared to the pages.json file , for example:
"pages": [ { "path": "pages/index/index", "style": {} }, { "path": "pages/share/share", "style": {}, "navigationBarTitleText": "分享页面" } ],
- Set the sharing parameters into the onShareAppMessage() function
In the page that needs to be shared, use the onShareAppMessage() function to set the sharing parameters, as follows Display:
onShareAppMessage: function () { return { title: '分享标题', path: '/pages/share/share', imageUrl: '分享图片地址' } }
In this example, we can also set the shared title, sharing path, and shared image address.
- Get sharing parameters through wx.getShareInfo()
In the shared page, use wx.getShareInfo() to obtain the encrypted data during sharing, and then use The decryption function of the applet decrypts the data, as shown below:
// 获取分享参数 onLoad: function(options) { var that = this; //获取分享加密数据 wx.getShareInfo({ shareTicket: options.shareTicket, success: function(res) { //解密数据 wx.request({ url: '', data: { "encryptedData": res.encryptedData, "iv": res.iv, "sessionKey": that.data.sessionKey }, success: function(res) { console.log(res.data); } }) } }) }
In this sample code, we use wx.getShareInfo() to obtain the shared encrypted data, and then use the formula to decrypt and obtain the sharing parameters.
Summary
Through the above two methods, we can easily obtain the sharing parameters and realize our own sharing function. During the specific implementation process, we need to pay attention to some details to ensure the normal use of the sharing function. At the same time, uniapp also provides very comprehensive API documentation, which can be viewed and used at any time during the development process.
The above is the detailed content of How does uniapp obtain shared parameters?. 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



Article discusses using Sass and Less preprocessors in uni-app, detailing setup, benefits, and dual usage. Main focus is on configuration and advantages.[159 characters]

The article explains how to use uni-app's animation API, detailing steps to create and apply animations, key functions, and methods to combine and control animation timing.Character count: 159

The article discusses various testing types for UniApp applications, including unit, integration, functional, UI/UX, performance, cross-platform, and security testing. It also covers ensuring cross-platform compatibility and recommends tools like Jes

The article discusses strategies to reduce UniApp package size, focusing on code optimization, resource management, and techniques like code splitting and lazy loading.

The article discusses debugging tools and best practices for UniApp development, focusing on tools like HBuilderX, WeChat Developer Tools, and Chrome DevTools.

The article explains how to use uni-app's storage APIs (uni.setStorage, uni.getStorage) for local data management, discusses best practices, troubleshooting, and highlights limitations and considerations for effective use.

The article discusses using uni-app's APIs to access device features like camera and geolocation, including permission settings and error handling.Character count: 158

The article discusses validating user input in uni-app using JavaScript and data binding, emphasizing both client and server-side validation for data integrity. Plugins like uni-validate are recommended for form validation.
