小程序嵌套 h5 并分享给朋友

1.先准备一个https地址,用于在微信公众平台/小程序中/开发管理/开发设置/业务域名 中设置
在这里插入图片描述主要是后续要用到 web-view 组件

2.在小程序中设置跳转信息 task/index.vue
this.graceJS.navigate(“/pagesA/task/sharePage?path=yes&houseName=”+houseName+“&typeName=”+typeName)

?path=yes&houseName=“+houseName+”&typeName="+typeName 这个是我个人要传的值,可根据实际情况填写

3.在小程序中创建一个分享页面,task/sharePage.vue


export default {
	data() {
		return {
			web_src:'https:xxxxxxx',//这个是在小程序中配置的业务域名 <!-- 第一步 -->
			//下面参数是我需要传的值,用于分享标题 (动态)
			name: '', //登录人
			houseName: '', //站房名称
			typeName: '' //任务类型
		}
	},
	onLoad(options) {
		let that = this
		let type = uni.getStorageSync('taskType');
		let id = uni.getStorageSync('taskNodeId')
		let typeTaskId = uni.getStorageSync('taskAllType');
		that.name = this.$store.state.userInfo.name
		//从本程序页面点击过来的也是从卡片点击过来的参数
		if (options.houseName) { //从task/index页面传值过来的 站房名称  用于分享
			this.houseName = decodeURIComponent(options.houseName)
		}
		//从本程序页面点击过来的也是从卡片点击过来的参数
		if (options.typeName) { //从task/index页面传值过来的 任务类型  用于分享
			this.typeName = decodeURIComponent(options.typeName)
		}
		//从本程序页面点击过来的
		//因为我要传值 判断,所以对this.web_src 进行了拼接,不需要传值的直接用data里面的web_src
		if (options.path) { 
		this.web_src += '?id=' + id +'&houseName=' + this.houseName + '&typeName=' + this.typeName+ '&type=' + type + '&typeTaskId=' + typeTaskId+'&name='+this.name +'&erweilma=yes' 
		}

		//从卡片页面点击过来的 <!-- 第三步 -->
		//分享出去后,其他人点击分享的卡片,进入小程序页面
		if (options.name) { //decodeURIComponent 解码
			this.name = decodeURIComponent(options.name)
		}
		//从分享卡片 H5点击过来的 <!-- 第三步 -->
		if (options.shareUrl) { //decodeURIComponent 解码
			that.web_src = decodeURIComponent(options.shareUrl)+'&houseName=' + this.houseName + '&typeName=' + this.typeName+'&name='+this.name;
		}
		
	},
	/**
	 * 用户点击右上角分享 第二步 
	 */
	onShareAppMessage(options) {
		let that = this
		let return_url=this.web_src+'&erweilma=no'
		//分享出去的路径,当其他人从卡片点击进入时,就可以进入小程序pagesA/task/sharePage这个页面
		//'pagesA/task/sharePage?shareUrl=' + encodeURIComponent(return_url)这句与上方onload方法里面的if (options.shareUrl) {}对应
		var path = 'pagesA/task/sharePage?shareUrl=' + encodeURIComponent(return_url)+'&houseName=' + encodeURIComponent(this.houseName)+'&typeName='+ encodeURIComponent(this.typeName)+'&name='+encodeURIComponent(this.name) ; //此处加密,onload里面就要解密
		var titles = ''//动态title
		if (that.houseName != null && that.houseName != '' && that.houseName != 'null') {
			titles = that.name + '/' + that.houseName + '/' + that.typeName
		} else {
			titles = that.name + '/' + that.typeName
		}
		return {
			title: titles,
			path: path,
			success: function(res) {
				// 转发成功
				uni.showToast({
					title: "转发成功",
					icon: 'success',
					duration: 2000
				})
			},
			fail: function(res) {
				// 转发失败
			}
		}
	},
}

4.准备一个H5页面,放在服务器https:xxx里面

//因为我这次需要传参,所以H5里面的参数解析为
onLoad(option) {
			let that = this
			// 获取参数  必须这样获取 h5打包 后才有效  而且需相对路径 ./
			var url = window.location.search;
			// var url = 'https:/xxxx/?id=47&type=2&typeTaskId=1&erweilma=no'//为了本地测试
			var theRequest = new Object();
			if (url.indexOf("?") != -1) {
				var index = url.indexOf("?");
				var str = url.substr(index + 1, url.length); //04
				var strs = str.split("&");
				for (var i = 0; i < strs.length; i++) {
					theRequest[strs[i].split("=")[0]] = (strs[i].split("=")[1]);
				}
			}
			that.ids=theRequest['id']
			that.typeTaskId=theRequest['typeTaskId']
			that.type=theRequest['type']
			that.erweilma=theRequest['erweilma']
		},

/此文章 只是用于记录/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值