当前位置:Gxlcms > JavaScript > 代码详解ReactJs微信分享封装

代码详解ReactJs微信分享封装

时间:2021-07-01 10:21:17 帮助过:4人阅读

本篇文章给大家分享的内容是代码详解React Js 微信分享封装,有着一定的参考价值,有需要的朋友可以参考一下

话不多说,直接上源代码:

  1. /**
  2. * Created by wuyakun on 2017/5/23.
  3. */import Fetch from './FetchIt';
  4. import API_URL from './url';
  5. import Share from './Share';let wxUtils = {};////////////////////////////////////////////////////////////////////////////////////////// 分享/////////////////////////////////////////////////////////////////////////////////////////**
  6. getshareinfo?type=
  7. type :goods 课程详情 team 团详情
  8. id 课程id
  9. tid 团ID
  10. * @param config
  11. * @param shareInfo {imgUrl,title,description,link}
  12. */function share2wx(config, shareInfo) { const share = new Share({
  13. appid: config.appid, // 必填,公众号的唯一标识
  14. timestamp: config.timestamp, // 必填,生成签名的时间戳
  15. nonceStr: config.nonceStr, // 必填,生成签名的随机串
  16. signature: config.signature, // 必填,签名
  17. });
  18. share.init(Object.assign({}, shareInfo));
  19. }
  20. function getConfig(shareInfo) { let href = window.location.href.split('#')[0]; const url = encodeURIComponent(href /*window.location.href*/);
  21. Fetch.get(`${API_URL.mobile.signature_path}?url=${url}`).then(data => {
  22. share2wx(data, shareInfo);
  23. });
  24. }/**
  25. * @param shareInfo
  26. */wxUtils.share = function (shareInfo) {
  27. getConfig(shareInfo);
  28. };////////////////////////////////////////////////////////////////////////////////////////// 分享结束/////////////////////////////////////////////////////////////////////////////////////////**
  29. * 是否开启右上角Menu
  30. * @param open
  31. */wxUtils.optionMenu = function (open = true) { if (open) {
  32. openOptionMenu();
  33. } else {
  34. disabledOptionMenu();
  35. }
  36. };/**
  37. * 是否禁用右上角
  38. */function disabledOptionMenu() { if (typeof WeixinJSBridge === "undefined") { if (document.addEventListener) {
  39. document.addEventListener('WeixinJSBridgeReady', onBridgeReady(true), false);
  40. } else if (document.attachEvent) {
  41. document.attachEvent('WeixinJSBridgeReady', onBridgeReady(true));
  42. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(true));
  43. }
  44. } else {
  45. onBridgeReady(true);
  46. }
  47. }/**
  48. * 开启menu
  49. */function openOptionMenu() { if (typeof WeixinJSBridge === "undefined") { if (document.addEventListener) {
  50. document.addEventListener('WeixinJSBridgeReady', onBridgeReady(false), false);
  51. } else if (document.attachEvent) {
  52. document.attachEvent('WeixinJSBridgeReady', onBridgeReady(false));
  53. document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(false));
  54. }
  55. } else {
  56. onBridgeReady(false);
  57. }
  58. }
  59. function onBridgeReady(disable = true) { if (typeof WeixinJSBridge !== "undefined") WeixinJSBridge.call(disable ? 'hideOptionMenu' : 'showOptionMenu');
  60. }/**
  61. * 隐藏微信网页底部的导航栏
  62. * @param disable
  63. */wxUtils.disabledToolbar = function (disable = true) {
  64. document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() { // 通过下面这个API隐藏底部导航栏
  65. WeixinJSBridge.call(disable ? 'hideToolbar' : 'showToolbar');
  66. });
  67. };
  68. export default wxUtils;
  1. // 分享function Share(config) {
  2. wx.config({ debug: false, // 开启调试模式 appId: config.appid, // 必填,公众号的唯一标识 timestamp: config.timestamp, // 必填,生成签名的时间戳 nonceStr: config.nonceStr, // 必填,生成签名的随机串 signature: config.signature, // 必填,签名,见附录1
  3. jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareWeibo'], // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
  4. });
  5. }
  6. Share.prototype = { constructor: Share,
  7. init(config) { this.imgUrl = config.imgUrl; this.link = config.link; // this.musicPath = config.musicPath; this.description = config.description; this.title = config.title;
  8. wx.ready(() => { // if (this.musicPath) { // document.getElementById('musicIcon').play(); // } this.toFriend(); this.toTimeline();
  9. }); wx.error(res => { console.log(`${res}`);
  10. });
  11. }, toFriend() { wx.onMenuShareAppMessage({
  12. imgUrl: this.imgUrl,
  13. link: this.link,
  14. title: this.title,
  15. desc: this.description,
  16. success: function () { // 用户确认分享后执行的回调函数
  17. },
  18. });
  19. }, toTimeline() { wx.onMenuShareTimeline({
  20. imgUrl: this.imgUrl,
  21. link: this.link,
  22. title: this.title,
  23. desc: this.description,
  24. success: function () { // 用户确认分享后执行的回调函数
  25. },
  26. });
  27. },
  28. };export default Share;
  1. //开启分享
  2. BaseComponent.wxUtils.optionMenu(true);
  3. BaseComponent.wxUtils.share({
  4. imgUrl: activityData.sharePicUrl,
  5. title: activityData.shareTitle,
  6. description: activityData.shareContent,
  7. link: url,
  8. });

以上就是代码详解React Js 微信分享封装的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行