YZ_Tool_UC.ts 7.4 KB

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