在 main.js 中 引入,在页面使用时 找不到
在这里插入图片描述
在 main.js 中

首先npm 安装一下 JSSDK

npm install jweixin-module --save

引入jweixin

<script>
	let clikTime,inputTime,payTime
	var jweixin = require('jweixin-module');		// 这里安装后的名字 jweixin-module
	console.log(jweixin)
</script>

通过config接口注入权限验证配置

所有需要使用JS-SDK的页面必须先注入配置信息,否则将无法调用(同一个url仅需调用一次,对于变化url的SPA的web app可在每次url变化时进行调用,目前Android微信客户端不支持pushState的H5新特性,所以使用pushState来实现web app的页面会导致签名失败,此问题会在Android6.2中修复)

<script>
	onLoad() {
		// console.log(wx.config())
		setTimeOut(()=>{
			this.getConfig(jweixin)
		},100)
	},
	methods: {
		getConfig(jweixin) {
			var local = window.location.href; // 获取页面url
			this.$u.post('***', {
					url: local
				})
				.then(res => {
					let dat = res.data;
					// console.log(dat)
					if (res.rc == 200 || res.code == 200) {
					
						// 通过config接口注入权限验证配置
						jweixin.config({
							debug: false, 							// 开启调试模式,
							appId: res.data.appId, 					// 必填,企业号的唯一标识,此处填写企业号corpid
							timestamp: res.data.timestamp, 			// 必填,生成签名的时间戳
							nonceStr: res.data.nonceStr, 			// 必填,生成签名的随机串
							signature: res.data.signature, 			// 必填,签名,见附录1
							jsApiList: ['chooseWXPay','scanQRCode'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
						});
						jweixin.ready(function() {
							console.log('jweixin.ready');
						});
						jweixin.error(function(res) {
							console.log('jweixin.error', res);
							// config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
							/*alert("config信息验证失败");*/
						});
					} else {
						uni.showToast({
							icon: 'none',
							title: res.des || '请求失败',
							duration: 800
						});
					}
				});
		},
		
	}
</script>

调起支付

	toPay(order_id,order_sn){
		clearTimeout(payTime)
		payTime = setTimeout(()=>{
			uni.showLoading({
				title: '加载中',
				mask: true
			});
			uni.showLoading({title: '下单中',mask:true});
			this.$u.post('***', {
				order_id: order_id,
			}).then(res => {
				let dat = res.data
				console.log("toPay")
				if(res.rc == 200 || res.code == 200){
					this.wxPay(JSON.parse(dat.jsApiParameters),order_sn)
					/* this.isWxorAl((res)=>{
						if(res == 'wx'){
							this.wxPay(JSON.parse(dat.jsApiParameters))
						}
					}) */
				}else{
					uni.showToast({
						icon: 'none',
						title: res.des || '下单失败',
						duration: 800
					});
				}
			})	
		},500)
	},
	wxPay(data,order_sn){
		let _that = this;
		jweixin.ready((res)=> {
			jweixin.chooseWXPay({
			  timestamp: data.timeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
			  nonceStr: data.nonceStr, // 支付签名随机串,不长于 32 位
			  package: data.package, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
			  signType: data.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
			  paySign: data.paySign, // 支付签名
			  success: (res) =>{
				// 支付成功后的回调函数
				_that.$u.post('***', {
					order_sn: order_sn,
				}).then(res => {
					// alert(JSON.stringify('返回'+JSON.stringify(res)))
				})
				uni.redirectTo({
					url: '/pages/goodsList/success'
				})
			  },
			  err: (res)=>{
				alert(JSON.stringify('返回'+JSON.stringify(res)))
			  }
			});
		});
	},
	
Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐