Commit 4d97259c5ff479c591198d5db3614f2727c95d59

Authored by fengtao
1 parent 9fd79720

wip:测试接口开发中

... ... @@ -38,7 +38,7 @@
38 38 import { ref, nextTick } from 'vue';
39 39 import { Button } from 'ant-design-vue';
40 40 import { TestEditCellTable } from '../TestEditCellTable/index';
41   - // import { otherHttp } from '/@/utils/http/axios';
  41 + import { otherHttp } from '/@/utils/http/axios';
42 42
43 43 const emits = defineEmits(['testInterface']);
44 44
... ... @@ -81,6 +81,53 @@
81 81 return temps;
82 82 };
83 83
  84 + //获取测试表格的key value 数组对象形式
  85 + const getTestTableKeyValue = () => {
  86 + return testEditCellTableRef.value?.getValue().map((it) => {
  87 + const value = it.key === 'scope' ? it.keyValue : it.value;
  88 + const key = it.key === 'scope' ? it.value : it.key;
  89 + return {
  90 + key,
  91 + value,
  92 + };
  93 + });
  94 + };
  95 +
  96 + //格式化"http:xxxx/api/xx/{xx}/{xx}/{xx}这种格式"
  97 + String.prototype.restfulFormat = function (replacements) {
  98 + var formatString = function (str, replacements) {
  99 + replacements =
  100 + typeof replacements === 'object' ? replacements : Array.prototype.slice.call(arguments, 1);
  101 + return str.replace(/\{\{|\}\}|\{(\w+)\}/g, function (m, n) {
  102 + if (m == '{{') {
  103 + return '{';
  104 + }
  105 + if (m == '}}') {
  106 + return '}';
  107 + }
  108 + return replacements[n];
  109 + });
  110 + };
  111 + replacements =
  112 + typeof replacements === 'object' ? replacements : Array.prototype.slice.call(arguments, 0);
  113 + return formatString(this, replacements);
  114 + };
  115 +
  116 + //项目自带第三方请求
  117 + const otherHttpRequest = async (apiType, params = {}, api, joinPrefix = false) => {
  118 + console.log(params);
  119 + switch (apiType) {
  120 + case 'get':
  121 + return await otherHttp.get(
  122 + { url: api },
  123 + {
  124 + apiUrl: '',
  125 + joinPrefix,
  126 + }
  127 + );
  128 + }
  129 + };
  130 +
84 131 const getValue = async () => {
85 132 await nextTick();
86 133 await nextTick(() => {
... ... @@ -97,39 +144,15 @@
97 144 const handleExcute = async () => {
98 145 await nextTick();
99 146 await nextTick(async () => {
100   - const getTable = testEditCellTableRef.value?.getValue().map((it) => {
101   - const value = it.key === 'scope' ? it.keyValue : it.value;
102   - const key = it.key === 'scope' ? it.value : it.key;
103   - return {
104   - key,
105   - value,
106   - };
107   - });
  147 + const getTable = getTestTableKeyValue();
108 148 const apiGetUrl = `${props.data?.requestOriginUrl}${props.data?.requestUrl}`;
109 149 const apiType = props.data?.paramsType.toLowerCase();
110   - console.log(apiType);
111   -
112   - const formatStr = (str, args) => {
113   - if (str.length == 0) return str;
114   - let formatStr = '';
115   - args?.forEach((it) => {
116   - formatStr = str.replace(RegExp('\\{' + it.key + '\\}'), it.value);
117   - });
118   - return formatStr;
119   - };
120   -
121   - const api = formatStr(apiGetUrl, getTable);
122   - console.log(api);
123   - // const rest = await otherHttp.get(
124   - // {
125   - // // url: api,
126   - // },
127   - // {
128   - // apiUrl: api,
129   - // joinPrefix: false,
130   - // }
131   - // );
132   - // console.log(rest);
  150 + const getKeyValues = {};
  151 + getTable?.map((it) => (getKeyValues[it.key!] = it.value!));
  152 + const formatApi = apiGetUrl.restfulFormat(getKeyValues);
  153 + const rest = await otherHttpRequest(apiType, {}, formatApi, false);
  154 + if (!rest) return;
  155 + testResult.value = rest;
133 156 });
134 157 };
135 158
... ...
... ... @@ -52,6 +52,10 @@ declare global {
52 52 declare interface WheelEvent {
53 53 path?: EventTarget[];
54 54 }
  55 +
  56 + declare interface String {
  57 + restfulFormat: any;
  58 + }
55 59 interface ImportMetaEnv extends ViteEnv {
56 60 __: unknown;
57 61 }
... ...