【uniapp】uniapp小程序分享功能(App分享)

小程序分享

分享页面(part1)

分享触发

在分享触发的地方,如下函数
onShow()onLoad()同级

onShareAppMessage:function(option){
		const that = this;
		//来自页面按钮内的分享
		if(option.from === 'button'){
			//方法
		}
		return {
			title:'【'+this.companyName+'】'+'正在邀请您加入**!',//分享的标题
			path:'/pages/my/getSessionKey?shareCode='+that.shareCode,//好友点击分享之后跳转的页面/传参数
			imageUrl:"https://img2.woyaogexing.com/2022/12/07/f6980578ce23442940f5c85e29f646f4.jpeg",//分享的图片(地址需要为可访问的图片地址)
		}//一定要返回对象
},

分享跳转页面

在实际开发中,我选择了一个页面作为中间页面,进行参数的接收
用户在点击分享链接的时候,就会先进入这个页面再进行跳转到目标页面,实际也不需要,可以直接在目标页面进行参数接收

<template>
	<view>

	</view>
</template>

<script>
	import {
		defineComponent,
		reactive,
		toRefs
	} from 'vue';
	import {
		toasting
	} from '../../utils';
	import Api from '../../service/api'
	export default defineComponent({
		name: 'sharePage',
		onLoad(options) {
			this.shareCode = options.shareCode
			uni.setStorageSync('shareCode', this.shareCode)
		},
		onShow(){
			this.ifLogin()
		},
		setup() {
			const state = reactive({
				haveLogin: false,
				shareCode: ''
			})
			const ifLogin = () => {
				uni.navigateTo({
					url: '/pages/my/getSessionKey'
				})
			}
			return {
				...toRefs(state),
				ifLogin,
			}
		}
	})
</script>

<style lang="less" scoped>
	.joinButton{
		width: 100%;
		text-align: center;
		button{
			width: 90%;
			background-color: #32a4e6;
			color: #fff;
		}
	}
</style>

分享到朋友圈

开关

onLoad(){
    wx.showShareMenu({
		withShareTicket:true,
		menus:["shareAppMessage","shareTimeline"]
	})
}

触发

//小程序分享到朋友圈
onShareTimeline() {
	return {
		title: "分享标题",//标题
		imageUrl: "",//封面
		query:"abc="+this.abc+"&series="+this.series//传递多个参数书写方式,不用写页面位置
	};
}

ps:这里的分享到朋友圈,虽然不需要指定页面,但是默认在其他人访问分享时,是一个单页面,所以这个页面需要进行接口的适配和参数的处理

uniApp分享

小程序中的写法有的在uniapp中可以直接写,例如part1的实现

分享页面

uni.share({
	provider:'weixin',//分享服务提供商(即weixin|qq|sinaweibo)
	scene:"WXSceneSession",//WXSceneSession(分享到聊天界面)、WXSenceTimeline(分享到朋友圈)、WXSceneFavorite(分享到微信收藏)
	type:4,//0是文件,4是视频
	title:"分享视频标题",//标题
	summary:"",
	href:"https://xxx.mp4",//分享跳转地址
	mediaUrl:"https://xx.mp4",//分享视频
	imageUrl:"",//分享图片路径(必须是线上可访问图片:http://xxx、https://xxx等)
	success:function(res){
		console.log("success:" + JSON.stringify(res));
	},
	fail:function(err){
		console.log("fail:" + JSON.stringify(err));
	}
})

分享到朋友圈

uni.share({
	provider: 'weixin', //分享服务提供商(即weixin|qq|sinaweibo)
	scene: "WXSceneTimeline", //WXSceneSession(分享到聊天界面)、WXSenceTimeline(分享到朋友圈)、WXSceneFavorite(分享到微信收藏)
	type: 0,//分享文件为0,分享视频为4
	href: "https://xxxxxx.mp4", //分享跳转地址
	title: "文件标题",
	summary: "",
	imageUrl: "", //分享图片路径(必须是线上可访问图片:http://xxx、https://xxx等)
	success: function(res) {
		// console.log("success:" + JSON.stringify(res));
	},
	fail: function(err) {
		// console.log("fail:" + JSON.stringify(err));
	}
});
### 如何在 UniApp 中实现微信小程序分享功能UniApp 开发环境中,可以通过 Vue.js 的 **全局混入** 功能来实现微信小程序的全局分享能力。以下是具体的实现方法: #### 1. 全局配置分享参数 通过 `onLaunch` 方法,在 App.vue 文件中设置全局默认的分享数据。 ```javascript // App.vue export default { onLaunch() { this.globalData = { share: { title: '全局分享的标题', path: '/pages/index/index', // 默认跳转页面路径 imageUrl: '/static/images/share-default.png' // 默认分享图片 } }; }, }; ``` 此部分代码设置了全局共享的基础信息[^3]。 #### 2. 使用全局混入定义分享逻辑 利用 Vue.js 提供的 `mixins` 特性,可以统一管理所有页面的分享行为。 ```javascript // main.js 或单独创建文件 mixins/shareMixin.js import Vue from 'vue'; Vue.mixin({ data() { return { globalData: getApp().globalData, }; }, methods: { // 发送给朋友 onShareAppMessage(res) { return { title: this.globalData.share.title || '默认标题', path: `${this.globalData.share.path}?ref=${new Date().getTime()}`, // 可附加动态参数 imageUrl: this.globalData.share.imageUrl, }; }, // 分享到朋友圈 onShareTimeline(res) { return { title: this.globalData.share.title || '默认标题', path: this.globalData.share.path, imageUrl: this.globalData.share.imageUrl, }; }, }, }); ``` 上述代码实现了两个核心函数:`onShareAppMessage` 和 `onShareTimeline`,分别用于处理发送给朋友和分享到朋友圈的行为[^4]。 #### 3. 页面级覆盖全局分享配置 如果某些特定页面需要不同的分享内容,则可以在该页面重新定义 `onShareAppMessage` 和 `onShareTimeline` 函数。 ```javascript // pages/specificPage/specificPage.vue export default { data() { return { pageShareConfig: { title: '特殊页面的分享标题', path: '/pages/specificPage/specificPage?customParam=123', imageUrl: '/static/images/page-specific-share-image.jpg', }, }; }, methods: { onShareAppMessage(res) { return { title: this.pageShareConfig.title, path: this.pageShareConfig.path, imageUrl: this.pageShareConfig.imageUrl, }; }, onShareTimeline(res) { return { title: this.pageShareConfig.title, path: this.pageShareConfig.path, imageUrl: this.pageShareConfig.imageUrl, }; }, }, }; ``` 这样即可针对具体页面定制化分享内容[^2]。 #### 4. 自定义分享按钮样式 为了提供更灵活的用户体验,还可以设计自定义样式的分享按钮并绑定点击事件触发分享操作。 ```html <!-- 在页面模板中 --> <view class="share-btn-container"> <button open-type="share" class="custom-share-btn">分享</button> </view> <style lang="scss"> .custom-share-btn { width: 100px; height: 40px; background-color: #ff7f50; /* 设置背景颜色 */ color: white; /* 文字颜色 */ border-radius: 20px; /* 圆角效果 */ } </style> ``` 以上代码展示了如何通过 CSS 定制分享按钮外观。 --- ### 总结 通过组合使用 Vue.js 的全局混入机制与页面级别的局部重写策略,能够高效地完成 UniApp 微信小程序中的全局分享功能开发。同时,借助自定义组件的设计理念,可以让界面更加贴合实际需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值