React Js 위 챗 공유 패키지

본문 은 다음 과 같다.
http://blog.csdn.net/wyk304443164
직접 원본 코드 올 리 기:
/**
 * Created by wuyakun on 2017/5/23.
 */

import Fetch from './FetchIt';
import API_URL from './url';
import Share from './Share';

let wxUtils = {};

//////////////////////////////////////////////////////////////////////////////////////
//
//   
//
//////////////////////////////////////////////////////////////////////////////////////

/**
 getshareinfo?type=
 type :goods      team    
 id   id
 tid  ID
 * @param config
 * @param shareInfo {imgUrl,title,description,link}
 */

function share2wx(config, shareInfo) {
    const share = new Share({
        appid: config.appid, //   ,        
        timestamp: config.timestamp, //   ,        
        nonceStr: config.nonceStr, //   ,        
        signature: config.signature, //   ,  
    });
    share.init(Object.assign({}, shareInfo));
}

function getConfig(shareInfo) {
    let href = window.location.href.split('#')[0];
    const url = encodeURIComponent(href /*window.location.href*/);
    Fetch.get(`${API_URL.mobile.signature_path}?url=${url}`).then(data => {
        share2wx(data, shareInfo);
    });
}

/**
 * @param shareInfo
 */

wxUtils.share = function (shareInfo) {
    getConfig(shareInfo);
};

//////////////////////////////////////////////////////////////////////////////////////
//
//     
//
//////////////////////////////////////////////////////////////////////////////////////

/**
 *        Menu
 * @param open
 */

wxUtils.optionMenu = function (open = true) {
    if (open) {
        openOptionMenu();
    } else {
        disabledOptionMenu();
    }
};

/**
 *        
 */

function disabledOptionMenu() {
    if (typeof WeixinJSBridge === "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady(true), false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady(true));
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(true));
        }
    } else {
        onBridgeReady(true);
    }
}

/**
 *   menu
 */

function openOptionMenu() {
    if (typeof WeixinJSBridge === "undefined") {
        if (document.addEventListener) {
            document.addEventListener('WeixinJSBridgeReady', onBridgeReady(false), false);
        } else if (document.attachEvent) {
            document.attachEvent('WeixinJSBridgeReady', onBridgeReady(false));
            document.attachEvent('onWeixinJSBridgeReady', onBridgeReady(false));
        }
    } else {
        onBridgeReady(false);
    }
}

function onBridgeReady(disable = true) {
    if (typeof WeixinJSBridge !== "undefined") WeixinJSBridge.call(disable ? 'hideOptionMenu' : 'showOptionMenu');
}
/**
 *             
 * @param disable
 */

wxUtils.disabledToolbar = function (disable = true) {
    document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {
        //       API       
        WeixinJSBridge.call(disable ? 'hideToolbar' : 'showToolbar');
    });
};

export default wxUtils;
//   
function Share(config) {
    wx.config({
        debug: false, //       
        appId: config.appid, //timestamp: config.timestamp, //nonceStr: config.nonceStr, //signature: config.signature, //   ,  ,   1
        jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareWeibo'], //   ,     JS    ,  JS       2
    });
}

Share.prototype = {
    constructor: Share,

    init(config) {
        this.imgUrl = config.imgUrl;
        this.link = config.link;
        // this.musicPath = config.musicPath;
        this.description = config.description;
        this.title = config.title;

        wx.ready(() => {
            // if (this.musicPath) {
            //     document.getElementById('musicIcon').play();
            // }
            this.toFriend();
            this.toTimeline();
        });

        wx.error(res => {
            console.log(`${res}`);
        });
    },

    toFriend() {
        wx.onMenuShareAppMessage({
            imgUrl: this.imgUrl,
            link: this.link,
            title: this.title,
            desc: this.description,
            success: function () {
                //               
            },
        });
    },

    toTimeline() {
        wx.onMenuShareTimeline({
            imgUrl: this.imgUrl,
            link: this.link,
            title: this.title,
            desc: this.description,
            success: function () {
                //               
            },
        });
    },
};

export default Share;
//    
        BaseComponent.wxUtils.optionMenu(true);
        BaseComponent.wxUtils.share({
            imgUrl: activityData.sharePicUrl,
            title: activityData.shareTitle,
            description: activityData.shareContent,
            link: url,
        });

좋은 웹페이지 즐겨찾기