SDK.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. // Learn TypeScript:
  2. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
  3. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
  4. // Learn Attribute:
  5. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
  6. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
  7. // Learn life-cycle callbacks:
  8. // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
  9. // - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.htmlr
  10. import { EncryptType, EncryptUtil } from "./tools/EncryptUtil";
  11. import Utils, { LoaderListener } from "./tools/Utils";
  12. import { InterstitialType } from "./vivo/Interstitial";
  13. import { NativeType } from "./vivo/NativeAdComponent";
  14. import VivoAd from "./vivo/VivoAd";
  15. let keyS = ["eggplant_fwzoowni#","2021xianYou","Cm_touchmi"];
  16. const { ccclass, property } = cc._decorator;
  17. //广告节点
  18. export enum NodeAction {
  19. GAME_LOADING_IN_MAIN_UI = 1,//加载界面进入游戏
  20. GAME_ENTER_LEVEL,//进入关卡
  21. GAME_UNLOCK_LEVEL,//解锁关卡
  22. BANNER_SHOW,//banner展示
  23. BANNER_CLICK,//banner点击
  24. BANNER_CLOSE,//banner关闭
  25. INSERT_SHOW,//插屏展示
  26. INSERT_CLICK,//插屏点击
  27. INSERT_CLOSE,//插屏关闭
  28. VIDEO_SHOW,//视频展示
  29. VIDEO_CLOSE,//视频关闭
  30. VIDER_CLICK,//视频点击
  31. NATIVE_SHOW,//原生展示13
  32. NATIVE_CLICK,//原生点击14
  33. NATIVE_CLOSE,//原生关闭15
  34. INIT //16
  35. }
  36. @ccclass
  37. export default class SDK extends cc.Component implements LoaderListener {
  38. public _type: number = 1;
  39. private _config;r
  40. private static _instance: SDK = null;
  41. private _gameCode = null;//游戏id
  42. private _gameTime = 0;
  43. private _clipboardData = null;
  44. private _initCallbacks = null;
  45. public limitClick = 0;
  46. private _random = -1;
  47. public fristGame = false;
  48. private _levelTime = 0;
  49. private _utils: Utils;
  50. private _userId;
  51. private _source;
  52. private _sysytemInfo;
  53. private _netType;
  54. private _isVivo = false;
  55. private _netInitCount = 0;
  56. private _loading = false;
  57. public edType ;
  58. public ivKey=null;
  59. public static get Instance(): SDK {
  60. if (SDK._instance == null) {
  61. SDK._instance = new SDK();
  62. }
  63. return SDK._instance;
  64. }
  65. constructor() {
  66. super();
  67. if (cc.sys.platform == cc.sys.VIVO_GAME) {
  68. this._isVivo = true;
  69. }
  70. console.log("当前平台: " + cc.sys.platform + " " + cc.sys.VIVO_GAME +" "+ this._isVivo );
  71. this._utils = Utils.instance;
  72. if (!this._isVivo) {
  73. return;
  74. }
  75. VivoAd.Instance.onListenHome();
  76. VivoAd.Instance.init();
  77. this._userId = VivoAd.Instance.getUserId();
  78. this._source = VivoAd.Instance.getSource();
  79. let self = this;
  80. this._sysytemInfo = VivoAd.Instance.getSystemInfoSync();
  81. VivoAd.Instance.getNetType((netType) => {
  82. self._netType = netType;
  83. });
  84. }
  85. public startTime() {
  86. this._levelTime = new Date().getTime();
  87. }
  88. public endLevelTime(level: number) {
  89. let curTime = new Date().getTime();
  90. let time = curTime - this._levelTime;
  91. this.durationLog(Math.floor(time / 1000), level);
  92. }
  93. private requestConfig() {
  94. if (!this._isVivo) {
  95. return;
  96. }
  97. let self = this;
  98. this._utils.log(self._netType);
  99. VivoAd.Instance.getClipboardData((clipboard) => {
  100. self._clipboardData = clipboard;
  101. let key = keyS[this.edType];
  102. let bady = {
  103. key: key,
  104. gameCode: self._gameCode,
  105. clipboard: clipboard,
  106. manuId: self._type,
  107. openId: self._userId,
  108. source: JSON.stringify(self._source.query),
  109. platformVersionCode: self._sysytemInfo == null ? 0 : self._sysytemInfo.platformVersionCode,
  110. netType: self._netType,
  111. dateTime: new Date().getTime()
  112. }
  113. let body = JSON.stringify(bady);
  114. // this._utils.log(body);
  115. let u = ["pz","gg","cnf"];
  116. this._utils.sendPost(u[self.edType], body);
  117. });
  118. }
  119. /**日志上报
  120. * @param type 上报日志类型
  121. */
  122. public reportLog(type: number, content?: any) {
  123. if (!this._isVivo) {
  124. return;
  125. }
  126. let self = this;
  127. VivoAd.Instance.getClipboardData((clipboard) => {
  128. self._clipboardData = clipboard;
  129. let key = keyS[this.edType];
  130. let bady = {
  131. key: key,
  132. gameCode: self._gameCode,
  133. clipboard: clipboard,
  134. manuId: self._type,
  135. openId: self._userId,
  136. type: type,
  137. content: content,
  138. source: JSON.stringify(self._source.query),
  139. dateTime: new Date().getTime()
  140. }
  141. let u = ["rz","sb","rpt1"];
  142. this._utils.sendPost(u[self.edType], JSON.stringify(bady));
  143. });
  144. }
  145. /**游戏上报
  146. * @param secondes 上报日志类型
  147. */
  148. public durationLog(seconds: number, level: any = 0) {
  149. if (!this._isVivo) {
  150. return;
  151. }
  152. let self = this;
  153. let random = -1;
  154. if (seconds > 0) {
  155. random = this._random;
  156. }
  157. let key = keyS[this.edType];
  158. let bady = {
  159. key: key,
  160. gameCode: self._gameCode,
  161. clipboard: self._clipboardData,
  162. manuId: self._type,
  163. openId: self._userId,
  164. level: level,
  165. seconds: seconds,
  166. source: JSON.stringify(self._source.query),
  167. random: random,
  168. dateTime: new Date().getTime()
  169. }
  170. let u = ["sc","zq","rpt2"];
  171. this._utils.sendPost(u[self.edType], JSON.stringify(bady));
  172. }
  173. index = 0;
  174. /**
  175. * sdk初始化
  176. * @param gameCode 游戏id,为服务端分配
  177. * @param callbacks 请求回调 ,返回true请求成功,false请求失败
  178. * @param debug 调试模式 默认false
  179. * @returns
  180. */
  181. public init(gameCode,type:EncryptType, callbacks?) {
  182. this._initCallbacks = callbacks;
  183. this.edType = type;
  184. // console.log("=============init=========type: "+type);
  185. // if(type == 0){
  186. // console.log("=============指创=========");
  187. // }else if(type == 1){
  188. // console.log("=============闲游=========");
  189. // }else if(type == 2){
  190. // console.log("=============触米=========");
  191. // }
  192. // this.edType = EncryptType.ED_CM;
  193. // let de = EncryptUtil.de("961JU3BY4DjgsEsvjbH1VBWoxQLcT9dfvgGThac0WLgxl72I7V3+b3Uxcs3adY8gzkPvmsBxT4Os5hu9YwlqZXS6A+NA1HbZCjGdwqRHWZPOKWtOhCnKz1Pe+41YpGGj+9GKjBK5TTgKaZRSvQ9HznLtOT7RSLUZqaIdnSGObmI8AKMYcYnx4PAa9q/I5ksA5lB8/yWo1i6IR7hZxK3qXYPfDu9cD5IutZxvqm5xzxJOnjoiIvRCceKmxpqd66AumUjLGAcV2mBMMUe0B5NrO/X1bWUQgi2yKE6EB0ipQE9A7921RANGfTax5zIpSfnv8gLiv8/fc7aDoxFrVpPIhUyBp18G5kVXOwNftWje+4s0dTc67HKSXTEEMBbWIhSqfe5/Nrp306Ls8QsqO50L3WNCfEC+THO5XQgn57AN5X0j2FSz7Exy1fdscRFAf2q4Zx7R9NEMBnaDoxFrVpPIgQ0u4UQOeRSg7W/nQgRYhW0j2FSz7Exy3rToMyhSJesZqaIdnSGObmI8AKMYcYnx4PAa9q/I5ksA5lB8/yWo1i6IR7hZxK3qXYPfDu9cD5Irt3RlAGZCE7EO0auILwjWEU/QovnDWQ+Ng98O71wPkiEv31hBKtl9FNWSzD5W93HcDzUSiQz1irsEXvTx9t7m95U5Rex0B121a0BvjXmhTQiAOfhB+pVUXwkoJol/0gCBvO8RHqt7exx6XqgOxXAujWip285CirBY9CFOJ/tqxQAQSUaGUjFBwiZmiGzrKcT0PSEyYOvZdJkE1KFziq1+mwbLd9pa6vxAsS9XscBkB4OxA0tJkvk2g9sL4eyHHZmEFScfIMeTcJkJPHgxbNLOcEChcfQmIii3z0y2D6PwBm8JKCaJf9IAgDkci7sTftYMbTK9rLir8vJkjHiLQcxFIbzvER6re3scel6oDsVwLo1oqdvOQoqwWos3giRKK3UJlWj2zdEEu4sYvcfqEqZ655sGCl96/upxraKdWT6ToZyeTNJ+WQkjE426FLcuWc/xgV9dRmnQWy2OWwbd0Cii26yZlSKBRPSn/eAtmqX1D7DOQ++awHFPhzV2zakldbpcWrzj0NtQXyexdz/gt25eaQqlLcrTDPuuh2nUL7SKZLET28fSVMvwGxt/uXy2IdBBjW9l/GHWh5bx0XlWHIJfLZay2hRwOBHnaYRXBj9jyCAyGQn7Kzbsh4Uf2C3K3mJ45yZTMLFMDUQVqMUC3E/XUFaJp6XWZwBRPC7qG9aomgmUbnuVJToziBjQb7AiD939KiAXt7qh3/RgB3U4+ioxycRaYuNvMgjcRthfoVguHRt1G/yLCqyIRKWxMLNnxwQrxD1C1xS+iojp6m+OhdQ1mjaA6q9wcf6CEN+A6FbDVnBIJyVeLC1AdTUs7H53DApXn7RaSL/R5bdK7N8VJ0xh0B1pn2DNUCiIqBwmf+Vg40QVqMUC3E/XWaaiEuqhB/GjhXFMcYfeTuAWciCGbM2Rs=");
  194. // console.log("=============触米de: "+de);
  195. if (!this._isVivo) {
  196. if (callbacks) {
  197. callbacks(true);
  198. }
  199. return;
  200. }
  201. let self = this;
  202. if (this._random <= 0) {
  203. //随机8位
  204. this._random = this._utils.getRandomInt(0, 10000);
  205. }
  206. this._gameCode = gameCode;
  207. this._utils.addListener(this);
  208. this.requestConfig();
  209. self._gameTime = new Date().getTime();
  210. VivoAd.Instance.onHide(() => {
  211. let curTime = new Date().getTime();
  212. let time = curTime - self._gameTime;
  213. self.durationLog(Math.floor(time / 1000), 0);
  214. self._gameTime = 0;
  215. this._utils.log("********用户退出游戏或退到后台: " + (time / 1000));
  216. });
  217. VivoAd.Instance.onShow(() => {
  218. self._gameTime = new Date().getTime();
  219. this._utils.log("*******用户回到游戏");
  220. });
  221. // this.index++;
  222. // if(this.index>2){
  223. // this.index = 0;
  224. // }
  225. // this.scheduleOnce(()=>{
  226. // SDK.Instance.init(gameCode,this.index,null);
  227. // },10);
  228. }
  229. //自动刷新广告
  230. private flushConfig() {
  231. if (!this._isVivo) {
  232. return;
  233. }
  234. let self = this;
  235. let go = () => {
  236. if (self._loading) {
  237. self.unschedule(go);
  238. return;
  239. }
  240. if (self._netInitCount > 2) {
  241. self._utils.ok(null);
  242. self.unschedule(go);
  243. return;
  244. }
  245. self._netInitCount++;
  246. self.requestConfig();
  247. }
  248. this.schedule(go, 10);
  249. }
  250. //监听网络回调
  251. onOk(config: any) {
  252. if (!this._isVivo) {
  253. return;
  254. }
  255. this._config = config;
  256. let self = this;
  257. if (this._config) {
  258. this._loading = true;
  259. VivoAd.Instance.initConfig(this._config, () => {
  260. if (self._initCallbacks) {
  261. self._initCallbacks(true);
  262. }
  263. });
  264. } else {
  265. self._utils.log("********* initConfig is faill : " + JSON.stringify(this._config));
  266. if (self._initCallbacks && !this._loading) {
  267. self._initCallbacks(false);
  268. this.flushConfig();
  269. }
  270. }
  271. }
  272. /**
  273. * 展示banner
  274. * @param type 原生广告类型
  275. * @param top 是否展示顶部
  276. * @param x
  277. * @param y
  278. */
  279. public showBanner(type: NativeType = NativeType.BANNER, top: boolean = false, y: number = 0, x: number = 0) {
  280. if (!this._isVivo) {
  281. return;
  282. }
  283. this.hideBanner();
  284. VivoAd.Instance.showBanner(type, top, y, x);
  285. }
  286. /**
  287. * 隐藏banner
  288. */
  289. public hideBanner() {
  290. if (!this._isVivo) {
  291. return;
  292. }
  293. VivoAd.Instance.hideBanner();
  294. }
  295. public hideIcon() {
  296. if (!this._isVivo) {
  297. return;
  298. }
  299. VivoAd.Instance.hideIcon();
  300. }
  301. /**
  302. * 展示原生插屏
  303. * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参
  304. * @param node 默认0
  305. * @param isforever true时,每次调用都会展示,false时,受服务器调用次数控制,默认false
  306. * @param id
  307. */
  308. public showInterstitial(id: InterstitialType = InterstitialType.INTERSTITIAL_GAME_OVER) {
  309. if (!this._isVivo) {
  310. return;
  311. }
  312. VivoAd.Instance.showInterstitial(id);
  313. }
  314. /**
  315. * 展示原生icon广告
  316. * 默认id节点为第一个0,有多个时,按服务端配置为准,进行传参
  317. * @param node
  318. * @param x
  319. * @param y
  320. * @param id
  321. */
  322. public showIconAd(x: number, y: number, id: number = 0) {
  323. if (!this._isVivo) {
  324. return;
  325. }
  326. this.hideIcon();
  327. VivoAd.Instance.showIconAd(id, x, y);
  328. }
  329. /*0显示视频*/
  330. public showRewardVideo(callBacks: Function, fail: Function, noAd?: Function) {
  331. console.log('a');
  332. if (!this._isVivo) {
  333. console.log('b');
  334. return;
  335. }
  336. console.log('c');
  337. VivoAd.Instance.showRewardVideo(callBacks, fail, noAd);
  338. console.log('d');
  339. }
  340. //toast提示框
  341. public showToast(message: string) {
  342. if (!this._isVivo) {
  343. return;
  344. }
  345. if (VivoAd.Instance) {
  346. VivoAd.Instance.showToast(message);
  347. }
  348. }
  349. /**
  350. * 游戏结算界面数分延迟
  351. * @param lable
  352. * @param score
  353. * @param cb
  354. */
  355. public gameResultDelayedTime(lable: cc.Label, score: number, cb: Function) {
  356. if (this._config && score > 0) {
  357. let dalayedTime = this._config.maxClickCount;
  358. if (dalayedTime > 0) {
  359. let count = 0;
  360. let s = Math.floor((dalayedTime / score) * 100) / 100;
  361. let add = score / 200;
  362. if (add < 1) {
  363. add = 1;
  364. }
  365. this._utils.log("----time: " + dalayedTime + " s: " + s + " add: " + add);
  366. let go = () => {
  367. count = Math.floor(count + add);
  368. if (count > score) {
  369. this.unschedule(go);
  370. lable.string = "" + score
  371. if (cb) {
  372. cb();
  373. }
  374. return;
  375. }
  376. if (lable) {
  377. lable.string = "" + count
  378. }
  379. }
  380. this.schedule(go, s);
  381. } else {
  382. lable.string = "" + score
  383. if (cb) {
  384. this.scheduleOnce(() => {
  385. cb();
  386. }, 0.2);
  387. }
  388. }
  389. } else {
  390. lable.string = "" + score
  391. if (cb) {
  392. this.scheduleOnce(() => {
  393. cb();
  394. }, 0.2);
  395. }
  396. }
  397. }
  398. }
  399. cc["SDK"] = SDK.Instance;