QgNative.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import Tools from "../../Tools";
  2. import Global from "../../Global";
  3. import LogMgr from "../../LogMgr";
  4. export default class QgNative {
  5. private static nativeAd: any; //原生广告对象
  6. public static isLoad_Native: boolean = false;
  7. private static nativeResolve = null ;
  8. public static nativeMessage : any = null ;
  9. /**
  10. * 创建原生广告
  11. */
  12. public static createNative () {
  13. return new Promise((resolve, reject) => {
  14. if (!Global.isVivo) {
  15. LogMgr.error('当前非vivo平台,无法创建原生广告');
  16. reject(false) ;
  17. return
  18. }
  19. let unitId = Tools.getRandomByArray(Global.config.advertisingConfig.nativeAdId);
  20. if (!unitId) {
  21. LogMgr.error('原生ID获取失败:' + unitId);
  22. reject(false) ;
  23. return
  24. }
  25. // @ts-ignore
  26. this.nativeAd = qg.createNativeAd({posId : unitId});
  27. this.nativeAd.onLoad((res)=>{
  28. if (res && res.adList) {
  29. LogMgr.log('原生广告拉取成功>>>>>>',res.adList) ;
  30. this.nativeMessage = res.adList[0] ;
  31. this.nativeResolve(res.adList[0]) ;
  32. this.isLoad_Native = true ;
  33. }
  34. this.nativeResolve = null ;
  35. })
  36. resolve(true);
  37. })
  38. }
  39. /**
  40. * 加载原生广告
  41. */
  42. public static loadNative () {
  43. return new Promise((resolve)=>{
  44. this.nativeResolve = resolve ;
  45. if (!this.nativeAd) {
  46. LogMgr.error('原生广告未创建,无法加载......')
  47. this.createNative().then(()=>{}).catch() ;
  48. this.nativeResolve(false) ;
  49. this.nativeResolve = null ;
  50. return
  51. }
  52. if (this.nativeMessage != null) {
  53. this.nativeResolve(this.nativeMessage) ;
  54. this.nativeResolve = null ;
  55. }
  56. this.nativeAd.load().then(()=>{
  57. }).catch(()=>{
  58. this.nativeResolve(false) ;
  59. }) ;
  60. })
  61. }
  62. /**
  63. * 上报原生广告曝光
  64. */
  65. public static repAdShow (adId) {
  66. if (!this.nativeAd) return ;
  67. LogMgr.log('上报用户曝光>>>>>>')
  68. this.nativeAd.reportAdShow({
  69. adId: adId
  70. }) ;
  71. }
  72. /**
  73. * 上报原生广告点击
  74. */
  75. public static repAdClick (adId) {
  76. if (!this.nativeAd) return ;
  77. LogMgr.log('上报用户点击>>>>>>')
  78. this.nativeAd.reportAdClick({
  79. adId: adId
  80. })
  81. }
  82. /**
  83. * 重新拉去广告信息
  84. */
  85. public static anewLoad () {
  86. this.nativeMessage = null ;
  87. this.isLoad_Native = false ;
  88. this.loadNative().then() ;
  89. }
  90. }