根据swagger生成umi request请求模板代码
当前为
// ==UserScript==
// @name swagger-umi-request-generate
// @namespace
// @version 3.0
// @description 根据swagger生成umi request请求模板代码
// @author leesher
// @match http://*/swagger-ui.html*
// @grant GM_addStyle
// @run-at document-end
// @require https://cdn.bootcdn.net/ajax/libs/jquery/3.4.1/jquery.min.js
// @require https://cdn.bootcdn.net/ajax/libs/clipboard.js/2.0.6/clipboard.js
// @require https://cdn.bootcdn.net/ajax/libs/jquery-toast-plugin/1.3.2/jquery.toast.js
// @resource customCSS https://cdn.bootcdn.net/ajax/libs/jquery-toast-plugin/1.3.2/jquery.toast.css
// @grant GM_addStyle
// @grant GM_getResourceText
// @namespace lee
// ==/UserScript==
(function() {
'use strict'
console.log('油猴启动')
function addStyle(){
var newCSS = GM_getResourceText ("customCSS");
GM_addStyle ( `
.opblock-tag-section >div >span{
position:relative;
display:block;
}
.opblock-tag-section >div >span>div:nth-child(2){
position:absolute;
right:0;
top:10px;
width:250px;
}
` );
GM_addStyle (newCSS);
}
new ClipboardJS('.copy-btn')
function createATag(text, content) {
const temp = $(`<button style="margin-right:20px;margin-bottom:5px;border:none;background:white" class="btn copy-btn" data-clipboard-text="${content}">${text}</button>`)
temp.on('click', function(event) {
event.preventDefault();
$.toast({
heading: '复制成功',
position: 'mid-center',
stack: false,
hideAfter:200,
icon:"success"
})
})
return temp
}
function toHump(name) {
return name.replace(/\-(\w)/g, function(all, letter){
return letter.toUpperCase();
});
}
function createFunc(methoud, path, description) {
const methoudName = toHump(path.match('[^/]+(?!.*/)')[0])
switch (methoud.toUpperCase()) {
case 'POST':
return `// ${description}
export function ${methoudName}(data:DataType){
return request<ResType>('${path}',{
method: 'post',
data
})
}`
case 'GET':
return `// ${description}
export function ${methoudName}(params:paramType){
return request<ResType>('${path}',{
method: 'get',
params
})
}`
default:
return '暂不支持当前请求格式'
}
}
function eachChildAddBtn(index, $el){
$el = $($el)
const methoud = $($el).find('.opblock-summary-method').text()
const path = $($el).find('.opblock-summary-path > a > span').text().replace(/[\u200B-\u200D\uFEFF]/g, '');
const description = $($el).find('.opblock-summary-description').text()
$el.append($('<div></div>')
.append(createATag('复制URL', path))
.append(createATag('复制函数', createFunc(methoud, path, description)))
)
}
addStyle();
// 当页面载入时如果有tab,初始化按钮
var clear = setInterval(function (){
var isOpenDom = $(".opblock-tag-section.is-open");
if ( isOpenDom.length > 0 ) {
isOpenDom.children().eq(1).children('span').each(eachChildAddBtn)
clearInterval(clear)
}
},300)
$('body')
.delegate('.opblock-tag', 'click', function(event) {
clearInterval(clear)
console.log($(this).next())
setTimeout(() => {
$(this).next().children('span').each(eachChildAddBtn)
})
})
})();
(function (){
function createDom(type, props, style) {
const dom = document.createElement(type)
Object.assign(dom, props)
Object.assign(dom.style, style)
return dom
}
const appStyle = createDom('link', {
as: 'style',
rel: 'stylesheet',
type: 'text/css',
href: 'https://sf16-eacdn-tos.pstatp.com/obj/eaoffice/swagger-extends/css/app.6b076305.css'
})
const chunkStyle = createDom('link', {
as: 'style',
rel: 'stylesheet',
type: 'text/css',
href: 'https://sf16-eacdn-tos.pstatp.com/obj/eaoffice/swagger-extends/css/chunk-vendors.c63c8862.css'
})
const eleWoff = createDom('link', {
href: 'https://sf16-eacdn-tos.pstatp.com/obj/eaoffice/swagger-extends/fonts/element-icons.535877f5.woff'
})
const eleTtf = createDom('link', {
href: 'https://sf16-eacdn-tos.pstatp.com/obj/eaoffice/swagger-extends/fonts/element-icons.732389de.ttf'
})
document.head.append(appStyle)
document.head.append(chunkStyle)
document.head.append(eleWoff)
document.head.append(eleTtf)
const appScript = createDom('script', {
src: 'https://sf16-eacdn-tos.pstatp.com/obj/eaoffice/swagger-extends/js/app.262594ef.js'
})
const chunkScript = createDom('script', {
src: 'https://sf16-eacdn-tos.pstatp.com/obj/eaoffice/swagger-extends/js/chunk-vendors.9f96de28.js'
})
document.body.append(appScript)
document.body.append(chunkScript)
})();