Home Web Front-end uni-app How does uniapp obtain shared parameters?

How does uniapp obtain shared parameters?

Apr 19, 2023 pm 02:13 PM

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.

  1. 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": "分享页面"
        }
    ],
Copy after login
  1. 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('分享失败')
    }
  }
}
Copy after login

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.

  1. 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)
    })
  }
}
Copy after login

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.

  1. 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": "分享页面"
        }
    ],
Copy after login
  1. 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: '分享图片地址'
    }
}
Copy after login

In this example, we can also set the shared title, sharing path, and shared image address.

  1. 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);
            }
         })
      }
   })
}
Copy after login

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the different types of testing that you can perform in a UniApp application? What are the different types of testing that you can perform in a UniApp application? Mar 27, 2025 pm 04:59 PM

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

How can you reduce the size of your UniApp application package? How can you reduce the size of your UniApp application package? Mar 27, 2025 pm 04:45 PM

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

How can you use lazy loading to improve performance? How can you use lazy loading to improve performance? Mar 27, 2025 pm 04:47 PM

Lazy loading defers non-critical resources to improve site performance, reducing load times and data usage. Key practices include prioritizing critical content and using efficient APIs.

What debugging tools are available for UniApp development? What debugging tools are available for UniApp development? Mar 27, 2025 pm 05:05 PM

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

How can you optimize images for web performance in UniApp? How can you optimize images for web performance in UniApp? Mar 27, 2025 pm 04:50 PM

The article discusses optimizing images in UniApp for better web performance through compression, responsive design, lazy loading, caching, and using WebP format.

How can you optimize the loading speed of your UniApp application? How can you optimize the loading speed of your UniApp application? Mar 27, 2025 pm 04:43 PM

The article discusses strategies to optimize UniApp loading speed, focusing on minimizing bundle size, optimizing media, caching, code splitting, using CDNs, and reducing network requests.

How can you optimize network requests in UniApp? How can you optimize network requests in UniApp? Mar 27, 2025 pm 04:52 PM

The article discusses strategies for optimizing network requests in UniApp, focusing on reducing latency, implementing caching, and using monitoring tools to enhance application performance.

What are some common performance anti-patterns in UniApp? What are some common performance anti-patterns in UniApp? Mar 27, 2025 pm 04:58 PM

The article discusses common performance anti-patterns in UniApp development, such as excessive global data use and inefficient data binding, and offers strategies to identify and mitigate these issues for better app performance.

See all articles