Source: qianxiang_h5/share.js

'use strict';

/* 分享 */
/**
 * @module qianxiang_h5.share
 * @description 请在whale.share模块内调用
 */

import { extend, loadScript } from '../lib/utils';
import * as os from '../lib/os';


/** @type {Object}
 * @constant
 * @prop {String} basicShareConfig.title 分享标题
 * @prop {String} basicShareConfig.desc 分享文案
 * @prop {String} basicShareConfig.imgUrl 分享小图标链接
 * @prop {String} basicShareConfig.link 分享地址
 * */
const basicShareConfig = {
    title: '钱香金融 —知名创投供应链金融信息服务平台',
    desc: '银行存管,10.2%近3月平均历史年化,是您放心之选。',
    imgUrl: 'https://pic.qianxiangbank.com/waps/images/caonimadedasb.png',// 图片地址
    link: 'https://m.qianxiangbank.com'
};

const baseConfig = {
    title: basicShareConfig.title,
    desc: basicShareConfig.desc,
    link: basicShareConfig.link,
    imgUrl: basicShareConfig.imageUrl,
    shareToApp: true, // 是否可以分享到app
    shareToTimeLine: true, // 是否可以分享到朋友圈
    shareToQQ: true, // 是否可以分享到QQ
    success: function () {
    } // 分享成功
};

let appShareCache = {};

/**
 * 设置分享参数
 * @function setShareData
 * @param {Object} config 参数
 * @prop {String} [config.title] 分享标题
 * @prop {String} [config.desc] 分享文案
 * @prop {String} [config.imgUrl] 分享小图标链接
 * @prop {String} [config.link] 分享地址
 */
function setShareData(config) {
    let newConfig = extend(baseConfig, config);

    // app的分享
    if (os.plus) {
        appShareCache = {
            title: baseConfig.title,
            desc: baseConfig.desc,
            imgUrl: baseConfig.imgUrl,
            link: baseConfig.link
        };
        return;
    }

    if (!os.wechat) {
        return false;
    }

    /* JSONP */
    let callback = '_SHARE_' + Date.now();
    window[callback] = function (r) {
        wx.config({
            debug: false,
            appId: r.data.appId, // 必填,公众号的唯一标识
            timestamp: r.data.timestamp, // 必填,生成签名的时间戳
            nonceStr: r.data.nonceStr,  // 必填,生成签名的随机串
            signature: r.data.signature, // 必填,签名
            jsApiList: [
                'checkJsApi', 'onMenuShareTimeline', 'onMenuShareAppMessage', 'onMenuShareQQ',
                'onMenuShareWeibo', 'hideMenuItems', 'showMenuItems', 'hideAllNonBaseMenuItem',
                'showAllNonBaseMenuItem', 'translateVoice', 'startRecord', 'stopRecord',
                'onRecordEnd', 'playVoice', 'pauseVoice', 'stopVoice', 'uploadVoice',
                'downloadVoice', 'chooseImage', 'previewImage', 'uploadImage', 'downloadImage',
                'getNetworkType', 'hideOptionMenu', 'showOptionMenu'
            ]
        });
        wx.ready(function () {
            wx.onMenuShareAppMessage(newConfig);
            wx.onMenuShareTimeline(newConfig);
            wx.onMenuShareQQ(newConfig);
            let hideItems = [];
            if (!newConfig.shareToApp) {
                hideItems.push('menuItem:share:appMessage');
            }
            if (!newConfig.shareToTimeLine) {
                hideItems.push('menuItem:share:timeline');
            }
            if (!newConfig.shareToQQ) {
                hideItems.push('menuItem:share:qq');
                hideItems.push('menuItem:share:QZone');
            }
            wx.hideMenuItems({
                menuList: hideItems
            });
        });
    };

    loadScript('https://cunapi.qianxiangbank.com/share/shareSign.api?url=' + encodeURIComponent(location.href) + '&callback=' + callback, function () {});
}

/**
 * 显示分享弹层
 */
function showSharePanel() {
    if (os.plus) {
        let temp = appShareCache;
        if (temp.title == '' || temp.desc == '' || temp.link == '' || temp.imgUrl == '') {
            console.log('请先调用whale.share.setShareData()');
            return;
        }
        if(QxAppDomain && QxAppDomain.showSharePanel){
            QxAppDomain.showSharePanel(temp.title, temp.desc, temp.link, temp.imgUrl);
        }
    }else{
        if(document.getElementById('_share_panel')){
            return;
        }
        let div = document.createElement('DIV');
        div.setAttribute('id', '_share_panel');
        let img = document.createElement('IMG');
        div.appendChild(img);
        div.onclick = function(){
            document.getElementById('_share_panel').remove();
        };
        div.style = 'background-color: rgba(0,0,0,.8);position: fixed; left: 0;top: 0; width: 100%;height: 100%;z-index:19891020';
        img.style = 'width: 4.8rem;height:5.9rem;background: none;position:absolute;right:.2rem;top:0;';
        img.src = '//pic.qianxiangbank.com/waps/images/shareZyImg.png';
        document.body.appendChild(div);
    }
}

export {
    basicShareConfig,
    appShareCache,
    setShareData,
    showSharePanel
};