YZ_Tool_Cocosplay.ts 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 GameSDK = require('./GameSDK.js');
  7. // const AdSDK = require('./AdSDK.js');
  8. let ST_DefaultServerConfig: string = "";
  9. const ST_ServerUrl: string = "http://apps.youlesp.com/gss?";
  10. const POST_ServerUrl: string = "http://report.youlesp.com/gss?";
  11. const QTT_ServerUrl: string = "https://newidea4-gamecenter-backend.1sapp.com/x/open/user/ticket?"
  12. const QTT_Report: string = "https://newidea4-gamecenter-backend.1sapp.com/x/open/report/round"
  13. /**
  14. * cocos工具类
  15. */
  16. @ccclass
  17. export default class YZ_Tool_Cocosplay {
  18. _sysInfo: any = {};
  19. public get SysInfo() {
  20. return this._sysInfo;
  21. }
  22. /**
  23. * 服务器配置信息
  24. */
  25. _serverConfig: any = null;
  26. public get ServerConfig() {
  27. return this._serverConfig;
  28. }
  29. _shareCallback: Function = null;
  30. _isShare: boolean = false;
  31. //设备UID
  32. _uid: string = "0";
  33. public get uid() {
  34. // if (this._service_uid != "0") return this._uid;
  35. // this._login();
  36. return "0";
  37. }
  38. //服务器返回UID
  39. _service_uid: string = "0";
  40. /**
  41. * 服务器返回UID
  42. */
  43. public get serviceId() {
  44. if (this._service_uid != "0") return this._service_uid;
  45. this.reportLogin();
  46. return "0";
  47. }
  48. _loginTime: number = 0;
  49. _loginInterval: number = 30;
  50. async _login() {
  51. let curTime: number = new Date().getTime();
  52. let interval: number = (curTime - this._loginTime) / 1000;
  53. if (interval > 0 && interval < 30) {
  54. utils.showLog(`登录请求间隔小于:${this._loginInterval}秒`);
  55. return;
  56. }
  57. this._loginTime = curTime;
  58. let self = this;
  59. utils.showLog("cocos暂时不获取uid,uid全部为0");
  60. this._uid = "0";
  61. // this.reportLogin();
  62. }
  63. _reportLoginTime: number = 0;
  64. _reportLoginInterval: number = 30;
  65. isReport: boolean = false;
  66. /**
  67. * 上报登录接口获取UID
  68. */
  69. reportLogin() {
  70. if (this.isReport) return;
  71. this.isReport = true;
  72. let self = this;
  73. let curTime: number = new Date().getTime();
  74. let interval: number = (curTime - self._reportLoginTime) / 1000;
  75. // utils.showLog(interval, " <<<<<<,interval", " _reportLoginTime >>>", self._reportLoginTime);
  76. // console.log(curTime, curTime - self._reportLoginTime, interval)
  77. if (interval > 0 && interval < 30) {
  78. utils.showLog(`上报登录获取UID小于:${self._reportLoginInterval}秒`);
  79. return;
  80. }
  81. self._reportLoginTime = curTime;
  82. let method = "m=login";
  83. let url: string = ST_ServerUrl + method + `&device_data=0`;
  84. utils.commomHttpRequest(url, (ret, data) => {
  85. if (ret) {
  86. if (data) {
  87. let result = JSON.parse(data);
  88. utils.showLog("data=" + data);
  89. utils.showLog("result=" + result);
  90. utils.showLog("result.uid=" + result.uid);
  91. if (result.uid) {
  92. self._service_uid = "" + result.uid;
  93. console.log("self._service_uid:" + self._service_uid)
  94. utils.showLog("服务器请求登录成功! _service_uid=" + self._service_uid);
  95. YZ_LocalStorage.setItem(YZ_Constant.ST_SERVICE_UID, self._service_uid);
  96. }
  97. }
  98. } else {
  99. utils.showLog("获取数据失败1");
  100. }
  101. this.isReport = false;
  102. })
  103. }
  104. nickName: string = "";
  105. /**
  106. *
  107. * @param data 配置数据
  108. */
  109. public init(data: string) {
  110. if (PlatUtils.ISCocos) {
  111. /**
  112. * Param Json对象:
  113. {
  114. error, // int 错误代码:0、成功(见文末)
  115. userId, // int当前玩家id
  116. nickName, // string当前玩家名称
  117. headUrl, // string 当前玩家头像地址
  118. location, //string当前玩家地址
  119. sex, // string 玩家性别:"f" - 女;"m" - 男;"x" – 未知
  120. age // int 当前玩家年龄
  121. }
  122. */
  123. //@ts-ignore
  124. GameSDK.setOnInitCB((param) => {
  125. if (param.error == 0) {
  126. utils.showLog("初始化成功")
  127. this._uid = param.userId;
  128. this.nickName = param.nickName
  129. };
  130. });
  131. utils.showLog("utils.config.cocosConfig.appID:" + utils.config.cocosConfig.appID)
  132. //@ts-ignore
  133. GameSDK.init(utils.config.cocosConfig.appID);
  134. //@ts-ignore
  135. AdSDK.init();
  136. if (data) {
  137. let configObj: any = JSON.parse(data);
  138. if (configObj && configObj.cocos) {
  139. ST_DefaultServerConfig = JSON.stringify(configObj.cocos);
  140. }
  141. }
  142. this._service_uid = YZ_LocalStorage.getItem(YZ_Constant.ST_SERVICE_UID);
  143. this._service_uid = this._service_uid ? this._service_uid : "0";
  144. try {
  145. //@ts-ignore
  146. this._sysInfo = GameSDK.getSystemInfoSync()
  147. utils.showLog("cocos 小游戏平台信息: " + JSON.stringify(this.SysInfo));
  148. } catch (e) {
  149. utils.showLog("cocos 小游戏平台数据获取失败!");
  150. }
  151. this._loadConfig();
  152. }
  153. }
  154. _loadConfig() {
  155. if (PlatUtils.ISCocos) {
  156. let method: string = "m=g";
  157. utils.commomHttpRequest(ST_ServerUrl + method, (ret, data) => {
  158. if (ret) {
  159. utils.showLog("cocos服务器配置数据获取成功: data = " + data);
  160. if (data) {
  161. let result = JSON.parse(data);
  162. if (result) {
  163. if (!utils.DebugLoacalConfig) {
  164. this._serverConfig = result;
  165. } else {
  166. cc.warn("开启了本地数据测试,使用本地配置!");
  167. }
  168. } else {
  169. utils.showLog("cocos服务器配置数据不是合法的JSON数据, 使用本地配置!");
  170. }
  171. }
  172. } else {
  173. utils.showLog("cocos服务器配置数据获取失败, 使用本地配置!");
  174. }
  175. if (!this._serverConfig) {
  176. this._serverConfig = JSON.parse(ST_DefaultServerConfig);
  177. } else {
  178. if (this._serverConfig.shares) {
  179. if (this._serverConfig.shares.sy_title) {
  180. utils.config.otherconfig.shareTitle = this._serverConfig.shares.sy_title;
  181. }
  182. if (this._serverConfig.shares.sy_img) {
  183. utils.config.otherconfig.shareImgUrl = this._serverConfig.shares.sy_img;
  184. }
  185. }
  186. if (this._serverConfig.is_local_pos_id
  187. && this._serverConfig.is_local_pos_id == "false") {
  188. // 使用服务器下发的广告id
  189. utils.showLog("使用服务器下发的广告id");
  190. // utils.config.cocosConfig.bannerId = this._serverConfig.banner_pos_id;
  191. // utils.config.cocosConfig.insertId = this._serverConfig.intersititia_pos_id;
  192. // utils.config.cocosConfig.videoId = this._serverConfig.video_pos_id;
  193. } else {
  194. utils.showLog("使用本地配置的广告ID");
  195. }
  196. }
  197. utils.emitServerInitEvent();
  198. });
  199. }
  200. }
  201. public getSystemInfo() {
  202. if (PlatUtils.ISCocos) {
  203. return this._sysInfo;
  204. }
  205. }
  206. public share(callback: Function = null) {
  207. }
  208. public isOverMinVersion(minVersion: string) {
  209. let curVersion: string = this._sysInfo.SDKVersion;
  210. return this._compareVersion(curVersion, minVersion) >= 0;
  211. }
  212. _compareVersion(v1, v2) {
  213. if (!v1 || !v2) return -1;
  214. v1 = v1.split('.')
  215. v2 = v2.split('.')
  216. const len = Math.max(v1.length, v2.length)
  217. while (v1.length < len) {
  218. v1.push('0')
  219. }
  220. while (v2.length < len) {
  221. v2.push('0')
  222. }
  223. for (let i = 0; i < len; i++) {
  224. const num1 = parseInt(v1[i])
  225. const num2 = parseInt(v2[i])
  226. if (num1 > num2) {
  227. return 1
  228. } else if (num1 < num2) {
  229. return -1
  230. }
  231. }
  232. return 0
  233. }
  234. /**
  235. * 上报关卡数据
  236. * @param level 当前关卡ID
  237. * @param levelName 关卡名称
  238. * @param status 状态
  239. */
  240. public postLevel(level: string, status: LevelStatus, levelName?: string) {
  241. if (PlatUtils.ISCocos) {
  242. let method = "m=rlevel";
  243. let url: string = POST_ServerUrl + method + `&level_id=${level}&level_name=${encodeURI(levelName)}&status=${status}`;
  244. utils.commomHttpRequest(url, function (ret, data) {
  245. if (ret) {
  246. utils.showLog("关卡数据上报成功!");
  247. } else {
  248. utils.showLog("关卡数据上报失败!");
  249. }
  250. }.bind(this));
  251. }
  252. }
  253. }