您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
try to take over the world!
当前为
// ==UserScript== // @name knife4j接口导出 // @namespace http://tampermonkey.net/ // @version 0.0.7 // @description try to take over the world! // @author You // @include http://172.16.10.*:*/*/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.tags[0]}-${api.summary} ${calculateDocParams(api.parameters)} */ export function ${calculateFunctionName(api.showUrl,api.parameters)}(${calculateFunctionParam(api.parameters)}) { return ${calculateRequestModule(api.showUrl)}.${_.toLower(api.methodType)}(\`${calculateRequestUrl(api.showUrl)}\`${hasBody?', body':''}) }`.replace(/\n[\n]+/g, "\n") } function calculateFunctionName(url, params) { const actionHash = { get: 'get', post: 'create', add: 'create', create: 'create', save: 'save', update: 'update', delete: 'remove', batchDelete: 'batchDelete', 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] let result = '' if (_.includes(action, 'list')) { result = `get${_.upperFirst(secondToLst)}${_.upperFirst(action)}` } else if (action) { result = action + _.upperFirst(secondToLst) } else { result = lastWithoutPathParams } const pathParams = _.filter(params, { in: 'path' }) if(pathParams.length===1){ result += 'By'+ _.upperFirst(pathParams[0].name) } return result } 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) { if(localStorage.getItem('module')){ return localStorage.getItem('module') } return _.get(url.match(/.*cim6d-(.*)-/),'[1]') } function calculateRequestUrl(url) { return url.replaceAll('{', '${').replace(/^\/.+?\//,'') } 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或关注我们的公众号极客氢云获取最新地址