544 lines
23 KiB
JavaScript
544 lines
23 KiB
JavaScript
(function (vc) {
|
||
vc.extends({
|
||
|
||
data: {
|
||
aba_pay_status:true,
|
||
ABAQRCode: null,
|
||
pollingTimerId: null,
|
||
trainId: null,
|
||
preCheck:true,
|
||
batchPayConfirmInfo: {
|
||
payType: '',
|
||
fees: [],
|
||
primeRate: '',
|
||
primeRates: [],
|
||
payerObjName: '',
|
||
orderId: '',
|
||
paymentPoolId: '',
|
||
authCode: '',
|
||
feePrices: '',
|
||
detailIds: '',
|
||
printUrl: '/print.html#/pages/property/printPayFee',
|
||
accountAmount: '',
|
||
acctId: ''
|
||
}
|
||
},
|
||
watch: {
|
||
'batchPayConfirmInfo.primeRate': {
|
||
deep: true,
|
||
handler: function () {
|
||
let _primeRate = $that.batchPayConfirmInfo.primeRate;
|
||
if(_primeRate=="9"){
|
||
$that.preCheck = true;
|
||
$that._doPayFee()
|
||
}
|
||
|
||
}
|
||
}
|
||
},
|
||
_initMethod: function () {
|
||
//与字典表支付方式关联
|
||
vc.getDict('pay_fee_detail', "prime_rate", function (_data) {
|
||
$that.batchPayConfirmInfo.primeRates = _data;
|
||
});
|
||
$that._listFeePrintPages();
|
||
},
|
||
_initEvent: function () {
|
||
vc.on('batchPayConfirm', 'openBatchPayFee', function (_param) {
|
||
console.log("清除定时器");
|
||
clearInterval($that.pollingTimerId);
|
||
$that._resetBatchPayConfirm();
|
||
$that.batchPayConfirmInfo.payType = _param.payType;
|
||
$that.batchPayConfirmInfo.fees = _param.fees;
|
||
$that.batchPayConfirmInfo.feePrices = _param.feePrices;
|
||
$that.batchPayConfirmInfo.payerObjName = _param.payerObjName;
|
||
$that.batchPayConfirmInfo.accountAmount = _param.accountAmount;
|
||
$that.batchPayConfirmInfo.acctId = _param.acctId;
|
||
$("#doBatchPayFeeModal").modal('show');
|
||
if (_param.payType == 'qrCode') {
|
||
setTimeout('document.getElementById("authCode").focus()', 500);
|
||
}
|
||
console.log("创建aba支付连接");
|
||
$that._createAba();
|
||
});
|
||
},
|
||
methods: {
|
||
_closeDoBatchPayFeeModal: function () {
|
||
$("#doBatchPayFeeModal").modal('hide');
|
||
|
||
if (this.pollingTimerId) {
|
||
clearInterval(this.pollingTimerId);
|
||
console.log("离开页面,已清除 ABA 支付轮询定时器");
|
||
this.pollingTimerId = null; // 重置变量,避免内存泄漏
|
||
}
|
||
},
|
||
_doPayFee: function () {
|
||
if (!$that.batchPayConfirmInfo.primeRate) {
|
||
vc.toast('请选择支付方式');
|
||
return;
|
||
}
|
||
let _fees = $that.batchPayConfirmInfo.fees;
|
||
_fees.forEach(_fee => {
|
||
_fee.primeRate = $that.batchPayConfirmInfo.primeRate;
|
||
});
|
||
|
||
let _data = {
|
||
communityId: vc.getCurrentCommunity().communityId,
|
||
fees: _fees,
|
||
trainId: $that.trainId,
|
||
acctId: $that.batchPayConfirmInfo.acctId,
|
||
accountAmount: $that.batchPayConfirmInfo.accountAmount,
|
||
thirdPayId: $that.trainId,
|
||
preCheck:$that.preCheck,
|
||
}
|
||
vc.http.apiPost(
|
||
'/fee.payBatchFee',
|
||
JSON.stringify(_data), {
|
||
emulateJSON: true
|
||
},
|
||
function (json, res) {
|
||
let _json = JSON.parse(json);
|
||
if (_json.code == 0) {
|
||
$that._doDealPayResult(_json);
|
||
return;
|
||
}
|
||
if ($that.preCheck==true){
|
||
if (_json.msg == "预检完成,可正常缴费") {
|
||
$that.preCheck = false;
|
||
}else{
|
||
$that.ABAQRCode = _json.msg;
|
||
}
|
||
}
|
||
|
||
vc.toast(_json.msg);
|
||
},
|
||
function (errInfo, error) {
|
||
console.log("预检完成")
|
||
if ($that.preCheck==true){
|
||
if (_json.msg == "预检完成,可正常缴费") {
|
||
$that.preCheck = false;
|
||
}else{
|
||
$that.ABAQRCode = _json.msg;
|
||
}
|
||
}
|
||
|
||
console.log('请求失败处理');
|
||
$that._closeDoBatchPayFeeModal();
|
||
vc.toast(errInfo);
|
||
});
|
||
},
|
||
_qrCodePayFee: function () {
|
||
if ($that.batchPayConfirmInfo.primeRate == '') {
|
||
vc.toast('请选择支付方式');
|
||
return;
|
||
}
|
||
let _fees = $that.batchPayConfirmInfo.fees;
|
||
_fees.forEach(_fee => {
|
||
_fee.primeRate = $that.batchPayConfirmInfo.primeRate;
|
||
});
|
||
|
||
let _data = {
|
||
communityId: vc.getCurrentCommunity().communityId,
|
||
fees: _fees,
|
||
acctId: $that.batchPayConfirmInfo.acctId,
|
||
accountAmount: $that.batchPayConfirmInfo.accountAmount,
|
||
authCode: $that.batchPayConfirmInfo.authCode,
|
||
receivedAmount: $that.batchPayConfirmInfo.feePrices,
|
||
payerObjName: $that.batchPayConfirmInfo.payerObjName,
|
||
subServiceCode: 'fee.payBatchFee'
|
||
}
|
||
vc.http.apiPost(
|
||
'/payment.qrCodePayment',
|
||
JSON.stringify(_data), {
|
||
emulateJSON: true
|
||
},
|
||
function (json, res) {
|
||
let _data = JSON.parse(json);
|
||
if (_data.code == 404) {
|
||
vc.toast(_data.msg);
|
||
if (_data.data && _data.data.orderId) {
|
||
$that.batchPayConfirmInfo.orderId = _data.data.orderId;
|
||
$that.batchPayConfirmInfo.paymentPoolId = _data.data.paymentPoolId;
|
||
setTimeout('$that._qrCodeCheckPayFinish()', 5000);
|
||
}
|
||
return;
|
||
} else if (_data.code == 0) {
|
||
$that._doDealPayResult(_data);
|
||
} else {
|
||
vc.toast(_data.msg);
|
||
}
|
||
},
|
||
function (errInfo, error) {
|
||
console.log('请求失败处理');
|
||
vc.toast(errInfo);
|
||
}
|
||
);
|
||
},
|
||
_qrCodeCheckPayFinish: function () {
|
||
let _fees = $that.batchPayConfirmInfo.fees;
|
||
_fees.forEach(_fee => {
|
||
_fee.primeRate = $that.batchPayConfirmInfo.primeRate;
|
||
});
|
||
|
||
let _data = {
|
||
communityId: vc.getCurrentCommunity().communityId,
|
||
fees: _fees,
|
||
acctId: $that.batchPayConfirmInfo.acctId,
|
||
accountAmount: $that.batchPayConfirmInfo.accountAmount,
|
||
authCode: $that.batchPayConfirmInfo.authCode,
|
||
receivedAmount: $that.batchPayConfirmInfo.feePrices,
|
||
subServiceCode: 'fee.payBatchFee',
|
||
orderId: $that.batchPayConfirmInfo.orderId,
|
||
paymentPoolId: $that.batchPayConfirmInfo.paymentPoolId
|
||
|
||
}
|
||
vc.http.apiPost(
|
||
'/payment.checkPayFinish',
|
||
JSON.stringify(_data), {
|
||
emulateJSON: true
|
||
},
|
||
function (json, res) {
|
||
let _data = JSON.parse(json);
|
||
if (_data.code == 404) {
|
||
vc.toast(_data.msg);
|
||
return;
|
||
} else if (_data.code == 41) {
|
||
setTimeout('$that._qrCodeCheckPayFinish()', 5000);
|
||
return;
|
||
}
|
||
$that._doDealPayResult(_data);
|
||
},
|
||
function (errInfo, error) {
|
||
console.log('请求失败处理');
|
||
vc.toast(errInfo);
|
||
}
|
||
);
|
||
},
|
||
_doDealPayResult: function (_json) {
|
||
$that._closeDoBatchPayFeeModal();
|
||
let _data = _json.data;
|
||
let detailIds = '';
|
||
_data.details.forEach(item => {
|
||
detailIds += (item + ',');
|
||
})
|
||
$that.batchPayConfirmInfo.detailIds = detailIds;
|
||
//todo 这里等三秒,有可能收据队列还没有生成
|
||
setTimeout(function () {
|
||
$("#payFeeResult").modal({
|
||
backdrop: "static", //点击空白处不关闭对话框
|
||
show: true
|
||
});
|
||
}, 2000);
|
||
|
||
vc.emit('batchPayFeeOrder', 'loadBatchFees', {});
|
||
},
|
||
_printAndBack: function (_merge) {
|
||
$('#payFeeResult').modal("hide");
|
||
window.open($that.batchPayConfirmInfo.printUrl + "?detailIds=" + $that.batchPayConfirmInfo.detailIds + "&merge=" + _merge)
|
||
},
|
||
|
||
_back: function () {
|
||
$('#payFeeResult').modal("hide");
|
||
vc.getBack();
|
||
},
|
||
_listFeePrintPages: function (_page, _rows) {
|
||
let param = {
|
||
params: {
|
||
page: 1,
|
||
row: 1,
|
||
state: 'T',
|
||
communityId: vc.getCurrentCommunity().communityId
|
||
}
|
||
};
|
||
//发送get请求
|
||
vc.http.apiGet('/feePrintPage.listFeePrintPage',
|
||
param,
|
||
function (json, res) {
|
||
let _json = JSON.parse(json);
|
||
let feePrintPages = _json.data;
|
||
if (feePrintPages && feePrintPages.length > 0) {
|
||
$that.batchPayConfirmInfo.printUrl = feePrintPages[0].url;
|
||
}
|
||
},
|
||
function (errInfo, error) {
|
||
console.log('请求失败处理');
|
||
}
|
||
);
|
||
},
|
||
|
||
_resetBatchPayConfirm: function () {
|
||
let _primeRates = $that.batchPayConfirmInfo.primeRates;
|
||
let _printUrl = $that.batchPayConfirmInfo.printUrl;
|
||
$that.batchPayConfirmInfo = {
|
||
payType: '',
|
||
fees: [],
|
||
primeRate: '',
|
||
primeRates: _primeRates,
|
||
payerObjName: '',
|
||
orderId: '',
|
||
paymentPoolId: '',
|
||
authCode: '',
|
||
feePrices: '',
|
||
detailIds: '',
|
||
printUrl: _printUrl,
|
||
accountAmount: '',
|
||
acctId: ''
|
||
}
|
||
},
|
||
_createAba() {
|
||
// 假设这些变量已定义
|
||
const data = [
|
||
// {name: $that.ABANeedData.ownerPhone, quantity: 1, price: 1},
|
||
// {name: $that.payFeeOrderInfo.communityId, quantity: 0, price: 0},
|
||
// {name: $that.payFeeOrderInfo.payerObjName, quantity: 0, price: 0},
|
||
// {name: $that.ABANeedData.ownerId, quantity: 0, price: 0}
|
||
];
|
||
// $that.ABANeedData.selectPayFeeIds=
|
||
// $that.ABANeedData.accountAmount=$that.batchPayFeeOrderInfo.accountAmount;
|
||
// $that.ABANeedData.feePrices=$that.batchPayFeeOrderInfo.feePrices;
|
||
// console.log( $that.ABANeedData)
|
||
// console.log( $that.batchPayFeeOrderInfo)
|
||
|
||
|
||
const custom = $that.convertFeeIdsList($that.batchPayFeeOrderInfo.selectPayFeeIds)
|
||
console.log(custom)
|
||
|
||
// 先序列化为JSON字符串
|
||
const jsonStr = JSON.stringify(data);
|
||
const customStr = JSON.stringify(custom);
|
||
|
||
// 再进行Base64编码
|
||
const base64Str = btoa(unescape(encodeURIComponent(jsonStr)));
|
||
const customBase64Str = btoa(unescape(encodeURIComponent(customStr)));
|
||
|
||
console.log(base64Str);
|
||
|
||
// 构建第二个请求的参数(此时ABANeedData已准备好)
|
||
const payParams = {
|
||
amount: $that.batchPayFeeOrderInfo.feePrices,
|
||
shipping: "",
|
||
itemsJson: "",
|
||
firstname: $that.batchPayFeeOrderInfo.payerObjNames[0],
|
||
lastname: "",
|
||
email: "",
|
||
phone: $that.batchPayFeeOrderInfo.communityId,
|
||
type: "purchase",
|
||
paymentOption: "abapay_khqr",
|
||
currency: "USD",
|
||
returnParams: "",
|
||
googlePayToken: "",
|
||
customFields: customBase64Str
|
||
};
|
||
console.log(payParams);
|
||
|
||
const config = {
|
||
params: payParams // 关键:参数需要放在 params 属性中
|
||
};
|
||
|
||
// 发送第二个请求
|
||
vc.http.apiGet(
|
||
'/acct.abaPayCreate', // URL
|
||
config, // 配置对象,包含 params 属性
|
||
function (json, res) { // 成功回调
|
||
let response = JSON.parse(json);
|
||
console.log('请求成功', response);
|
||
|
||
$that.aba_pay_status=false
|
||
// 处理成功逻辑,将qrImage赋值给目标变量
|
||
$that.ABAQRCode = response.qrImage;
|
||
|
||
// vc.emit('payFeeOrderConfirm', 'openConfirm', $that.payFeeOrderInfo);
|
||
|
||
// todo 启动定时查询任务
|
||
console.log(response)
|
||
console.log(response.status)
|
||
$that.isLoadData = true
|
||
|
||
console.log("isLoadData赋值为 true")
|
||
|
||
|
||
// 启动定时查询,3分钟超时
|
||
|
||
// 第一次先查 queryLifetime
|
||
$that._getABAPayQueryLifetime(response.status.tran_id, function (trainId, duration) {
|
||
// 拿到查询时长后再启动定时轮询
|
||
|
||
$that.trainId = trainId;
|
||
$that._startABAPayPolling(trainId, duration);
|
||
});
|
||
|
||
|
||
},
|
||
function (errInfo, error) { // 错误回调
|
||
console.error('请求失败:', errInfo);
|
||
// 处理错误逻辑,例如:
|
||
// vc.toast('支付请求失败,请稍后重试');
|
||
}
|
||
);
|
||
},
|
||
/**
|
||
* 查询 ABA 支付状态允许的查询时长(带 queryLifetime)
|
||
* @param {String} trainId - 支付交易 ID
|
||
* @param {Function} callback - 回调函数,返回 (trainId, duration),单位毫秒
|
||
*/
|
||
_getABAPayQueryLifetime: function (trainId, callback) {
|
||
if (!trainId) {
|
||
console.error("trainId 不能为空");
|
||
return;
|
||
}
|
||
|
||
vc.http.apiGet(
|
||
'/acct.abaPayStatus',
|
||
{params: {trainId: trainId, queryLifetime: true}},
|
||
function (json, res) {
|
||
try {
|
||
let result = JSON.parse(json);
|
||
console.log("首次查询结果:", result);
|
||
|
||
if (result && result) {
|
||
let duration = parseInt(result) * 60 * 1000; // 分钟转毫秒
|
||
console.log("允许查询时长(毫秒):", duration);
|
||
console.log("允许查询时长(分钟", result);
|
||
|
||
if (callback) {
|
||
callback(trainId, duration);
|
||
}
|
||
} else {
|
||
console.warn("未返回 queryLifetime,使用默认 3 分钟");
|
||
if (callback) {
|
||
callback(trainId, 3 * 60 * 1000);
|
||
}
|
||
}
|
||
} catch (e) {
|
||
console.error("解析 queryLifetime 响应失败:", e);
|
||
if (callback) {
|
||
callback(trainId, 3 * 60 * 1000);
|
||
}
|
||
}
|
||
},
|
||
function (errInfo, error) {
|
||
console.error("请求失败:", errInfo);
|
||
if (callback) {
|
||
callback(trainId, 3 * 60 * 1000);
|
||
}
|
||
}
|
||
);
|
||
},
|
||
/**
|
||
* 启动定时查询 ABA 支付状态
|
||
* @param {String} trainId - 支付交易 ID
|
||
* @param {Number} duration - 查询总时长(毫秒),默认 3 分钟
|
||
* @param {Number} interval - 查询间隔(毫秒),默认 3 秒
|
||
*/
|
||
_startABAPayPolling: function (trainId, duration = 3 * 60 * 1000, interval = 3000) {
|
||
|
||
$that.batchPayConfirmInfo.thirdPayId = trainId;
|
||
// 页面中间消息提示
|
||
function showMessage(message, duration = 20000) {
|
||
let msgBox = document.createElement("div");
|
||
msgBox.innerText = message;
|
||
|
||
Object.assign(msgBox.style, {
|
||
position: "fixed",
|
||
top: "50%",
|
||
left: "50%",
|
||
transform: "translate(-50%, -50%)",
|
||
background: "rgba(0,0,0,0.8)",
|
||
color: "#fff",
|
||
padding: "15px 25px",
|
||
borderRadius: "10px",
|
||
fontSize: "16px",
|
||
textAlign: "center",
|
||
zIndex: 9999,
|
||
opacity: "0",
|
||
transition: "opacity 0.5s"
|
||
});
|
||
|
||
document.body.appendChild(msgBox);
|
||
|
||
setTimeout(() => {
|
||
msgBox.style.opacity = "1";
|
||
}, 50);
|
||
|
||
setTimeout(() => {
|
||
msgBox.style.opacity = "0";
|
||
setTimeout(() => {
|
||
if (msgBox && msgBox.parentNode) {
|
||
msgBox.parentNode.removeChild(msgBox);
|
||
}
|
||
}, 500);
|
||
}, duration);
|
||
}
|
||
|
||
|
||
if (!trainId) {
|
||
console.error("trainId 不能为空");
|
||
return;
|
||
}
|
||
|
||
let startTime = new Date().getTime();
|
||
|
||
// 保存定时器 id,方便后续清理
|
||
let pollingTimer = setInterval(() => {
|
||
let now = new Date().getTime();
|
||
|
||
// 超时判断
|
||
if (now - startTime > duration) {
|
||
clearInterval(pollingTimer);
|
||
console.warn("支付状态查询超时,未完成支付");
|
||
return;
|
||
}
|
||
|
||
// 请求支付状态
|
||
vc.http.apiGet(
|
||
'/acct.abaPayStatus',
|
||
{params: {trainId: trainId}},
|
||
function (json, res) {
|
||
try {
|
||
let result = JSON.parse(json);
|
||
console.log("支付状态查询结果:", result);
|
||
|
||
// 这里假设接口直接返回 bool 值 { success: true/false }
|
||
if (result === true || result.success === true) {
|
||
console.log("支付成功 ✅,停止轮询");
|
||
$that.aba_pay_status= true
|
||
clearInterval(pollingTimer);
|
||
// 其他页面中调用_payFee方法
|
||
// 可以传递参数,也可以不传递
|
||
|
||
|
||
// vc.emit('payFeeOrderConfirm', 'callPayFee', {});
|
||
$that._doPayFee()
|
||
// 页面提示 20 秒
|
||
showMessage("Payment succeeded ✅ order form " + trainId + " Completed", 5000);
|
||
|
||
// 可以在这里发事件通知
|
||
vc.emit('payFeeOrder', 'ABAPaySuccess', {trainId: trainId});
|
||
}
|
||
} catch (e) {
|
||
console.error("解析支付状态响应失败:", e);
|
||
}
|
||
},
|
||
function (errInfo, error) {
|
||
console.error("支付状态请求失败:", errInfo);
|
||
// 请求失败也不中断,继续下次轮询
|
||
}
|
||
);
|
||
|
||
}, interval);
|
||
$that.pollingTimerId = pollingTimer;
|
||
},
|
||
convertFeeIdsList: function (feeIdsList) {
|
||
const keyValueMap = {};
|
||
if (Array.isArray(feeIdsList)) {
|
||
feeIdsList.forEach((value, index) => {
|
||
const key = "f" + (index + 1);
|
||
keyValueMap[key] = value;
|
||
});
|
||
}
|
||
return keyValueMap;
|
||
},
|
||
}
|
||
});
|
||
})(window.vc);
|