//封装全局APP支付方法
import {weixinPay} from "@/utils/api/api.js"; //微信支付
import {aliPay} from "@/utils/api/api.js"; //支付宝支付
//封装APP支付方法
/**
* 参数1 必传 log_id
* 参数2 money 价格
* 参数3 e 1微信支付 2支付宝支付
* 参数4 order_id 订单id
* 参数5 type 订单类型
* **/
const apppay = (log_id,money,e,order_id,type,isshowmode=true)=>{
return new Promise((resolve,reject)=>{
if(e==1){ //微信支付
weixinPay({
log_id:log_id,//=》支付日志id
amount:money,//=》支付金额
//区分是什么设备 wechat app
// #ifdef APP-PLUS
user_type:"app",
// #endif
// #ifndef APP-PLUS
user_type:"wechat",
// #endif
}).then(res=>{
console.info("111",res);
uni.hideLoading();
if(res.code ==1){
// #ifdef APP-PLUS
let orderInfo = res.data.result;
console.info("orderInfo",orderInfo);
uni.requestPayment({
provider:"wxpay" ,
orderInfo:orderInfo,
success: (e) => {
if(isshowmode){
uni.showToast({
title:'支付成功',
icon:'none'
});
}
resolve(res);
},
fail: (e) => {
console.info("支付错误",e);
uni.showToast({
title:'取消支付',
icon:'none'
})
reject(false);
},
complete: (e) => {
console.info("e",e);
reject(false);
}
})
// #endif
//小程序支付
// #ifndef APP-PLUS
let objinfo = {
timeStamp: res.data.result.timeStamp,
nonceStr: res.data.result.nonceStr,
package: res.data.result.package,
signType: res.data.result.signType,
paySign: res.data.result.paySign,
};
wx.requestPayment({
provider: "wxpay",
// orderInfo: objinfo,
...objinfo,
success: (e) => {
resolve(true);
},
fail: (e) => {
console.info(e,"错误");
uni.showToast({
title: '取消支付',
icon: 'none'
})
reject(false);
},
complete: (e) => {
reject(false);
}
})
// #endif
//----------------小程序微信支付-------------------
}else{
uni.showToast({
title:res.msg,
icon:'none'
})
}
}).catch(err=>{
console.info("err",err);
uni.hideLoading();
reject(false);
})
}else{ //支付宝支付
aliPay({
log_id:log_id,
amount:money,
}).then(res=>{
console.info(res,"res");
uni.hideLoading();
// https://segmentfault.com/q/1010000022492163?bd_source_light=4746641
//--------------H5 PLUS --------------------------------
// try{
// let channel;
// plus.payment.getChannels((channels) =>{
// channel = channels[1];
// let payserve = res.data.response;
// // 手机调用请求
// plus.payment.request(channel,payserve,(result)=>{
// // plus.nativeUI.alert("支付成功!", function() {
// // back();
// // });
// reject(true);
// },(error)=>{
// let err = JSON.stringify(
// "支付失败:" + error.code + error.message
// );
// uni.showToast({
// title:'取消支付',
// icon:'none'
// })
// console.info(err);
// reject(false);
// }
// );
// },function(e) {
// console.info("获取支付通道失败:" + e.message);
// uni.showToast({
// title:'取消支付',
// icon:'none'
// })
// reject(false);
// }
// );
// }catch(e){
// console.info("catch e:" + e);
// uni.showToast({
// title:'取消支付',
// icon:'none'
// })
// reject(false);
// }
//----------------------------------------------
//-------------------------uni-APP ALIAPY-------------------------------------
try{
console.info("----------5555555555-----");
uni.requestPayment({
provider:"alipay" ,
orderInfo:res.data.response,//params,//res.data,
success: (e) => {
if(isshowmode){
uni.showToast({
title:'支付成功',
icon:'none'
});
}
resolve(res);
},
fail: (err) => {
console.info("支付错误",err);
uni.showToast({
title:'取消支付',
icon:'none'
})
reject(false);
},
complete: (e) => {
console.info("err",e);
reject(false);
}
})
}catch(e){
console.info('e',e);
}
//--------------------------------------------------------------
}).catch(err=>{
console.info("err",err);
uni.hideLoading();
reject(false);
})
}
});
}
export {apppay}