QQ 微博分享功能封装

QQ 微博分享功能封装

/**
 * @Description: 分享公共函数
 * @params {String} type 分享类型 // qq or weibo 目前只有两种
 * @params {Object} info 分享的内容
 *                  info: {
 *                      url: String 分享的链接[必选]
 *                      title: String 分享链接的标题[qq必选]
 *                      summary: String 分享链接的内容概述[可选]
 *                      desc: String 分享的描述内容 [可选]
 *                      image: String 分享内容的封面图 [可选]
 *                  }
 * @return 打开一个新的分享窗口
 * @LastEditors: 蔡江旭
 * @LastEditTime: Do not edit
 * @Date: 2019-03-19 16:06:49
 */
const urlList = {
    qq: 'http://connect.qq.com/widget/shareqq/index.html?url={{url}}&title={{title}}&source={{source}}&desc={{desc}}&pics={{image}}&summary={{summary}}',
    weibo: 'http://service.weibo.com/share/share.php?url={{url}}&title={{title}}&source={{source}}&desc={{desc}}&pics={{image}}&summary={{summary}}',
};
export default function(type, info) {
    console.log('info', info);
    if (urlList[type] && urlList[type].length) {
        // 替换url
        const shareUrl = urlList[type].replace(/{{(\w+)}}/g, (match, p1) => {
            return encodeURIComponent(info[p1] || '');
        });
        // 获取窗口的宽高
        const screenInfo = window.screen;
        const newWindowArea = {
            height: screenInfo.height / 3 * 2,
            width: screenInfo.width / 3 * 2,
        };
        // 打开窗口
        window.open(shareUrl, 'newwindow', `height=${newWindowArea.height}, width=${newWindowArea.width}`);
    } else {
        console.warn('url不存在');
    }
}
// 传值
// 分享
      toShare(type,value){
        const { introduction = '', name: courseTitle = '' } = course;
        let summary = introduction.replace(/<[^>]+>|&[^>]+;/g, '');
        summary = summary.replace(/[\\r| ]*\\n/g,''); //去除行尾空白
        summary = summary.substring(0, 50);

        // 其他操作
        const info = {
            url: window.location.href,
            summary,
            image: course.coverPicture,
        };
        const shareTitle = `我正在行家学习${courseTitle},一起来学习吧。`;
        info.title = type === 'qq' ? courseTitle : shareTitle;
        info.desc = type === 'qq' ? shareTitle : courseTitle;
        ShareFun(type, info);
      }

你可能感兴趣的:(QQ 微博分享功能封装)