Share the way

In the first way, applet sharing can be achieved by clicking the three dots in the upper right corner or clicking the button. It should be noted that the current file must have onShareAppMessage method, and the onShareTimeline method is required if the file is shared to the moments of friends, in which several properties can be configured to share the copy As well as images,title share title, imageUrl share image, path share links to friends, query share links to moments, pay attention to there can not be asynchronous requests, because sharing is a synchronous operation, not asynchronous operation,ios currently does not open the function of sharing to moments.

Sharing can be done in each file or as a common share, such as using the wx.onAppRoute method in app.js, for example:

wx.onAppRoute(()=>{
  const pages = getCurrentPages()
  const currentPage = pages[pages.length - 1]
  currentPage.onShareAppMessage = {
    title: '',
    imageUrl: '',
    path: ''
  }
})
Copy the code

Second way to generate small program qr code, scan to enter the corresponding page, generating parameters of qr code information stored in the scene, but the scene parameters cannot be more than 32 characters, so you also need to call an interface to information into a short string, call the small program interface to generate small program qr code, use canvas to qr code onto a Block area, and then generate a picture to save to the phone album, the user scan this picture will enter the corresponding page, to achieve the sharing function

Problems with the sharing function

  • Both A and B are distributors. A shares A page with B, and B shares the page with his friends. After returning to the current page,B finds that the shareId of the current page has changed
  • Continuation 1: After entering other pages through the sharing page and returning to the sharing page again, I found that the newly added parameters in the URL did not exist
  • The shared shareId is not obtained

The first problem is that I modified the options of the current sharing page. Options. shareId is equal to the external shareId or its own shareId, because options is an object, which stores the parameters of the current page, that is, the parameters that are concatenated at the end of the URL The onShow method is triggered after sharing, which reassigns the shareId to the external shareId.

Summary: Modifying onLoad options directly will change the current page URL

Second question although I changed the current options, found after share url has also changed, but I went into another page, again to find the new url parameter after returning to share not again, this time I realized that I just changed the current page url, but did not change the original url, so the return of the parameters on the url after early again Get the route of the current page to add parameters to the URL. Add to app.js:

wx.onAppRoute(route => {
  route.query.shareId = wx.getStorageSync('shareId')
})
Copy the code

Summary: Modifying options directly only changes the url of the current page, but the original URL does not change, so you need to change the route

The third problem was caused by an interface error. The page interface error caused the code to not continue, so the assignment was unsuccessful.

Promise to use

If A calls B and then uses then, then B must be guaranteed to use A promise. If B calls C and C uses A promise, there will be A problem if B doesn’t use it