sdk.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /**
  2. * 全局变量 sdk
  3. */
  4. let sdk = new class {
  5. readonly default: string = "csj"
  6. readonly tt: string = "tt"
  7. readonly wx: string = "wx"
  8. readonly android: string = "android"
  9. private deviceId: string
  10. //安卓广告渠道
  11. public ANDROID_AD_CHANNEL = {
  12. CSJ: "csj",
  13. YLH: "ylh",
  14. KS: "ks",
  15. }
  16. channel: string
  17. currSDK: any
  18. // 视频回调
  19. private videoSucceed: Function
  20. private videoFail: Function
  21. //每天广告总数限制
  22. public readonly VIDEO_LIMIT: number = 10000
  23. constructor() {
  24. setTimeout(() => {
  25. this.init()
  26. }, 100)
  27. }
  28. init() {
  29. this.channel = this.default
  30. this.currSDK = TestDefault
  31. if (cc.sys.isNative && cc.sys.os === cc.sys.OS_ANDROID) {
  32. this.channel = this.android
  33. this.currSDK = android
  34. }
  35. if (this.currSDK && this.currSDK.init) {
  36. this.currSDK.init()
  37. }
  38. console.log("sdk.channel = " + this.channel)
  39. }
  40. public getDeviceId() {
  41. if (this.deviceId) {
  42. return this.deviceId
  43. }
  44. this.deviceId = cc.sys.localStorage.getItem("zsdk_deviceid")
  45. if (this.deviceId) {
  46. if (CC_JSB && cc.sys.os == cc.sys.OS_ANDROID && jsb.reflection) {
  47. jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "setDeviceId", "(Ljava/lang/String;)V", this.deviceId)
  48. }
  49. return this.deviceId
  50. }
  51. this.deviceId = new Date().getTime() + "-" +parseInt( Math.random() * 10000+"")
  52. cc.sys.localStorage.setItem('zsdk_deviceid', this.deviceId)
  53. if (CC_JSB && cc.sys.os == cc.sys.OS_ANDROID && jsb.reflection) {
  54. jsb.reflection.callStaticMethod("org.cocos2dx.javascript.AppActivity", "setDeviceId", "(Ljava/lang/String;)V", this.deviceId)
  55. }
  56. return this.deviceId
  57. }
  58. // ======================= video =======================
  59. // ======================= video =======================
  60. // ======================= video =======================
  61. isHaveVideo() {
  62. if (this.currSDK && this.currSDK.isHaveVideo) {
  63. return this.currSDK.isHaveVideo()
  64. }
  65. return false
  66. }
  67. //激励视频
  68. showVideoAd(succeed: Function, fail: Function) {
  69. console.log("showVideoAd>>")
  70. this.videoSucceed = succeed
  71. this.videoFail = fail
  72. if (window["TTSliderBarAward"] == 1) {
  73. window["TTSliderBarAward"] = 0
  74. sdk.onVideoSucceed()
  75. return
  76. }
  77. if (this.currSDK && this.currSDK.showVideoAd) {
  78. this.currSDK.showVideoAd()
  79. return
  80. }
  81. sdk.onVideoFail()
  82. }
  83. /**
  84. * 视频成功回调
  85. */
  86. onVideoSucceed() {
  87. this.videoSucceed && this.videoSucceed()
  88. this.videoSucceed = null
  89. this.videoFail = null
  90. ADmultiTool.saveEcpm(sdk.ANDROID_AD_CHANNEL.CSJ)
  91. }
  92. //激励视频,安卓多渠道
  93. showVideoAdMulti(channel: string, succeed: Function, fail: Function) {
  94. console.log("showVideoAdMulti")
  95. if (CC_JSB && cc.sys.os == cc.sys.OS_ANDROID && jsb.reflection) {
  96. switch (channel) {
  97. case sdk.ANDROID_AD_CHANNEL.CSJ:
  98. android && android.showVideoAdCSJ()
  99. break;
  100. case sdk.ANDROID_AD_CHANNEL.YLH:
  101. android && android.showVideoAdYLH()
  102. break;
  103. case sdk.ANDROID_AD_CHANNEL.KS:
  104. android && android.showVideoAdKS()
  105. break;
  106. default:
  107. console.log("channel erro:" + channel)
  108. return;
  109. }
  110. this.videoSucceed = succeed
  111. this.videoFail = fail
  112. }
  113. }
  114. /**
  115. * 安卓渠道单独的回调
  116. * @param channel
  117. */
  118. onVideoSucceedMulti(channel: string) {
  119. this.videoSucceed && this.videoSucceed(channel)
  120. this.videoSucceed = null
  121. this.videoFail = null
  122. }
  123. /**
  124. * 视频失败回调
  125. */
  126. onVideoFail() {
  127. this.videoFail && this.videoFail()
  128. this.videoSucceed = null
  129. this.videoFail = null
  130. }
  131. showInterstitial() {
  132. if (this.currSDK && this.currSDK.showInterstitial) {
  133. this.currSDK.showInterstitial()
  134. }
  135. }
  136. share(parm?) {
  137. if (this.currSDK && this.currSDK.shareAppMessage) {
  138. this.currSDK.shareAppMessage(parm)
  139. }
  140. }
  141. }
  142. /**
  143. * 广告单价数据
  144. */
  145. class AdData {
  146. channel: string = ""
  147. price: number = 0 //单价
  148. private today_earning: { [key: number]: number } = {} //今日收入
  149. private count_today: { [key: number]: number } = {} //今天点击
  150. private count_month: { [key: number]: number } = {} //月点击
  151. constructor(data: AdData) {
  152. if (data) {
  153. this.channel = data.channel
  154. this.price = data.price
  155. this.today_earning = data.today_earning
  156. this.count_today = data.count_today
  157. this.count_month = data.count_month
  158. }
  159. }
  160. //添加数据
  161. addData(price: number) {
  162. let day = ZTime.getTimeYMD()
  163. let mon = ZTime.getTimeYM()
  164. this.price = price
  165. this.today_earning[day] = (this.today_earning[day] || 0) + price
  166. this.count_today[day] = (this.count_today[day] || 0) + 1
  167. this.count_month[mon] = (this.count_month[mon] || 0) + 1
  168. }
  169. getTodayEarning(): number {
  170. let day = ZTime.getTimeYMD()
  171. return this.today_earning[day] || 0
  172. }
  173. getCountToday(): number {
  174. let day = ZTime.getTimeYMD()
  175. return this.count_today[day] || 0
  176. }
  177. getCountMonth(): number {
  178. let mon = ZTime.getTimeYM()
  179. return this.count_month[mon] || 0
  180. }
  181. }
  182. window["sdk"] = sdk
  183. window["AdData"] = AdData