HTTPS.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. // {
  13. // "id": 7,
  14. // "username": "13137717769",
  15. // "nickname": "131****7769",
  16. // "avatar": "",
  17. // "balance": "9989.00",
  18. // "status_text": "正常"
  19. // }
  20. // user_info = null;
  21. public user_info =
  22. {
  23. "id": 7,
  24. "username": "13137717769",
  25. "nickname": "131****7769",
  26. "avatar": "",
  27. "balance": "9989.00",
  28. "status_text": "正常"
  29. }
  30. // private _URL = "http://120.79.5.67:7000";
  31. //得到现在的时间
  32. private GetNowTime(): string {
  33. const date = new Date(cc.sys.now());
  34. const hours = date.getHours();
  35. const minutes = date.getMinutes();
  36. const seconds = date.getSeconds();
  37. return `${hours}:${minutes}:${seconds} `;
  38. }
  39. post(action: string, data: object | string): Promise<any> {
  40. if (CC_DEBUG) {
  41. cc.error(this.GetNowTime() + "发送消息 >>>>>> :" + action, data);
  42. // cc.error(this.GetNowTime() + "消息内容 >>>>>> :", data);
  43. }
  44. const promise: Promise<any> = new Promise((resolve, reject) => {
  45. var timeoutKey: any;
  46. var retry = 5;//失败重试次数
  47. const send = () => {
  48. const httpRequest = new XMLHttpRequest();
  49. const method = 'POST'
  50. httpRequest.onreadystatechange = (event: Event): void => {
  51. const readyState = httpRequest.readyState;
  52. if (readyState !== 4) return;
  53. if (!timeoutKey) return;//已通知超时,之后收到数据后也不处理
  54. clearTimeout(timeoutKey);
  55. timeoutKey = null;
  56. const requestStatus = httpRequest.status;
  57. if (requestStatus >= 200 && requestStatus < 400) {
  58. let resp = JSON.parse(httpRequest.responseText);
  59. resolve(resp);
  60. if (CC_DEBUG) {
  61. cc.error(this.GetNowTime() + `收到消息(${retry}) >>>>>> :` + action, resp);
  62. // cc.error(httpRequest.responseText);
  63. }
  64. }
  65. else if (retry-- > 0) send();
  66. else reject(new Error('全部超时'));
  67. }
  68. httpRequest.open(method, Url + action, true);
  69. // httpRequest.setRequestHeader("Access-Control-Allow-Origin", "*");
  70. httpRequest.setRequestHeader("Content-Type", "application/json;charset=utf-8");
  71. if (this.token) {
  72. httpRequest.setRequestHeader("token", this.token);
  73. }
  74. var text = typeof (data) == "string" ? data : JSON.stringify(data);
  75. data ? httpRequest.send(text) : httpRequest.send();
  76. timeoutKey = setTimeout(() => {
  77. timeoutKey = null;
  78. httpRequest.abort();
  79. reject(new Error(`Timeout`));
  80. }, 12000);//超时时间(毫秒)
  81. }
  82. send();
  83. })
  84. return promise;
  85. }
  86. get(action: string): Promise<any> {
  87. if (CC_DEBUG) {
  88. cc.error(this.GetNowTime() + "发送消息 >>>>>> :" + action);
  89. }
  90. const promise: Promise<any> = new Promise((resolve, reject) => {
  91. var timeoutKey: any;
  92. var retry = 5;//失败重试次数
  93. const send = () => {
  94. const httpRequest = new XMLHttpRequest();
  95. const method = 'GET'
  96. httpRequest.onreadystatechange = (event: Event): void => {
  97. const readyState = httpRequest.readyState;
  98. if (readyState !== 4) return;
  99. if (!timeoutKey) return;//已通知超时,之后收到数据后也不处理
  100. clearTimeout(timeoutKey);
  101. timeoutKey = null;
  102. const requestStatus = httpRequest.status;
  103. if (requestStatus >= 200 && requestStatus < 400) {
  104. let resp = JSON.parse(httpRequest.responseText);
  105. resolve(resp);
  106. if (CC_DEBUG) {
  107. cc.error(this.GetNowTime() + `收到消息(${retry}) >>>>>> :` + action, resp);
  108. }
  109. }
  110. else if (retry-- > 0) send();
  111. else reject(new Error('全部超时'));
  112. }
  113. httpRequest.open(method, action, true);
  114. if (this.token) {
  115. httpRequest.setRequestHeader("token", this.token);
  116. }
  117. httpRequest.send();
  118. timeoutKey = setTimeout(() => {
  119. timeoutKey = null;
  120. httpRequest.abort();
  121. reject(new Error(`Timeout`));
  122. }, 12000);//超时时间(毫秒)
  123. }
  124. send();
  125. })
  126. return promise;
  127. }
  128. }
  129. export var HTTPSIns = HTTPS.Instance
  130. export var Url = "http://yx.dashenwangluo.com";
  131. export var NetGet = {
  132. /**获取任务配置 */
  133. Task: Url + "/v1/task/config",//token: 用户token
  134. /**获取提现规则 */
  135. rule: Url + "/v1/withdraw/rule",//token: 用户token
  136. /**获取提现记录 */
  137. withdrawHistory: Url + "/v1/withdraw/log?page=1&limit=20",//token: 用户token
  138. }
  139. export var NetPost = {
  140. /**发送验证码 */
  141. SendIphoneCode: "/v1/user/send_code",
  142. /**手机号登录 */
  143. IphoneLogin: "/v1/user/login",
  144. /**完成任务 */
  145. complete: "/v1/task/complete",// task_type: 任务类型(ad_watch/sign_in)
  146. /**申请提现 */
  147. withdraw: "/v1/withdraw/apply",// task_type: 任务类型(ad_watch/sign_in)
  148. /**记录广告观看 */
  149. record: "/v1/ad/record",// task_type: 任务类型(ad_watch/sign_in)
  150. }