• 【sgCreateAPI】自定义小工具:敏捷开发→自动化生成API接口脚本(接口代码生成工具)


     

    具体步骤:登录Apifoxicon-default.png?t=N7T8https://app.apifox.com/

     

    圈选复制上面的内容粘贴到【接口地址列表】输入框,自动生成脚本代码


     

    新特性:

    1. 支持切换不同项目前缀
    2. 支持自定义接口前缀 

    sgCreateAPI接口代码生成工具源码 

    1. <template>
    2. <div :class="$options.name">
    3. <div class="sg-head">接口代码生成工具div>
    4. <div class="sg-container">
    5. <div class="sg-start">
    6. <div style="margin-bottom: 10px">接口地址列表div>
    7. <div style="display: flex; margin-bottom: 10px">
    8. <el-select
    9. :style="{ width: showCustom_prefix ? `100px` : `300px` }"
    10. v-model="selectValue_prefix"
    11. @change="
    12. (d) => {
    13. showCustom_prefix = d === `custom`;
    14. }
    15. "
    16. :placeholder="`请选择`"
    17. >
    18. <el-option
    19. v-for="(select, index) in selectOptions_prefix"
    20. :key="index"
    21. :value="select.value"
    22. :label="`${select.label}${
    23. select.value === `custom` ? `` : `(前缀:${select.value})`
    24. }`"
    25. :disabled="select.disabled"
    26. >el-option>
    27. el-select>
    28. <el-input
    29. v-if="showCustom_prefix"
    30. style="width: 100%; margin-left: 10px"
    31. ref="input"
    32. v-model.trim="inputValue__prefix"
    33. maxlength="20"
    34. :show-word-limit="false"
    35. :placeholder="`请输入自定义前缀(注意要有/)`"
    36. @focus="$refs.input.select()"
    37. clearable
    38. />
    39. div>
    40. <el-input
    41. style="margin-bottom: 10px"
    42. type="textarea"
    43. :placeholder="`请粘贴apifox.com网站的内容`"
    44. v-model="textareaValue1"
    45. show-word-limit
    46. />
    47. <el-button type="primary" @click="createResult">生成接口列表el-button>
    48. div>
    49. <div class="sg-center">div>
    50. <div class="sg-end">
    51. <div style="margin-bottom: 10px">生成结果div>
    52. <el-input
    53. style="margin-bottom: 10px"
    54. type="textarea"
    55. :placeholder="`请复制代码`"
    56. v-model="textareaValue2"
    57. show-word-limit
    58. />
    59. <el-button type="primary" @click="copyResult">复制el-button>
    60. div>
    61. div>
    62. div>
    63. template>
    64. <script>
    65. export default {
    66. name: "sgCreateAPI",
    67. data() {
    68. return {
    69. textareaValue1: "",
    70. textareaValue2: "",
    71. showCustom_prefix: false,
    72. inputValue__prefix: ``,
    73. selectValue_prefix: ``,
    74. selectOptions_prefix: [
    75. {
    76. label: "XXXX管理系统",
    77. value: "/sr/",
    78. default: true,
    79. },
    80. {
    81. label: "XXXXXXX服务平台",
    82. value: "/rp/",
    83. },
    84. {
    85. label: "其他",
    86. value: `custom`,
    87. },
    88. ],
    89. };
    90. },
    91. computed: {
    92. //api路径固定前缀(用于区别哪一个字符串才是路径开头)
    93. apiPathPrefix() {
    94. return this.selectValue_prefix === `custom`
    95. ? this.inputValue__prefix
    96. : this.selectValue_prefix;
    97. },
    98. },
    99. watch: {
    100. textareaValue1(newValue, oldValue) {
    101. newValue && this.createResult(newValue);
    102. },
    103. },
    104. created() {
    105. this.selectValue_prefix = this.selectOptions_prefix.find((v) => v.default).value;
    106. },
    107. methods: {
    108. createResult(d) {
    109. let texts = this.textareaValue1
    110. .split("\n")
    111. .map((v) => v.split("\t").join("").trim());
    112. texts = texts.filter((v, i, ar) => v !== ``);
    113. let r = [];
    114. texts.forEach((v, i, arr) => {
    115. if (i % 3 === 0 && v) {
    116. let path = arr[i + 2];
    117. let apiPath = this.apiPathPrefix
    118. ? path.includes(this.apiPathPrefix)
    119. ? path.split(this.apiPathPrefix)[1]
    120. : path
    121. : path;
    122. apiPath.indexOf("/") === 0 && (apiPath = apiPath.substr(1)); //去掉多余的第一根斜杠
    123. r.push([arr[i], apiPath]);
    124. }
    125. });
    126. let apis = [];
    127. r.forEach((v) =>
    128. apis.push(
    129. `/* ${v[0]} */${v[1]
    130. .split("/")
    131. .slice(1)
    132. .join("_")}({ data, r }) { this.__sd("post", \`\${API_ROOT_URL}/${
    133. v[1]
    134. }\`, data, r); },`
    135. )
    136. );
    137. this.textareaValue2 = apis.join("\n");
    138. this.copyResult(); //自动复制生成结果
    139. },
    140. copyResult(d) {
    141. this.$g.copy(this.textareaValue2, true);
    142. },
    143. },
    144. };
    145. script>
    146. <style lang="scss" scoped>
    147. .sgCreateAPI {
    148. width: 100%;
    149. height: 100%;
    150. position: absolute;
    151. box-sizing: border-box;
    152. padding: 20px;
    153. .sg-head {
    154. display: flex;
    155. align-items: center;
    156. font-size: 24px;
    157. font-weight: bold;
    158. color: #409eff;
    159. margin-bottom: 10px;
    160. }
    161. .sg-container {
    162. display: flex;
    163. flex-wrap: nowrap;
    164. height: calc(100vh - 70px);
    165. & > .sg-start {
    166. width: calc(50% - 20px);
    167. height: 100%;
    168. flex-shrink: 0;
    169. display: flex;
    170. flex-direction: column;
    171. }
    172. & > .sg-center {
    173. display: flex;
    174. justify-content: center;
    175. align-items: center;
    176. flex-grow: 1;
    177. margin: 0 10px;
    178. font-size: 24px;
    179. color: #409eff;
    180. font-weight: bold;
    181. }
    182. & > .sg-end {
    183. width: calc(50% - 20px);
    184. height: 100%;
    185. flex-shrink: 0;
    186. display: flex;
    187. flex-direction: column;
    188. }
    189. >>> .el-textarea {
    190. width: 100%;
    191. height: 100%;
    192. textarea {
    193. width: 100%;
    194. height: 100%;
    195. max-height: revert;
    196. }
    197. }
    198. }
    199. }
    200. style>

    生成的接口请求代码是基于【Vue.js最新版】【基于jQuery Ajax】[sd.js]最新原生完整版for凯哥API版本_你挚爱的强哥的博客-CSDN博客【代码】【最新版】【基于jQuery Ajax】[sd.js]最新原生完整版for凯哥API版本。https://blog.csdn.net/qq_37860634/article/details/129976375

  • 相关阅读:
    1330_硬件测试中的BCI测试
    JUC并发编程学习笔记(十五)JMM
    springboot集成mybatis
    2022年零售行业BI商业智能应用白皮书
    京东数据分析:2023年9月京东白酒行业品牌销售排行榜
    springmvc5.x-mvc实现原理及源码实现
    vue:功能:table动态合并+前端导出
    JavaScript 基本数据类型 和基本包装类型
    【微服务保护】Sentinel 流控规则 —— 深入探索 Sentinel 的流控模式、流控效果以及对热点参数进行限流
    十一、为影院添加影片制作准备服务《仿淘票票系统前后端完全制作(除支付外)》
  • 原文地址:https://blog.csdn.net/qq_37860634/article/details/132709087