AdAgentNative.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import AdAgent from "./AdAgent";
  2. import { BannerLocation, BannerLocationToString } from "./YZ_Constant";
  3. import PlatUtils from "./PlatUtils";
  4. import { utils } from "./Utils";
  5. const { ccclass, property } = cc._decorator;
  6. const ST_JNIMessage: string = "JNIMessage";
  7. const ST_VideoCallback: string = "VideoCallback";
  8. @ccclass
  9. export default class AdAgentNative extends AdAgent {
  10. private get _className() {
  11. return utils.Tool_Native.jniClassName;
  12. }
  13. private _videoCallback: Function = null;
  14. public get ServerConfig() {
  15. return utils.Tool_Native.ServerConfig;
  16. }
  17. public Init() {
  18. if (PlatUtils.IsNativeAndroid) {
  19. cc.game.on(ST_JNIMessage, (event: any) => {
  20. if (event.type == ST_VideoCallback) {
  21. if (this._videoCallback) {
  22. if (event.ret == true) {
  23. this._videoCallback && this._videoCallback(true);
  24. } else {
  25. this._videoCallback && this._videoCallback(false, event.msg);
  26. }
  27. this._videoCallback = null;
  28. }
  29. }
  30. });
  31. }
  32. }
  33. _showBannerTimerId: number = 0;
  34. public ShowBanner(location: BannerLocation = BannerLocation.Home, args: any = null, isTimeRefresh: boolean = false) {
  35. if (PlatUtils.IsNativeAndroid) {
  36. utils.showLog("AdAgentNative ShowBanner");
  37. let interval: number = 60;
  38. if (this.ServerConfig && this.ServerConfig.refresh_ad_time) {
  39. interval = this.ServerConfig.refresh_ad_time;
  40. }
  41. let jsonObj: any = {};
  42. jsonObj.location = location;
  43. jsonObj.isTimeRefresh = isTimeRefresh ? "true" : "false";
  44. try {
  45. utils.showLog("调用banner Json >>>" + JSON.stringify(jsonObj));
  46. jsb.reflection.callStaticMethod(this._className, "showBanner", "(Ljava/lang/String;)V", JSON.stringify(jsonObj));
  47. } catch (error) {
  48. utils.showLog(error);
  49. }
  50. clearInterval(this._showBannerTimerId);
  51. this._showBannerTimerId = setInterval(function () {
  52. utils.showLog(`定时刷新显示Banner广告!location:${location}; args:${JSON.stringify(args)}; 间隔时间:${interval}`);
  53. this.ShowBanner(location, args, true);
  54. }.bind(this), interval * 1000);
  55. }
  56. }
  57. public HideBanner(location: BannerLocation = BannerLocation.Home) {
  58. utils.showLog("AdAgentNative HideBanner");
  59. clearInterval(this._showBannerTimerId);
  60. jsb.reflection.callStaticMethod(this._className, "hideBanner", "(Ljava/lang/String;)V", location);
  61. }
  62. public ShowInterstitial() {
  63. if (PlatUtils.IsNativeAndroid) {
  64. try {
  65. let delayShowTime = 0.5;
  66. if (this.ServerConfig && this.ServerConfig.intersititia_delay_show_time) {
  67. delayShowTime = this.ServerConfig.intersititia_delay_show_time;
  68. }
  69. utils.showLog("AdAgentNative ShowInterstitial 延迟", delayShowTime, "秒调用!");
  70. utils.delayCall(() => {
  71. jsb.reflection.callStaticMethod(this._className, "showInterstitial", "()V");
  72. }, delayShowTime);
  73. } catch (error) {
  74. utils.showLog(error);
  75. }
  76. }
  77. }
  78. _showNativeIconTimerId: number = 0;
  79. /**
  80. * 显示浮窗广告挂件
  81. * @param params
  82. * ```
  83. * {
  84. * group:string
  85. * left:number
  86. * bottom:number
  87. * scale:number
  88. * parent:cc.Node
  89. * }
  90. * ```
  91. * @returns 生成的组件
  92. */
  93. public showNativeTryGameWidget(params: any = null) {
  94. if (PlatUtils.IsNativeAndroid) {
  95. utils.showLog("AdAgentNative showNativeTryGameWidget=" + this.ServerConfig.show_native_icon_method);
  96. try {
  97. let interval: number = 15;
  98. if (this.ServerConfig && this.ServerConfig.icon_jump_native) {
  99. let iv = parseInt(this.ServerConfig.icon_jump_native);
  100. if (iv > 3) {
  101. interval = iv;
  102. } else {
  103. utils.showLog("悬浮ICON必须大于3秒,当前默认为15秒刷新");
  104. }
  105. }
  106. if (this.ServerConfig.show_native_icon_method) {
  107. let style: any = {};
  108. "left" in params && (style.left = params.left)
  109. "right" in params && (style.right = params.right)
  110. "top" in params && (style.top = params.top)
  111. "bottom" in params && (style.bottom = params.bottom)
  112. "location" in params && (style.location = params.location)
  113. style.winSizeWidth = cc.winSize.width;
  114. style.winSizeHeight = cc.winSize.height;
  115. // style.left = params.left != undefined ? params.left : -1;
  116. // style.right = params.right != undefined ? params.right : -1;
  117. // style.top = params.top != undefined ? params.top : -1;
  118. // style.bottom = params.bottom != undefined ? params.bottom : -1;
  119. // style.location = params.location != undefined ? params.location : -1;
  120. jsb.reflection.callStaticMethod(utils.Tool_Native.jniClassName, this.ServerConfig.show_native_icon_method, "(Ljava/lang/String;)V", JSON.stringify(style));
  121. } else {
  122. let x = 10;
  123. let y = 250;
  124. if (params) {
  125. if (params.top != null) {
  126. y = params.top;
  127. } else if (params.bottom != null) {
  128. y = cc.winSize.height - params.bottom;
  129. }
  130. if (params.left != null) {
  131. x = params.left;
  132. } else if (params.right != null) {
  133. x = cc.winSize.width - params.right;
  134. }
  135. }
  136. jsb.reflection.callStaticMethod(utils.Tool_Native.jniClassName, "showFloatIcon", "(II)V", x, y);
  137. }
  138. clearInterval(this._showNativeIconTimerId);
  139. this._showNativeIconTimerId = setInterval(function () {
  140. utils.showLog(`定时刷新显示原生悬浮ICON广告! args:${params}; 间隔时间:${interval}`);
  141. this.showNativeTryGameWidget(params);
  142. }.bind(this), interval * 1000);
  143. } catch (error) {
  144. utils.showLog(error);
  145. }
  146. }
  147. }
  148. public ShowVideo(callback: Function) {
  149. if (PlatUtils.IsNativeAndroid) {
  150. utils.showLog("AdAgentNative ShowVideo");
  151. this._videoCallback = callback;
  152. try {
  153. jsb.reflection.callStaticMethod(this._className, "showVideo", "()V");
  154. } catch (error) {
  155. utils.showLog(error);
  156. if (callback) {
  157. callback(false);
  158. }
  159. }
  160. }
  161. }
  162. public showFullScreenVideo(callback: Function) {
  163. if (PlatUtils.IsNativeAndroid) {
  164. utils.showLog("AdAgentNative showFullScreenVideo");
  165. this._videoCallback = callback;
  166. try {
  167. jsb.reflection.callStaticMethod(this._className, "showFullScreenVideo", "()V");
  168. } catch (error) {
  169. utils.showLog(error);
  170. if (callback) {
  171. callback(false);
  172. }
  173. }
  174. }
  175. }
  176. /**
  177. * 隐藏浮动试玩挂件
  178. */
  179. public hideNativeTryGameWidget() {
  180. if (PlatUtils.IsNativeAndroid) {
  181. clearInterval(this._showNativeIconTimerId);
  182. jsb.reflection.callStaticMethod(this._className, "hideFloatIcon", "()V");
  183. return;
  184. }
  185. }
  186. }
  187. export class NativeCallBack {
  188. /**
  189. * 1:播放完成
  190. * 2:播放失败
  191. * 3:无广告
  192. *
  193. */
  194. public static videoCallBack(result: number, msg?: string) {
  195. console.log("视频广告回调函数 ------>result=", result, " msg=", msg);
  196. if (result == 1) {
  197. //播放成功
  198. cc.game.emit(ST_JNIMessage, { type: ST_VideoCallback, ret: true });
  199. } else {
  200. //播放失败
  201. cc.game.emit(ST_JNIMessage, { type: ST_VideoCallback, ret: false, msg: msg ? msg : "暂无视频!" });
  202. }
  203. }
  204. public static sendEvent(eventMsg: string) {
  205. utils.showLog("事件上报:" + eventMsg);
  206. utils.SendEvent(eventMsg);
  207. }
  208. public static sendEventNew(eventName: string, eventId?: string, eventData?: string) {
  209. utils.showLog("事件上报:" + eventName);
  210. if (utils.Tool_Native) {
  211. utils.Tool_Native.sendEventNew(eventName, eventId, eventData)
  212. }
  213. }
  214. /**
  215. * 上报插屏点击时间
  216. */
  217. public static reportInsertClick() {
  218. utils.showLog("上报插屏点击时间:" + utils.overPageInsertAdIsTouch);
  219. if (utils.overPageInsertAdIsTouch) return;
  220. utils.overPageInsertAdIsTouch = true;
  221. let time = (new Date().getTime() - utils.overPageShowTime) / 1000;
  222. let json: any = {};
  223. json.data = time;
  224. utils.SendEventNew(`结算页面-插屏点击时间`, "overPageInsertAdTouch", JSON.stringify(json));
  225. }
  226. /**
  227. *
  228. * @param idCard
  229. * @param realName
  230. */
  231. public static realNameAuth(idCard, realName) {
  232. utils.showLog("realNameAuth>>>> #idCard=" + idCard + " #realName=" + realName);
  233. utils.Tool_Native.realNameAuth(idCard, realName, (res, result) => {
  234. utils.showLog("realNameAuth>>>> #res=" + res + " #result=" + result);
  235. if (res) {
  236. if (result) {
  237. let res = JSON.parse(result);
  238. switch (res.code) {
  239. case 1:
  240. utils.showMsg(res.msg);
  241. utils.Tool_Native.realNameAuthResult(result);
  242. break;
  243. case 0:
  244. if (res.nonage == "0") {
  245. utils.setRealNameAuthLocalData("2")
  246. } else {
  247. utils.setRealNameAuthLocalData("1")
  248. }
  249. utils._isRealNameAuth = true;
  250. if (res.msg) {
  251. utils.showMsg(res.msg);
  252. }
  253. utils.Tool_Native.realNameAuthResult(result);
  254. utils.scheduleOnce(() => {
  255. utils.emitRealNameAuthCloseEvent();
  256. }, 0.5);
  257. break;
  258. case 2:
  259. //未成年限制内,显示下线
  260. utils.setRealNameAuthLocalData("2");
  261. utils.Tool_Native.realNameAuthResult(result);
  262. break;
  263. }
  264. } else {
  265. let result: any = {};
  266. result.code = "-1";
  267. result.msg = "请求失败,请重新提交验证!"
  268. utils.Tool_Native.realNameAuthResult(JSON.stringify(result))
  269. utils.showMsg("请求失败,请重新提交验证!")
  270. }
  271. } else {
  272. utils.showMsg("请求失败,请重新提交验证!")
  273. let result: any = {};
  274. result.code = "-1";
  275. result.msg = "请求失败,请重新提交验证!"
  276. utils.Tool_Native.realNameAuthResult(JSON.stringify(result))
  277. }
  278. });
  279. }
  280. }
  281. cc["NativeCallBack"] = NativeCallBack;