QQ 微博分享功能封裝
/**
* @Description: 分享公共函數(shù)
* @params {String} type 分享類型 // qq or weibo 目前只有兩種
* @params {Object} info 分享的內(nèi)容
* info: {
* url: String 分享的鏈接[必選]
* title: String 分享鏈接的標(biāo)題[qq必選]
* summary: String 分享鏈接的內(nèi)容概述[可選]
* desc: String 分享的描述內(nèi)容 [可選]
* image: String 分享內(nèi)容的封面圖 [可選]
* }
* @return 打開(kāi)一個(gè)新的分享窗口
* @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,
};
// 打開(kāi)窗口
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 = `我正在行家學(xué)習(xí)${courseTitle},一起來(lái)學(xué)習(xí)吧。`;
info.title = type === 'qq' ? courseTitle : shareTitle;
info.desc = type === 'qq' ? shareTitle : courseTitle;
ShareFun(type, info);
}