uniapp封装公共分享方法
使用mixins封装分享方法
创建share.js
import urlConfig from "@/utils/urlConfig"; export const shareMixins = { data() { return { shareData: { title: '', //分享内容的标题 path: '', //分享内容跳转的页面 imageUrl: '', //分享内容展示的图片 content: '', //分享内容展示的文本内容 desc: '' } } }, //#ifdef MP-WEIXIN onShareAppMessage(e) { //分享到微信页面 // console.log(e) let shareInfo = this.shareData; if (e.from == 'button') { //自定义按钮分享 let _data = e.target.dataset; if (_data.name == 'invite') { //邀约 shareInfo = _data.datum; } } let sharePath = shareInfo.path; if (uni.getStorageSync('userInfo')) { //处理分享用户 let userInfo = JSON.parse(uni.getStorageSync('userInfo')); sharePath += '?inviteCustId=' + userInfo.custId; } let shareImg = shareInfo.imageUrl; //处理分享图片 if (shareInfo.imageUrl.indexOf('http') == -1) { shareImg = urlConfig.fixedCodeUrl + '/resources/miniHMWelfare' + shareInfo.imageUrl; } console.log(shareInfo, sharePath, shareImg) return { title: shareInfo.title, path: sharePath, imageUrl: shareImg, content: shareInfo.content, desc: shareInfo.desc, success: res => { console.info(res) } } }, onShareTimeline(e) { //分享到微信朋友圈 // console.log(e) let shareInfo = this.shareData; if (e.from == 'button') { //自定义按钮分享 let _data = e.target.dataset; if (_data.name == 'invite') { //邀约 shareInfo = _data.datum; } } let sharePath = shareInfo.path; if (uni.getStorageSync('userInfo')) { //处理分享用户 let userInfo = JSON.parse(uni.getStorageSync('userInfo')); sharePath += '?inviteCustId=' + userInfo.custId; } let shareImg = shareInfo.imageUrl; //处理分享图片 if (shareInfo.imageUrl.indexOf('http') == -1) { shareImg = urlConfig.fixedCodeUrl + '/resources/miniHMWelfare' + shareInfo.imageUrl; } console.log(shareInfo, sharePath, shareImg) return { title: shareInfo.title, path: sharePath, imageUrl: shareImg, content: shareInfo.content, desc: shareInfo.desc, success: res => { console.info(res) } } }, //#endif onLoad(option) {} }
页面使用
import { shareMixins} from '@/mixins/share' export default { mixins: [ shareMixins], data() { return { shareData: { title: '我是分享标题', path: '/pages/index/index?id=1' // 分享的页面路径 } } }, }