HTTPS.ts 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const { ccclass, property } = cc._decorator;
  2. @ccclass
  3. export default class HTTPS {
  4. private static _instance = null;
  5. public static get Instance(): HTTPS {
  6. if (this._instance == null) {
  7. this._instance = new HTTPS();
  8. }
  9. return this._instance;
  10. }
  11. token = null;
  12. // private _URL = "http://120.79.5.67:7000";
  13. //得到现在的时间
  14. private GetNowTime(): string {
  15. const date = new Date(cc.sys.now());
  16. const hours = date.getHours();
  17. const minutes = date.getMinutes();
  18. const seconds = date.getSeconds();
  19. return `${hours}:${minutes}:${seconds} `;
  20. }
  21. post(action: string, data: object | string): Promise<any> {
  22. if (CC_DEBUG) {
  23. cc.error(this.GetNowTime() + "发送消息 >>>>>> :" + action, data);
  24. // cc.error(this.GetNowTime() + "消息内容 >>>>>> :", data);
  25. }
  26. const promise: Promise<any> = new Promise((resolve, reject) => {
  27. var timeoutKey: number;
  28. var retry = 5;//失败重试次数
  29. const send = () => {
  30. const httpRequest = new XMLHttpRequest();
  31. const method = 'POST'
  32. httpRequest.onreadystatechange = (event: Event): void => {
  33. const readyState = httpRequest.readyState;
  34. if (readyState !== 4) return;
  35. if (!timeoutKey) return;//已通知超时,之后收到数据后也不处理
  36. clearTimeout(timeoutKey);
  37. timeoutKey = null;
  38. const requestStatus = httpRequest.status;
  39. if (requestStatus >= 200 && requestStatus < 400) {
  40. let resp = JSON.parse(httpRequest.responseText);
  41. resolve(resp);
  42. if (CC_DEBUG) {
  43. cc.error(this.GetNowTime() + `收到消息(${retry}) >>>>>> :` + action, resp);
  44. // cc.error(httpRequest.responseText);
  45. }
  46. }
  47. else if (retry-- > 0) send();
  48. else reject(new Error('全部超时'));
  49. }
  50. httpRequest.open(method, Url + action, true);
  51. // httpRequest.setRequestHeader("Access-Control-Allow-Origin", "*");
  52. httpRequest.setRequestHeader("Content-Type", "application/json;charset=utf-8");
  53. httpRequest.setRequestHeader("Authorization", this.token != null ? this.token : "");
  54. var text = typeof (data) == "string" ? data : JSON.stringify(data);
  55. data ? httpRequest.send(text) : httpRequest.send();
  56. timeoutKey = setTimeout(() => {
  57. timeoutKey = null;
  58. httpRequest.abort();
  59. reject(new Error(`Timeout`));
  60. }, 12000);//超时时间(毫秒)
  61. }
  62. send();
  63. })
  64. return promise;
  65. }
  66. get(action: string): Promise<any> {
  67. if (CC_DEBUG) {
  68. cc.error(this.GetNowTime() + "发送消息 >>>>>> :" + action);
  69. }
  70. const promise: Promise<any> = new Promise((resolve, reject) => {
  71. var timeoutKey: number;
  72. var retry = 5;//失败重试次数
  73. const send = () => {
  74. const httpRequest = new XMLHttpRequest();
  75. const method = 'GET'
  76. httpRequest.onreadystatechange = (event: Event): void => {
  77. const readyState = httpRequest.readyState;
  78. if (readyState !== 4) return;
  79. if (!timeoutKey) return;//已通知超时,之后收到数据后也不处理
  80. clearTimeout(timeoutKey);
  81. timeoutKey = null;
  82. const requestStatus = httpRequest.status;
  83. if (requestStatus >= 200 && requestStatus < 400) {
  84. let resp = JSON.parse(httpRequest.responseText);
  85. resolve(resp);
  86. if (CC_DEBUG) {
  87. cc.error(this.GetNowTime() + `收到消息(${retry}) >>>>>> :` + action, resp);
  88. }
  89. }
  90. else if (retry-- > 0) send();
  91. else reject(new Error('全部超时'));
  92. }
  93. httpRequest.open(method, action, true);
  94. httpRequest.setRequestHeader("Authorization", this.token != null ? this.token : "");
  95. httpRequest.send();
  96. timeoutKey = setTimeout(() => {
  97. timeoutKey = null;
  98. httpRequest.abort();
  99. reject(new Error(`Timeout`));
  100. }, 12000);//超时时间(毫秒)
  101. }
  102. send();
  103. })
  104. return promise;
  105. }
  106. }
  107. export var Url = "https://api.sheishishangbanwang.top";
  108. export var NetGet = {
  109. /**获取地图数据 */
  110. Map: Url + "/api/tokenpass/Login/Map",
  111. /**收藏装扮Id 玩家穿戴收藏装 */
  112. UserWearCollect: Url + "/api/User/UserWearCollect?collectId=",
  113. /**玩家穿戴荣誉 */
  114. UserWearHonor: Url + "/api/User/UserWearHonor?honorId=",
  115. /**每日总数记录 */
  116. DailyRecord: Url + "/api/tokenpass/Login/DailyRecord",
  117. }
  118. export var NetPost = {
  119. /**登录 */
  120. Login: "/api/tokenpass/Login/UserInfo",
  121. TestLogin: "/api/tokenpass/Login/Login",
  122. /**用户授权 */
  123. Authorisation: "/api/User/Authorisation",
  124. /**获取玩家荣誉表 */
  125. GetUserHonorList: "/api/User/GetUserHonorList",
  126. /**获取玩家收藏列表 */
  127. GetUserCollectList: "/api/User/GetUserCollectList",
  128. /**查询邀请申请 */
  129. InvitationRecord: "/api/User/InvitationRecord",
  130. /**领取邀请奖励 */
  131. ReceiveInvitation: "/api/User/ReceiveInvitation",
  132. /**获取用户信息 */
  133. GetUserInfo: "/api/User/GetUserInfo",
  134. /**添加登录游玩记录 */
  135. AddRecord: "/api/User/AddRecord",
  136. /**结束本局游戏 */
  137. EndThisGam: "/api/User/EndThisGame",
  138. /**消息通知 */
  139. GetToBenotifiedList: "/api/User/GetToBenotifiedList",
  140. /**修改消息通知为已读 */
  141. EditToBenotified: "/api/User/EditToBenotified",
  142. /**新增消息通知 */
  143. AddToBenotified: "/api/User/AddToBenotified",
  144. /**查询职级排行 */
  145. GetPositionLevelRanking: "/api/User/GetPositionLevelRanking",
  146. /**获取省份排行 */
  147. GetProvinceRanking: "/api/User/GetProvinceRanking",
  148. /**获取玩家道具 */
  149. GetUserProp: "/api/User/GetUserProp",
  150. }