Files
2025-12-19 11:20:59 +08:00

253 lines
5.5 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const app = getApp()
// var pay = require('../../utils/pay.js');
function operation(a, b, digits, op) {
var o1 = toInteger(a)
var o2 = toInteger(b)
var n1 = o1.num
var n2 = o2.num
var t1 = o1.times
var t2 = o2.times
var max = t1 > t2 ? t1 : t2
var result = null
switch (op) {
case 'add':
if (t1 === t2) { // 两个小数位数相同
result = n1 + n2
} else if (t1 > t2) { // o1 小数位 大于 o2
result = n1 + n2 * (t1 / t2)
} else { // o1 小数位 小于 o2
result = n1 * (t2 / t1) + n2
}
return result / max
case 'subtract':
if (t1 === t2) {
result = n1 - n2
} else if (t1 > t2) {
result = n1 - n2 * (t1 / t2)
} else {
result = n1 * (t2 / t1) - n2
}
return result / max
case 'multiply':
result = (n1 * n2) / (t1 * t2)
return result
case 'divide':
result = (n1 / n2) * (t2 / t1)
return result
}
}
function nullRes(a) {
if (a == 'null') {
a = "";
return a;
} else if (a == null) {
a = "";
return a;
} else {
return a;
};
}
function toFixed(num, s) {
var times = Math.pow(10, s)
var des = num * times + 0.6
des = parseInt(des, 10) / times
return des + ''
}
/*
* 将一个浮点数转成整数,返回整数和倍数。如 3.14 >> 314倍数是 100
* @param floatNum {number} 小数
* @return {object}
* {times:100, num: 314}
*/
function toInteger(floatNum) {
var ret = {
times: 1,
num: 0
}
if (isInteger(floatNum)) {
ret.num = floatNum
return ret
}
var strfi = floatNum + ''
var dotPos = strfi.indexOf('.')
var len = strfi.substr(dotPos + 1).length
var times = Math.pow(10, len)
var intNum = parseInt(floatNum * times + 0.5, 10)
ret.times = times
ret.num = intNum
return ret
}
/** 判断obj是否为一个整数*/
function isInteger(obj) {
return Math.floor(obj) === obj
}
function wxpay(phone, money, originalPrice, uuid, cooperationId, that, number) {
wx.showLoading({
title: '支付请求中....',
});
wx.request({
url: app.globalData.devUrl + "/api/v1/wxpay/pay/pre/" + phone,
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: {
phone: phone,
// openId: wx.getStorageSync("weChatId"),
// tradeType: "JSAPI",
// type: 3,
actualMoney: toFixed(operation(money, 100, 2, "multiply"), 2),
totalMoney: toFixed(operation(originalPrice, 100, 2, "multiply"), 2),
// uuid: uuid,
cooperationId: cooperationId,
number: number,
},
method: 'POST',
success: function(res) {
wx.hideLoading()
if (res.data.code == 0) {
// 发起支付
var param = res.data.data;
var outTradeNo = param.outTradeNo;
wx.requestPayment({
// appId: param.appid,
timeStamp: param.timeStamp,
nonceStr: param.nonceStr,
package: param.package,
signType: param.signType,
paySign: param.paySign,
fail: function(res) {
that.setData({
maskFlag: true,
})
wx.showToast({
title: '支付取消'
})
},
success: function() {
that.setData({
maskFlag: true,
})
payResult(cooperationId, outTradeNo);
}
})
} else {
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
})
that.setData({
maskFlag: true,
})
}
},
error: function() {
that.setData({
maskFlag: true,
})
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
})
},
fail: function(data) {
wx.hideLoading()
wx.showModal({
title: '提示',
content: '网络传输异常',
showCancel: false,
})
}
})
}
function payResult(cooperationId, outTradeNo) {
wx.request({
url: app.globalData.devUrl + "/api/v1/wxpay/pay/status",
data: {
cooperationId: cooperationId,
outTradeNo: outTradeNo
},
method: 'GET',
error: function(res) {
console.log(res);
},
success: function(res) {
if (res.data.code == 0) {
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false,
success: function() {
findId()
wx.navigateBack({
delta: 1
})
}
})
} else {
setInterval(payResult(orderNo), 1000);
}
}
})
}
function throttle(fn, gapTime) {
if (gapTime == null || gapTime == undefined) {
gapTime = 1500
}
let _lastTime = null
// 返回新的函数
return function() {
let _nowTime = +new Date()
if (_nowTime - _lastTime > gapTime || !_lastTime) {
fn.apply(this, arguments) //将this和参数传给原函数
_lastTime = _nowTime
}
}
}
function findId() {
var that = this;
wx.request({
url: app.globalData.devUrl + '/api/v1/user/detail/info/' + wx.getStorageSync("phone"),
success: function(res) {
if (res.data.code == 0) {
wx.setStorageSync('id', res.data.data.name)
wx.setStorageSync('number', res.data.data.number)
}
}
})
}
module.exports = {
wxpay: wxpay,
payResult: payResult,
isInteger: isInteger,
toInteger: toInteger,
toFixed: toFixed,
nullRes: nullRes,
operation: operation,
throttle: throttle
}