This article shares the implementation code of js WeChat for your reference. The specific content is as follows
Share Js API on WeChat
Function:
1. Share to WeChat Moments
2. Share with WeChat friends
3. Share to Tencent Weibo
4. Hide/show the menu entrance in the upper right corner
5. Hide/Show the bottom browser toolbar
6. Get the current network status
7. Adjust the image playback component of the WeChat client
8. Close the public platform web page
/**! * The Javascript API of WeChat's built-in browser, the functions include: * * 1. Share to WeChat Moments* 2. Share to WeChat friends* 3. Share to Tencent Weibo* 4. Hide/Show the menu entrance in the upper right corner* 5. Hide/Show the bottom browser toolbar* 6. Get the current network status* 7. Reset the image playback component of the WeChat client* 8. Close the public platform web page* * @author zhaoxianlie */var WeixinApi = (function () { "use strict"; /** * Share to WeChat Moments* @param {Object} data Information to be shared* @p-config {String} appId The public platform appId (service number is available) * @p-config {String} imageUrl Image address* @p-config {String} link link address* @p-config {String} desc Description* @p-config {String} title Shared title* * @param {Object} callbacks Related callback methods* @p-config {Boolean} Async ready method needs to be executed asynchronously, default false * @p-config {Function} ready(argv) Ready status* @p-config {Function} dataLoaded(data) Called after data loading is completed. It is useful when async is true, or can be empty* @p-config {Function} cancel(resp) Cancel * @p-config {Function} fail(resp) Fail* @p-config {Function} confirm(resp) Success* @p-config {Function} all(resp) Callbacks that will be executed regardless of success and failure*/ function weixinShareTimeline(data, callbacks) { callbacks = callbacks || {}; var shareTimeline = function (theData) { WeixinJSBridge.invoke('shareTimeline', { "appid":theData.appId ? theData.appId : '', "img_url":theData.imgUrl, "link":theData.link, "desc":theData.title, "title":theData.desc, // Note that the content to be shared here is desc "img_width":"120", "img_height":"120" }, function (resp) { switch (resp.err_msg) { // share_timeline:cancel User cancels case 'share_timeline:cancel': callbacks.cancel && callbacks.cancel(resp); break; // share_timeline:fail Send failed case 'share_timeline:fail': callbacks.fail && callbacks.fail(resp); break; // share_timeline:confirm Send successfully case 'share_timeline:confirm': case 'share_timeline:ok': callbacks.confirm && callbacks.confirm(resp); break; } // Callbacks.all && callbacks.all(resp); }); }; WeixinJSBridge.on('menu:share:timeline', function (argv) { if (callbacks.async && callbacks.ready) { window["_wx_loadedCb_"] = callbacks.dataLoaded || new Function(); if(window["_wx_loadedCb_"].toString().indexOf("_wx_loadedCb_") > 0) { window["_wx_loadedCb_"] = new Function(); } callbacks.dataLoaded = function (newData) { window["_wx_loadedCb_"](newData); shareTimeline(newData); }; // Then callbacks.ready && callbacks.ready(argv); } else { // Ready status callbacks.ready && callbacks.ready(argv); shareTimeline(data); } }); } /** * Send to friends on WeChat* @param {Object} data Information to be shared* @p-config {String} appId The appId of the public platform (service number available) * @p-config {String} imageUrl Image Address* @p-config {String} link Link address* @p-config {String} desc Description* @p-config {String} title Shared title* * @param {Object} callbacks Related callback methods* @p-config {Boolean} async Whether the ready method needs to be executed asynchronously, default false * @p-config {Function} ready(argv) Ready status * @p-config {Function} dataLoaded(data) Called after data loading is completed. It is useful when async is true, or can be empty* @p-config {Function} cancel(resp) Cancel* @p-config {Function} fail(resp) Fail* @p-config {Function} confirm(resp) Success* @p-config {Function} all(resp) Callbacks that will be executed regardless of success and failure*/ function weixinSendAppMessage(data, callbacks) { callbacks = callbacks || {}; var sendAppMessage = function (theData) { WeixinJSBridge.invoke('sendAppMessage', { "appid":theData.appId ? theData.appId : '', "img_url":theData.imgUrl, "link":theData.link, "desc":theData.desc, "title":theData.title, "img_width":"120", "img_height":"120" }, function (resp) { switch (resp.err_msg) { // send_app_msg:cancel User cancel case 'send_app_msg:cancel': callbacks.cancel && callbacks.cancel(resp); break; // send_app_msg:fail Send failed case 'send_app_msg:fail': callbacks.fail && callbacks.fail(resp); break; // send_app_msg:confirm Send successfully case 'send_app_msg:confirm': case 'send_app_msg:ok': callbacks.confirm && callbacks.confirm(resp); break; } // Callbacks.all && callbacks.all(resp); }); }; WeixinJSBridge.on('menu:share:appmessage', function (argv) { if (callbacks.async && callbacks.ready) { window["_wx_loadedCb_"] = callbacks.dataLoaded || new Function(); if(window["_wx_loadedCb_"].toString().indexOf("_wx_loadedCb_") > 0) { window["_wx_loadedCb_"] = new Function(); } callbacks.dataLoaded = function (newData) { window["_wx_loadedCb_"](newData); sendAppMessage(newData); }; // Then callbacks.ready && callbacks.ready(argv); } else { // Ready status callbacks.ready && callbacks.ready(argv); sendAppMessage(data); } }); } /** * Share to Tencent Weibo* @param {Object} data Information to be shared* @p-config {String} link Link address* @p-config {String} desc Description* * @param {Object} callbacks Related callback methods* @p-config {Boolean} Does the async ready method require asynchronous execution, default false * @p-config {Function} ready(argv) Ready status * @p-config {Function} dataLoaded(data) Called after data loading is completed. It is useful when async is true, or can be empty* @p-config {Function} cancel(resp) Cancel* @p-config {Function} fail(resp) Fail* @p-config {Function} confirm(resp) Success* @p-config {Function} all(resp) Callback that will be executed regardless of success and failure*/ function weixinShareWeibo(data, callbacks) { callbacks = callbacks || {}; var shareWeibo = function (theData) { WeixinJSBridge.invoke('shareWeibo', { "content":theData.desc, "url":theData.link }, function (resp) { switch (resp.err_msg) { // share_weibo:cancel User cancels case 'share_weibo:cancel': callbacks.cancel && callbacks.cancel(resp); break; // share_weibo:fail Send failed case 'share_weibo:fail': callbacks.fail && callbacks.fail(resp); break; // share_weibo:confirm Send successfully case 'share_weibo:confirm': case 'share_weibo:ok': callbacks.confirm && callbacks.confirm(resp); break; } // Callbacks.all && callbacks.all(resp); }); }; WeixinJSBridge.on('menu:share:weibo', function (argv) { if (callbacks.async && callbacks.ready) { window["_wx_loadedCb_"] = callbacks.dataLoaded || new Function(); if(window["_wx_loadedCb_"].toString().indexOf("_wx_loadedCb_") > 0) { window["_wx_loadedCb_"] = new Function(); } callbacks.dataLoaded = function (newData) { window["_wx_loadedCb_"](newData); shareWeibo(newData); }; // Then ready callbacks.ready && callbacks.ready(argv); } else { // Ready status callbacks.ready && callbacks.ready(argv); shareWeibo(data); } }); } /** * Reset the image playback component of WeChat Native. * The parameters must be strongly detected here. If the parameters are not legal, it will directly cause the WeChat client to crash * * @param {String} curSrc The currently played image address* @param {Array} srcList Picture address list*/ function imagePreview(curSrc,srcList) { if(!curSrc || !srcList || srcList.length == 0) { return; } WeixinJSBridge.invoke('imagePreview', { 'current' : curSrc, 'urls' : srcList }); } /** * Show the button in the upper right corner of the web page */ function showOptionMenu() { WeixinJSBridge.call('showOptionMenu'); } /** * Hide the button in the upper right corner of the web page */ function hideOptionMenu() { WeixinJSBridge.call('hideOptionMenu'); } /** * Show the bottom toolbar*/ function showToolbar() { WeixinJSBridge.call('showToolbar'); } /** * Hide the bottom toolbar*/ function hideToolbar() { WeixinJSBridge.call('hideToolbar'); } /** * Return the following types: * * network_type:wifi wifi network* network_type:edge Non-wifi, including 3G/2G * network_type:fail Network disconnect* network_type:wwan 2g or 3g * * Usage: * WeixinApi.getNetworkType(function(networkType){ * * }); * * @param callback */ function getNetworkType(callback) { if (callback && typeof callback == 'function') { WeixinJSBridge.invoke('getNetworkType', {}, function (e) { // Get e.err_msg here, which contains all network types callback(e.err_msg); }); } } /** * Close the current WeChat public platform page*/ function closeWindow() { WeixinJSBridge.call("closeWindow"); } /** * When the page is loaded, execute it, use method: * WeixinApi.ready(function(Api){ * // Only use Api from here is WeixinApi * }); * @param readyCallback */ function wxJsBridgeReady(readyCallback) { if (readyCallback && typeof readyCallback == 'function') { var Api = this; var wxReadyFunc = function () { readyCallback(Api); }; if (typeof window.WeixinJSBridge == "undefined"){ if (document.addEventListener) { document.addEventListener('WeixinJSBridgeReady', wxReadyFunc, false); } else if (document.attachEvent) { document.attachEvent('WeixinJSBridgeReady', wxReadyFunc); document.attachEvent('onWeixinJSBridgeReady', wxReadyFunc); } }else{ wxReadyFunc(); } } } return { version :"1.8", ready :wxJsBridgeReady, shareToTimeline :weixinShareTimeline, shareToWeibo :weixinShareWeibo, shareToFriend :weixinSendAppMessage, showOptionMenu :showOptionMenu, hideOptionMenu :hideOptionMenu, showToolbar :showToolbar, hideToolbar :hideToolbar, getNetworkType :getNetworkType, imagePreview :imagePreview, closeWindow :closeWindow };})();Source code download: js WeChat share
This article has been compiled into "Summary of JavaScript WeChat Development Skills", and everyone is welcome to learn and read.
I would like to recommend a tutorial on WeChat mini program that is highly concerned: "WeChat Mini Program Development Tutorial" has been carefully compiled by the editor of everyone, I hope you like it.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.