swagger 的页面生成相关的接口

try to take over the world!

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         swagger 的页面生成相关的接口
// @namespace    http://tampermonkey.net/
// @version      0.9.2
// @description  try to take over the world!
// @match        http://localhost:8092/swagger-ui.html
// @author       kkopite
// @grant        none
// @require      https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js
// ==/UserScript==

(function() {
    'use strict';
      function copy(copyTxt) {
    // input不会保留换行符
    var createInput = document.createElement("textarea");
    createInput.value = copyTxt;
    document.body.appendChild(createInput);
    createInput.select(); //选择对象
    document.execCommand("Copy"); //执行浏览器复制命令
    createInput.className = "createInput";
    createInput.style.display = "none";
    //alert("复制成功,可以粘贴了!");
    console.log(copyTxt);
  }
    
    const select = document.createElement('select')
     select.style.position = "fixed";
    select.style.top = "120px";
    select.style.right = 0;
    document.body.append(select);
    
    const options = [
        { value: 'axios', text: '正常' },
        { value: 'wx', text: '小程序' }
    ]
    
    options.forEach(({ value, text }) => {
       const option = document.createElement('option')
       option.value = value
       option.innerText = text
       select.appendChild(option)
    })
    
    select.value = localStorage.getItem('type') || 'axios'

    select.addEventListener('change', e => {
        localStorage.setItem('type', select.value)
    })
    
    
    // Your code here...
    const foo = () => {
            const all = document.querySelectorAll('.operations .heading')
    for (let i = 0; i < all.length; i++) {
       const ele = all[i]
       const method = ele.querySelector('.http_method').innerText.replace(/\s/g, '')
       const path = ele.querySelector('.path').innerText.replace(/\s/g, '')
       const arr = path.split('/')
       let name = arr[arr.length - 1]
       name = name.slice(0, name.indexOf('.'))
       name = _.camelCase(name)
       let params = 'params'
       if (method.toUpperCase() === 'POST') {
           params = 'data'
       }
       let code = `export const ${name} = (${params} = {}) => {
  return request.request({
    method: '${method}',
    url: '${path}',
    ${params}
  })
}
`
       if (select.value === 'wx') {
       code = `export const ${name} = (data = {}) => {
  return wx.request({
    method: '${method.toUpperCase()}',
    url: URL + '${path}',
    data
  })
}`
       }
       
       const btn = document.createElement('button')
       btn.innerText = '生成代码到粘贴板'
       btn.onclick = () => {
           copy(code)
       }
       ele.appendChild(btn)
    }
    }
    
        const btn = document.createElement('button')
    btn.innerText = '生成'
    btn.onclick = foo
    btn.style.position = "fixed";
    btn.style.top = "64px";
    btn.style.right = 0;
    btn.style.padding = "10px";
    document.body.append(btn);
})();