d3ac85ca-1273-48e3-b25f-6f23e1169950.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. "use strict";
  2. cc._RF.push(module, 'd3ac8XKEnNI47JfbyPhFplQ', 'NativeAdComponent');
  3. // Script/sdk/vivo/NativeAdComponent.ts
  4. "use strict";
  5. // Learn TypeScript:
  6. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  7. // Learn Attribute:
  8. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  9. // Learn life-cycle callbacks:
  10. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  11. var __extends = (this && this.__extends) || (function () {
  12. var extendStatics = function (d, b) {
  13. extendStatics = Object.setPrototypeOf ||
  14. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  15. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  16. return extendStatics(d, b);
  17. };
  18. return function (d, b) {
  19. extendStatics(d, b);
  20. function __() { this.constructor = d; }
  21. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  22. };
  23. })();
  24. var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
  25. var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
  26. if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
  27. 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;
  28. return c > 3 && r && Object.defineProperty(target, key, r), r;
  29. };
  30. Object.defineProperty(exports, "__esModule", { value: true });
  31. exports.NativeType = void 0;
  32. var SDK_1 = require("../SDK");
  33. var Utils_1 = require("../tools/Utils");
  34. var VivoAd_1 = require("./VivoAd");
  35. var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
  36. var NativeType;
  37. (function (NativeType) {
  38. NativeType[NativeType["INSET"] = 0] = "INSET";
  39. NativeType[NativeType["BANNER"] = 1] = "BANNER";
  40. NativeType[NativeType["ICON"] = 2] = "ICON";
  41. NativeType[NativeType["SPLASH"] = 3] = "SPLASH";
  42. NativeType[NativeType["BANNER_BUTTON"] = 4] = "BANNER_BUTTON";
  43. NativeType[NativeType["BANNER_NO_NATIVE"] = 5] = "BANNER_NO_NATIVE";
  44. NativeType[NativeType["BANNER_LITTLE_BUTTON"] = 6] = "BANNER_LITTLE_BUTTON";
  45. NativeType[NativeType["ICON_GAME"] = 7] = "ICON_GAME";
  46. NativeType[NativeType["INSET_NO_NATIVE"] = 8] = "INSET_NO_NATIVE";
  47. })(NativeType = exports.NativeType || (exports.NativeType = {}));
  48. /**原生广告 */
  49. var NativeAdComponent = /** @class */ (function (_super) {
  50. __extends(NativeAdComponent, _super);
  51. function NativeAdComponent() {
  52. var _this = _super !== null && _super.apply(this, arguments) || this;
  53. _this.iconShow = false;
  54. _this._closeButton = null; //关闭按钮
  55. _this._content = null; //内容按钮
  56. _this._type = NativeType.INSET; //默认插屏
  57. _this._nativeCurrentAd = null; //广告内容
  58. _this._config = null; //广告配置
  59. _this._adCallBack = null; //banner和icon刷新回调
  60. _this._clickCallBack = null; //点击回调
  61. _this._showCallBack = null; //展示回调
  62. _this._closeCallBack = null; //关闭回调
  63. _this._title = null; //广告标题
  64. _this._info = null; //广告描述
  65. _this._isClick = false; //防止重复点击
  66. _this._splashCallBacks = null; //开屏回调
  67. _this._labelCall = null; //开屏倒时候任务
  68. _this._vivoNativeDate = null;
  69. return _this;
  70. }
  71. NativeAdComponent.prototype.start = function () {
  72. Utils_1.default.instance.log("-----------------iconShow: " + this.iconShow);
  73. if (this.iconShow) {
  74. var shareVivoNativeDate = VivoAd_1.default.Instance.getVivoNativeData();
  75. if (shareVivoNativeDate == null || !shareVivoNativeDate.isReady()) {
  76. this.node.active = false;
  77. return;
  78. }
  79. this.initData(shareVivoNativeDate);
  80. var clickCallBack = function () {
  81. VivoAd_1.default.Instance.creatorNativeData();
  82. SDK_1.default.Instance.reportLog(SDK_1.NodeAction.NATIVE_CLICK, "原生icon点击");
  83. };
  84. var showCallBack = function () {
  85. //缓存下一条广告
  86. SDK_1.default.Instance.reportLog(SDK_1.NodeAction.NATIVE_SHOW, "原生icon展示");
  87. };
  88. var closeCallBack = function () {
  89. SDK_1.default.Instance.reportLog(SDK_1.NodeAction.NATIVE_CLOSE, "原生icon关闭");
  90. };
  91. this.listionCall(showCallBack, clickCallBack, closeCallBack);
  92. this.showAd(NativeType.ICON_GAME, VivoAd_1.default.Instance.getConfig());
  93. this.node.active = true;
  94. }
  95. };
  96. NativeAdComponent.prototype.initButton = function () {
  97. var curContent = this.node.getChildByName("content");
  98. if (curContent) {
  99. this._content = curContent.getChildByName("contentAd").getComponent(cc.Sprite);
  100. var button = curContent.getChildByName("Button");
  101. if (button && button.active) {
  102. //广告详情按键默认关闭
  103. button.on(cc.Node.EventType.TOUCH_START, this.adClick, this);
  104. if (this._type == NativeType.BANNER_BUTTON || this._type == NativeType.BANNER_LITTLE_BUTTON) {
  105. button.active = false;
  106. if (this._config.insertAdNodeList) { //存在插屏列表
  107. var adNodeList = JSON.parse(this._config.insertAdNodeList);
  108. //黑夜首节点是否存在
  109. var adNode = adNodeList[0];
  110. if (adNode) {
  111. //广告详情是否打开
  112. if (adNode.bntDetails && adNode.bntDetails > 0) {
  113. button.active = true;
  114. }
  115. }
  116. }
  117. button.runAction(cc.repeatForever(cc.sequence(cc.scaleTo(0.5, 1.1), cc.scaleTo(0.5, 1))));
  118. }
  119. }
  120. curContent.on(cc.Node.EventType.TOUCH_START, this.adClick, this);
  121. }
  122. };
  123. NativeAdComponent.prototype.onDisable = function () {
  124. if (this._adCallBack) {
  125. this.unschedule(this._adCallBack);
  126. }
  127. };
  128. NativeAdComponent.prototype.initData = function (vivoNativeDate) {
  129. this._vivoNativeDate = vivoNativeDate;
  130. };
  131. NativeAdComponent.prototype.init = function () {
  132. var _this = this;
  133. this.node.active = true;
  134. var self = this;
  135. // let nativeData = {
  136. // imgUrlList: "https://pic.cr173.com/up/2019-9/201991783518653750.jpg",
  137. // icon: "https://p.e5n.com/up/2019-9/2019917834498180.jpg",
  138. // title: "超强马丽",
  139. // desc: "这个游戏很不错",
  140. // clickBtnTxt: "立即查看"
  141. // }
  142. this._nativeCurrentAd = this._vivoNativeDate.getNativeDate();
  143. if (this._nativeCurrentAd == null) {
  144. console.log("------没有广告数据");
  145. VivoAd_1.default.Instance.creatorNativeData(function (success) {
  146. if (success) {
  147. _this._vivoNativeDate = VivoAd_1.default.Instance.getVivoNativeData();
  148. self.init();
  149. }
  150. });
  151. return;
  152. }
  153. var curContent = this.node.getChildByName("content");
  154. if (curContent) {
  155. var adId = curContent.getChildByName("adId");
  156. if (adId) {
  157. adId.active = false;
  158. adId.y = -50;
  159. adId.x = -10;
  160. adId.color = new cc.Color(255, 0, 0);
  161. var adIdLable = adId.getComponent(cc.Label);
  162. adIdLable.string = this._nativeCurrentAd.adId.toString();
  163. }
  164. if (this._type == NativeType.SPLASH) {
  165. var closeContinu = curContent.getChildByName("closeContinu");
  166. closeContinu.on(cc.Node.EventType.TOUCH_START, this.close, this);
  167. var label_1 = closeContinu.getChildByName("cLabel").getComponent(cc.Label);
  168. Utils_1.default.instance.log("----------splash label: " + label_1);
  169. if (label_1) {
  170. var count_1 = 3;
  171. self._labelCall = function () {
  172. count_1--;
  173. if (count_1 <= 0) {
  174. Utils_1.default.instance.log("----------开屏关闭: " + label_1);
  175. self.hide();
  176. }
  177. label_1.string = count_1 + "秒 跳过";
  178. Utils_1.default.instance.log("----------splash label: " + label_1.string);
  179. };
  180. self.schedule(self._labelCall, 3);
  181. }
  182. }
  183. }
  184. var url = this._nativeCurrentAd.imgUrlList[0];
  185. //加载插屏图片
  186. if (this._type != NativeType.ICON && this._type != NativeType.BANNER_BUTTON && this._type != NativeType.ICON_GAME) {
  187. if (curContent) {
  188. var title = curContent.getChildByName("title");
  189. if (title && title.active) {
  190. self._title = title.getComponent(cc.Label);
  191. self._title.string = self._nativeCurrentAd.title;
  192. }
  193. var info = curContent.getChildByName("info");
  194. if (info && info.active) {
  195. self._info = info.getComponent(cc.Label);
  196. self._info.string = self._nativeCurrentAd.desc;
  197. }
  198. url = this._nativeCurrentAd.icon;
  199. if (this._type != NativeType.BANNER && this._type != NativeType.BANNER_LITTLE_BUTTON) {
  200. var icon_ = curContent.getChildByName("icon");
  201. if (icon_ && icon_.active) {
  202. var icon_1 = icon_.getComponent(cc.Sprite);
  203. this.getImageSpriteFrame(url).then(function (frame) {
  204. icon_1.spriteFrame = frame;
  205. }).catch(function (err) {
  206. console.log(err);
  207. self.getImageSpriteFrame(url).then(function (frame) {
  208. icon_1.spriteFrame = frame;
  209. });
  210. });
  211. }
  212. }
  213. }
  214. }
  215. if (this._type == NativeType.ICON) {
  216. url = this._nativeCurrentAd.icon;
  217. }
  218. else {
  219. url = this._nativeCurrentAd.imgUrlList[0];
  220. }
  221. this.getImageSpriteFrame(url).then(function (frame) {
  222. self._content.spriteFrame = frame;
  223. }).catch(function (err) {
  224. console.log(err);
  225. url = _this._nativeCurrentAd.imgUrlList[0];
  226. self.getImageSpriteFrame(url).then(function (frame) {
  227. self._content.spriteFrame = frame;
  228. }).catch(function (err) {
  229. console.log(err);
  230. self.getImageSpriteFrame(url).then(function (frame) {
  231. self._content.spriteFrame = frame;
  232. });
  233. });
  234. });
  235. };
  236. /**广告状态监听 */
  237. NativeAdComponent.prototype.listionCall = function (showCallBack, clickCallBack, closeCallBack) {
  238. this._showCallBack = showCallBack;
  239. this._clickCallBack = clickCallBack;
  240. this._closeCallBack = closeCallBack;
  241. };
  242. NativeAdComponent.prototype.closeSize = function () {
  243. if (this._closeButton == null) {
  244. return;
  245. }
  246. if (this._config && this._closeButton) { //控制关闭按键大小
  247. var adNodeList = JSON.parse(this._config.insertAdNodeList);
  248. if (adNodeList) {
  249. var adNode = adNodeList[0];
  250. if (adNode) {
  251. var size = adNode.size;
  252. if (size) {
  253. if (size.enable) { //关闭按键大小开关
  254. var width = 20;
  255. var height = 20;
  256. if (size.width == null || size.width == 0) {
  257. width = 35;
  258. }
  259. else if (size.width < 20) {
  260. width = 20;
  261. }
  262. else {
  263. width = size.width;
  264. }
  265. if (size.height == null || size.height == 0) {
  266. height = 35;
  267. }
  268. else if (size.height < 20) {
  269. height = 20;
  270. }
  271. else {
  272. height = size.height;
  273. }
  274. this._closeButton.width = width;
  275. this._closeButton.height = height;
  276. }
  277. }
  278. }
  279. }
  280. }
  281. };
  282. /**展示广告
  283. * @param
  284. */
  285. NativeAdComponent.prototype.showAd = function (type, config, x, y, callBacks) {
  286. var _this = this;
  287. this._type = type;
  288. this._config = config;
  289. this.initButton();
  290. var curContent = this.node.getChildByName("content");
  291. if (curContent) {
  292. this._closeButton = curContent.getChildByName("close");
  293. this.closeSize();
  294. // this.content.node.on(cc.Node.EventType.TOUCH_START, this.adClick, this);
  295. this._closeButton.on(cc.Node.EventType.TOUCH_START, this.close, this);
  296. this.node.active = false;
  297. this._splashCallBacks = callBacks;
  298. this.show();
  299. var adFlushTime = this._config.iconAdFlush;
  300. var self_1 = this;
  301. if (type == NativeType.BANNER_BUTTON || this._type == NativeType.BANNER_LITTLE_BUTTON) {
  302. var bigBannerCloseEnable = this._config.bigBannerCloseEnable;
  303. if (bigBannerCloseEnable <= 0) {
  304. Utils_1.default.instance.log("-----------大banner----------");
  305. this._closeButton.active = false;
  306. }
  307. }
  308. //不是插屏时,要刷新广告
  309. if (type == NativeType.BANNER ||
  310. type == NativeType.ICON ||
  311. type == NativeType.BANNER_BUTTON ||
  312. type == NativeType.ICON_GAME ||
  313. type == NativeType.BANNER_LITTLE_BUTTON) {
  314. this.node.x = x;
  315. this.node.y = y;
  316. if (adFlushTime <= 0) {
  317. Utils_1.default.instance.log("-----------不刷新广告----------");
  318. return;
  319. }
  320. this._adCallBack = function () {
  321. Utils_1.default.instance.log("-----------广告刷新----------");
  322. var curContent = _this.node.getChildByName("content");
  323. if (curContent) {
  324. curContent.active = true;
  325. }
  326. self_1._vivoNativeDate = VivoAd_1.default.Instance.getVivoNativeData();
  327. self_1.show();
  328. };
  329. this.schedule(this._adCallBack, adFlushTime);
  330. }
  331. }
  332. };
  333. NativeAdComponent.prototype.show = function () {
  334. var self = this;
  335. this._isClick = false;
  336. // if(this.node.active == false){
  337. // this.node.active = true;
  338. // }
  339. if (self._vivoNativeDate) {
  340. self.init();
  341. self._vivoNativeDate.reportAdShow();
  342. if (this._showCallBack) {
  343. this._showCallBack();
  344. }
  345. }
  346. };
  347. NativeAdComponent.prototype.onDestroy = function () {
  348. Utils_1.default.instance.log("-------广告 destroy " + this._type);
  349. };
  350. /**
  351. * 隐藏广告
  352. */
  353. NativeAdComponent.prototype.hide = function (remove) {
  354. if (remove === void 0) { remove = true; }
  355. if (this.node) {
  356. if (remove) {
  357. this.unscheduleAllCallbacks();
  358. this.node.removeFromParent();
  359. }
  360. else {
  361. if (this._type != NativeType.ICON_GAME) {
  362. var curContent = this.node.getChildByName("content");
  363. if (curContent) {
  364. curContent.active = false;
  365. }
  366. }
  367. else {
  368. this.node.active = false;
  369. }
  370. }
  371. }
  372. if (this._closeCallBack) {
  373. this._closeCallBack();
  374. }
  375. if (this._type == NativeType.SPLASH) {
  376. if (this._splashCallBacks) {
  377. this._splashCallBacks();
  378. }
  379. this.unschedule(this._labelCall);
  380. }
  381. };
  382. //全屏点击
  383. NativeAdComponent.prototype.allSceen = function () {
  384. if (this._type == NativeType.INSET) {
  385. return;
  386. }
  387. this.adClick();
  388. //
  389. };
  390. //点击事件
  391. NativeAdComponent.prototype.adClick = function () {
  392. if (this._isClick) {
  393. return;
  394. }
  395. this._isClick = true;
  396. var self = this;
  397. if (self._vivoNativeDate) {
  398. self._vivoNativeDate.reportAdClick();
  399. if (this._clickCallBack) {
  400. this._clickCallBack();
  401. }
  402. }
  403. //如果是插屏或开屏,点击直接关闭
  404. if (this._type == NativeType.INSET || this._type == NativeType.SPLASH) {
  405. this.hide(true);
  406. }
  407. else { //banner和icon则刷新广告
  408. this._vivoNativeDate = VivoAd_1.default.Instance.getVivoNativeData();
  409. if (this._vivoNativeDate != null && this._vivoNativeDate.isReady()) {
  410. this.show();
  411. }
  412. else {
  413. this.hide(false);
  414. }
  415. }
  416. };
  417. /**
  418. *
  419. * @returns 关闭广告
  420. */
  421. NativeAdComponent.prototype.close = function () {
  422. var mistakeRate = this._config.insertAdMistakeRate;
  423. this._isClick = true;
  424. var random = Utils_1.default.instance.getRandomInt(1, 100);
  425. //误次数已用完,直接关闭
  426. if (SDK_1.default.Instance.limitClick >= this._config.maxClickCount) {
  427. console.log("=============数已用完,直接关闭");
  428. this.hide();
  429. return;
  430. }
  431. // mistakeRate = 0;
  432. //满足误点
  433. if (random < mistakeRate) { //关击按键是事能点击广告
  434. SDK_1.default.Instance.limitClick++;
  435. console.log("------------random: ", random < mistakeRate);
  436. console.log("。。。。。。原生广告close adClick。。。。。。");
  437. this.adClick();
  438. return;
  439. }
  440. if (this._type == NativeType.INSET || this._type == NativeType.SPLASH) {
  441. this.hide();
  442. }
  443. else {
  444. this.hide(false);
  445. }
  446. };
  447. /**
  448. * 获取内容图片
  449. * @returns
  450. */
  451. NativeAdComponent.prototype.getImageSpriteFrame = function (url) {
  452. var self = this;
  453. return new Promise(function (resolve, reject) {
  454. console.log("图片下载地址:", url);
  455. cc.loader.load(url, function (err, texture) {
  456. if (err) {
  457. console.log("图片下载失败", url, err);
  458. reject("图片下载失败: " + url);
  459. return;
  460. }
  461. var frame = new cc.SpriteFrame(texture);
  462. resolve(frame);
  463. });
  464. });
  465. };
  466. __decorate([
  467. property(cc.Boolean)
  468. ], NativeAdComponent.prototype, "iconShow", void 0);
  469. NativeAdComponent = __decorate([
  470. ccclass
  471. ], NativeAdComponent);
  472. return NativeAdComponent;
  473. }(cc.Component));
  474. exports.default = NativeAdComponent;
  475. cc._RF.pop();