YZ_Tool_FaceBook.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. import { QQConfig } from "./CommonConfig";
  2. import PlatUtils from "./PlatUtils";
  3. import { utils } from "./Utils";
  4. import YZ_Constant, { LevelStatus } from "./YZ_Constant";
  5. import YZ_LocalStorage from "./YZ_LocalStorage";
  6. // const i18n = require('LanguageData');
  7. const { ccclass, property } = cc._decorator;
  8. const ST_ServerUrl: string = "https://apps.youlesp.com/gss?";
  9. const POST_ServerUrl: string = "https://report.youletd.com/gss?";
  10. let ST_DefaultServerConfig: string = "";
  11. @ccclass
  12. export default class YZ_Tool_FaceBook {
  13. //@ts-ignore
  14. // hg: any = hg;
  15. _sysInfo: any = null;
  16. public get SysInfo() {
  17. return this._sysInfo;
  18. }
  19. _serverConfig: any = null;
  20. public get ServerConfig() {
  21. return this._serverConfig;
  22. }
  23. /**
  24. * 当前版本号
  25. */
  26. public gameVersion(): string {
  27. return utils.config.faceBookConfig.version;
  28. }
  29. //设备UID
  30. _uid: string = "0";
  31. public get uid() {
  32. // if (this._service_uid != "0") return this._uid;
  33. // this._login();
  34. return "0";
  35. }
  36. //服务器返回UID
  37. _service_uid: string = "0";
  38. /**
  39. * 服务器返回UID
  40. */
  41. public get serviceId() {
  42. if (this._service_uid != "0") return this._service_uid;
  43. this.reportLogin();
  44. return "0";
  45. }
  46. _loginTime: number = 0;
  47. _loginInterval: number = 30;
  48. async _login() {
  49. let curTime: number = new Date().getTime();
  50. let interval: number = (curTime - this._loginTime) / 1000;
  51. if (interval > 0 && interval < this._loginInterval) {
  52. utils.showLog(`登录请求间隔小于:${this._loginInterval}秒`);
  53. return;
  54. }
  55. this._loginTime = curTime;
  56. let self = this;
  57. utils.showLog("facebook暂时不获取uid,uid全部为0");
  58. this._uid = "0";
  59. this.reportLogin();
  60. }
  61. _reportLoginTime: number = 0;
  62. _reportLoginInterval: number = 30;
  63. isReport: boolean = false;
  64. /**
  65. * 上报登录接口获取UID
  66. */
  67. reportLogin() {
  68. if (this.isReport) return;
  69. this.isReport = true;
  70. let self = this;
  71. let curTime: number = new Date().getTime();
  72. let interval: number = (curTime - this._reportLoginTime) / 1000;
  73. if (interval > 0 && interval < this._reportLoginInterval) {
  74. utils.showLog(`上报登录获取UID小于:${this._reportLoginInterval}秒`);
  75. return;
  76. }
  77. this._reportLoginTime = curTime;
  78. let method = "m=login";
  79. let url: string = ST_ServerUrl + method + `&device_data=${encodeURI(JSON.stringify(this._sysInfo))}`;
  80. utils.commomHttpRequest(url, (ret, data) => {
  81. if (ret) {
  82. if (data) {
  83. let result = JSON.parse(data);
  84. utils.showLog("data=" + data);
  85. utils.showLog("result=" + result);
  86. utils.showLog("result.uid=" + result.uid);
  87. if (result.uid) {
  88. self._service_uid = "" + result.uid;
  89. utils.showLog("服务器请求登录成功! _service_uid=" + self._service_uid);
  90. YZ_LocalStorage.setItem(YZ_Constant.ST_SERVICE_UID, self._service_uid);
  91. }
  92. }
  93. } else {
  94. utils.showLog("获取数据失败1");
  95. }
  96. this.isReport = false;
  97. })
  98. }
  99. /**
  100. *
  101. * @param data 配置数据
  102. */
  103. public init(data: string) {
  104. utils.showLog("init faceBook");
  105. if (PlatUtils.IsFaceBook) {
  106. if (data) {
  107. let configObj: any = JSON.parse(data);
  108. if (configObj && configObj.facebook) {
  109. ST_DefaultServerConfig = JSON.stringify(configObj.facebook);
  110. }
  111. }
  112. this._service_uid = YZ_LocalStorage.getItem(YZ_Constant.ST_SERVICE_UID);
  113. this._service_uid = this._service_uid ? this._service_uid : "0";
  114. this._loadConfig();
  115. }
  116. }
  117. _loadConfig() {
  118. if (PlatUtils.IsFaceBook) {
  119. let method: string = "m=g";
  120. let url: string = ST_ServerUrl + method
  121. utils.commomHttpRequest(url, (ret, data) => {
  122. if (ret) {
  123. utils.showLog("faceBook服务器配置数据获取成功: data = " + data);
  124. if (data) {
  125. let result = JSON.parse(data);
  126. if (result) {
  127. if (!utils.DebugLoacalConfig) {
  128. this._serverConfig = result;
  129. if (this._serverConfig.is_show_log_view && this._serverConfig.is_show_log_view == "true") {
  130. utils.showLogView = true;
  131. }
  132. } else {
  133. utils.showLog("开启了本地数据测试,使用本地配置!");
  134. }
  135. } else {
  136. utils.showLog("faceBook服务器配置数据不是合法的JSON数据, 使用本地配置!");
  137. }
  138. }
  139. } else {
  140. utils.showLog("facebook服务器配置数据获取失败, 使用本地配置!");
  141. }
  142. if (!this._serverConfig) {
  143. this._serverConfig = JSON.parse(ST_DefaultServerConfig);
  144. } else {
  145. if (this._serverConfig.is_local_pos_id
  146. && this._serverConfig.is_local_pos_id == "false") {
  147. // 使用服务器下发的广告id
  148. utils.showLog("使用服务器下发的广告id");
  149. utils.config.faceBookConfig.videoId = this._serverConfig.video_pos_id || utils.config.faceBookConfig.videoId;
  150. utils.config.faceBookConfig.insertId = this._serverConfig.intersititia_pos_id || utils.config.faceBookConfig.insertId;
  151. utils.config.faceBookConfig.bannerId = this._serverConfig.banner_pos_id || utils.config.faceBookConfig.bannerId;
  152. } else {
  153. utils.showLog("使用本地配置的广告ID");
  154. }
  155. }
  156. utils.emitServerInitEvent();
  157. });
  158. }
  159. }
  160. /**
  161. * 比较当前平台版本是否高于指定的版本号
  162. * @param miniVersion 最低平台版本号
  163. */
  164. public isOverMiniVersion(miniVersion: string) {
  165. if (PlatUtils.IsFaceBook) {
  166. if (this._sysInfo && this._sysInfo.platformVersionCode) {
  167. return this._sysInfo.platformVersionCode >= miniVersion;
  168. }
  169. }
  170. return false;
  171. }
  172. /**
  173. * 上报关卡数据
  174. * @param level 当前关卡ID
  175. * @param levelName 关卡名称
  176. * @param status 状态
  177. */
  178. public postLevel(level: string, status: LevelStatus, levelName?: string) {
  179. if (PlatUtils.IsFaceBook) {
  180. let method = "m=rlevel";
  181. let url: string = POST_ServerUrl + method + `&level_id=${level}&level_name=${encodeURI(levelName)}&status=${status}`;
  182. utils.commomHttpRequest(url, function (ret, data) {
  183. if (ret) {
  184. utils.showLog("关卡数据上报成功!");
  185. } else {
  186. utils.showLog("关卡数据上报失败!");
  187. }
  188. }.bind(this));
  189. }
  190. }
  191. /**
  192. * 上报自定义事件
  193. * @param level 当前关卡ID
  194. * @param levelName 关卡名称
  195. * @param status 状态
  196. */
  197. public sendEvent(eventName: string) {
  198. if (PlatUtils.IsFaceBook) {
  199. let method = "m=revent";
  200. let url: string = POST_ServerUrl + method + `&event=${encodeURI(eventName)}`;
  201. utils.commomHttpRequest(url, function (ret, data) {
  202. if (ret) {
  203. utils.showLog("上报自定义事件成功!");
  204. } else {
  205. utils.showLog("上报自定义事件失败!");
  206. }
  207. }.bind(this));
  208. }
  209. }
  210. }