WeChat development practical sharing function

PHP中文网
Release: 2017-06-20 13:57:28
Original
2828 people have browsed it

By understanding the requirements, it can be broken down into:

(1) WeChat mobile phone users can use WeChat’s JSSDK.

(2) Select the image and use "chooseImage" of JSSDK. Since the local address cannot be shared when sharing the image, "uploadImage" of JSSDK is also needed.

(3) Sharing to Moments requires "onMenuShareTimeline" of JSSDK.

Taken together, the business logic is shown in Figure 4.5.

Figure 4.5 Business logic structure diagram

First copy the JSSDK environment to the directory of this section and create an index. html file, imageSharing.js file, the directory structure is shown in Figure 4.6.

Figure 4.6 Section 4.2 Directory Structure

Modify the configuration file of the JSSDK environment, the code is as follows:

01 jsApiList: [ // 必填,需要使用的JS接口列表,所有JS接口列表见附录B 02 "chooseImage", 03 "previewImage", 04 "uploadImage", 05 "onMenuShareTimeline" 06 ] 07 //其他代码略
Copy after login

Build a The click button with "id" equal to "chooseImage", and the container used to display the selected image after clicking the button, add the following code to the index.html file:

01  02  03  04  05  07 第4章 4.2节-实例:从手机相册中选照片然后分享 08  09  10  11  12  13  14  15  16  17  18  32 
33 34

:)

35 实例:从手机相册中选照片然后分享,支持选择1张图片! 36

37

38

没有自定义分享图片

39

40 41
Copy after login

Add processing business to the imageSharing.js file The code is as follows:

01 /* 02 函数名称:wxJSSDK.shareApi 03 函数功能:为wxJSSDK增加分享模块 04 参数: 05 shareList(Array) 必选项,待分享的API配置表 06 */ 07 wxJSSDK.shareApi = function(shareList){ 08 if(wxJSSDK.isReady){//wxJSSDK.isReady 查看微信JSSDK是否初始化完毕 09 10 //获取“分享到朋友圈”按钮点击状态及自定义分享内容接口 11 if(shareList.onMenuShareTimeline){ 12 var ParametersTimeline = shareList.onMenuShareTimeline; 13 wx.onMenuShareTimeline({ 14 title: ParametersTimeline.title, // 分享标题 15 link: ParametersTimeline.link, // 分享链接 16 imgUrl: ParametersTimeline.imgUrl, // 分享图标 17 success: function () { 18 // 用户确认分享后执行的回调函数 19 ParametersTimeline.success && ParametersTimeline.success(); 20 }, 21 cancel: function () { 22 // 用户取消分享后执行的回调函数 23 ParametersTimeline.cancel && ParametersTimeline.cancel(); 24 } 25 }); 26 } 27 28 }else{ 29 console.log("抱歉,wx没有初始化完毕,请等待wx初始化完毕,再调用分享服务。"); 30 } 31 } 32 /* 33 函数名称:wxJSSDK.imageApi 34 函数功能:为wxJSSDK增加图像服务 35 参数: 36 imageApi 图像API Object 配置 37 */ 38 wxJSSDK.imageApi = function(imageApi){ 39 if(wxJSSDK.isReady){//wxJSSDK.isReady 查看微信JSSDK是否初始化完毕 40 if(imageApi){ 41 42 imageApi.chooseImage && wx.chooseImage({//拍照或从手机相册中选图接口 43 success: function (res) { 44 imageApi.chooseImage.success && 45 imageApi.chooseImage.success(res); 46 } 47 }); 48 49 imageApi.previewImage && wx.previewImage({ // 预览图片接口 50 current: imageApi.previewImage.current, // 当前显示的图片链接 51 urls: imageApi.previewImage.urls // 需要预览的图片链接列表 52 }); 53 54 imageApi.uploadImage && wx.uploadImage({ // 上传图片接口 55 localId: imageApi.uploadImage.localId, // 需要上传的图片的本地ID, 56 由chooseImage接口获得 57 isShowProgressTips: imageApi.uploadImage.isShowProgressTips || 1, // 58 默认为1,显示进度提示 59 success: function (res) { 60 imageApi.uploadImage.success && 61 imageApi.uploadImage.success(res); 62 } 63 }); 64 65 imageApi.downloadImage && wx.downloadImage({//下载图片接口 66 serverId:imageApi.downloadImage.serverId, // 需要下载的图片的服务器端 67 ID,由uploadImage接口获得 68 isShowProgressTips: imageApi.downloadImage.isShowProgressTips || 1, // 69 默认为1,显示进度提示 70 success: function (res) { 71 imageApi.downloadImage.success && 72 imageApi.downloadImage.success(res); 73 } 74 }); 75 }else{ 76 console.log("缺少配置参数"); 77 } 78 }else{ 79 console.log("抱歉,wx没有初始化完毕,请等待wx初始化完毕,再调用图像接口服 80 务。"); 81 } 82 83 } 84 85 window.onload = function(){ 86 var chooseImageID, // 拍照或从手机相册中选图接口 87 shareImage, 88 uploadImage = function(back){ 89 wxJSSDK.imageApi({ // 上传图片··· 90 uploadImage:{ 91 localId:chooseImageID.toString(), 92 success:function(res){//临时access_token,上传图片成功之后,执行分 93 享操作 94 shareImage = 95 "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token= 96 eQv3HPwEFxwsw8cyh5O7DjaNOoGd4d-jYtG_c9uW-YbwUYxkMywh_O3LCC 97 ZtmX8ZWr8np0Q5CqAox7lghNkNuiNHU8M618jbRvcaLjQuHq8&media_id="+res.serverId; // 返回图片的服务器端ID 98 back && back(); 99 } 100 } 101 }); 102 }, 103 shareTimeline = function(){ 104 uploadImage(function(){ 105 wxJSSDK.shareApi({ // 分享图片··· 106 onMenuShareTimeline : { // 分享到朋友圈 107 title: "实例:从手机相册中选照片然后分享!", // 分享标题 108 link: 'http://weibo.com/xixinliang', // 分享链接 109 imgUrl: shareImage, // 分享图标 110 success: function () { 111 112 }, 113 cancel: function () { 114 115 } 116 } 117 }); 118 }); 119 }; 120 $("#chooseImage").click(function(){ 121 wxJSSDK.imageApi({ 22 chooseImage:{ 23 success:function(res){ 24 chooseImageID = res.localIds[0]; // 返回选定照片的本地ID列表, 25 localId可以作为img标签的src属性显示图片 26 $("#imageContainer").html(""); 28 $("#selectImg").html("已选择图片,请点击右上角分享到朋友圈按钮"); 29 shareTimeline(); 30 } 31 } 32 }); 33 }); 34 }
Copy after login

[Code explanation]

  • In index.html, a button to share a custom image is created, such as As shown in Figure 4.7.

  • Click the share button and call the JSSDK’s image selection API to allow the user to select the image, as shown in Figure 4.8.

Figure 4.7 Custom Sharing Picture UI

Figure 4.8 After selecting the UI

  • , call "uploadImage" to upload the image.

  • After the upload is successful, return the server "serverId" after uploading, then call the multimedia download API, and assign the image to the JSSDK sharing API "onMenuShareTimeline".

Users can view the sharing effect, as shown in Figure 4.9 and Figure 4.10.

Figure 4.9 Sharing to Moments Editing UI

Figure 4.10 Successfully Sharing Custom Pictures to Moments

The above is the detailed content of WeChat development practical sharing function. 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
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!