index.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>sa-doc 接口测试</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
  7. <!-- 所有的 css & js 资源 -->
  8. <link rel="stylesheet" href="https://unpkg.com/element-ui@2.13.0/lib/theme-chalk/index.css">
  9. <link rel="stylesheet" href="./sa.css">
  10. <script src="https://unpkg.com/vue@2.6.10/dist/vue.min.js"></script>
  11. <script src="https://unpkg.com/element-ui@2.13.0/lib/index.js"></script>
  12. <script src="https://unpkg.com/jquery@3.4.1/dist/jquery.js"></script>
  13. <script src="https://www.layuicdn.com/layer-v3.1.1/layer.js"></script>
  14. <script src="./sa.js"></script>
  15. <style type="text/css">
  16. .vue-box{max-width: 1000px; margin: auto;}
  17. /* api-url盒子 */
  18. .ajax-api-box{width: 100%;}
  19. .c-panel .ajax-api-box .el-input{width: calc(100% - 200px); }
  20. /* .c-panel .ajax-api-box .el-input__inner{font-weight: bold;} */
  21. .c-panel .el-input__inner{color: #333;}
  22. /* type */
  23. .ajax-type,
  24. .ajax-type .el-input,
  25. .ajax-type .el-input__inner{width: 100px !important; text-align: center;
  26. font-weight: bold; background-color: #409EFF; color: #FFF; border-color: #409EFF !important; border-radius: 4px;}
  27. .ajax-type{margin-right: 5px; }
  28. /* 参数盒子 */
  29. .header-box .el-table__header-wrapper{height: 0px;}
  30. .body-box .el-table__header-wrapper{height: 0px;}
  31. .el-table td{border: 0px !important;}
  32. .el-table::before{background-color: rgba(0,0,0,0)}
  33. .add-btn{padding: 2px 15px; margin-left: 0px; display: inline-block;}
  34. .add-btn .el-link--inner{position: relative; top: -1px;}
  35. .el-table__empty-block{height: 0px !important; display: none;}
  36. .el-textarea__inner{padding: 10px 15px; color: inherit;/* font-family: Roboto Mono,Monaco,courier,monospace; */}
  37. </style>
  38. </head>
  39. <body>
  40. <div style="margin-top: -1em;" title="防止margin向下击穿"><br></div>
  41. <div class="vue-box" style="display: none;" :style="'display: block;'">
  42. <div class="c-panel">
  43. <!--------------- 请求地址 --------------->
  44. <div class="c-title">
  45. <span>请求地址</span>
  46. <el-link class="add-btn" type="primary" icon="el-icon-plus" @click="g_setting()">全局参数设置</el-link>
  47. <el-checkbox v-model="isCookie">发送Cookie</el-checkbox>
  48. </div>
  49. <div class="c-item ajax-api-box" style="min-width: 0px; padding-right: 5px;">
  50. <el-select size="small" v-model="ajaxType" class="ajax-type">
  51. <el-option label="GET" value="GET"></el-option>
  52. <el-option label="POST" value="POST"></el-option>
  53. <el-option label="PUT" value="PUT"></el-option>
  54. <el-option label="DELETE" value="DELETE"></el-option>
  55. </el-select>
  56. <el-input size="small" v-model="ajaxUrl" class="ajax-url"></el-input>
  57. <el-button size="small" type="primary" icon="el-icon-plus" @click="send()">发送</el-button>
  58. </div>
  59. <!--------------- header 参数 --------------->
  60. <div class="c-title" style="margin-top: 20px;">
  61. <span>header 参数</span>
  62. <el-link class="add-btn" type="primary" icon="el-icon-plus" @click="headerListAdd()">添加</el-link>
  63. </div>
  64. <div class="header-box">
  65. <el-table :data="headerList" size="mini" ref="headerTable">
  66. <el-table-column type="selection" width="45px" selection="-1"></el-table-column>
  67. <el-table-column label="Name">
  68. <template slot-scope="s">
  69. <el-input size="small" v-model="s.row.name"></el-input>
  70. </template>
  71. </el-table-column>
  72. <el-table-column label="Value">
  73. <template slot-scope="s">
  74. <el-input size="small" v-model="s.row.value"></el-input>
  75. </template>
  76. </el-table-column>
  77. <el-table-column label="操作" width="100px">
  78. <template slot-scope="s">
  79. <el-button class="c-btn" type="danger" icon="el-icon-close" @click="sa.arrayDelete(headerList, s.row)" tabindex="-1">删除</el-button>
  80. </template>
  81. </el-table-column>
  82. </el-table>
  83. </div>
  84. <!--------------- body 参数 --------------->
  85. <div class="c-title" style="margin-top: 20px;">
  86. <span>body 参数</span>
  87. <el-link class="add-btn" type="primary" icon="el-icon-plus" @click="bodyListAdd()">添加</el-link>
  88. <el-link class="add-btn" type="primary" icon="el-icon-edit" @click="bodyListCopy()">复制参数</el-link>
  89. <el-link class="add-btn" type="primary" icon="el-icon-edit" @click="bodyListImport()">导入参数</el-link>
  90. </div>
  91. <div class="body-box">
  92. <el-table :data="bodyList" size="mini" ref="bodyTable">
  93. <el-table-column type="selection" width="45px"></el-table-column>
  94. <el-table-column label="Name">
  95. <template slot-scope="s">
  96. <el-input size="small" v-model="s.row.name"></el-input>
  97. </template>
  98. </el-table-column>
  99. <el-table-column label="Value">
  100. <template slot-scope="s">
  101. <el-input size="small" v-model="s.row.value" :placeholder="s.row.tips"></el-input>
  102. </template>
  103. </el-table-column>
  104. <el-table-column label="操作" width="100px">
  105. <template slot-scope="s">
  106. <el-button class="c-btn" type="danger" icon="el-icon-close" @click="sa.arrayDelete(bodyList, s.row)">删除</el-button>
  107. </template>
  108. </el-table-column>
  109. </el-table>
  110. </div>
  111. <!--------------- 返回结果 --------------->
  112. <div class="c-title" style="margin-top: 20px;">
  113. <span>接口返回</span>
  114. <el-link class="add-btn" type="primary" icon="el-icon-plus" @click="sa.copyText(resText); sa.ok2('复制成功')">复制</el-link>
  115. </div>
  116. <div class="res-box">
  117. <el-input class="res-text" type="textarea" placeholder="接口返回" v-model="resText" :autosize="{ minRows: 15, maxRows: 40}" :style="{color: resColor}"></el-input>
  118. </div>
  119. </div>
  120. </div>
  121. <script src="./input-tab.js"></script>
  122. <script>
  123. var app = new Vue({
  124. el: '.vue-box',
  125. data: {
  126. ajaxUrl: '', //
  127. ajaxType: 'GET',
  128. headerList: [], // headr数据集合
  129. bodyList: [], // body数据集合
  130. resText: '', // 接口返回
  131. resColor: 'green', // 接口返回颜色
  132. isCookie: false, // 是否携带cookie
  133. },
  134. methods: {
  135. // 从本地缓存中加载参数
  136. init: function() {
  137. try{
  138. // 是否发送cookie
  139. var isCookieCC = localStorage.getItem('isCookieCC');
  140. this.isCookie = isCookieCC === "true";
  141. // 加载默认header参数
  142. var headerListCC = localStorage.getItem('headerListCC');
  143. if(headerListCC && headerListCC != '') {
  144. headerListCC = JSON.parse(headerListCC);
  145. headerListCC.forEach(function(item) {
  146. if(item.name == null || item.name == '') {
  147. return;
  148. }
  149. this.headerListAdd(item);
  150. }.bind(this))
  151. }
  152. // 加载默认body参数
  153. var bodyListCC = localStorage.getItem('bodyListCC');
  154. if(bodyListCC && bodyListCC != '') {
  155. bodyListCC = JSON.parse(bodyListCC);
  156. bodyListCC.forEach(function(item) {
  157. if(item.name == null || item.name == '') {
  158. return;
  159. }
  160. this.bodyListAdd(item);
  161. }.bind(this))
  162. }
  163. }catch(e){
  164. console.error(e);
  165. }
  166. // 加载链接参数
  167. var id = sa.p('id', '');
  168. if(id == null || id == '') {
  169. return;
  170. }
  171. // 从本地缓存读取出来
  172. var key = 'sa-doc-cc-' + id;
  173. var cc = sessionStorage.getItem(key);
  174. // console.log(key);
  175. // console.log(cc);
  176. if(cc == undefined || cc == '') {
  177. return;
  178. }
  179. // 赋值
  180. cc = JSON.parse(cc);
  181. this.ajaxUrl = cc.ajaxUrl || ''; // 地址
  182. this.ajaxType = cc.ajaxType || 'GET'; // 请求类型
  183. if(cc.headerList) {
  184. cc.headerList.forEach(function(item) {
  185. this.headerListAdd(item);
  186. }.bind(this));
  187. }
  188. if(cc.bodyList) {
  189. cc.bodyList.forEach(function(item) {
  190. this.bodyListAdd(item);
  191. }.bind(this));
  192. }
  193. },
  194. // header添加参数
  195. headerListAdd: function(header) {
  196. header = header || {name: '', value: '', tips: ''};
  197. this.headerList.push(header);
  198. this.$refs.headerTable.selection.push(header);
  199. this.f5_tabindex();
  200. },
  201. // body添加参数
  202. bodyListAdd: function(body) {
  203. body = body || {name: '', value: '', tips: ''};
  204. this.bodyList.push(body);
  205. this.$refs.bodyTable.selection.push(body);
  206. this.f5_tabindex();
  207. },
  208. // 复制body参数
  209. bodyListCopy: function(body) {
  210. // 获取body参数
  211. var body = {};
  212. this.$refs.bodyTable.selection.forEach(function(item) {
  213. if(item.name != null && item.name != '') {
  214. body[item.name] = item.value || '';
  215. }
  216. })
  217. var str = JSON.stringify(body, null, "\t");
  218. sa.copyText(str);
  219. sa.ok2('复制成功');
  220. },
  221. // 导入body参数
  222. bodyListImport: function() {
  223. sa.prompt('请输入一个JSON字符串', function(pass, index){
  224. try{
  225. pass = pass.replace(/(\s*?{\s*?|\s*?,\s*?)(['"])?([a-zA-Z0-9]+)(['"])?:/g, '$1"$3":');
  226. var obj = JSON.parse(pass);//JSON.parse();
  227. for (var key in obj) {
  228. this.bodyListAdd({
  229. name: key,
  230. value: obj[key],
  231. tips: ''
  232. })
  233. }
  234. } catch (e) {
  235. sa.alert('解析出错:' + JSON.stringify(e))
  236. }
  237. }.bind(this), 2);
  238. },
  239. // 发送
  240. send: function() {
  241. // 获取header参数
  242. var header = {};
  243. this.$refs.headerTable.selection.forEach(function(item) {
  244. if(item.name != null && item.name != '') {
  245. header[item.name] = item.value || '';
  246. }
  247. })
  248. // 获取body参数
  249. var body = {};
  250. this.$refs.bodyTable.selection.forEach(function(item) {
  251. if(item.name != null && item.name != '') {
  252. body[item.name] = item.value || '';
  253. }
  254. })
  255. // 发送请求
  256. this.resColor = '#000';
  257. sa.ajax(this.ajaxUrl, this.ajaxType, header, body, this.isCookie, function(res) {
  258. this.resColor = 'green';
  259. var str = res;
  260. if(typeof str === 'object') {
  261. str = JSON.stringify(str, null, "\t");
  262. }
  263. this.resText = str;
  264. }.bind(this), function(xhr) {
  265. this.resColor = 'red';
  266. var str = xhr;
  267. if(typeof str === 'object') {
  268. str = JSON.stringify(str, null, "\t");
  269. }
  270. this.resText = str;
  271. }.bind(this))
  272. },
  273. // 设置一下 tabindex
  274. f5_tabindex: function() {
  275. this.$nextTick(function() {
  276. $('.el-checkbox__original').attr('tabindex', -1);
  277. })
  278. },
  279. // 全局默认参数设置
  280. g_setting: function() {
  281. sa.showIframe('全局默认参数设置', './default-setting.html', '80%', '90%')
  282. }
  283. },
  284. created: function() {
  285. // this.f5();
  286. },
  287. mounted: function() {
  288. // 使textarea可以插入tab
  289. this.init();
  290. inputTab('.res-text textarea');
  291. }
  292. })
  293. // 如果是从iframe打开的
  294. if(parent != window) {
  295. // document.body.backgroundColor = '#FFF';
  296. $('body').css('background-color', '#FFF');
  297. }
  298. </script>
  299. </body>
  300. </html>