uni-app微信小程序支付

张伯寅
2023-12-01
	//main.js文件
	import md5 from './static/md5.js'
	Vue.prototype.$md5 = md5
	//prepay_id是下单之后后台传过来的id
	function wxPay(prepay_id, fun = function() {}, fun1 = function() {}) {
		var chars = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' ];
		//生成随机字符串
		var a = "";
		for (var i = 0; i < 30; i++) {
		    var id = Math.ceil(Math.random() * 35);
		    a += chars[id];
		}
		//生成签名
		var paySign = this.$md5('appId=wx****94&nonceStr=' + a + '&package=prepay_id=' + prepay_id + '&signType=MD5&timeStamp=' + String(parseInt(new Date().getTime() / 1000)) + '&key=Sh*********cl')
	     uni.requestPayment({
	        provider: 'wxpay',
	        timeStamp: String(parseInt(new Date().getTime() / 1000)),
	        nonceStr: a,
	        package: 'prepay_id=' + prepay_id,
	        signType: 'MD5',
	        paySign: paySign,
	        success: () => {
	            fun()
	        },
	        fail: () => {
	            fun1()
	        }
	    })
	}
	Vue.prototype.$wxPay = wxPay

	//其他页面调用
	uni.request({
       url: this.$url + '/api/Order/addOrder',
       method: 'POST',
       data: {
           order_type: 1,
           orders: JSON.stringify(arr)
       },
       success: (res) => {
           console.log(res);
           this.$wxPay(res.data.data.prepay_id, this.huidiao,this.huidiao)
       }
   })
   huidiao() {
      uni.navigateBack({
           delta: 1
       })
   }
 类似资料: