index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * FeHelper 简易版Postman
  3. */
  4. // json with bigint supported
  5. Tarp.require('../static/vendor/json-bigint/index');
  6. new Vue({
  7. el: '#pageContainer',
  8. data: {
  9. urlContent: '',
  10. urlParams: [],
  11. methodContent: 'GET',
  12. resultContent: '',
  13. paramContent: '',
  14. responseHeaders: [],
  15. jfCallbackName_start: '',
  16. jfCallbackName_end: '',
  17. errorMsgForJson: ''
  18. },
  19. watch: {
  20. urlContent: function (val) {
  21. let url = val;
  22. let reg = /[?&]([^?&#]+)=([^?&#]+)/g;
  23. let params = [];
  24. let ret = reg.exec(url);
  25. while (ret) {
  26. params.push({
  27. key: ret[1],
  28. value: ret[2],
  29. });
  30. ret = reg.exec(url);
  31. }
  32. this.urlParams = params;
  33. },
  34. urlParams: {
  35. handler(val) {
  36. if (!val.length) return;
  37. this.urlContent =
  38. this.urlContent.substr(0, this.urlContent.indexOf("?") + 1) +
  39. val.map((item) => `${item.key}=${item.value}`).join("&");
  40. },
  41. deep: true,
  42. },
  43. },
  44. mounted: function () {
  45. this.$refs.url.focus();
  46. },
  47. methods: {
  48. postman: function () {
  49. this.$nextTick(() => {
  50. this.sendRequest(this.urlContent, this.methodContent, this.paramContent);
  51. });
  52. },
  53. sendRequest: function (url, method, body) {
  54. let xhr = new XMLHttpRequest();
  55. xhr.addEventListener("readystatechange", (resp) => {
  56. let result = 'Loading...';
  57. switch (resp.target.readyState) {
  58. case resp.target.OPENED:
  59. result = 'Senting...';
  60. break;
  61. case resp.target.HEADERS_RECEIVED:
  62. result = 'Headers received';
  63. this.responseHeaders = resp.target.getAllResponseHeaders().trim().split('\n').map(item => {
  64. return item.split(': ').map(x => x.trim())
  65. });
  66. break;
  67. case resp.target.LOADING:
  68. result = 'Loading...';
  69. break;
  70. case resp.target.DONE:
  71. try {
  72. result = JSON.stringify(JSON.parse(resp.target.responseText), null, 4);
  73. } catch (e) {
  74. result = resp.target.responseText;
  75. }
  76. this.jsonFormat(result);
  77. this.renderTab();
  78. break;
  79. }
  80. this.resultContent = result || '无数据';
  81. });
  82. xhr.open(method, url);
  83. if(method.toLowerCase() === 'post') {
  84. xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  85. xhr.send(body);
  86. }else{
  87. xhr.send();
  88. }
  89. },
  90. renderTab: function () {
  91. jQuery('#tabs').tabs({
  92. show: (event, ui) => {
  93. }
  94. });
  95. this.$refs.resultContainer.classList.remove('hide');
  96. },
  97. jsonFormat: function (source) {
  98. this.errorMsgForJson = '';
  99. this.jfCallbackName_start = '';
  100. this.jfCallbackName_end = '';
  101. if (!source) {
  102. return false;
  103. }
  104. // JSONP形式下的callback name
  105. let funcName = null;
  106. // json对象
  107. let jsonObj = null;
  108. // 下面校验给定字符串是否为一个合法的json
  109. try {
  110. // 再看看是不是jsonp的格式
  111. let reg = /^([\w\.]+)\(\s*([\s\S]*)\s*\)$/igm;
  112. let matches = reg.exec(source);
  113. if (matches != null) {
  114. funcName = matches[1];
  115. source = matches[2];
  116. }
  117. // 这里可能会throw exception
  118. jsonObj = JSON.parse(source);
  119. } catch (ex) {
  120. // new Function的方式,能自动给key补全双引号,但是不支持bigint,所以是下下策,放在try-catch里搞
  121. try {
  122. jsonObj = new Function("return " + source)();
  123. } catch (exx) {
  124. try {
  125. // 再给你一次机会,是不是下面这种情况: "{\"ret\":\"0\", \"msg\":\"ok\"}"
  126. jsonObj = new Function("return '" + source + "'")();
  127. if (typeof jsonObj === 'string') {
  128. // 最后给你一次机会,是个字符串,老夫给你再转一次
  129. jsonObj = new Function("return " + jsonObj)();
  130. }
  131. } catch (exxx) {
  132. this.errorMsgForJson = exxx.message;
  133. }
  134. }
  135. }
  136. // 是json格式,可以进行JSON自动格式化
  137. if (jsonObj != null && typeof jsonObj === "object" && !this.errorMsgForJson.length) {
  138. try {
  139. // 要尽量保证格式化的东西一定是一个json,所以需要把内容进行JSON.stringify处理
  140. source = JSON.stringify(jsonObj);
  141. } catch (ex) {
  142. // 通过JSON反解不出来的,一定有问题
  143. this.errorMsgForJson = ex.message;
  144. }
  145. if (!this.errorMsgForJson.length) {
  146. // 格式化
  147. Tarp.require('../json-format/format-lib').format(source);
  148. // 如果是JSONP格式的,需要把方法名也显示出来
  149. if (funcName != null) {
  150. this.jfCallbackName_start = funcName + '(';
  151. this.jfCallbackName_end = ')';
  152. } else {
  153. this.jfCallbackName_start = '';
  154. this.jfCallbackName_end = '';
  155. }
  156. }
  157. }
  158. // 不是json,都格式化不了,一定会出错
  159. if (this.errorMsgForJson) {
  160. let el = document.querySelector('#optionBar');
  161. el && (el.style.display = 'none');
  162. }
  163. },
  164. setDemo: function (type) {
  165. if (type === 1) {
  166. this.urlContent = 'https://www.sojson.com/api/qqmusic/8446666/json';
  167. this.methodContent = 'GET';
  168. } else {
  169. this.urlContent = 'https://www.baidufe.com/test-post.php';
  170. this.methodContent = 'POST';
  171. this.paramContent = 'username=postman&password=123456'
  172. }
  173. }
  174. }
  175. });