uni-app微信支付和H5打开微信小程序

一、微信支付

1. this.payType==0就是微信支付  支付宝支付代码就不贴了

paysubmit(){
	if(this.payType==0){
		//便于维护 单独写
		// #ifdef H5 
			if(this.is_weixn()){
			   this.inWxWebPay(); 
			  //  this.wxMpPay();
			}else{
			   this.wxWebPay();
			}
		// #endif
		// #ifdef APP-PLUS
		   this.wxAppPay();
		// #endif
		// #ifdef MP-WEIXIN
		   this.wxMpPay();
		// #endif
	}else{
		this.zfbPay();
		
	}
}, 
/**
			  * @Description: H5微信支付
			  * @author: ZXL
			  * @createTime: 2022-6-10 8:51:18
			  * @Copyright by 蓝创科技有限公司
			*/
			async wxWebPay(){
				uni.showLoading({
					title:'请稍等...'
				})
				let _this=this;
				let payInfo = {
					token: _this.token,
					duesWaterId: _this.duesWaterId,
					money: _this.shouldPay,
					type: 3,
					count: 1,
				}
				let res = await _this.request.post("/dues/setOrderWx", payInfo)
				uni.hideLoading()
				if(res.code==1){
					// #ifdef APP-PLUS
					  plus.runtime.openURL(res.map2.mweb_url);
					// #endif
					// #ifdef H5
					  window.location.href=res.map2.mweb_url
					// #endif
				}
			},

/**
  * @Description: 微信小程序支付
  * @author: ZXL
  * @createTime: 2022-6-10 11:05:26
  * @Copyright by 蓝创科技有限公司
*/
async wxMpPay(){
	uni.showLoading({
		title:'请稍等...'
	})
	let _this=this;
	let payInfo = {
		token: _this.token,
		duesWaterId: _this.duesWaterId,
		money: _this.shouldPay,
		type: 4,
		count: 1,
	}
	let res = await _this.request.post("/dues/setOrderWx", payInfo)
	uni.hideLoading()
	if(res.code==1){
		let payInfoDetail = res.map
		uni.requestPayment({
			provider: 'wxpay',
			timeStamp: res.map2.xcxtimeStamp,
			nonceStr: res.map2.xcxnonceStr,
			package: res.map2.xcxpackage,
			signType: res.map2.signType,
			paySign: res.map2.xcxpaySign,
			success: function (res) {
				_this.$set(_this.dfPayList[_this.payIndex],'payFlag',1)
				_this.$set(_this.dfPayList[_this.payIndex],'isNormal','已缴费')
				uni.showToast({
					title:'支付成功',
					icon:'success'
				})
			},
			fail: function (err) {
				uni.showToast({
					title:'支付失败',
					icon:'error'
				})
			}
		});
	}else{
		uni.showToast({
			title:res.mag,
			icon:'none'
		})
	}
},
/**
  * @Description: APP微信支付
  * @author: ZXL
  * @createTime: 2022-4-16 1:56:56 
  * @Copyright by 蓝创科技有限公司
*/
async wxAppPay(){
	uni.showLoading({
		title:'请稍等...'
	})
	let _this=this;
	let payInfo = {
		token: _this.token,
		duesWaterId: _this.duesWaterId,
		money: _this.shouldPay,
		type: 2,
		count: 1,
	}
	let res = await _this.request.post("/dues/setOrderWx", payInfo)
	uni.hideLoading()
	if(res.code==1){ 
		let payInfoDetail = res.map
		let payorderInfo =  { 
			appid: payInfoDetail.appid,
			noncestr: payInfoDetail.noncestr,
			package: 'Sign=WXPay',
			partnerid: '1534302791',
			prepayid: payInfoDetail.prepayid,
			timestamp: payInfoDetail.timestamp,
			sign: payInfoDetail.sign,
		}
		uni.requestPayment({
			provider: "wxpay",
			orderInfo: JSON.stringify( payorderInfo ),
			async success( pays ) {
				_this.$set(_this.dfPayList[_this.payIndex],'payFlag',1)
				_this.$set(_this.dfPayList[_this.payIndex],'isNormal','已缴费')
				uni.showToast({
					title:'支付成功',
					icon:'success'
				})
			},
			fail( error ) {
				uni.showToast({
					title:'支付失败',
					icon:'error'
				})
			}
		})
	}else{
		uni.showToast({
			title:res.mag,
			icon:'none'
		})
	}
},

下面是微信内置浏览器支付

安装npm install jweixin-module --save

##判断是否是微信内置浏览器
is_weixin(){
	var ua = navigator.userAgent.toLowerCase();
	if(ua.match(/MicroMessenger/i)=='micromessenger') {
	  return true;
	} else {
	   return false;
	}
}, 
onLoad() {
	this.code=this.getUrlCode().code
	if(this.is_weixin()&&this.getUrlCode().code){ //判断是否是微信内置浏览器和code是否为空  然后取openid
		this.getopenId(this.getUrlCode().code);
	}
	if (this.is_weixin()&&!this.wxAuthopenId&&!this.getUrlCode().code) {
		window.location.href =
		'https://open.weixin.qq.com/connect/oauth2/authorize?appid=”你自己appid“&redirect_uri='+
		this.reqUrl+'/h5&response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect';
	}
},
getUrlCode() { // 截取url中的code方法
	var url = location.search
	this.winUrl = url
	var theRequest = new Object()
	if (url.indexOf("?") != -1) {
		var str = url.substr(1)
		var strs = str.split("&")
		for(var i = 0; i < strs.length; i ++) {
			theRequest[strs[i].split("=")[0]]=(strs[i].split("=")[1])
		}
	}
	return theRequest
},

async getopenId(codes){
	let res = await this.request.post("/pro/Wx/WxInformation",{code:codes})
	if(res.code==1){
		this.res=JSON.stringify(res)
		uni.setStorageSync('wxAuthopenId', res.openId);
		this.setWxAuthopenId(uni.getStorageSync('wxAuthopenId'))
	}
},

 然后就是

var jweixin = require('jweixin-module');
	async gopay(){
				let datas={
					duesWaterId:1,
					money:0.01, 
					token:this.token,
					count:1,
					type:4,
					openId:this.wxAuthopenId
				}
				let res = await this.request.post("/pro/dues/setOrderWx",datas)
				this.resmsg=JSON.stringify(res)
				if(res.code==1){
					this.paysign(res)
				} 
			},
			paysign(res){ 
				jweixin.config({
					debug: true, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
					appId: res.mapConfig.appId, // 必填,公众号的唯一标识
					timestamp: res.mapConfig.timestamp, // 必填,生成签名的时间戳
					nonceStr: res.mapConfig.noncestr, // 必填,生成签名的随机串
					signature: res.mapConfig.signature, // 必填,签名
					jsApiList: ['chooseWXPay'], 
				});
				jweixin.ready(function() {
					jweixin.checkJsApi({
						jsApiList: ['chooseWXPay'], // 需要检测的JS接⼝列表,所有JS接⼝列表见附录2,
						success: function(res) {
						},
						fail:function(res) {
						}
					});
					jweixin.chooseWXPay({
						appId: res.map2.xcxappId,
						timestamp: res.map2.xcxtimeStamp, // 支付签名时间戳,注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
						nonceStr: res.map2.xcxnonceStr, // 支付签名随机串,不长于 32
						package: res.map2.xcxpackage, // 统一支付接口返回的prepay_id参数值,提交格式如:prepay_id=\*\*\*)
						signType: res.map2.signType, // 签名方式,默认为'SHA1',使用新版支付需传入'MD5'
						paySign: res.map2.xcxpaySign, // 支付签名
						success: function(res) {
							uni.showToast({
								title: "付款成功!",
								duration: 1000
							})
						},
						cancel: function(res) {
							uni.showToast({
								title: "付款失败!",
								duration: 1000
							})
						},
						fail: function(res) {
							uni.showToast({
								title: "付款失败!",
								duration: 1000
							})
						}
					});
				})
			},

二、H5打开微信小程序

1.微信js-sdk安装

npm install weixin-js-sdk
我用的uni-app
npm install wecomjsdk

2.在使用的页面引入

import wx from 'weixin-js-sdk'

我使用的uni-app
var wx = require('wecomjsdk')

3.代码

<wx-open-launch-weapp id="launch-btn" appid="" path="/pages/index/index">
	       <script type="text/wxtag-template">
	           <style>.btn { display:block; padding: 12px; color: red }</style>
	           <button class="btn">打开小程序</button>
	       </script>
	    </wx-open-launch-weapp>

methods: {
    // 监听error 函数
    handleErrorFn(e) {
      console.log('跳转失败', JSON.stringify(e))
    },
    // 监听launch 函数
    handleLaunchFn(e) {
      console.log('跳转成功', JSON.stringify(e))
    },
    wx.config({
		beta: true,// 必须这么写,否则wx.invoke调用形式的jsapi会有问题
		debug: true,
		appId: res.corpId, // 企业微信的corpID
		timestamp: res.timeStampSec, // 生成签名的时间戳
		nonceStr: res.nonceStr, // 生成签名的随机串
		signature: res.sign,// 签名, --------------需要后台把生成签名的url改为当前页面url(pagesWork/skipWx/skipWx),后台生成签名的请求接口参考文档:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#62
		jsApiList: ['wx-open-launch-weapp','chooseImage', 'previewImage'],
		openTagList: ['wx-open-launch-weapp'], // 填入打开小程序的开放标签名
		success(){
			uni.showModal({
			   title:"成功提示",
			   content:"success"
		    })
		},
		fail(){
			uni.showModal({
			   title:"失败提示",
			   content: JSON.stringify(res)
			})
		}
    }

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uni-app是一款基于Vue.js框架的跨平台开发工具,可以同时开发微信小程序H5App等多个平台的应用。在uni-app中,我们可以很方便地使用地图组件来标记点。 首先,我们需要引入uni-app官方提供的地图组件,在页面的json文件中添加以下代码: ``` { "usingComponents": { "uni-map": "@dcloudio/uni-map/uni-map" } } ``` 然后,在需要使用地图的页面中,在template中添加以下代码: ``` <template> <view> <uni-map :longitude="longitude" :latitude="latitude" :markers="markers" :include-points="true" ></uni-map> </view> </template> ``` 在script中,我们需要定义地图的经纬度和标记点的数据: ``` <script> export default { data() { return { longitude: 113.324520, latitude: 23.099994, markers: [{ id: 1, longitude: 113.324520, latitude: 23.099994, title: '标记点1', iconPath: '/static/marker.png', width: 30, height: 30 }, { id: 2, longitude: 113.326520, latitude: 23.099994, title: '标记点2', iconPath: '/static/marker.png', width: 30, height: 30 }] } } } </script> ``` 我们可以通过设置longitude和latitude来指定地图的中心点,通过markers来设置标记点的位置、标题、图标等信息。iconPath需要提前准备好对应的图标文件。 最后,在地图组件上设置:include-points="true",可以使得地图自动包含所有标记点,确保能够显示所有标记点。 以上就是使用uni-app来在微信小程序中标记点的方法。通过引入uni-app提供的地图组件,结合相关的属性和数据即可实现地图的标记点功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值