首先在我们提交订单的时候,先创建订单
//提交订单
submitOrder() {
// console.log(this.data.goodsid);
if(!this.data.hasDefaultAddress){
wx.showToast({
title: '请您先设置地址',
icon:"error"
})
}else{
//调取创建订单方法
this.createOrder();
this.setData({
//支付弹出层
playshow: true
})
}
},
生成订单
//生成订单,注意这里有直播,根据需求看需要传什么参数来创建订单
createOrder() {
var that = this;
let usercode = wx.getStorageSync('');//用户code
let zhiId = wx.getStorageSync('LiveID');
// console.log(zhiId);
let goodid = that.data.goodsid; //商品id
let sukid = that.data.skuid; //规格skuid
let goodnum = that.data.nums //商品数量
let ptOrderCode = that.data.ptOrderCode; //收获id
let Remark = ''; //签名
let ZbjCode = zhiId; //直播佳丽id
let jialiid = that.data.jialiid
console.log(jialiid);
//如果收货地址为空
if (ptOrderCode == '') {
wx.showToast({
title: '请选择收货地址',
icon: 'none',
})
return false;
}
let data = {
UsersCode: usercode,
GoodsIds: goodid,
Nums: goodnum,
ReceiptId: ptOrderCode,
ParaIds: sukid,
Remark: Remark,
ZbjCode: ZbjCode,
JialiId:jialiid
}
console.log(data);
“http请求”(data).then(res => {
console.log(res);
if (res.data.code == 1) {
//订单号
let orderCode = res.data.OrderCode;
this.setData({
palyorder: orderCode
})
} else {
wx.showToast({
title: res.message,
icon: 'none',
})
}
}, function (res) {
wx.showToast({
title: '加载数据失败',
icon: 'none',
})
})
},
获取code,也就是微信给我们返回的一个标识
//获取code
getcode() {
let that = this
wx.login({
success: function (res) {
console.log(res)
if (res.code) {
that.setData({
code: res.code
})
// 获取code成功后 获取OpenId,AccessToken
that.getOpenid();
} else {
// console.log('获取用户登录态失败!' + res.errMsg)
wx.showToast({
title: '获取用户登录态失败!',
icon: 'none',
})
}
}
});
},
然后我们拿着code来,获取OpenID
//获取openid
getOpenid() {
let that = this
//获取openid接口
let codesss = {
code: this.data.code
}
// console.log(that);
"http请求"(codesss).then((res) => {
console.log(res);
/* console.log(res.data.openid);
console.log(res.data.session_key); */
if (res.data.code == 1) {
that.setData({
//获取到openid
openid: res.data.openid
})
//调取微信预支付
// console.log(that);
that.wxPayment();
} else {
wx.showToast({
title: res.message,
icon: 'none',
})
}
})
},
然后吊起微信支付wx.requestPayment
//获取微信支付
wxPayment() {
let that = this;
let body = "购买商品" //根据需求传递参数
let openid = this.data.openid;
let payMoney = this.data.payMoney; //商品价格
let order = this.data.palyorder //商品订单
let datas = {
OpenId: openid,
total_fee: payMoney,
body: body,
out_trade_no: order
}
“发送http请求”(datas).then((res) => {
// console.log("222:" + res.data.result);
if (res.data.result == 0) {
console.log(res);
// console.log("111:" + res.data.noncestr);
wx.requestPayment({
nonceStr: res.data.noncestr,
package: 'prepay_id=' + res.data.prepayid,
paySign: res.data.sign,
timeStamp: res.data.timestamp,
signType: 'MD5',
success(res) {
wx.showToast({
title: '支付成功!',
icon: 'none',
duration: 2000,
success: function () {
that.getUserInfo();
that.setData({
rechargeMoney: '',
payResult: '0'
})
}
})
//支付成功后让他返回当前订单页面
wx.navigateTo({
url: '../myorder/myorder',
})
},
fail(res) {
wx.showToast({
title: '支付失败',
icon: 'none',
})
}
})
}
})
},
最后搞定,关注小生快乐一生!!!