f7a859d6-49de-4a73-8dbd-07062c31fc3e.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. "use strict";
  2. cc._RF.push(module, 'f7a85nWSd5Kc429BwYsMfw+', 'AdAgentDouyin');
  3. // common-plugin/Scripts/AdAgentDouyin.ts
  4. "use strict";
  5. var __extends = (this && this.__extends) || (function () {
  6. var extendStatics = function (d, b) {
  7. extendStatics = Object.setPrototypeOf ||
  8. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  9. function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
  10. return extendStatics(d, b);
  11. };
  12. return function (d, b) {
  13. extendStatics(d, b);
  14. function __() { this.constructor = d; }
  15. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  16. };
  17. })();
  18. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  19. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  20. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  21. else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
  22. return c > 3 && r && Object.defineProperty(target, key, r), r;
  23. };
  24. Object.defineProperty(exports, "__esModule", { value: true });
  25. var AdAgent_1 = require("./AdAgent");
  26. var YZ_Constant_1 = require("./YZ_Constant");
  27. var PlatUtils_1 = require("./PlatUtils");
  28. var Utils_1 = require("./Utils");
  29. var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
  30. var AdAgentDouyin = /** @class */ (function (_super) {
  31. __extends(AdAgentDouyin, _super);
  32. function AdAgentDouyin() {
  33. var _this = _super !== null && _super.apply(this, arguments) || this;
  34. _this._bannerAd = null;
  35. _this._videoAd = null;
  36. _this._sysData = null;
  37. _this._bannerAds = [];
  38. _this._isBannerShow = false;
  39. //@ts-ignore
  40. _this.tt = window.tt;
  41. _this._bannerBottom = 0;
  42. _this._lastShowInterstitialTime = 0;
  43. _this.interstitialAd = null;
  44. _this._videoCallback = null;
  45. _this._isVideoLoaded = false;
  46. _this._isVideoShow = false;
  47. return _this;
  48. }
  49. Object.defineProperty(AdAgentDouyin.prototype, "ServerConfig", {
  50. get: function () {
  51. return Utils_1.utils.Tool_Douyin.ServerConfig;
  52. },
  53. enumerable: false,
  54. configurable: true
  55. });
  56. AdAgentDouyin.prototype.Init = function () {
  57. if (PlatUtils_1.default.IsDouyin) {
  58. //@ts-ignore
  59. this._sysData = Utils_1.utils.Tool_Douyin._sysInfo;
  60. // utils.registerServerInitEvent(() => {
  61. // // this._initVideoAd();
  62. // }, this)
  63. }
  64. };
  65. AdAgentDouyin.prototype.ShowBanner = function (location, args) {
  66. var _this = this;
  67. if (args === void 0) { args = null; }
  68. if (PlatUtils_1.default.IsDouyin) {
  69. if (Utils_1.utils.isShowRecommondGamesBanner() && Utils_1.utils.Tool_Douyin.isShowMoreGamesModal()) {
  70. Utils_1.utils.showRecommendGamesBanner();
  71. Utils_1.utils.showLog("\u670D\u52A1\u5668\u914D\u7F6E\u5C55\u793A\u81EA\u5B9A\u4E49banner");
  72. return;
  73. }
  74. if (Utils_1.utils.Tool_Douyin.isNewsArticleLite) {
  75. Utils_1.utils.showLog("头条极速版不显示Banner广告");
  76. return;
  77. }
  78. if (this._isConfigValid()) {
  79. var argsTmp = args;
  80. var bannerSizePercent = 1;
  81. if (argsTmp && argsTmp.width) {
  82. bannerSizePercent = ((argsTmp.width < 0) ? 0.1 : argsTmp.width);
  83. bannerSizePercent = ((argsTmp.width > 1) ? 1 : bannerSizePercent);
  84. }
  85. if (argsTmp && argsTmp.bottom) {
  86. this._bannerBottom = argsTmp.bottom / this._sysData.pixelRatio;
  87. this._bannerBottom = ((this._bannerBottom < 0) ? 0 : this._bannerBottom);
  88. this._bannerBottom = ((this._bannerBottom > this._sysData.screenHeight) ? this._sysData.screenHeight : this._bannerBottom);
  89. }
  90. var targetBannerAdWidth_1 = 60;
  91. var left = (this._sysData.screenWidth - targetBannerAdWidth_1) * 0.5;
  92. var top = this._sysData.screenHeight - (targetBannerAdWidth_1 / 16 * 9 - this._bannerBottom);
  93. // 创建一个居于屏幕底部正中的广告
  94. var bannerId = Utils_1.utils.config.douyinconfig.bannerId;
  95. Utils_1.utils.showLog("显示Banner广告: bannerId=" + bannerId + " #targetBannerAdWidth=", targetBannerAdWidth_1);
  96. //@ts-ignore
  97. try {
  98. var bannerAd_1 = this.tt.createBannerAd({
  99. adUnitId: bannerId,
  100. style: {
  101. width: targetBannerAdWidth_1,
  102. left: left,
  103. top: top
  104. }
  105. });
  106. if (bannerAd_1) {
  107. var self_1 = this;
  108. bannerAd_1.onLoad(function () {
  109. bannerAd_1.show().then(function () {
  110. Utils_1.utils.showLog('广告显示成功');
  111. for (var i = 0; i < self_1._bannerAds.length; i++) {
  112. if (self_1._bannerAds[i] != bannerAd_1 && self_1._bannerAds[i] != null) {
  113. self_1._bannerAds[i].destroy();
  114. }
  115. }
  116. self_1._bannerAds.length = 0;
  117. self_1._bannerAds.push(bannerAd_1);
  118. }).catch(function (err) {
  119. Utils_1.utils.showLog("\u5E7F\u544A\u7EC4\u4EF6\u51FA\u73B0\u95EE\u9898 " + (err.errCode, err.errMsg));
  120. });
  121. });
  122. bannerAd_1.onError(function (err) {
  123. if (err) {
  124. Utils_1.utils.showLog("Banner \u5E7F\u544A\u51FA\u9519: errCode: " + err.errCode + " errMsg:" + err.errMsg);
  125. }
  126. });
  127. bannerAd_1.onResize(function (res) {
  128. // 如果一开始设置的 banner 宽度超过了系统限制,可以在此处加以调整
  129. bannerAd_1.style.top = _this._sysData.screenHeight - res.height - _this._bannerBottom;
  130. if (res.width > 0) {
  131. bannerAd_1.style.left = (_this._sysData.screenWidth - res.width) * 0.5;
  132. }
  133. else {
  134. bannerAd_1.style.left = (_this._sysData.screenWidth - targetBannerAdWidth_1) / 2;
  135. }
  136. });
  137. this._bannerAds.push(bannerAd_1);
  138. }
  139. }
  140. catch (error) {
  141. }
  142. }
  143. else {
  144. Utils_1.utils.showLog("抖音小游戏配置文件出错!");
  145. }
  146. }
  147. };
  148. AdAgentDouyin.prototype.HideBanner = function (location) {
  149. if (PlatUtils_1.default.IsDouyin) {
  150. for (var i = 0; i < this._bannerAds.length; i++) {
  151. if (this._bannerAds[i] != null) {
  152. this._bannerAds[i].destroy();
  153. }
  154. }
  155. this._bannerAds.length = 0;
  156. }
  157. };
  158. AdAgentDouyin.prototype.checkCanShowInterstitial = function () {
  159. if (this.tt.createInterstitialAd) {
  160. return true;
  161. }
  162. return false;
  163. };
  164. AdAgentDouyin.prototype.ShowInterstitial = function (location) {
  165. if (PlatUtils_1.default.IsDouyin) {
  166. if (this.checkCanShowInterstitial()) {
  167. var curTime = new Date().getTime();
  168. var interval = (curTime - this._lastShowInterstitialTime) / 1000;
  169. if (interval < 30) {
  170. Utils_1.utils.showLog("距离插屏广告或者激励视频广告上次播放时间间隔不足30秒");
  171. return;
  172. }
  173. this._lastShowInterstitialTime = curTime;
  174. Utils_1.utils.delayCall(this._createMiniGameInsertAd.bind(this), this.ServerConfig.intersititia_delay_show_time || 0);
  175. }
  176. else {
  177. Utils_1.utils.showLog("当前客户端版本不支持插屏!");
  178. }
  179. }
  180. };
  181. AdAgentDouyin.prototype._createMiniGameInsertAd = function () {
  182. var _this = this;
  183. try {
  184. if (!Utils_1.utils.config.douyinconfig.insertId) {
  185. Utils_1.utils.showLog("插屏ID配置有误");
  186. return;
  187. }
  188. if (this.interstitialAd) {
  189. this.interstitialAd.destroy();
  190. this.interstitialAd = null;
  191. }
  192. this.interstitialAd = this.tt.createInterstitialAd({
  193. adUnitId: Utils_1.utils.config.douyinconfig.insertId
  194. });
  195. this.interstitialAd.load().then(function () {
  196. _this.interstitialAd.show();
  197. }).catch(function (err) {
  198. Utils_1.utils.showLog(err);
  199. Utils_1.utils.showLog("err.errCode:" + err.errCode);
  200. switch (err.errCode) {
  201. case 2001:
  202. Utils_1.utils.showLog("小程序启动一定时间内不允许展示插屏广告");
  203. break;
  204. case 2002:
  205. Utils_1.utils.showLog("距离小程序插屏广告或者激励视频广告上次播放时间间隔不足,不允许展示插屏广告");
  206. break;
  207. case 2003:
  208. Utils_1.utils.showLog("当前正在播放激励视频广告或者插屏广告,不允许再次展示插屏广告");
  209. break;
  210. case 2004:
  211. Utils_1.utils.showLog("该项错误不是开发者的异常情况,或因小程序页面切换导致广告渲染失败");
  212. break;
  213. case 2005:
  214. Utils_1.utils.showLog("插屏广告实例不允许跨页面调用");
  215. break;
  216. default:
  217. // 参考 https://minigame.vivo.com.cn/documents/#/lesson/open-ability/ad?id=广告错误码信息 对错误码做分类处理
  218. Utils_1.utils.showLog("插屏广告展示失败");
  219. break;
  220. }
  221. });
  222. }
  223. catch (error) {
  224. Utils_1.utils.showLog(error);
  225. }
  226. };
  227. AdAgentDouyin.prototype.ShowVideo = function (callback) {
  228. var _this = this;
  229. if (PlatUtils_1.default.IsDouyin) {
  230. this._videoCallback = callback;
  231. this._isVideoShow = true;
  232. if (Utils_1.utils.Tool_Douyin.ServerConfig) {
  233. var posId = Utils_1.utils.config.douyinconfig.videoId.trim();
  234. Utils_1.utils.showLog("video广告ID:" + posId);
  235. if (!this._videoAd) {
  236. //@ts-ignore
  237. this._videoAd = tt.createRewardedVideoAd({
  238. adUnitId: posId
  239. });
  240. if (this._videoAd) {
  241. Utils_1.utils.showLog("初始化注册视频回调!");
  242. // this._videoAd.onLoad(() => {
  243. // utils.showLog("激励视频加载成功", this._isVideoShow);
  244. // // this._isVideoLoaded = true;
  245. // // if (this._isVideoShow) {
  246. // // this._isVideoShow = false;
  247. // // }
  248. // })
  249. this._videoAd.onError(function (err) {
  250. Utils_1.utils.showLog("\u6FC0\u52B1\u89C6\u9891\u51FA\u9519: " + (err.code, err.msg));
  251. _this._isVideoLoaded = false;
  252. if (_this._videoCallback) {
  253. _this._videoCallback(false, "暂无视频广告!");
  254. _this._videoCallback = null;
  255. }
  256. });
  257. this._videoAd.onClose(function (res) {
  258. _this._isVideoShow = false;
  259. _this._isVideoLoaded = false;
  260. if (res.isEnded) {
  261. Utils_1.utils.showLog('激励视频广告完成,发放奖励');
  262. if (_this._videoCallback) {
  263. _this._videoCallback(true, "");
  264. _this._videoCallback = null;
  265. }
  266. }
  267. else {
  268. Utils_1.utils.showLog('激励视频广告取消关闭,不发放奖励');
  269. if (_this._videoCallback) {
  270. _this._videoCallback(false, "观看完视频才能获得奖励!");
  271. _this._videoCallback = null;
  272. }
  273. }
  274. });
  275. }
  276. else {
  277. Utils_1.utils.showLog("videoAd 对象创建失败,播放失败!");
  278. if (this._videoCallback) {
  279. this._videoCallback(false, "暂无视频广告!");
  280. this._videoCallback = null;
  281. }
  282. }
  283. }
  284. // else {
  285. // if (this._isVideoLoaded) {
  286. // this._videoAd.show().then(() => {
  287. // utils.showLog("激励视频播放成功!");
  288. // }).catch((ero) => {
  289. // utils.showLog("激励视频播放失败! >>>>" + ero);
  290. // if (this._videoCallback) {
  291. // this._videoCallback(false, "视频播放失败,请稍后再试!");
  292. // this._videoCallback = null;
  293. // }
  294. // });
  295. // this._isVideoShow = false;
  296. // } else {
  297. this._videoAd.load().then(function () {
  298. Utils_1.utils.showLog("激励视频加载成功");
  299. _this._videoAd.show().then(function () {
  300. Utils_1.utils.showLog("激励视频播放成功!");
  301. _this._lastShowInterstitialTime = new Date().getTime();
  302. }).catch(function () {
  303. Utils_1.utils.showLog("激励视频播放失败!");
  304. if (_this._videoCallback) {
  305. _this._videoCallback(false, "视频播放失败,请稍后再试!");
  306. _this._videoCallback = null;
  307. }
  308. });
  309. }).catch(function () {
  310. Utils_1.utils.showLog("再次播放视频资源加载失败!");
  311. if (_this._videoCallback) {
  312. _this._videoCallback(false, "视频播放失败,请稍后再试!");
  313. _this._videoCallback = null;
  314. }
  315. });
  316. }
  317. // }
  318. }
  319. else {
  320. Utils_1.utils.showLog("获取配置失败,视频无法播放!");
  321. if (this._videoCallback) {
  322. this._videoCallback(false, "暂无视频广告!");
  323. this._videoCallback = null;
  324. }
  325. }
  326. // }
  327. };
  328. AdAgentDouyin.prototype._initVideoAd = function () {
  329. var _this = this;
  330. if (!this._videoAd) {
  331. if (Utils_1.utils.Tool_Douyin.ServerConfig) {
  332. var posId = Utils_1.utils.config.douyinconfig.videoId.trim();
  333. Utils_1.utils.showLog("video广告ID:" + posId);
  334. //@ts-ignore
  335. this._videoAd = tt.createRewardedVideoAd({
  336. adUnitId: posId
  337. });
  338. if (this._videoAd) {
  339. Utils_1.utils.showLog("初始化注册视频回调!");
  340. this._videoAd.onLoad(function () {
  341. Utils_1.utils.showLog("激励视频加载成功", _this._isVideoShow);
  342. _this._isVideoLoaded = true;
  343. if (_this._isVideoShow) {
  344. _this._videoAd.show().then(function () {
  345. Utils_1.utils.showLog("激励视频播放成功!");
  346. }).catch(function () {
  347. Utils_1.utils.showLog("激励视频播放失败!");
  348. if (_this._videoCallback) {
  349. _this._videoCallback(false, "视频播放失败,请稍后再试!");
  350. _this._videoCallback = null;
  351. }
  352. });
  353. _this._isVideoShow = false;
  354. }
  355. });
  356. this._videoAd.onError(function (err) {
  357. Utils_1.utils.showLog("\u6FC0\u52B1\u89C6\u9891\u51FA\u9519: " + (err.code, err.msg), err);
  358. _this._isVideoLoaded = false;
  359. if (_this._videoCallback) {
  360. _this._videoCallback(false, "暂无视频广告!");
  361. _this._videoCallback = null;
  362. }
  363. });
  364. this._videoAd.onClose(function (res) {
  365. _this._isVideoShow = false;
  366. _this._isVideoLoaded = false;
  367. if (res.isEnded) {
  368. Utils_1.utils.showLog('激励视频广告完成,发放奖励');
  369. if (_this._videoCallback) {
  370. _this._videoCallback(true, "");
  371. _this._videoCallback = null;
  372. }
  373. }
  374. else {
  375. Utils_1.utils.showLog('激励视频广告取消关闭,不发放奖励');
  376. if (_this._videoCallback) {
  377. _this._videoCallback(false, "观看完视频才能获得奖励!");
  378. _this._videoCallback = null;
  379. }
  380. }
  381. _this._videoAd.load().then(function () {
  382. Utils_1.utils.showLog("关闭视频后重新加载视频资源成功!");
  383. _this._isVideoShow = false;
  384. _this._isVideoLoaded = true;
  385. });
  386. });
  387. }
  388. else {
  389. Utils_1.utils.showLog("暂无视频广告!");
  390. if (this._videoCallback) {
  391. this._videoCallback(false, "暂无视频广告!");
  392. this._videoCallback = null;
  393. }
  394. }
  395. }
  396. else {
  397. Utils_1.utils.showLog("暂无视频广告!");
  398. if (this._videoCallback) {
  399. this._videoCallback(false, "暂无视频广告!");
  400. this._videoCallback = null;
  401. }
  402. }
  403. }
  404. };
  405. AdAgentDouyin.prototype._isConfigValid = function () {
  406. if (PlatUtils_1.default.IsDouyin) {
  407. return (Utils_1.utils.config.douyinconfig
  408. && Utils_1.utils.config.douyinconfig.appID
  409. && Utils_1.utils.config.douyinconfig.bannerId
  410. && Utils_1.utils.config.douyinconfig.videoId);
  411. }
  412. return false;
  413. };
  414. AdAgentDouyin.prototype.ShowCloseBtnBanner = function (location, args) {
  415. if (location === void 0) { location = YZ_Constant_1.BannerLocation.Home; }
  416. Utils_1.utils.showLog("ShowCloseBtnBanner >>>>>>>>>.");
  417. var isMoveBtn = 0;
  418. var fadeInTime = 0;
  419. var btn = args.closeBtn;
  420. var winHeight = cc.winSize.height;
  421. btn.opacity = 0;
  422. if (this.ServerConfig) {
  423. if (this.ServerConfig.show_close_btn_delay) {
  424. fadeInTime = this.ServerConfig.show_close_btn_delay;
  425. }
  426. // setTimeout(() => {
  427. // utils.showLog("延迟调用关闭按钮的Banner >>>>");
  428. // this.ShowBanner(location, args);
  429. // var adY = 200;
  430. // utils.showLog('utils - adY:' + adY);
  431. // if (adY > 0 && btn) {
  432. // btn.y = -(winHeight / 2 - adY) + btn.height;
  433. // utils.showLog("btnClose.y" + btn.y);
  434. // }
  435. // }, isMoveBtn);
  436. setTimeout(function () {
  437. btn.runAction(cc.fadeIn(0.3));
  438. }, fadeInTime * 1000);
  439. }
  440. };
  441. AdAgentDouyin = __decorate([
  442. ccclass
  443. ], AdAgentDouyin);
  444. return AdAgentDouyin;
  445. }(AdAgent_1.default));
  446. exports.default = AdAgentDouyin;
  447. cc._RF.pop();