HttpManager.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /** @format */
  2. import {Log} from '../utils/LogUtils'
  3. import {Data, Mgr} from '../GameControl'
  4. import {LANGUAGE_TYPE} from '../enums/Enum'
  5. export interface IHttpReq {
  6. url: string
  7. method: 'POST' | 'GET'
  8. success: (data: any) => void
  9. fail?: (data?: any) => void
  10. data?: any
  11. type?: 'text' | 'arraybuffer'
  12. contentType?: 'application/json' | 'text/plain' | 'application/x-www-form-urlencoded'
  13. err?: () => void
  14. timeout?: () => void
  15. }
  16. export class HttpManager {
  17. request({url, method, success, fail, data, type = 'text', err, timeout, contentType}: IHttpReq) {
  18. Log.net('requestUrl--->', url)
  19. url = encodeURI(url)
  20. let successCB = res => {
  21. if (res.code != undefined) {
  22. if (res.code == 0) {
  23. success && success(res)
  24. } else {
  25. fail && fail(res)
  26. Mgr.ui.tip(res.code + ':' + res.msg)
  27. }
  28. } else {
  29. success && success(res)
  30. }
  31. }
  32. if (cc.sys.isBrowser || cc.sys.isNative) {
  33. let xhr = new XMLHttpRequest()
  34. try {
  35. xhr.open(method, url, true)
  36. if (data) {
  37. Log.net('requestData--->', data)
  38. }
  39. if (!contentType) {
  40. if (data) {
  41. if (typeof data == 'object') {
  42. contentType = 'application/json'
  43. } else {
  44. contentType = 'text/plain'
  45. }
  46. } else {
  47. contentType = 'application/x-www-form-urlencoded'
  48. }
  49. }
  50. xhr.setRequestHeader('Content-Type', contentType)
  51. xhr.onreadystatechange = function () {
  52. if (xhr.readyState == 4) {
  53. if (xhr.status == 200) {
  54. let response
  55. if (type == 'text') {
  56. response = xhr.responseText
  57. if (response == '') {
  58. response = '{}'
  59. }
  60. try {
  61. response = JSON.parse(response)
  62. } catch (e) {}
  63. Log.net('responseData--->url:' + url + '--->', response)
  64. successCB(response)
  65. } else if (type == 'arraybuffer') {
  66. response = xhr.response
  67. try {
  68. successCB(response)
  69. } catch (e) {
  70. cc.log(e)
  71. }
  72. }
  73. } else {
  74. Log.net('http request status---->', xhr.status, '---->', url)
  75. Mgr.ui.tip(Mgr.i18n.getLabel(LANGUAGE_TYPE.httpFail, [xhr.status]))
  76. if (fail) fail()
  77. }
  78. }
  79. }
  80. xhr.responseType = type
  81. if (data && typeof data == 'object') {
  82. data = JSON.stringify(data)
  83. }
  84. xhr.onerror = data => {
  85. Log.net('http request onerror---->', data)
  86. if (err) err()
  87. }
  88. xhr.timeout = 15000
  89. xhr.ontimeout = () => {
  90. Log.net('http request timeout---->', url)
  91. if (timeout) {
  92. timeout()
  93. } else {
  94. Mgr.ui.tip(LANGUAGE_TYPE.httpTimeout)
  95. }
  96. }
  97. xhr.send(data)
  98. } catch (e) {
  99. Log.net('http request err---->', e)
  100. }
  101. }
  102. }
  103. }