NativeAdComponent.ts 17 KB

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