Since the micro channel small program supports the function of sharing to the circle of friends, the business side seems to see the huge flow brought by the circle of friends, has been some eager to try the mood, by demand, there is the realization of the exploration of sharing the circle of friends.

The onShareTimeline method is just like onShareAppMessage. It is Easy to return several configuration items.

However, according to the convention of wechat, happiness will not come too suddenly, the function is supported, but must obey the rules (strange), in addition to a Beta version, does not support IOS, the biggest limitation is the single-page mode, as shown in the picture:

When you click the shared content from moments, you don’t really enter the mini-program, but enter a single-page mode first. The single-page mode has many restrictions, and the most significant one is that wx.login is not supported. If the shared page is static or doesn’t rely on wx.login to do automatic login and so on, then this isn’t a problem, and this article documents some of the solutions when you can’t get the code.

Some solutions

1. The transfer page

First of all, refer to the official method for judging single-page mode, as shown in the figure:

const { scene } = wx.getLaunchOptionsSync()

if(scene === 1154) {
 // Single page mode ADAPTS logic
}
Copy the code

To determine the current single-page mode, switch to a static transit page (component). This may not be what the customer wants, however, as the preview page is one and the applet is another, and the experience is still a bit bad.

2. Interface modification, support not logged in to call down

Since the data interface depends on the login process, is it possible to modify the interface so that it can be used down without login to ensure the normal display of the page in single-page mode? The answer is yes, but after a look, at least a dozen interfaces need to be changed, and there is a lot of work on both front and back ends, so we gave up and came up with solution 3.

3. Special code

I can’t get the code, but can I pass in a special code in single-page mode with the backend convention to simulate login to match the original automatic login process?

The answer is yes, but there are also a lot of points to note. First, the code is false. If the shared page still relies on the code to do some operations in the single-page mode, it may produce errors.

Secondly, whether the simulated login of a new user is successful, or an experience account is returned to adapt the login, some business process errors may be caused, especially in the data statistics, so it needs to be handled carefully.

To OK? There are big pit

Since sharing moments is a public beta function, wechat may make some adjustments at any time (even if it is a stable version, it will still change 😥). The test before the National Day found that when I click to share content under Android phone, I will enter the single-page mode first, and when I come back after a holiday, I find that the single-page mode is gone. I am not sure whether the single-page mode has been cancelled for all Android phones, and it may also be related to wechat version.

If scene === 1154 is displayed in the official document, then a serious bug will occur if scene === 1154 is displayed in the official document.

A judgment that may be temporarily feasible

The mode of getLaunchOptionsSync returns singlePage in single-page mode, but this field is not declared in the official documentation and is still unreliable. Or if scene === 1154 is IOS, it can be judged as single-page mode. This method is not as reliable as mode. Or with the ability to disable single-page mode…

const { scene, mode } = wx.getLaunchOptionsSync()

// mode === 'singlePage' OR scene === 1154 && isIOS
Copy the code

It can be seen that a more mature and more complex business small program to share online circle of friends function, the risk is relatively large. Please let me know if you have a more reliable solution.