search
HomeWeb Front-enduni-appHow 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.

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

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)
    })
  }
}

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

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);
            }
         })
      }
   })
}

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
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
How do you debug issues on different platforms (e.g., mobile, web)?How do you debug issues on different platforms (e.g., mobile, web)?Mar 27, 2025 pm 05:07 PM

The article discusses debugging strategies for mobile and web platforms, highlighting tools like Android Studio, Xcode, and Chrome DevTools, and techniques for consistent results across OS and performance optimization.

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 do you perform end-to-end testing for UniApp applications?How do you perform end-to-end testing for UniApp applications?Mar 27, 2025 pm 05:04 PM

The article discusses end-to-end testing for UniApp applications across multiple platforms. It covers defining test scenarios, choosing tools like Appium and Cypress, setting up environments, writing and running tests, analyzing results, and integrat

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

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.

How can you use profiling tools to identify performance bottlenecks in UniApp?How can you use profiling tools to identify performance bottlenecks in UniApp?Mar 27, 2025 pm 04:57 PM

The article discusses using profiling tools to identify and resolve performance bottlenecks in UniApp, focusing on setup, data analysis, and optimization.

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.

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.

See all articles

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools