YZ_Tool_Xiaomi.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. import PlatUtils from "./PlatUtils";
  2. import { utils } from "./Utils";
  3. import YZ_Constant, { LevelStatus } from "./YZ_Constant";
  4. import YZ_LocalStorage from "./YZ_LocalStorage";
  5. const { ccclass, property } = cc._decorator;
  6. const ST_ServerUrl: string = "http://apps.youlesp.com/gss?";
  7. const POST_ServerUrl: string = "http://report.youlesp.com/gss?";
  8. let ST_DefaultServerConfig: string = "";
  9. const ST_NativeInsertAdClickTimes: string = "NativeInsertAdClickTimes";
  10. const ST_LastDateTime: string = "LastDateTime";
  11. const ST_InsertAdShowCounts: string = "NativeInsertAdShowCount"; //插屏显示次数
  12. const ST_BannerAdCloseCounts: string = "ST_BannerAdCloseCounts"; //banner广告关闭次数
  13. const ST_YZBannerShowCounts: string = "YZBannerShowCounts"; //banner广告关闭次数
  14. @ccclass
  15. export default class YZ_Tool_Xiaomi {
  16. _sysInfo: any = null;
  17. public get SysInfo() {
  18. return this._sysInfo || {};
  19. }
  20. _serverConfig: any = null;
  21. public get ServerConfig() {
  22. return this._serverConfig;
  23. }
  24. _nativeInsertAdClickTimes: number = 0;
  25. public get NativeInsertAdClickTimes() {
  26. return this._nativeInsertAdClickTimes;
  27. }
  28. _insertAdShowCounts: number = 0;
  29. /**
  30. * 插屏广告展示次数
  31. */
  32. public get insertAdShowCounts() {
  33. return this._insertAdShowCounts;
  34. }
  35. _bannerAdCloseCounts: number = 0;
  36. /**
  37. * banner关闭次数
  38. */
  39. public get bannerAdCloseCounts() {
  40. return this._bannerAdCloseCounts;
  41. }
  42. _yzBannerShowCounts: number = 0;
  43. /**
  44. * 自定义banner展示次数
  45. */
  46. public get yzBannerShowCounts() {
  47. return this._yzBannerShowCounts;
  48. }
  49. //设备UID
  50. _uid: string = "0";
  51. public get uid() {
  52. // if (this._service_uid != "0") return this._uid;
  53. // this._login();
  54. return "0";
  55. }
  56. //服务器返回UID
  57. _service_uid: string = "0";
  58. /**
  59. * 服务器返回UID
  60. */
  61. public get serviceId() {
  62. if (this._service_uid != "0") return this._service_uid;
  63. this.reportLogin();
  64. return "0";
  65. }
  66. _loginTime: number = 0;
  67. _loginInterval: number = 30;
  68. async _login() {
  69. let curTime: number = new Date().getTime();
  70. let interval: number = (curTime - this._loginTime) / 1000;
  71. if (interval > 0 && interval < this._loginInterval) {
  72. utils.showLog(`登录请求间隔小于:${this._loginInterval}秒`);
  73. return;
  74. }
  75. this._loginTime = curTime;
  76. let self = this;
  77. utils.showLog("vivo暂时不获取uid,uid全部为0");
  78. this._uid = "0";
  79. this.reportLogin();
  80. }
  81. _reportLoginTime: number = 0;
  82. _reportLoginInterval: number = 30;
  83. isReport: boolean = false;
  84. public countNativeInserClick() {
  85. this._nativeInsertAdClickTimes++;
  86. YZ_LocalStorage.setItem(utils.config.xiaomiConfig.appID + "_" + ST_NativeInsertAdClickTimes, this._nativeInsertAdClickTimes);
  87. }
  88. /**
  89. * 增加插屏显示的次数
  90. */
  91. public countInserShowCount() {
  92. this._insertAdShowCounts++;
  93. YZ_LocalStorage.setItem(utils.config.xiaomiConfig.appID + "_" + ST_InsertAdShowCounts, this._insertAdShowCounts);
  94. }
  95. /**
  96. * 增加banner的关闭次数
  97. */
  98. public countBannerCloseCount() {
  99. this._bannerAdCloseCounts++;
  100. YZ_LocalStorage.setItem(utils.config.xiaomiConfig.appID + "_" + ST_BannerAdCloseCounts, this._bannerAdCloseCounts);
  101. }
  102. /**
  103. * 增加自定义banner的显示次数
  104. */
  105. public countYzBannerShowCount() {
  106. this._yzBannerShowCounts++;
  107. YZ_LocalStorage.setItem(utils.config.xiaomiConfig.appID + "_" + ST_YZBannerShowCounts, this._yzBannerShowCounts);
  108. }
  109. /**
  110. * 上报登录接口获取UID
  111. */
  112. reportLogin() {
  113. if (this.isReport) return;
  114. this.isReport = true;
  115. let self = this;
  116. let curTime: number = new Date().getTime();
  117. let interval: number = (curTime - this._reportLoginTime) / 1000;
  118. if (interval > 0 && interval < this._reportLoginInterval) {
  119. utils.showLog(`上报登录获取UID小于:${this._reportLoginInterval}秒`);
  120. return;
  121. }
  122. this._reportLoginTime = curTime;
  123. let method = "m=login";
  124. let url: string = ST_ServerUrl + method + `&device_data=${encodeURI(JSON.stringify(this._sysInfo))}`;
  125. utils.commomHttpRequest(url, (ret, data) => {
  126. if (ret) {
  127. if (data) {
  128. let result = JSON.parse(data);
  129. utils.showLog("data=" + data);
  130. utils.showLog("result=" + result);
  131. utils.showLog("result.uid=" + result.uid);
  132. if (result.uid) {
  133. self._service_uid = "" + result.uid;
  134. utils.showLog("服务器请求登录成功! _service_uid=" + self._service_uid);
  135. YZ_LocalStorage.setItem(YZ_Constant.ST_SERVICE_UID, self._service_uid);
  136. }
  137. }
  138. } else {
  139. utils.showLog("获取数据失败1");
  140. }
  141. this.isReport = false;
  142. })
  143. }
  144. /**
  145. *
  146. * @param data 配置数据
  147. */
  148. public init(data: string) {
  149. if (PlatUtils.IsXiaoMi) {
  150. if (data) {
  151. let configObj: any = JSON.parse(data);
  152. if (configObj && configObj.xiaomi) {
  153. ST_DefaultServerConfig = JSON.stringify(configObj.xiaomi);
  154. }
  155. }
  156. this._service_uid = YZ_LocalStorage.getItem(YZ_Constant.ST_SERVICE_UID);
  157. this._service_uid = this._service_uid ? this._service_uid : "0";
  158. //@ts-ignore
  159. if (qg.getUpdateManager) {
  160. //@ts-ignore
  161. const updateManager = qg.getUpdateManager()
  162. updateManager.onCheckForUpdate(function (res) {
  163. // 请求完新版本信息的回调
  164. utils.showLog('onCheckForUpdate', res.hasUpdate)
  165. })
  166. updateManager.onUpdateReady(function () {
  167. // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启
  168. updateManager.applyUpdate()
  169. })
  170. updateManager.onUpdateFailed(function () {
  171. // 新版本下载失败
  172. utils.showMsg("自动更新失败,请手动重启游戏!");
  173. })
  174. }
  175. this.getSystemInfo();
  176. this._loadConfig();
  177. }
  178. }
  179. _loadConfig() {
  180. if (PlatUtils.IsXiaoMi) {
  181. let method: string = "m=g";
  182. let url: string = ST_ServerUrl + method
  183. utils.commomHttpRequest(url, (ret, data) => {
  184. if (ret) {
  185. utils.showLog("小米服务器配置数据获取成功: data = " + data);
  186. if (data) {
  187. let result = JSON.parse(data);
  188. if (result) {
  189. if (!utils.DebugLoacalConfig) {
  190. this._serverConfig = result;
  191. if (this._serverConfig.is_show_log_view && this._serverConfig.is_show_log_view == "true") {
  192. utils.showLogView = true;
  193. }
  194. } else {
  195. utils.showLog("开启了本地数据测试,使用本地配置!");
  196. }
  197. } else {
  198. utils.showLog("小米服务器配置数据不是合法的JSON数据, 使用本地配置!");
  199. }
  200. }
  201. } else {
  202. utils.showLog("小米服务器配置数据获取失败, 使用本地配置!");
  203. }
  204. if (!this._serverConfig) {
  205. this._serverConfig = JSON.parse(ST_DefaultServerConfig);
  206. } else {
  207. if (this._serverConfig.is_local_pos_id
  208. && this._serverConfig.is_local_pos_id == "false") {
  209. // 使用服务器下发的广告id
  210. utils.showLog("使用服务器下发的广告id");
  211. utils.config.xiaomiConfig.insertId = this._serverConfig.intersititia_pos_id;
  212. utils.config.xiaomiConfig.videoId = this._serverConfig.video_pos_id;
  213. utils.config.xiaomiConfig.nativeInsertIds = this.ServerConfig.native_intersititial_pos_id;
  214. utils.config.xiaomiConfig.nativeBannerIds = this.ServerConfig.native_banner_pos_id;
  215. utils.config.xiaomiConfig.nativeTryGameIds = this.ServerConfig.native_trygame_pos_id;
  216. utils.config.xiaomiConfig.bannerId = this._serverConfig.banner_pos_id;
  217. } else {
  218. utils.showLog("使用本地配置的广告ID");
  219. }
  220. if (this.ServerConfig.native_banner_configs) {
  221. for (let i = 0; i < this.ServerConfig.native_banner_configs.length; i++) {
  222. utils.showLog("获取到原生广告配置:" + this.ServerConfig.native_banner_configs[i].location, ">>>>", JSON.stringify(this.ServerConfig.native_banner_configs[i]));
  223. utils.config.xiaomiConfig.setNativeBannerInfo(this.ServerConfig.native_banner_configs[i].location, this.ServerConfig.native_banner_configs[i]);
  224. }
  225. }
  226. }
  227. utils.emitServerInitEvent();
  228. });
  229. }
  230. }
  231. /**
  232. * 比较当前平台版本是否高于指定的版本号
  233. * @param miniVersion 最低平台版本号
  234. */
  235. public isOverMiniVersion(miniVersion: string) {
  236. if (PlatUtils.IsXiaoMi) {
  237. if (this._sysInfo && this._sysInfo.platformVersionCode) {
  238. return this._sysInfo.platformVersionCode >= miniVersion;
  239. }
  240. }
  241. return false;
  242. }
  243. /**
  244. * 上报关卡数据
  245. * @param level 当前关卡ID
  246. * @param levelName 关卡名称
  247. * @param status 状态
  248. */
  249. public postLevel(level: string, status: LevelStatus, levelName?: string) {
  250. if (PlatUtils.IsXiaoMi) {
  251. let method = "m=rlevel";
  252. let url: string = POST_ServerUrl + method + `&level_id=${level}&level_name=${encodeURI(levelName)}&status=${status}`;
  253. utils.commomHttpRequest(url, function (ret, data) {
  254. if (ret) {
  255. utils.showLog("关卡数据上报成功!");
  256. } else {
  257. utils.showLog("关卡数据上报失败!");
  258. }
  259. }.bind(this));
  260. }
  261. }
  262. /**
  263. * 上报自定义事件
  264. * @param level 当前关卡ID
  265. * @param levelName 关卡名称
  266. * @param status 状态
  267. */
  268. public sendEvent(eventName: string) {
  269. if (PlatUtils.IsXiaoMi) {
  270. let method = "m=revent";
  271. let url: string = POST_ServerUrl + method + `&event=${encodeURI(eventName)}`;
  272. utils.commomHttpRequest(url, function (ret, data) {
  273. if (ret) {
  274. utils.showLog("上报自定义事件成功!");
  275. } else {
  276. utils.showLog("上报自定义事件失败!");
  277. }
  278. }.bind(this));
  279. }
  280. }
  281. /**
  282. * 获取系统信息
  283. */
  284. public getSystemInfo() {
  285. if (PlatUtils.IsXiaoMi) {
  286. //@ts-ignore
  287. this._sysInfo = qg.getSystemInfoSync();
  288. utils.showLog("获取系统信息成功:" + JSON.stringify(this._sysInfo));
  289. }
  290. }
  291. public GameExit() {
  292. if (PlatUtils.IsXiaoMi) {
  293. //@ts-ignore
  294. //可以退出游戏
  295. qg.exitApplication({
  296. success: () => {
  297. utils.showLog("exit Game success");
  298. },
  299. fail: () => {
  300. utils.showLog("exit Game fail");
  301. }
  302. });
  303. }
  304. }
  305. }