您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
try to take over the world!
当前为
// ==UserScript== // @name knife4j接口导出 // @namespace http://tampermonkey.net/ // @version 0.0.3 // @description try to take over the world! // @author You // @match http://172.16.10.230:20005/*/doc.html // @grant GM_setClipboard // @require https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js // @require https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js // ==/UserScript== (function() { 'use strict'; knife4jGeneratate() function knife4jGeneratate() { $('.knife4j-header-default .right').prepend( ` <button type="button" class="ant-btn ant-btn-primary generate">复制接口</button> <button type="button" class="ant-btn ant-btn-info generateModule">复制本模块</button> ` ) $('body').append({}) $('.generate').click(function () { const documentEl = document.querySelector('.ant-tabs-tabpane-active .knife4j-body-content .document') const vm = documentEl.__vue__ const api = vm.api const result = generateAxios(api) GM_setClipboard(result) }) $('.generateModule').click(function () { const documentEl = document.querySelector('.ant-tabs-tabpane-active .knife4j-body-content .document') const vm = documentEl.__vue__ const targetMenuList = _.find(vm.swaggerInstance.tags, { name: vm.$route.params.controller }).childrens const result = targetMenuList.map(generateAxios).join('\n\n') GM_setClipboard(result) }) function generateAxios(api) { const hasBody = !!_.find(api.parameters, { in: 'body' }) return `/** * ${api.summary} ${calculateDocParams(api.parameters)} */ export function ${calculateFunctionName(api.showUrl)}(${calculateFunctionParam(api.parameters)}) { ${calculateRequestModule(api.showUrl)}.${_.toLower(api.methodType)}(\`${calculateRequestUrl(api.showUrl)}\`${hasBody?', body':''}) }`.replace(/\n[\n]+/g, "\n") } function calculateFunctionName(url) { const actionHash = { get: 'get', post: 'create', add: 'create', create: 'create', update: 'update', delete: 'remove', del: 'remove', list: 'list', listAll: 'listAll' } const lastWithoutPathParams = _.last(url.split('/').filter(string => !/\{.*\}/.test(string))) const secondToLst = _.nth(url.split('/').filter(string => !/\{.*\}/.test(string)),-2) const action = actionHash[lastWithoutPathParams] if (_.includes(action, 'list')) { return`get${_.upperFirst(secondToLst)}${_.upperFirst(action)}` } else if (action) { return action + _.upperFirst(secondToLst) } else { return lastWithoutPathParams } } function calculateFunctionParam(params) { const result = [] const pathParams = _.filter(params, { in: 'path' }) const bodyParams = _.filter(params, { in: 'body' }) pathParams.forEach(param => { result.push(param.name) }) if (bodyParams.length) { result.push('body') } return result.join(', ') } function calculateRequestModule(url) { return url.match(/.*cim6d-(.*)-/)[1] } function calculateRequestUrl(url) { return url.replaceAll('{', '${').replace(/\/cim6d.+?\//,'') } function calculateDocParams(params) { const result = [] const pathParams = _.filter(params, { in: 'path' }) const bodyParams = _.filter(params, { in: 'body' }) pathParams.forEach(param => { result.push(`* @param {${param.type}} ${param.name} ${param.description}`) }) if (bodyParams.length) { result.push('* @param {Object} body') } return result.join('\n') } } })();
QingJ © 2025
镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址