Share WeChat mini-games with your friends or circle of friends

Preface : This article mainly describes the content as shown in the title. If you want to know about the WeChat login function authorized by the mini game, please refer to the article on WeChat mini game access WeChat login .

Tip: This article was written in 2019, and many interfaces have been deprecated (interface documents are no longer synchronized). From 24:00 on April 28, 2021, the interface related to mini-program login and user information has been adjusted. For example, the getUserInfo method has been changed to getUserProfile. Click here to view the Mini Program Announcement

1. Introduction

The sharing function of WeChat mini games can be realized in two ways:
1. Passive sharing : as shown in the figure below, click the "dot in the upper right corner" to call up the sharing interface, which is called passive sharing.
We can see that the "Send to Friends" and "Share to Moments" shown in Figure 2 are both on and clickable. This can also be turned off, as described in the code below.
insert image description here
2. Active sharing : As shown in the picture, click the "Share" created by yourself in the game to call up the sharing interface, which is called active sharing.
insert image description here

Tip : If you want to test the sharing function, you can only see the effect by running it on a real device. Development tools can only perform simple simulation effects at present.
After understanding the above two concepts, let's start the code implementation.

2. Code implementation

1. Passive sharing : We write this function in the passiveShare() function and call it when onLoad() or init() .

passiveShare ()
{
    
    
	// @ts-ignore
	wx.showShareMenu({
    
    
		withShareTicket: true,

		// shareAppMessage(可以删除):显示分享给好友选项,shareTimeline(可以删除):显示分享至朋友圈选项
		// 可以只开启前者。如果要开启后者,则两者必须都开启才能生效。
		menus: ['shareAppMessage', 'shareTimeline']
	});

	// 函数参数写回调函数,再修改属性即可,属性有很多,这里主要写两个常用的,可以参考官方开发文档。
	// @ts-ignore
	wx.onShareAppMessage( () => {
    
    
		return {
    
    
			// 标题,不传则默认使用小游戏的名称
			title: "邀您一起来嗨!",

			// 转发链接所显示的图片,比例5:4,资源可以是本地或远程。不传则默认使用游戏截图。           
			imageUrl: "shareImage.png"
		}
	});
}

2. Active sharing : We write this function in the autoShare() function, and then call it when the "Share" button is clicked:

autoShare ()
{
    
    
	// @ts-ignore
	wx.shareAppMessage({
    
    
		title: "邀您一起来嗨!",
		imageUrl: "shareImage.png"
	});
}

Well, the function of WeChat mini-game sharing has basically been realized, and I hope it will be helpful to you.

3. Get the source code for free

I applied for and registered a WeChat public platform account, which was specially used to investigate its rules and restrictions. The following QR code is a small game written by myself, with login and sharing functions. For beginners, you can also private message me to get the source code and distribute it for free, just want to be able to scan the code to experience it (the message reply may not be timely, but I will definitely reply), so that I can explore later and share it with everyone .
insert image description here

Guess you like

Origin blog.csdn.net/HYNN12/article/details/109674244