NativeView.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import LayerPanel, {UrlInfo} from "../../Common/manage/Layer/LayerPanel";
  2. import Texture2D = cc.Texture2D;
  3. import QgNative from "../../Common/manage/Api/QgNative";
  4. import Emit from "../../Common/manage/Emit/Emit";
  5. import EmitData from "../../Common/manage/Emit/EmitData";
  6. import QgBanner from "../../Common/manage/Api/QgBanner";
  7. import PanelMgr from "../../Common/manage/PanelMgr";
  8. const {ccclass, property} = cc._decorator;
  9. const {TOUCH_START} = cc.Node.EventType ;
  10. @ccclass
  11. export default class NativeView extends LayerPanel {
  12. public static getUrl(): UrlInfo {
  13. return {
  14. bundle: "nativeView",
  15. name: "nativeView"
  16. }
  17. }
  18. @property(cc.Node)
  19. native_inters: cc.Node = null;
  20. @property(cc.Node)
  21. native_implant: cc.Node = null;
  22. private nativeMessage : any = null ;
  23. private currSprite : cc.Texture2D = null ;
  24. private adId : string = null ;
  25. private labelType : number = null ;
  26. private labelArr : string[] = ['点击查看','点击跳过'] ;
  27. private type : number = null ;
  28. initUI() {
  29. this.initTouch() ;
  30. }
  31. show(param: any): void {
  32. this.node.getChildByName('inputEvent').active = false ;
  33. this.node.getChildByName('inputEvent_implant').active = false ;
  34. this.native_inters.active = false ;
  35. this.native_implant.active = false ;
  36. this.labelType = param.labelType ;
  37. this.type = param.type ;
  38. this.scheduleOnce(()=>{
  39. if (QgNative.nativeMessage != null) {
  40. this.nativeMessage = QgNative.nativeMessage ;
  41. this.open_type(param.type) ;
  42. }else {
  43. QgNative.loadNative().then((res)=>{
  44. if (res && res != false) {
  45. this.nativeMessage = res ;
  46. this.open_type(param.type) ;
  47. }else {
  48. }
  49. })
  50. }
  51. },param.time) ;
  52. if (this.type == 2) {
  53. Emit.instance().on(EmitData.CLOSE_NATIVE,this.closeNative,this) ;
  54. }
  55. }
  56. openNative (node : cc.Node) {
  57. node.getChildByName('title').getComponent(cc.Label).string = this.nativeMessage.desc ;
  58. let imgUrl = null ;
  59. if (this.nativeMessage.imgUrlList && this.nativeMessage.imgUrlList.length > 0) {
  60. imgUrl = this.nativeMessage.imgUrlList[0] ;
  61. }
  62. if (imgUrl != null) {
  63. cc.assetManager.loadRemote(imgUrl,{ext: '.png'},(err, asset : Texture2D)=>{
  64. if (err) {
  65. console.error('原生广告图片加载错误>>>>>>',err) ;
  66. return
  67. }
  68. this.currSprite = asset ;
  69. node.getChildByName('image').getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(this.currSprite) ;
  70. })
  71. }
  72. this.adId = this.nativeMessage.adId ;
  73. QgNative.repAdShow(this.nativeMessage.adId) ;
  74. }
  75. open_type (type) {
  76. if (!this.nativeMessage) {
  77. return
  78. }
  79. if (type == 1) {
  80. this.node.getChildByName('inputEvent').active = true ;
  81. this.native_inters.getChildByName('nativeBtn').getChildByName('label').getComponent(cc.Label).string = this.labelArr[this.labelType - 1] ;
  82. this.native_inters.getChildByName('close').active = false ;
  83. this.native_inters.active = true ;
  84. this.scheduleOnce(()=>{
  85. this.native_inters.getChildByName('close').active = true ;
  86. },2)
  87. this.openNative(this.native_inters) ;
  88. }
  89. if (type == 2) {
  90. QgBanner.hideBanner() ;
  91. this.node.getChildByName('inputEvent_implant').active = true ;
  92. this.native_implant.getChildByName('nativeBtn').getChildByName('label').getComponent(cc.Label).string = this.labelArr[this.labelType - 1] ;
  93. this.native_implant.getChildByName('close').active = false ;
  94. this.native_implant.active = true ;
  95. this.scheduleOnce(()=>{
  96. this.native_implant.getChildByName('close').active = true ;
  97. },2)
  98. this.openNative(this.native_implant) ;
  99. }
  100. }
  101. initTouch () {
  102. this.native_inters.getChildByName('image').on(TOUCH_START,()=>{
  103. QgNative.repAdClick(this.adId) ;
  104. })
  105. this.native_implant.getChildByName('image').on(TOUCH_START,()=>{
  106. QgNative.repAdClick(this.adId) ;
  107. })
  108. this.native_inters.getChildByName('nativeBtn').on(TOUCH_START,()=>{
  109. this.handlerNativeBtn() ;
  110. })
  111. this.native_implant.getChildByName('nativeBtn').on(TOUCH_START,()=>{
  112. this.handlerNativeBtn() ;
  113. })
  114. this.native_inters.getChildByName('close').on(TOUCH_START,()=>{
  115. this.closeNative() ;
  116. })
  117. this.native_implant.getChildByName('close').on(TOUCH_START,()=>{
  118. this.closeNative() ;
  119. })
  120. }
  121. handlerNativeBtn () {
  122. if (this.labelType == 1) {
  123. QgNative.repAdClick(this.adId) ;
  124. }else {
  125. this.closeNative() ;
  126. }
  127. }
  128. hide() {
  129. QgNative.anewLoad() ;
  130. Emit.instance().emit(EmitData.IN_NATIVE_NEXT) ;
  131. if (this.type == 2) {
  132. QgBanner.showBanner() ;
  133. Emit.instance().off(EmitData.CLOSE_NATIVE,this.closeNative,this) ;
  134. }
  135. }
  136. closeNative() {
  137. PanelMgr.INS.closePanel(NativeView,false) ;
  138. }
  139. // update (dt) {}
  140. }