Home > Web Front-end > uni-app > body text

How does uniapp obtain shared parameters?

PHPz
Release: 2023-04-19 14:38:29
Original
3093 people have browsed it

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!