CHttp.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import ViewTop from "../main/ViewTop";
  2. import CHttpEvent from "./CHttpEvent";
  3. import CUtil from "./CUtil";
  4. /**
  5. * 网络状态
  6. */
  7. export enum HttpStateType{
  8. SUCCESS,
  9. TIME_OUT,
  10. ERROR,
  11. }
  12. /**
  13. * 服务器反馈消息
  14. */
  15. export interface ReveData{
  16. retCode:number,
  17. message:string,
  18. data:any,
  19. }
  20. /**
  21. * http post
  22. */
  23. export default class CHttp {
  24. private url:string = null;
  25. public id:string;
  26. public userId:number;
  27. public token:string = null;
  28. /**
  29. * 网络消息监听
  30. */
  31. public event:Array<CHttpEvent> = [];
  32. constructor(url) {
  33. this.url = url;
  34. }
  35. /**
  36. * 添加事件到头部
  37. * @param httpEvent
  38. */
  39. public unshiftEvent(httpEvent:CHttpEvent){
  40. this.event.unshift(httpEvent);
  41. }
  42. public pushEvent(httpEvent:CHttpEvent){
  43. this.event.push(httpEvent);
  44. // cc.log('添加监听:')
  45. // for (let i = 0; i < this.event.length; i++) {
  46. // const element = this.event[i];
  47. // cc.log(element);
  48. // }
  49. // cc.log('----------end')
  50. }
  51. public popEvent(httpEvent:CHttpEvent){
  52. for (let i = 0; i < this.event.length; i++) {
  53. const element = this.event[i];
  54. if(element == httpEvent){
  55. this.event.splice(i,1);
  56. return
  57. }
  58. }
  59. // this.event.pop();
  60. // for (let i = 0; i < this.event.length; i++) {
  61. // const element = this.event[i];
  62. // cc.log(element);
  63. // }
  64. // cc.log('----------end')
  65. }
  66. /**
  67. * sendjson
  68. */
  69. public sendForm(action,msg,callback:(state:Number,reve?:ReveData)=>void) {
  70. if(this.id){
  71. msg.I = this.id;
  72. }
  73. if(this.userId){
  74. msg.userId = this.userId;
  75. }
  76. if(this.token){
  77. CUtil.makeSign(msg,this.token);
  78. }
  79. let xhr:XMLHttpRequest = new XMLHttpRequest();
  80. xhr.open('post',this.url+action,true);
  81. // xhr.setRequestHeader("Content-Type","text/plain");
  82. xhr.setRequestHeader("Content-Type","application/json");
  83. xhr.onerror = function(eventname){
  84. callback(HttpStateType.ERROR);
  85. };
  86. xhr.ontimeout = function(eventname){
  87. callback(HttpStateType.TIME_OUT);
  88. }
  89. let myself = this;
  90. xhr.onreadystatechange = function () {
  91. // cc.log('readyState = '+xhr.readyState);
  92. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  93. if(CC_DEBUG){
  94. cc.log('reve:'+xhr.responseText);
  95. }
  96. let jsonObj = JSON.parse(xhr.responseText)
  97. if(jsonObj.retCode == 0){
  98. for (let i = 0; i < myself.event.length; i++) {
  99. const element = myself.event[i];
  100. element.httpEvent(jsonObj);
  101. }
  102. }else if(jsonObj.retCode == 500106){
  103. myself.initViewTop()
  104. }
  105. callback(HttpStateType.SUCCESS,jsonObj);
  106. }
  107. };
  108. if(CC_DEBUG){
  109. cc.log('send',JSON.stringify(msg));
  110. }
  111. xhr.send(JSON.stringify(msg));
  112. }
  113. /**
  114. * sendjson
  115. */
  116. public sendJson(action,msg,callback:(state:Number,reve?:ReveData)=>void) {
  117. if(this.id){
  118. msg.I = this.id;
  119. }
  120. if(this.userId){
  121. msg.userId = this.userId;
  122. }
  123. if(this.token){
  124. CUtil.makeSign(msg,this.token);
  125. }
  126. let xhr:XMLHttpRequest = new XMLHttpRequest();
  127. xhr.open('post',this.url+action,true);
  128. xhr.setRequestHeader("Content-Type","text/plain");
  129. // xhr.setRequestHeader("Content-Type","application/json");
  130. xhr.onerror = function(eventname){
  131. callback(HttpStateType.ERROR);
  132. };
  133. xhr.ontimeout = function(eventname){
  134. callback(HttpStateType.TIME_OUT);
  135. }
  136. let myself = this;
  137. xhr.onreadystatechange = function () {
  138. // cc.log('readyState = '+xhr.readyState);
  139. if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) {
  140. if(CC_DEBUG){
  141. cc.log('reve:'+xhr.responseText);
  142. }
  143. let jsonObj = JSON.parse(xhr.responseText)
  144. if(jsonObj.retCode == 0){
  145. for (let i = 0; i < myself.event.length; i++) {
  146. const element = myself.event[i];
  147. element.httpEvent(jsonObj);
  148. }
  149. }else if(jsonObj.retCode == 500106){
  150. myself.initViewTop()
  151. }
  152. callback(HttpStateType.SUCCESS,jsonObj);
  153. }
  154. };
  155. if(CC_DEBUG){
  156. cc.log('send:'+action,JSON.stringify(msg));
  157. }
  158. xhr.send(JSON.stringify(msg));
  159. }
  160. private initViewTop(){
  161. cc.resources.load('prefab/common/common_hint', cc.Prefab, (err, prefab: cc.Prefab) => {
  162. if (err) {
  163. cc.error(err);
  164. } else {
  165. //加载结束
  166. let node: cc.Node = cc.instantiate(prefab);
  167. let viewTop = node.getComponent(ViewTop);
  168. viewTop.show()
  169. }
  170. });
  171. }
  172. }