// #ifdef APP-PLUS let alphaBg, shareMenu; // 关闭弹窗 export const closeShare = function(){ alphaBg && alphaBg.close(); alphaBg && shareMenu.close(); } // 分享 function onShare(item, shareInfo,callback) { if (shareInfo.type == undefined) { shareInfo.type = item.type; } let shareObj = { provider: item.provider, type: shareInfo.type, success: (res) => { callback && callback(item); console.log('success:' + JSON.stringify(res)); }, fail: (err) => { console.log('分享失败,参数缺失 fail:' + JSON.stringify(err)); } }; if (shareInfo.shareTitle) { shareObj.title = shareInfo.shareTitle; }else if(item.provider == 'qq'){ uni.showToast({ title: '缺失分享的标题', icon: 'none' }); return; } if(shareInfo.type == 0 || item.provider == 'qq'){ if (shareInfo.shareUrl) { shareObj.href = shareInfo.shareUrl; }else{ uni.showToast({ title: '缺失分享的地址', icon: 'none' }); return; } } if([0,1,3,4].includes(shareInfo.type)){ if (shareInfo.shareContent) { shareObj.summary = shareInfo.shareContent; }else{ uni.showToast({ title: '缺失分享的描述', icon: 'none' }); return; } } if([0,2,5].includes(shareInfo.type)){ if (shareInfo.shareImg) { shareObj.imageUrl = shareInfo.shareImg; }else{ uni.showToast({ title: '缺失分享的图片', icon: 'none' }); return; } } if([3,4].includes(shareInfo.type)){ if (shareInfo.mediaUrl) { shareObj.mediaUrl = shareInfo.mediaUrl; }else{ uni.showToast({ title: '缺失分享的音视频地址', icon: 'none' }); return; } } if(shareInfo.type == 5){ if (shareInfo.appId && shareInfo.appPath && shareInfo.appWebUrl) { shareObj.miniProgram = { id:shareInfo.appId, path:shareInfo.appPath, webUrl:shareInfo.appWebUrl, }; if(shareInfo.appType){ shareObj.miniProgram.type = shareInfo.appType; } }else{ uni.showToast({ title: '缺失分享小程序的参数', icon: 'none' }); return; } }

if (item.scene) {
	shareObj.scene = item.scene;
}
uni.share(shareObj);

} let platformShareList = []; // 获取服务商支持的分享 uni.getProvider({ service: 'share', success: function (res) { if (res.provider.includes('qq')) { platformShareList = [{ icon: '/static/share/icon_qq.png', text: 'QQ', onClick: onShare, provider: 'qq', type: 1 }].concat(platformShareList); } if (res.provider.includes('weixin')) { platformShareList = [{ icon: '/static/share/icon_weixin.png', text: '微信好友', onClick: onShare, provider: 'weixin', scene: 'WXSceneSession', type: 0 }, { icon: '/static/share/icon_pengyouquan.png', text: '朋友圈', onClick: onShare, provider: 'weixin', scene: 'WXSenceTimeline', type: 0 }].concat(platformShareList); } } }); // 根据type类型过滤掉不支持的平台 function platformFilter(data){ let platformList = []; let supportList = [ ['weixin'], ['weixin'], ['qq'], ]; let currentSupport = []; if(data.type >= 0 && data.type <= 5){ currentSupport = supportList[data.type]; } platformShareList.forEach((item,index) => { if(data.type >= 0 && data.type <= 5){ if(currentSupport.includes(item.provider)){ if(item.provider == 'weixin'){ if(item.text == '小程序'){ if(data.type == 5){ platformList.push(item); } }else if(data.type !== 5){ platformList.push(item); } } else { platformList.push(item); } } }else{ if(item.provider == 'weixin'){ if(item.text == '小程序'){ if(data.appId && data.appPath){ platformList.push(item); } }else { platformList.push(item); } } else { platformList.push(item); } } }); return platformList; } // 数据处理 function dataFactory(shareInfo = {}) { let config = { ...shareInfo }; config.shareUrl = shareInfo.shareUrl || ''; config.shareContent = shareInfo.shareContent || '分享的描述'; config.shareImg = shareInfo.shareImg || '分享的图片'; return config; } export default function (shareInfo, callback) { shareInfo = dataFactory(shareInfo); let shareList = platformFilter(shareInfo); if(shareList.length == 0){ uni.showToast({ title: '该平台不支持分享', icon: 'none' }); return; } alphaBg = new plus.nativeObj.View('alphaBg', { top: '0px', left: '0px', height: '100%', width: '100%', backgroundColor: 'rgba(0,0,0,0.5)' }); alphaBg.addEventListener('click', function () { alphaBg.close(); shareMenu.close(); }); shareMenu = new plus.nativeObj.View('shareMenu', { bottom: '0px', left: '0px', height: '134px', width: '100%', backgroundColor: 'rgb(255,255,255)' }); shareMenu.draw([{ tag: 'rect', color: '#e7e7e7', position: { top: '0px', height: '1px' } },{ tag: 'font', id: 'sharecancel', text: '取消分享', textStyles: { size: '14px' }, position: { bottom: '0px', height: '44px' } },{ tag: 'rect', color: '#e7e7e7', position: { bottom: '45px', height: '1px' } }]); shareList.map((v, k) => { let time = new Date().getTime(); let row = Math.floor(k / 2); let col = k % 2; let item = [{ src: v.icon, id: Math.random() * 1000 + time, tag: 'img', position: { top: row * 68 + 25, left: col * 170 + 25, width: 55, height: 55 } },{ text: v.text, id: Math.random() * 1000 + time, tag: 'font', textStyles: { size: 12 }, position: { top: row * 68 + 85, left: col * 170 + 25, width: 55, height: 55 } }]; shareMenu.draw(item); }); shareMenu.addEventListener('click', function (e) { if (e.screenY > plus.screen.resolutionHeight - 44) { alphaBg.close(); shareMenu.close(); } else if (e.clientX < 5 || e.clientX > plus.screen.resolutionWidth - 5 || e.clientY < 5) { //屏幕左右边缘5像素及菜单顶部5像素不处理点击 } else { let x = e.clientX; let y = e.clientY; let colIdx = Math.floor(x / 170); let rowIdx = Math.floor(y / 68); let tapIndex = colIdx + rowIdx * 2; shareList[tapIndex].onClick(shareList[tapIndex], shareInfo,callback); } }); alphaBg.show(); shareMenu.show(); return { close: function(){ alphaBg && alphaBg.close(); alphaBg && shareMenu.close(); } }; }; // #endif