微信支付封装
/**
* data 支付所需数据
* timeStamp {Str} 时间戳
* nonceStr {Str} 随机字符串
* package {Str} 统一下单接口返回的 prepay_id 参数值
* signType {Str} 签名算法
* paySign {Str} 签名
* */
wx_pay(data) {
return new Promise((resolve, reject) => {
uni.requestPayment({
provider: 'wxpay',//支付方式 微信:wxpay 支付宝:alipay 百度:baidu 苹果:appleiap
orderInfo: data,//订单数据
timeStamp: data.timeStamp,
nonceStr: data.nonceStr,
package: data.package,
signType: data.signType,
paySign: data.paySign,
success(res) {
resolve(res)
},
fail(err) {
reject(err)
uni.showModal({
content: "支付失败:" + JSON.stringify(e),
showCancel: false
})
},
complete(result) {}
});
})
}
/**
* uni-app get请求
* */
get(url, params, header) {
let token = uni.getStorageSync('token') || '';
return new Promise((resolve, reject) => {
uni.request({
//带token值验证
url: base_url + url,
data: params,
header: header || {
'token': token
},
method: "GET",
success: (res) => {
resolve(res.data);
console.table(res.data.data);
},
fail: (err) => {
reject(err);
}
});
});
},
/**
* uni-app post请求
* */
post(url, data, header) {
let token = uni.getStorageSync('token') || '';
return new Promise((resolve, reject) => {
uni.showLoading({
title: '加载中'
});
uni.request({
//带token值验证
url: base_url + url,
data: data,
header: header || {
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
'token': token
},
method: "POST",
success: (res) => {
uni.hideLoading();
resolve(res.data);
console.table(res.data.data);
},
fail: (err) => {
uni.hideLoading();
reject(err);
}
})
});
}
/*
**手机格式验证
*/
checkMobile(mobile){
let reg = /^1[345678]\d{9}$/;
return reg.test(mobile);
}
/*
图片预览
list {Arr} 图片
url {Num} 下标
*/
imgPreview(list, idx) {
if (list && list.length > 0) {
let curr_arr = [];
list.map(item => {
curr_arr.push(item.filepath)
})
uni.previewImage({
current: curr_arr[idx], // 传 Number H5端出现不兼容
urls: curr_arr
});
}
}
/*
* 二次弹窗封装 confirm
* content {Str} 弹窗内容
* title {Fun} 提示内容
*/
confirm(content, title = '提示') {
return new Promise((resolve, reject) => {
uni.showModal({
title,
content,
success: res => {
if (res.confirm) {
console.log('用户点击确定');
resolve(true)
} else if (res.cancel) {
resolve(false)
console.log('用户点击取消');
}
}
});
})
}
/*
* 二次弹窗封装 toast
* title {Str} 弹窗内容
* duration {Num} 间隔多久执行回调
*/
toast(title, duration = 1000) {
return new Promise((resolve, reject) => {
uni.showToast({
title: title,
icon: 'none',
success: () => {
setTimeout(() => {
resolve()
}, duration)
}
});
})
}