VivoAd.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. import NavtiveData from "./NavtiveData";
  2. import SDK, { NodeAction } from "../SDK";
  3. import Utils from "../tools/Utils";
  4. import { NativeType } from "./NativeAdComponent";
  5. import Banner from "./Banner";
  6. import Interstitial, { InterstitialType } from "./Interstitial";
  7. import Video from "./Video";
  8. import Icon from "./Icon";
  9. // Learn TypeScript:
  10. // - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
  11. // Learn Attribute:
  12. // - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
  13. // Learn life-cycle callbacks:
  14. // - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html
  15. const { ccclass, property } = cc._decorator;
  16. // {"id" : 1, "open" : true,"info" : "主界面插屏" },
  17. // {"id" : 2, "open" : true, "info" : "关卡插屏" },
  18. // {"id" : 3, "open" : true, "info" : "皮肤插屏" },
  19. // {"id" : 4, "open" : true, "info" : "设置插屏" },
  20. // {"id" : 5, "open" : true, "info" : "暂停插屏" },
  21. // {"id" : 6, "open" : true, "info" : "游戏插屏" },
  22. // {"id" : 7, "open" : true, "info" : "胜利插屏" },
  23. // {"id" : 8, "open" : true, "info" : "失败插屏" },
  24. // {"id" : 9, "open" : true, "info" : "复活插屏" }
  25. @ccclass
  26. export default class VivoAd extends cc.Component {
  27. private static _instance: VivoAd = null;
  28. private _config = null;
  29. private _banner: Banner;
  30. private _interstitial: Interstitial;
  31. private _video: Video;
  32. private _icon : Icon;
  33. private _vivoNativeDateList = [];
  34. private _hideCallback = null;
  35. private _showCallback = null;
  36. private _nativeAdInitTime = 0;
  37. private _utils: Utils;
  38. private _netType;
  39. private _adCreating = false;
  40. public static get Instance(): VivoAd {
  41. if (VivoAd._instance == null) {
  42. VivoAd._instance = new VivoAd();
  43. }
  44. return VivoAd._instance;
  45. }
  46. constructor() {
  47. super();
  48. let self = this;
  49. this._utils = Utils.instance;
  50. this._banner = new Banner();
  51. this._interstitial = new Interstitial();
  52. this._video = new Video();
  53. this._icon = new Icon();
  54. }
  55. public getBanner() {
  56. return this._banner;
  57. }
  58. public getConfig() {
  59. return this._config;
  60. }
  61. public onListenHome() {
  62. let self = this;
  63. let isBG = false;
  64. cc.game.on(cc.game.EVENT_SHOW, function (event) {
  65. if (isBG) {
  66. self._utils.log("切换前台");
  67. isBG = false;
  68. if (self._showCallback) {
  69. self._showCallback();
  70. }
  71. }
  72. });
  73. cc.game.on(cc.game.EVENT_HIDE, function (event) {
  74. if (!isBG) {
  75. self._utils.log("切换后台");
  76. isBG = true;
  77. if (self._hideCallback) {
  78. self._hideCallback();
  79. }
  80. }
  81. });
  82. }
  83. public initConfig(config, callBack?) {
  84. this._config = config;
  85. this._utils.log("config:------- " + JSON.stringify(config));
  86. if (config) {
  87. this._banner.init(config);
  88. this._interstitial.init(config);
  89. this._video.init(config);
  90. this._icon.init(config);
  91. this.creatorNativeData(callBack);
  92. this.schedule(() => {
  93. this.creatorNativeData(null);
  94. }, 15);//开启定时器,定时请求广告
  95. }
  96. }
  97. public onHide(callbacks) {
  98. this._hideCallback = callbacks;
  99. }
  100. public onShow(callback) {
  101. this._showCallback = callback;
  102. }
  103. public getNetType(cb) {
  104. qg.getNetworkType({
  105. success: function (data) {
  106. console.log(`handling success: ${data.type}`)
  107. if(cb){
  108. cb(data.type);
  109. }
  110. // VivoAd._instance ._netType = data.type;
  111. }
  112. });
  113. }
  114. //广告初始化
  115. public init() {
  116. }
  117. //创建广告
  118. public creatorNativeData(callBack?) {
  119. let self = this;
  120. if (self._adCreating) {
  121. this._utils.log("----有广告未完成创建 ");
  122. return;
  123. }
  124. this._utils.log("--------load ad----------");
  125. if (this._vivoNativeDateList.length >= 2) {
  126. this._utils.log("----广告未消耗,广告列表: " + this._vivoNativeDateList.length);
  127. return;
  128. }
  129. let curTime = new Date().getTime();
  130. let bigTime = (curTime - this._nativeAdInitTime) / 1000;
  131. this._utils.log("--------load ad time : " + bigTime);
  132. let initADgo = () => {
  133. self._adCreating = true;
  134. let navtiveData = new NavtiveData();
  135. let go = (success) => {
  136. self._adCreating = false;
  137. if (success) {
  138. self._nativeAdInitTime = new Date().getTime();
  139. self._vivoNativeDateList.push(navtiveData);
  140. this._utils.log("--------广告缓存成功,列表: " + this._vivoNativeDateList.length);
  141. }
  142. if (callBack) {
  143. callBack(success);
  144. }
  145. }
  146. navtiveData.initAd(self._config, go);
  147. }
  148. if (bigTime < 3.1) {//广告加载时间,离上次未到3秒,等待剩余时间
  149. let time = 3.1 - bigTime;
  150. this._utils.log("--------离上次未到3秒,等待剩余时间 : " + bigTime);
  151. this.scheduleOnce(initADgo, time);
  152. return;
  153. }
  154. initADgo();
  155. }
  156. public hasNativeData() {
  157. if (this._vivoNativeDateList.length > 0) {
  158. this._utils.log("----列表已存在广告: " + this._vivoNativeDateList.length);
  159. return true;
  160. }
  161. return false;
  162. }
  163. //获取广告数据
  164. public getVivoNativeData() {
  165. this._utils.log("----广告列表: " + this._vivoNativeDateList.length);
  166. let date = null;
  167. if (this._vivoNativeDateList.length > 0) {
  168. date = this._vivoNativeDateList.shift();
  169. }
  170. if (date && date.isReady()) {
  171. this._utils.log("-----存在新广告,用新的--------");
  172. return date;
  173. } else {
  174. this._utils.log("-----不存在广告或只存在旧广告--------");
  175. }
  176. this._utils.log("-----广告列表还有: " + this._vivoNativeDateList.length);
  177. return null;
  178. }
  179. /**
  180. * 展示原生banner
  181. * @param top 是否顶部显示
  182. */
  183. public showBanner(type: NativeType = null, top: boolean = false, y: number = 0, x: number = 0) {
  184. if (this._banner)
  185. {
  186. this._banner.showBanner(type, top, x, y);
  187. }
  188. }
  189. public hideBanner() {
  190. if (this._banner) {
  191. this._banner.hideBanner();
  192. }
  193. }
  194. /**
  195. * 隐藏原生icon
  196. */
  197. public hideIcon() {
  198. if(this._icon){
  199. this._icon. hideIcon();
  200. }
  201. }
  202. /**
  203. * 展示原生插屏
  204. * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参
  205. * @param id 默认0
  206. * @param isforever true时,每次调用都会展示,false时,受服务器调用次数控制,默认false
  207. */
  208. public showInterstitial(type: InterstitialType = InterstitialType.INTERSTITIAL_GAME_OVER) {
  209. if (this._interstitial) {
  210. this._interstitial.showInterstitial(type);
  211. }
  212. }
  213. /**
  214. * 展示原生icon广告
  215. * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参
  216. * @param id
  217. */
  218. public showIconAd(id: number = 0, x: number, y: number) {
  219. if(this._icon){
  220. this._icon.showIconAd(id,x,y);
  221. }
  222. }
  223. /*显示视频*/
  224. public showRewardVideo(callBacks: Function, fail: Function, noAd: Function) {
  225. this._utils.log("---------showRewardVideo-----");
  226. let self = this
  227. let sucCall = () => {
  228. if (callBacks) {
  229. callBacks();
  230. }
  231. }
  232. let failCall = () => {
  233. if (fail) {
  234. fail();
  235. }
  236. }
  237. let noAdCall = () => {
  238. if (noAd) {
  239. noAd();
  240. } else {
  241. self.showToast("暂无广告!");
  242. }
  243. }
  244. if (this._video)
  245. this._video.showRewardVideo(sucCall, failCall, noAdCall);
  246. }
  247. /**获取剪切板 */
  248. public getClipboardData(callbacks) {
  249. let self = this;
  250. qg.getClipboardData({
  251. success: function (data) {
  252. if (callbacks) {
  253. callbacks(data.text);
  254. }
  255. self._utils.log(`Clipboar handling success: ${data.text}`)
  256. },
  257. fail: function (data, code) {
  258. self._utils.log(`handling fail, code = ${code}`)
  259. }
  260. })
  261. }
  262. public getSystemInfoSync() {
  263. return qg.getSystemInfoSync();
  264. }
  265. public getUserId() {
  266. let user = qg.getUserIdSync();;
  267. if(user && user.userId){
  268. return user.userId;
  269. }
  270. return "vivo not user id";
  271. }
  272. /**
  273. * 用户来源
  274. */
  275. public getSource() {
  276. return qg.getLaunchOptionsSync();
  277. }
  278. public showToast(message: string) {
  279. qg.showToast({
  280. message: message
  281. })
  282. }
  283. }