WxGamePlatform.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. var t = require;
  2. var e = module;
  3. var i = exports;
  4. var s =
  5. (this && this.__awaiter) ||
  6. function (t, n, r, l) {
  7. return new (r = r || Promise)(function (i, e) {
  8. function s(t) {
  9. try {
  10. a(l.next(t));
  11. } catch (t) {
  12. e(t);
  13. }
  14. }
  15. function o(t) {
  16. try {
  17. a(l.throw(t));
  18. } catch (t) {
  19. e(t);
  20. }
  21. }
  22. function a(t) {
  23. var e;
  24. t.done
  25. ? i(t.value)
  26. : ((e = t.value) instanceof r
  27. ? e
  28. : new r(function (t) {
  29. t(e);
  30. })
  31. ).then(s, o);
  32. }
  33. a((l = l.apply(t, n || [])).next());
  34. });
  35. };
  36. Object.defineProperty(i, "__esModule", {value: !0}), (i.WxgameVideo = void 0);
  37. const r = t("App"),
  38. o = t("WxMiniGameUtil"),
  39. a = (t("ADManageBase"), t("MidashiPay")),
  40. n = t("PlatformBase"),
  41. l = t("DybAd"),
  42. h = t("DybSdk");
  43. (i.default = class extends n.default {
  44. constructor() {
  45. super(),
  46. (this.appid = "wx9b25fce9f330f9b6"),
  47. (this.isShare = !1),
  48. (this.isShareSuccess = !1),
  49. (this.failedTexts = [
  50. "奖励获取失败,请分享到不同好友试试",
  51. "奖励获取失败,请分享到不同的群试试",
  52. "奖励获取失败,请分享到群试试"
  53. ]),
  54. (this.lastShareTime = -1),
  55. (this.pay = null),
  56. (this.isServiceOpen = !0),
  57. (this.isShowIosPay = !1),
  58. (this.wxVersion = ""),
  59. (this.dybSdk = null),
  60. (this._isNew = !1),
  61. (this._launchOptions = wx.getLaunchOptionsSync()),
  62. (this.systemInfo = wx.getSystemInfoSync()),
  63. wx.onMemoryWarning(t => {
  64. (IS_AUTO_RELEASE = !0),
  65. r.default.ResRecycle.startAutoRelease(),
  66. cc.sys.os === cc.sys.OS_IOS
  67. ? this.record({type: 50, tid: 0, numinc: 1})
  68. : this.record({type: 50, tid: (t && t.level) || 1, numinc: 1});
  69. }),
  70. wx.onShow(this.wxGameOnShow.bind(this)),
  71. o.WxMiniGameUtil.enablePassiveShare(),
  72. this.initDybSdk(),
  73. (this.wxVersion = versionInfo.packVersion.replace(/\./g, "_"));
  74. }
  75. isShowGameCircle() {
  76. return !0;
  77. }
  78. initPay() {
  79. this.pay = new a.default();
  80. }
  81. initDybSdk() {
  82. this.dybSdk = new h.DybSdk();
  83. }
  84. getPrice(t) {
  85. t = this.getProductConfigByPayString(t);
  86. return t ? "¥" + t.cost : null;
  87. }
  88. checkIsNotch() {
  89. return 0 < this.systemInfo.safeArea.left || 0 < this.systemInfo.safeArea.top;
  90. }
  91. isShowDebugUI() {
  92. return this.systemInfo.enableDebug || "devtools" === this.systemInfo.brand;
  93. }
  94. vibrator(t = 0) {
  95. wx.vibrateShort({type: "light", success: function () {}, fail: function () {}, complete: function () {}});
  96. }
  97. getAdKey() {
  98. return r.default.RandomUtils.randomArray(["adunit-8912a66c11fe595c"]);
  99. }
  100. initAd() {
  101. super.initAd();
  102. var t = new l.DybAd(this.getAdKey());
  103. this._videoManage.setAdObj(t);
  104. }
  105. loadZipFile(s) {
  106. return new Promise(r => {
  107. if ("windows" != this.systemInfo.platform && "mac" != this.systemInfo.platform) {
  108. const l = cc.assetManager.cacheManager.cacheDir,
  109. e = cc.sys.os === cc.sys.OS_ANDROID ? "/a_" : "/i_",
  110. i = l + e + "file.zip";
  111. fsUtils.exists(i, t => {
  112. if (t) r(!1);
  113. else {
  114. const n = REMOTE_SERVER_ROOT + "/remote/";
  115. fsUtils.downloadFile(
  116. n + `/${e}${versionInfo.zipVersion}.zip`,
  117. i,
  118. null,
  119. t => {
  120. s(t.totalBytesWritten, t.totalBytesExpectedToWrite);
  121. },
  122. (t, e) =>
  123. t
  124. ? (cc.error("下载zip文件失败", t), void r(!1))
  125. : (console.log("开始解压"),
  126. void fsUtils.unzip(e, l, e => {
  127. if (e) r(!1);
  128. else {
  129. console.log("解压成功");
  130. const i = cc.assetManager.cacheManager.cachedFiles,
  131. s = Date.now(),
  132. o = fsUtils.readJsonSync(l + "/fileList.json"),
  133. a = o.length;
  134. for (let t = 0; t < a; t += 2) {
  135. const e = n + o[t],
  136. a = o[t].split("/")[0];
  137. i.add(e, {url: `${l}/${a}/${o[t + 1]}`, lastTime: s, bundle: a});
  138. }
  139. r(!0);
  140. }
  141. }))
  142. );
  143. }
  144. });
  145. } else r(!1);
  146. });
  147. }
  148. wxGameOnShow(t) {
  149. Log.warn("wxGameOnShow"), (this._launchOptions = t), (this.isShareSuccess = this.isShare = !1);
  150. }
  151. checkRandom() {
  152. var t = Math.random();
  153. return Log.warn("分享成功概率:", t, 0.4 < t), 0.4 < t;
  154. }
  155. loginWx() {
  156. return new Promise((e, t) => {
  157. wx.login({
  158. success: t => {
  159. e(t);
  160. },
  161. fail: () => {
  162. t();
  163. }
  164. });
  165. });
  166. }
  167. getAccountInfoSync() {
  168. return wx.getAccountInfoSync();
  169. }
  170. getSenceCode() {
  171. var t = this._launchOptions;
  172. return null != t.scene || null != t.scene ? t.scene : -1;
  173. }
  174. share(t, s) {
  175. return new Promise(() => {
  176. this.isShareSuccess = this.isShare = !0;
  177. let t = "";
  178. for (var e in s.paramList) t += e + "=" + s.paramList[e] + "&";
  179. (t = t.substr(0, t.length - 1)), (this.lastShareTime = Date.now());
  180. var i =
  181. -1 == s.url.indexOf("http")
  182. ? r.default.ConfigManager.gameConf.serverInfos.interface + "/" + s.url
  183. : s.url;
  184. wx.shareAppMessage({title: s.title, imageUrl: i, query: t});
  185. });
  186. }
  187. getLaunchOptions() {
  188. let t,
  189. e,
  190. i,
  191. s = this._launchOptions,
  192. o = "",
  193. a = 0;
  194. if (s.query && s.query.chl) {
  195. var n = s.query;
  196. return {
  197. chl: (t = n.chl || r.default.ConfigManager.gameConf.channel),
  198. serverid: (e = n.serverid || r.default.ConfigManager.gameConf.serverInfos.serverid),
  199. upplayerid: (i = n.playerid || ""),
  200. sourceAppid: o,
  201. shareid: (a = n.shareType || 0)
  202. };
  203. }
  204. return (
  205. s.referrerInfo && s.referrerInfo.appId && (o = s.referrerInfo.appId),
  206. {
  207. chl: (t = r.default.ConfigManager.gameConf.channel),
  208. serverid: (e = r.default.ConfigManager.gameConf.serverInfos.serverid),
  209. upplayerid: i,
  210. sourceAppid: o,
  211. shareid: a
  212. }
  213. );
  214. }
  215. getUseInfoFinish(t) {
  216. "string" == typeof t.reg_time ? new Date(t.reg_time).getTime() : t.reg_time;
  217. const e = wx.getAccountInfoSync();
  218. if (this._isNew && "release" === e.miniProgram.envVersion) {
  219. const e = r.default.ConfigManager.gameConf;
  220. t.channel !== e.channel &&
  221. r.default.Http.post(
  222. e.serverInfos.interface + "/Interface/user/set_channel.php",
  223. {token: this.loginInfo.token, channel: e.channel},
  224. 3
  225. );
  226. }
  227. }
  228. syncData() {
  229. const t = Object.create(null, {syncData: {get: () => super.syncData}});
  230. return s(this, void 0, void 0, function* () {
  231. return cc.sys.os === cc.sys.OS_IOS && (yield this.getIosPayConfig()), t.syncData.call(this);
  232. });
  233. }
  234. getServiceConfig() {
  235. return s(this, void 0, void 0, function* () {
  236. var t = yield r.default.Http.post(
  237. r.default.ConfigManager.gameConf.serverInfos.interface + "/Interface/game_conf/get.php",
  238. {key: "server_switch_" + this.wxVersion},
  239. 5,
  240. 1e3
  241. );
  242. let e;
  243. t && 0 == (e = JSON.parse(t)).code && (this.isServiceOpen = !!e.data);
  244. });
  245. }
  246. initAccountServer() {
  247. const t = wx.getAccountInfoSync(),
  248. e = r.default.ConfigManager.getConfig("Server")[this.getServerId()];
  249. if ("release" === t.miniProgram.envVersion) r.default.ConfigManager.gameConf.serverInfos.interface = e.stat;
  250. else {
  251. (r.default.ConfigManager.gameConf.serverInfos.game = e.games[0].game),
  252. (r.default.ConfigManager.gameConf.serverInfos.interface = e.stat);
  253. const t = r.default.ConfigManager.getServerCacheInfo();
  254. t &&
  255. ((r.default.ConfigManager.gameConf.serverInfos.game = t.game),
  256. (r.default.ConfigManager.gameConf.serverInfos.interface = t.stat));
  257. }
  258. }
  259. initGameServer(e) {
  260. var t = wx.getAccountInfoSync(),
  261. i = r.default.ConfigManager.getConfig("Server")[this.getServerId()];
  262. if ("release" === t.miniProgram.envVersion)
  263. if (this.isServiceOpen) {
  264. r.default.ConfigManager.gameConf.serverInfos.game = i.games[1].game;
  265. for (let t = i.games.length - 1; 1 < t; t--) {
  266. var s = i.games[t];
  267. if (s.regTime && e > s.regTime) {
  268. (r.default.ConfigManager.gameConf.serverInfos.game = s.game),
  269. (r.default.ConfigManager.gameConf.channel = s.chl),
  270. console.log("根据注册时间选择了服务器:", s);
  271. break;
  272. }
  273. }
  274. } else r.default.ConfigManager.gameConf.serverInfos.game = i.games[0].game;
  275. }
  276. login() {
  277. return this.loginInfo.token
  278. ? new Promise(t => {
  279. wx.checkSession({
  280. success: () => {
  281. t({code: 0, loginKey: this.loginInfo.token});
  282. },
  283. fail: () => {
  284. t(this.dybSdkLogin());
  285. }
  286. });
  287. })
  288. : this.dybSdkLogin();
  289. }
  290. dybSdkLogin() {
  291. return s(this, void 0, void 0, function* () {
  292. var t = yield this.dybSdk.getDybUserInfo();
  293. if (!t) return {code: null, loginKey: null};
  294. if (this.loginInfo.token) return Promise.resolve({code: 0, loginKey: this.loginInfo.token});
  295. var e = yield r.default.Http.requestAsync(
  296. r.default.ConfigManager.gameConf.serverInfos.interface + "/api/login/simple",
  297. {code: t, channel: this.getChannel()},
  298. "GET"
  299. );
  300. Log.trace(">>>>>>>>>>>>>>>游客登录", e, t);
  301. e = JSON.parse(e);
  302. return 0 === e.code
  303. ? ((this.loginInfo.playerid = e.data.id),
  304. (this.loginInfo.token = e.token),
  305. r.default.SaveManage.save(n.LoginDataSaveKey.loginCache),
  306. r.default.DateUtils.secondNow() - e.data.reg_time < 60 && this.dybSdk.createRole(),
  307. {code: e.code, loginKey: e.token})
  308. : {code: e.code, loginKey: null};
  309. });
  310. }
  311. getServerId() {
  312. return "elve";
  313. }
  314. wxLogin() {
  315. return s(this, void 0, void 0, function* () {
  316. var {code: t} = yield this.loginWx(),
  317. e = this.getLaunchOptions(),
  318. i = this.getSenceCode(),
  319. i = yield r.default.Http.requestAsync(
  320. r.default.ConfigManager.gameConf.serverInfos.interface + "/api/login/weixin",
  321. {code: t, channel: e.chl, scene: i}
  322. ),
  323. i = JSON.parse(i);
  324. return 0 === i.code
  325. ? ((this.loginInfo.unionid = i.session.unionid),
  326. (this.loginInfo.playerid = i.data.id),
  327. (this.loginInfo.token = i.token),
  328. (this.loginInfo.openid = i.session.openid),
  329. r.default.SaveManage.save(n.LoginDataSaveKey.loginCache),
  330. {code: i.code, loginKey: i.token})
  331. : {code: i.code, loginKey: null};
  332. });
  333. }
  334. saveWxUserInfo() {
  335. return s(this, void 0, void 0, function* () {
  336. let i = i =>
  337. s(this, void 0, void 0, function* () {
  338. let t = 3;
  339. do {
  340. try {
  341. var e = yield r.default.Http.requestAsync(
  342. r.default.ConfigManager.gameConf.serverInfos.interface +
  343. "/Interface/data/save_user_info.php",
  344. {
  345. token: r.default.GameDataMsr.playerInfo.token,
  346. appid: this.appid,
  347. userInfo: JSON.stringify(i.userInfo),
  348. rawData: i.rawData,
  349. signature: i.signature,
  350. encryptedData: i.encryptedData,
  351. iv: i.iv,
  352. cloudID: i.cloudID
  353. }
  354. );
  355. if (0 === JSON.parse(e).code) break;
  356. } catch (t) {
  357. console.log("saveUserInfo 失败", t);
  358. }
  359. } while ((yield r.default.TimerManager.sleep(1e3), --t));
  360. });
  361. wx.getSetting({
  362. success(t) {
  363. t.authSetting["scope.userInfo"]
  364. ? wx.getUserInfo({
  365. withCredentials: !1,
  366. success: t => {
  367. if (t) {
  368. const e = r.default.GameDataMsr.playerInfo;
  369. (e.nickName = t.userInfo.nickName), (e.head = t.userInfo.avatarUrl);
  370. }
  371. },
  372. fail: () => {}
  373. })
  374. : wx.authorize({
  375. scope: "scope.userInfo",
  376. success() {
  377. wx.getUserInfo({
  378. success: t => {
  379. if (t) {
  380. const e = r.default.GameDataMsr.playerInfo;
  381. (e.nickName = t.userInfo.nickName),
  382. e.head != t.userInfo.avatarUrl &&
  383. ((e.head = t.userInfo.avatarUrl), i(t));
  384. }
  385. },
  386. fail: () => {}
  387. });
  388. },
  389. fail: () => {}
  390. });
  391. },
  392. fail: () => {}
  393. });
  394. });
  395. }
  396. buy(i) {
  397. return s(this, void 0, void 0, function* () {
  398. r.default.EasyLoading.showLoading();
  399. var t = this.getProductConfigByPayString(i),
  400. e = yield this.dybSdk.buy(t);
  401. return e && (this.playerSpendMoney = t.cost), r.default.EasyLoading.hideLoading(), e;
  402. });
  403. }
  404. resetGame() {
  405. wx.restartMiniProgram();
  406. }
  407. getIosPayConfig() {
  408. return s(this, void 0, void 0, function* () {
  409. var t = yield r.default.Http.requestAsync(
  410. r.default.ConfigManager.gameConf.serverInfos.interface + `/api/config/payment_switch/${this.wxVersion}`,
  411. {},
  412. "GET"
  413. );
  414. let e;
  415. t && 0 == (e = JSON.parse(t)).code && (this.isShowIosPay = e.data[this.wxVersion]);
  416. });
  417. }
  418. getIsShowPay() {
  419. return cc.sys.os !== cc.sys.OS_IOS || this.isShowIosPay;
  420. }
  421. syncDataCallBack() {
  422. super.syncDataCallBack(), r.default.AnalyticMgr.dayStatistics(this.appid), this.saveWxUserInfo();
  423. }
  424. isAuthorization() {
  425. return s(this, void 0, void 0, function* () {
  426. return new Promise(e => {
  427. wx.getSetting({
  428. success(t) {
  429. e(t.authSetting["scope.userInfo"]);
  430. },
  431. fail: () => {
  432. e(!1);
  433. }
  434. });
  435. });
  436. });
  437. }
  438. resumOrder() {
  439. return s(this, void 0, void 0, function* () {
  440. r.default.EasyLoading.showLoading(),
  441. yield this.dybSdk.getNotReceivedOrder(),
  442. r.default.EasyLoading.hideLoading();
  443. });
  444. }
  445. }),
  446. (i.WxgameVideo = class {
  447. constructor(t) {
  448. (this.onLoadAdComplete = null), (this.onLoadAdFail = null), (this.adState = 0), (this.id = t);
  449. }
  450. addADLoadSuccessCallBack() {
  451. this.onLoadAdComplete();
  452. }
  453. addADloadFailCallBack(t) {
  454. cc.log("addADloadFailCallBack error", t), this.onLoadAdFail(t);
  455. }
  456. addADCloesCallBack(t) {
  457. let e = {code: 1};
  458. ((t && t.isEnded) || void 0 === t) && (e.code = 0), this.onShowAdComplete(e);
  459. }
  460. init() {
  461. this.initAd();
  462. }
  463. initAd() {
  464. cc.log("adUnitId:", this.id);
  465. let t = (this.adVideo = wx.createRewardedVideoAd({adUnitId: this.id, multiton: !0}));
  466. t.onLoad(this.addADLoadSuccessCallBack.bind(this)),
  467. t.onError(this.addADloadFailCallBack.bind(this)),
  468. t.onClose(this.addADCloesCallBack.bind(this)),
  469. (this.adState = 1);
  470. }
  471. load() {
  472. return cc.log("加载广告"), this.adVideo.load();
  473. }
  474. show() {
  475. return this.adVideo.show();
  476. }
  477. });