const utils = require('./utils'); const db = require("./db"); const ordermgr = require('./ordermgr'); const bc = require('./blockchain'); const UNPAID_STATE = 0; //未支付 const PAID_STATE = 1; //已支付 class BoxOrder { constructor(orderDb, isNewOrder) { this.isNewOrder = isNewOrder; this.orderDb = orderDb; } init () { if (this.isNewOrder) { setTimeout(this.start.bind(this), 1000 * 0.1 + Math.floor(Math.random() * 100)); } else { setTimeout(this.start.bind(this), 1000 * 1 + Math.floor(Math.random() * 1000 * 3)); } } async start() { if (this.isPaid()) { //尝试修复已经支付成功订单的t_box表关系 await this.repairPaidOrder(); ordermgr.removePendingOrder(this.getOrderId()); return; } if (this.isExpired()) { //尝试修复已过期订单的t_box表关系 await this.repairExpiredOrder(); ordermgr.removePendingOrder(this.getOrderId()); return; } if (!this.isSynced()) { //上链 await this.blockChainSync(); } //等待确认支付成功 await this.blockChainConfirm(); } async repairPaidOrder() { } async repairExpiredOrder() { } async blockChainSync() { let syncFunc = async () => { let result = await bc.mallInstance.methods.buyBoxWithSignature( boxId, type, buyerAddress, price, paymentTokenAddress, nonce, signature).send({gas: 1000000}); }; while (true) { await utils.sleep(1000 * 3); } } async blockChainConfirm() { while (true) { await utils.sleep(1000 * 3); } } getOrderId() { return this.orderDb['order_id']; } isExpired() { return this.orderDb['expired']; } isPaid() { return this.orderDb['state'] == PAID_STATE; } isSynced() { return this.orderDb['self_node_confirm_time'] > 0; } }; exports.BoxOrder = BoxOrder;