var t = require; var e = module; var i = exports; var a = (this && this.__awaiter) || function (t, n, r, l) { return new (r = r || Promise)(function (i, e) { function s(t) { try { a(l.next(t)); } catch (t) { e(t); } } function o(t) { try { a(l.throw(t)); } catch (t) { e(t); } } function a(t) { var e; t.done ? i(t.value) : ((e = t.value) instanceof r ? e : new r(function (t) { t(e); }) ).then(s, o); } a((l = l.apply(t, n || [])).next()); }); }; Object.defineProperty(i, "__esModule", {value: !0}), (i.DybSdk = void 0); const n = t("App"); i.DybSdk = class { constructor() { (this.dybSdk = window.dyb), (this.receivedOrders = {}), (this.localOrder = {}), (this.localSaveKey = "DybSdk"), this.getDybUserInfo(); var t = cc.sys.localStorage.getItem(this.localSaveKey); t && (this.localOrder = JSON.parse(t)); } getDybUserInfo() { return new Promise(e => { this.openId && e(this.openId), this.dybSdk.getDybUserInfo(t => { (this.openId = t.user.openid), e(this.openId); }); }); } createRole() { var t = {role_name: "" + n.default.GameDataMsr.playerInfo.id, server_id: 1, server_name: "服务器"}; this.dybSdk.createRole(t); } gameLevel(t, e) { this.dybSdk.gameLevel(t, e); } submitOrder(t) { return new Promise(e => { this.dybSdk.submitOrder(t, t => { console.log("生成订单:" + JSON.stringify(t), t.code), e(1 === t.code); }); }); } buy(i) { return a(this, void 0, void 0, function* () { if ( (this.localOrder[i.payId] && (this.receivedOrders[i.payId] || (yield this.getNotReceivedOrder(i.payId, 0, 0))), this.deletAbnomalOrder(i.payId), this.receivedOrders[i.payId] && (yield this.consumeOrder(i.payId))) ) return !0; const t = yield this.createOrder(i); if (t) { var e = { pay_amount: i.cost, extra_info: "", goods_name: i.goods_name, redirect_uri: t.notify_moyang_midas, role_name: "" + n.default.GameDataMsr.playerInfo.id, server_name: "无", order_sn: t.pay_order_id }; if (yield this.submitOrder(e)) { if ( (this.addAbnomalOrder(i.payId, e.order_sn), yield n.default.TimerManager.sleep(3e3), yield this.getNotReceivedOrder(i.payId), this.receivedOrders[i.payId]) ) { const t = yield this.consumeOrder(i.payId); return this.deletAbnomalOrder(i.payId), t; } return !1; } } return !1; }); } addAbnomalOrder(t, e) { (this.localOrder[t] = e), cc.sys.localStorage.setItem(this.localSaveKey, JSON.stringify(this.localOrder)); } deletAbnomalOrder(t) { this.localOrder[t] && ((this.localOrder[t] = null), cc.sys.localStorage.setItem(this.localSaveKey, JSON.stringify(this.localOrder))); } createOrder(e) { return a(this, void 0, void 0, function* () { var t = yield n.default.Http.post( n.default.ConfigManager.gameConf.serverInfos.interface + "/api/purchase/create", { api_token: n.default.GameDataMsr.playerInfo.token, goods: e.payId, count: 1, amount: e.cost, desc: e.goods_name, body: e.desc }, 3, 3e3 ), t = JSON.parse(t); return t && 0 === t.code ? t.data : null; }); } getNotReceivedOrder(e = null, i = 5, s = 2e3) { return a(this, void 0, void 0, function* () { for (;;) { var t = yield n.default.Http.requestAsync( n.default.ConfigManager.gameConf.serverInfos.interface + "/api/purchase/paid_orders", {api_token: n.default.GameDataMsr.playerInfo.token}, "GET", s ); if (t) { const i = JSON.parse(t); if (i && 0 == i.code && i.data && i.data.length) { this.receivedOrders = {}; for (const e of i.data) this.receivedOrders[e.goods] = e; if (!e) return; if (this.receivedOrders[e]) return; } } if (i <= 0) return null; i--, yield n.default.TimerManager.sleep(s); } }); } consumeOrder(i, s = 5, o = 2e3) { return a(this, void 0, void 0, function* () { var t = this.receivedOrders[i]; if (!t) return !1; for (;;) { var e = yield n.default.Http.requestAsync( n.default.ConfigManager.gameConf.serverInfos.interface + "/api/purchase/confirm/" + t.id, {api_token: n.default.GameDataMsr.playerInfo.token, oid: t.id}, "GET", o ); if (e) { const s = JSON.parse(e); if (s && 0 === s.code) return !(this.receivedOrders[i] = null); } if (s <= 0) return null; s--, yield n.default.TimerManager.sleep(o); } }); } dot(t) { this.dybSdk.dot(t); } subscribeMsg(t) { this.dybSdk.subscribeMsg(t); } };