1.微信小程序发红包
// 发起网络请求获取目标金额
this.$http.post("api/award/regRedPackage").then(res => {
console.log(res);
if (res.code == 200) {
this.moneyShow = true
this.receive = "红包正在路上..."
// this.targetAmount = res.data;
this.ontargetAmount = res.data.amount
const signRegex = /sign=([^&]+)/;
const signMatch = res.data.package.match(signRegex);
if (signMatch) {
const sign = signMatch[1];
console.log(sign); // 输出提取到的 sign 值
wx.sendBizRedPacket ({
timeStamp: res.data.timeStamp, // 支付签名时间戳,
nonceStr: res.data.nonceStr, // 支付签名随机串,不长于 32 位
package: encodeURIComponent(res.data.package), //扩展字段,由商户传入
signType: res.data.signType, // 签名方式,
paySign: res.data. sign, // 支付签名
success(secc){
console.log(secc,'secc');
},
fail(err){
console.log(err,"err");
},
complete(res){
console.log(res,'res');
}
})
} else {
console.log('未找到 sign 值');
}
// 领取红包
// 创建动画实例
this.animation = uni.createAnimation({
duration: this.duration,
timingFunction: 'ease',
delay: 0
});
// 更新金额
this.updateAmount();
} else if (res.code == 501) {
uni.showToast({
title: res.msg,
icon: 'none'
});
}
})
踩坑血泪史!!!!!!
1.文档必须仔细认真看完,package需要进行urlencode
2.各个参数
wx. sendBizRedPacket ({
timeStamp: '', // 支付签名时间戳,
nonceStr: '', // 支付签名随机串,不长于 32 位
package: '', //扩展字段,由商户传入
signType: '', // 签名方式,
paySign: '', // 支付签名
success:function(res){},
fail:function(res){},
complete:function(res){}
})
3.参与paySign 签名的字段包括:appId、timeStamp、nonceStr、package 。这里signType 并不参与签名。
这里划重点
sendBizRedPacket:fail scene 1001 not support? 会报错
此接口不能在开发工具使用 需要真机进行调试
小程序开通了小程序红包的接口,但是目前该接口的的使用场景有限,仅限于场景值1011,1025,1047,1124时,基本为用户扫码打开小程序领取红包,这种方式的优势是有红包封面,有点击拆红包的按钮体验比较好,比较适合线下推广二维码时使用。