uni公众号和APP分享

uni公众号内嵌H5分享

我们首先要安装一个模块(jweixin-module) npm install jweixin-module
封装了一个js,内容为

import line from '../uni_modules/uview-ui/libs/config/props/line';

var jweixin = require('jweixin-module');
// import {
// 	get_jssdk
// } from '@/api/index.js' // 分享接口后台提供,用于获取appid,timestamp,nonceStr,signature

var Shares = {
	async get_jssdk(res) {
		jweixin.config({
			debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
			appId: res.appId, // 必填,公众号的唯一标识
			timestamp: res.timestamp, // 必填,生成签名的时间戳
			nonceStr: res.nonceStr, // 必填,生成签名的随机串
			signature: res.signature, // 必填,签名
			jsApiList: res.jsApiList // 必填,需要使用的JS接口列表
		})
	},
	get_share(aa = 0, title, desc, href, share_img, id, num = 0) {

		return new Promise((resolve, reject) => {
			if (aa == 1) {
				href = id ? href + '?id=' + id : href;
			}
			console.log(title, desc, href, share_img, id, num)
			//a:判断是否调用回调
			console.log(href)
			jweixin.error(function(res) {
				console.log(res, '错误')
			});
			var that = this
			jweixin.ready(function() {
				jweixin.updateAppMessageShareData({
					title: title, // 分享标题
					link: href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					desc: desc,
					imgUrl: share_img, // 分享图标
					success: (res) => {
						console.log('设置分享给朋友成功')
						console.log(res)
						if (aa == 1) {
							let nums = that.cabk(title, href, 1);
							nums.then(res => {
								console.log(num + res)
								resolve(num + res)
								// this.num = this.num + res
							})
						}

					},
					cancel: function(res) {
						alert('取消分享')
						// resolve(0)
					}
				});
				jweixin.updateTimelineShareData({
					title: title, // 分享标题
					link: href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
					desc: desc,
					imgUrl: share_img, // 分享图标
					success: (res) => {
						console.log('设置分享到朋友圈成功')
						if (aa == 1) {
							let nums = that.cabk(title, href, 1);
							nums.then(res => {
								console.log(res)
								console.log(num + res)
								resolve(num + res)
								// this.num = this.num + res
							})
						}

					},
					cancel: function(res) {
						console.log('取消分享')
						// resolve(0)
					}
				});
			});
		})
	},
	cabk(title, href, api) { //分享回调
		return new Promise((resolve, reject) => {
			uni.request({
				url: 'https://shouyin.zhilongyihuo.com/app/card/shareBack',
				method: 'POST',
				data: {
					user_id: uni.getStorageSync("user_id"),
					link: href,
					jsapi: api,
					title: title
				},
				header: {
					'token': uni.getStorageSync("token"), //自定义请求头信息
				},
				success: (res) => {
					resolve(1)
				}
			});
		})
	},
//callback写法
	get_share1(aa = 0, title, desc, href, share_img, id, num = 0, callback) {
		if (aa == 1) {
			href = id ? href + '?id=' + id : href;
		}
		console.log(title, desc, href, share_img, id, num)
		//a:判断是否调用回调
		console.log(href)
		jweixin.error(function(res) {
			console.log(res, '错误')
		});
		var that = this
		jweixin.ready(function() {
			jweixin.updateAppMessageShareData({
				title: title, // 分享标题
				link: href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				desc: desc,
				imgUrl: share_img, // 分享图标
				success: (res) => {
					console.log('设置分享给朋友成功')
					console.log(res)
					if (aa == 1) {
						that.cabk1(title, href, 1, function(res) {
							console.log(num + res)
							callback && callback(num + res)
						});
					}

				},
				cancel: function(res) {
					alert('取消分享')
					// resolve(0)
				}
			});
			jweixin.updateTimelineShareData({
				title: title, // 分享标题
				link: href, // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
				desc: desc,
				imgUrl: share_img, // 分享图标
				success: (res) => {
					console.log('设置分享到朋友圈成功')
					if (aa == 1) {
						that.cabk1(title, href, 1, function(res) {
							console.log(num + res)
							callback && callback(num + res)
						});
					}

				},
				cancel: function(res) {
					console.log('取消分享')
					// resolve(0)
				}
			});
		});
	},
	cabk1(title, href, api, callback) { //分享回调
		uni.request({
			url: 'https://shouyin.zhilongyihuo.com/app/card/shareBack',
			method: 'POST',
			data: {
				user_id: uni.getStorageSync("user_id"),
				link: href,
				jsapi: api,
				title: title
			},
			header: {
				'token': uni.getStorageSync("token"), //自定义请求头信息
			},
			success: (res) => {
				callback && callback(1)
			}
		});
	}
}
//抛出
export {
	Shares
}

main.js里引入

import {Shares} from './until/share.js'
		//挂载到全局
Vue.prototype.$wxH5Share = Shares

页面使用

	async fx() {
				console.log(window.location.href)
				const res = await this.$myrequest({
					url: '/app/user/getWechatJssdk',//示例接口获取微信的appid,timestamp,nonceStr,signature
					method: 'POST',
					header: {
						// 'token': uni.getStorageSync("token"), //自定义请求头信息
					},
					data: {
						url: window.location.href
					}
				})
				if (res.data.code == 1) {
					let img = "https://www.zhilongyihuo.com/down/logo.png"
					console.log(this.dect)
					console.log(this.num)
					// 初始化微信接口
					this.$wxH5Share.get_jssdk(res.data.data);
					// 初始化分享参数
					this.$wxH5Share.get_share1(1, this.radiovalue1, this.dect, this.shareLink, img, this.user_id, this.num,
						(res) => {
							console.log(res)
							this.num = res
						});//callback回调取值
					// const nums = this.$wxH5Share.get_share(1, this.radiovalue1, this.dect, this.shareLink, img, this.user_id,
					// 	this.num);
					// nums.then(res => {//promise用.than取值
					// 	console.log(res)
					// 	this.num = res
					// })
					// async () => {//promise用async,await取值
					// 	let nums = await this.$wxH5Share.get_share(1, this.radiovalue1, this.dect, this.shareLink,img, this.user_id, this.num);
					// 	console.log(nums)
					// 	this.num = nums
					// }
					console.log(this.num)
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: "error",
						duration: 2000
					})
				}
			},

提示:虽然写了成功回调,但是uniH5分享用的是微信分享的新版本,新版本的分享成功回调取消了,所以这个回调是不对的,写上是为了学习promise的使用和callback的使用

在这里插入图片描述

APP分享

html里面写两个按钮分别为好友和朋友圈分享

<button type="default" @click="sharef('WXSceneSession')">分享好友</button>
<button type="default" @click="sharef('WXSceneTimeline')">分享朋友圈</button>

js 代码

sharef(scene) {
                //this.shareLink分享的链接地址
                //this.user_id获取的user_id
				console.log(this.shareLink + '?id=' + this.user_id)
				// #ifdef APP-PLUS

				uni.share({
					provider: "weixin",
					scene: scene,
					type: 0,
					href: this.shareLink + '?id=' + this.user_id,
					title: this.radiovalue1,//分享的名称
					summary: this.dect,//分享的简介
					imageUrl: "https://www.zhilongyihuo.com/down/logo.png",//分享的图片
					success: (res) => {
						this.num += 1
						this.shareBack(scene)
						console.log("success:" + JSON.stringify(res));
					},
					fail: function(err) {
						console.log("fail:" + JSON.stringify(err));
					}
				});
				// #endif
				// #ifdef H5
				uni.showToast({
					title: "点击右上角分享",
					icon: "none",
					duration: 2000
				})

				// #endif

			},
			async shareBack(scene) {//分享成功回调
				var jsapi
				if (scene == "WXSceneSession") {
					jsapi = 1
				} else {
					jsapi = 2
				}
				const res = await this.$myrequest({
					url: '/app/card/shareBack',//示例接口
					method: 'POST',
					header: {
						'token': uni.getStorageSync("token"), //自定义请求头信息
					},
					data: {
						user_id: this.user_id,
						link: this.shareLink + '?id=' + this.user_id,
						jsapi: jsapi,
						title: this.radiovalue1
					}
				})
			}

在这里插入图片描述

完结,撒花,有不完整的,还请大佬指正~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
//微信充值 //支付接口测试 function balance(url, data) { uni.request({ url: cfg.originUrl + '/wx/mp/js_sig.do', data: { route: url }, method: 'GET', success: (res) => { jweixin.config({ debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来 appId: res.data.appId, // 必填,公众号的唯一标识 timestamp: res.data.timestamp, // 必填,生成签名的时间戳 nonceStr: res.data.nonceStr, // 必填,生成签名的随机串 signature: res.data.signature, // 必填,签名 jsApiList: ['chooseWXPay'] // 必填,需要使用的JS接口列表 }); jweixin.ready(function() { uni.request({ url: cfg.originUrl + '/wx/recharge/pay.do', method: 'POST', header: { 'Content-type': "application/x-www-form-urlencoded", }, data: JSON.stringify(data), success: function(res) { alert("下单成功"); alert(JSON.stringify(res)); alert(res.data.order_id); all.globalData.orderId = res.data.order_id; uni.setStorageSync('orderId', res.data.order_id); jweixin.chooseWXPay({ timestamp: res.data.payParams.timeStamp, // 支付签名时间戳 nonceStr: res.data.payParams.nonceStr, // 支付签名随机串 package: res.data.payParams.package, // 接口返回的prepay_id参数 signType: res.data.payParams.signType, // 签名方式 paySign: res.data.payParams.paySign, // 支付签名 success: function(e) { alert("支付成功"); alert(JSON.stringify(e)); // 支付成功后的回调函数 } }); } }) }); jweixin.error(function(res) { // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。 console.log("验证失败!") }); } }) }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值