import { compile, uncompile, unixTimestamp } from "../utils/helper"; import httpClient from "../utils/httpClient"; import DnSdk from "./DnSdk"; class checkOrderAndReport { static instance = null; static timer = null; static reportDelayMap = [ //index是report次数 值是delayTime(秒) 20, // 立即 60, // 1分钟 120, // 2分钟 180, // 3分钟 300, // 5分钟 600, // 10分钟 1200, // 20分钟 1800, // 30分钟 3600, // 1小时 7200, // 2小时 ]; constructor() { } async checkOrder() { const res = wx.getStorageInfoSync(); for (let storageKey of res.keys) { if (storageKey.indexOf("_dnsdkOrderAndReport_") > -1) { const order = uncompile(wx.getStorageSync(storageKey)); console.log("order", order); const delayTimeMap = checkOrderAndReport.reportDelayMap; const delayTime = delayTimeMap[order.reportCount]; const timeDiff = unixTimestamp() - order.timestamp; let currentMapIndex = -1; for (let i = delayTimeMap.length - 1; i >= 0; i--) { if (timeDiff >= delayTimeMap[i]) { currentMapIndex = i; break; } } if (delayTime == undefined) { // 删除超过重试次数的订单 wx.removeStorageSync(storageKey); console.log(storageKey + " removed!"); continue; } if (timeDiff >= delayTime) { let sync_status = 0; // 请求判断是否支付成功 const res = await httpClient.post("/payment/order/query", { order_id: order.order_id ?? null, ext: order.ext, }); // 是否支付成功 sync_status = res.sync_status ?? 0; if (sync_status) { DnSdk.getInstance().onPurchase(order.money * 100); // 腾讯广告小游戏SDK上报 wx.removeStorageSync(storageKey); console.log(storageKey + " report success and removed!"); } else { if (currentMapIndex == delayTimeMap.length - 1) { // 删除超过最长重试延时的订单 wx.removeStorageSync(storageKey); console.log(storageKey + " removed!"); continue; } if (currentMapIndex > order.reportCount) { order.reportCount = currentMapIndex; } order.reportCount += 1; wx.setStorageSync(storageKey, compile(order)); } } } } checkOrderAndReport.getInstance().timer = setTimeout( checkOrderAndReport.getInstance().checkOrder, 60000 ); } run() { if (!checkOrderAndReport.getInstance().timer) { checkOrderAndReport.getInstance().checkOrder(); } } static getInstance() { if (!this.instance) { this.instance = new checkOrderAndReport(); } return this.instance; } } export default checkOrderAndReport;