YZ_Tool_Wifi.ts 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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. // 默认配置
  9. let ST_DefaultServerConfig: string = "";
  10. @ccclass
  11. export default class YZ_Tool_WiFi {
  12. /**
  13. * 服务器配置信息
  14. */
  15. _serverConfig: any = null;
  16. public get ServerConfig() {
  17. return this._serverConfig;
  18. }
  19. _sysInfo: any = {};
  20. public get SysInfo() {
  21. return this._sysInfo;
  22. }
  23. //设备UID
  24. _uid: string = "0";
  25. public get uid() {
  26. // if (this._service_uid != "0") return this._uid;
  27. // this._login();
  28. return "0";
  29. }
  30. /**
  31. * 当前版本号
  32. */
  33. public gameVersion(): string {
  34. return utils.config.wifiConfig.version;
  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 < 30) {
  52. utils.showLog(`登录请求间隔小于:${this._loginInterval}秒`);
  53. return;
  54. }
  55. this._loginTime = curTime;
  56. let self = this;
  57. utils.showLog("qq暂时不获取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 - self._reportLoginTime) / 1000;
  73. // utils.showLog(interval, " <<<<<<,interval", " _reportLoginTime >>>", self._reportLoginTime);
  74. // console.log(curTime, curTime - self._reportLoginTime, interval)
  75. if (interval > 0 && interval < 30) {
  76. utils.showLog(`上报登录获取UID小于:${self._reportLoginInterval}秒`);
  77. return;
  78. }
  79. self._reportLoginTime = curTime;
  80. let method = "m=login";
  81. let url: string = ST_ServerUrl + method + `&device_data=0`;
  82. utils.commomHttpRequest(url, (ret, data) => {
  83. if (ret) {
  84. if (data) {
  85. let result = JSON.parse(data);
  86. utils.showLog("data=" + data);
  87. utils.showLog("result=" + result);
  88. utils.showLog("result.uid=" + result.uid);
  89. if (result.uid) {
  90. self._service_uid = "" + result.uid;
  91. console.log("self._service_uid:" + self._service_uid)
  92. utils.showLog("服务器请求登录成功! _service_uid=" + self._service_uid);
  93. YZ_LocalStorage.setItem(YZ_Constant.ST_SERVICE_UID, self._service_uid);
  94. }
  95. }
  96. } else {
  97. utils.showLog("获取数据失败1");
  98. }
  99. this.isReport = false;
  100. })
  101. }
  102. /**
  103. *
  104. * @param data 配置数据
  105. */
  106. public init(data: string) {
  107. if (PlatUtils.IsWiFi) {
  108. if (data) {
  109. let configObj: any = JSON.parse(data);
  110. if (configObj && configObj.wifi) {
  111. ST_DefaultServerConfig = JSON.stringify(configObj.wifi);
  112. }
  113. }
  114. this._service_uid = YZ_LocalStorage.getItem(YZ_Constant.ST_SERVICE_UID);
  115. this._service_uid = this._service_uid ? this._service_uid : "0";
  116. try {
  117. //@ts-ignore
  118. this._sysInfo = wuji.getSystemInfoSync();
  119. utils.showLog("连尚 小游戏平台信息: ", JSON.stringify(this.SysInfo));
  120. } catch (e) {
  121. utils.showLog("连尚 小游戏平台数据获取失败!");
  122. }
  123. this._loadConfig();
  124. }
  125. }
  126. _loadConfig() {
  127. if (PlatUtils.IsWiFi) {
  128. let method: string = "m=g";
  129. utils.commomHttpRequest(ST_ServerUrl + method, (ret, data) => {
  130. if (ret) {
  131. utils.showLog("连尚服务器配置数据获取成功: data = ", data);
  132. if (data) {
  133. let result = JSON.parse(data);
  134. if (result) {
  135. if (!utils.DebugLoacalConfig) {
  136. this._serverConfig = result;
  137. } else {
  138. cc.warn("开启了本地数据测试,使用本地配置!");
  139. }
  140. } else {
  141. utils.showLog("连尚服务器配置数据不是合法的JSON数据, 使用本地配置!");
  142. }
  143. }
  144. } else {
  145. utils.showLog("连尚服务器配置数据获取失败, 使用本地配置!");
  146. }
  147. if (!this._serverConfig) {
  148. this._serverConfig = JSON.parse(ST_DefaultServerConfig);
  149. } else {
  150. // if (this._serverConfig.is_local_pos_id
  151. // && this._serverConfig.is_local_pos_id == "false") {
  152. // // 使用服务器下发的广告id
  153. // utils.showLog("使用服务器下发的广告id");
  154. // utils.config.qqconfig.bannerId = this._serverConfig.banner_pos_id;
  155. // utils.config.qqconfig.insertId = this._serverConfig.intersititia_pos_id;
  156. // utils.config.qqconfig.videoId = this._serverConfig.video_pos_id;
  157. // utils.config.qqconfig.boxId = this._serverConfig.box_pos_id;
  158. // utils.config.qqconfig.bannerBoxId = this._serverConfig.banner_box_pos_id;
  159. // } else {
  160. // utils.showLog("使用本地配置的广告ID");
  161. // }
  162. }
  163. utils.emitServerInitEvent();
  164. });
  165. }
  166. }
  167. public getSystemInfo() {
  168. if (PlatUtils.IsWiFi) {
  169. return this._sysInfo;
  170. }
  171. }
  172. /**
  173. * 上报数据
  174. */
  175. public postData(otherGameAppId: string) {
  176. if (PlatUtils.IsWiFi) {
  177. let appid: string = utils.config.wifiConfig.appID;
  178. let uid: string = "0";
  179. let channel: string = "qq";
  180. let url: string = `http://apps.youlesp.com/gs?m=jump&app_id=${appid}&uid=${uid}&channel=${channel}&jump_app_id=${otherGameAppId}`;
  181. utils.showLog("上报数据, url=", url);
  182. utils.commomHttpRequest(url, function (ret, data) {
  183. if (ret) {
  184. utils.showLog("数据上报成功!");
  185. } else {
  186. utils.showLog("数据上报失败!");
  187. }
  188. });
  189. }
  190. }
  191. public isOverMinVersion(minVersion: string) {
  192. let curVersion: string = this._sysInfo.SDKVersion;
  193. return this._compareVersion(curVersion, minVersion) >= 0;
  194. }
  195. _compareVersion(v1, v2) {
  196. if (!v1 || !v2) return -1;
  197. v1 = v1.split('.')
  198. v2 = v2.split('.')
  199. const len = Math.max(v1.length, v2.length)
  200. while (v1.length < len) {
  201. v1.push('0')
  202. }
  203. while (v2.length < len) {
  204. v2.push('0')
  205. }
  206. for (let i = 0; i < len; i++) {
  207. const num1 = parseInt(v1[i])
  208. const num2 = parseInt(v2[i])
  209. if (num1 > num2) {
  210. return 1
  211. } else if (num1 < num2) {
  212. return -1
  213. }
  214. }
  215. return 0
  216. }
  217. /**
  218. * 上报关卡数据
  219. * @param level 当前关卡ID
  220. * @param levelName 关卡名称
  221. * @param status 状态
  222. */
  223. public postLevel(level: string, status: LevelStatus, levelName?: string) {
  224. if (PlatUtils.IsWiFi) {
  225. let method = "m=rlevel";
  226. let url: string = POST_ServerUrl + method + `&level_id=${level}&level_name=${encodeURI(levelName)}&status=${status}`;
  227. utils.commomHttpRequest(url, function (ret, data) {
  228. if (ret) {
  229. utils.showLog("关卡数据上报成功!");
  230. } else {
  231. utils.showLog("关卡数据上报失败!");
  232. }
  233. }.bind(this));
  234. }
  235. }
  236. /**
  237. * 上报自定义事件
  238. * @param level 当前关卡ID
  239. * @param levelName 关卡名称
  240. * @param status 状态
  241. */
  242. public sendEvent(eventName: string) {
  243. if (PlatUtils.IsWiFi) {
  244. let method = "m=revent";
  245. let url: string = POST_ServerUrl + method + `&event=${encodeURI(eventName)}`;
  246. utils.commomHttpRequest(url, function (ret, data) {
  247. if (ret) {
  248. utils.showLog("上报自定义事件成功!");
  249. } else {
  250. utils.showLog("上报自定义事件失败!");
  251. }
  252. }.bind(this));
  253. }
  254. }
  255. /**
  256. * 弹出提示框
  257. * @param msg 消息
  258. */
  259. public showToast(msg: string) {
  260. if (PlatUtils.IsWiFi) {
  261. //@ts-ignore
  262. wuji.showToast({
  263. title: msg,
  264. icon: "none",
  265. duration: 2000
  266. })
  267. }
  268. }
  269. }