mirror of
https://github.com/certd/certd.git
synced 2026-07-06 20:37:34 +08:00
refactor: move
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import { request } from './service'
|
||||
import inputHandler from './util.input.handler'
|
||||
|
||||
export default {
|
||||
async list () {
|
||||
const ret = await request({
|
||||
url: '/access-providers/list'
|
||||
})
|
||||
|
||||
inputHandler.handle(ret)
|
||||
|
||||
return ret
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { request } from './service'
|
||||
import inputHandler from './util.input.handler'
|
||||
|
||||
export default {
|
||||
async list () {
|
||||
const ret = await request({
|
||||
url: '/dns-providers/list'
|
||||
})
|
||||
|
||||
inputHandler.handle(ret)
|
||||
|
||||
return ret
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { request } from './service'
|
||||
import _ from 'lodash-es'
|
||||
function arrayToMap (arr) {
|
||||
if (arr && arr instanceof Array) {
|
||||
const map = {}
|
||||
_.forEach(arr, item => {
|
||||
map[item.key] = item
|
||||
})
|
||||
return map
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
function transfer (options) {
|
||||
options.accessProviders = arrayToMap(options.accessProviders)
|
||||
}
|
||||
export default {
|
||||
exportsToZip (options) {
|
||||
transfer(options)
|
||||
return request({
|
||||
url: '/exports/toZip',
|
||||
data: { options },
|
||||
method: 'post',
|
||||
responseType: 'blob' // 重点在于配置responseType: 'blob'
|
||||
}).then(res => {
|
||||
console.log('res', res)
|
||||
const filename = decodeURI(res.headers['content-disposition'].replace('attachment;filename=', '')) // 由后端设置下载文件名
|
||||
const blob = new Blob([res.data], { type: 'application/zip' })
|
||||
const a = document.createElement('a')
|
||||
const url = window.URL.createObjectURL(blob)
|
||||
a.href = url
|
||||
a.download = filename
|
||||
const body = document.getElementsByTagName('body')[0]
|
||||
body.appendChild(a)
|
||||
a.click()
|
||||
body.removeChild(a)
|
||||
window.URL.revokeObjectURL(url)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { request } from './service'
|
||||
import inputHandler from './util.input.handler'
|
||||
export default {
|
||||
async list () {
|
||||
const ret = await request({
|
||||
url: '/plugins/list'
|
||||
})
|
||||
|
||||
inputHandler.handle(ret)
|
||||
|
||||
console.log('plugins', ret)
|
||||
return ret
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { assign, map } from 'lodash'
|
||||
import { service, request } from './service'
|
||||
|
||||
const files = require.context('./modules', false, /\.js$/)
|
||||
const generators = files.keys().map(key => files(key).default)
|
||||
|
||||
export default assign({}, ...map(generators, generator => generator({
|
||||
service,
|
||||
request
|
||||
})))
|
||||
@@ -0,0 +1,94 @@
|
||||
import axios from 'axios'
|
||||
import { get } from 'lodash-es'
|
||||
import { errorLog, errorCreate } from './tools'
|
||||
|
||||
/**
|
||||
* @description 创建请求实例
|
||||
*/
|
||||
function createService () {
|
||||
// 创建一个 axios 实例
|
||||
const service = axios.create()
|
||||
// 请求拦截
|
||||
service.interceptors.request.use(
|
||||
config => config,
|
||||
error => {
|
||||
// 发送失败
|
||||
console.log(error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
// 响应拦截
|
||||
service.interceptors.response.use(
|
||||
response => {
|
||||
console.log('response.config', response.config)
|
||||
if (response.config.responseType === 'blob') {
|
||||
return response
|
||||
}
|
||||
// dataAxios 是 axios 返回数据中的 data
|
||||
const dataAxios = response.data
|
||||
// 这个状态码是和后端约定的
|
||||
const { code } = dataAxios
|
||||
// 根据 code 进行判断
|
||||
if (code === undefined) {
|
||||
// 如果没有 code 代表这不是项目后端开发的接口 比如可能是 D2Admin 请求最新版本
|
||||
if (response.config.unpack) {
|
||||
return dataAxios
|
||||
}
|
||||
return dataAxios.data
|
||||
} else {
|
||||
// 有 code 代表这是一个后端接口 可以进行进一步的判断
|
||||
switch (code) {
|
||||
case 0:
|
||||
// [ 示例 ] code === 0 代表没有错误
|
||||
return dataAxios.data
|
||||
default:
|
||||
// 不是正确的 code
|
||||
errorCreate(`${dataAxios.msg}: ${response.config.url}`)
|
||||
return dataAxios
|
||||
}
|
||||
}
|
||||
},
|
||||
error => {
|
||||
const status = get(error, 'response.status')
|
||||
switch (status) {
|
||||
case 400: error.message = '请求错误'; break
|
||||
case 401: error.message = '未授权,请登录'; break
|
||||
case 403: error.message = '拒绝访问'; break
|
||||
case 404: error.message = `请求地址出错: ${error.response.config.url}`; break
|
||||
case 408: error.message = '请求超时'; break
|
||||
case 500: error.message = '服务器内部错误'; break
|
||||
case 501: error.message = '服务未实现'; break
|
||||
case 502: error.message = '网关错误'; break
|
||||
case 503: error.message = '服务不可用'; break
|
||||
case 504: error.message = '网关超时'; break
|
||||
case 505: error.message = 'HTTP版本不受支持'; break
|
||||
default: break
|
||||
}
|
||||
errorLog(error)
|
||||
return Promise.reject(error)
|
||||
}
|
||||
)
|
||||
return service
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 创建请求方法
|
||||
* @param {Object} service axios 实例
|
||||
*/
|
||||
function createRequestFunction (service) {
|
||||
return function (config) {
|
||||
const configDefault = {
|
||||
headers: {
|
||||
'Content-Type': get(config, 'headers.Content-Type', 'application/json')
|
||||
},
|
||||
timeout: 5000,
|
||||
baseURL: process.env.VUE_APP_API,
|
||||
data: {}
|
||||
}
|
||||
return service(Object.assign(configDefault, config))
|
||||
}
|
||||
}
|
||||
|
||||
// 用于真实网络请求的实例和请求方法
|
||||
export const service = createService()
|
||||
export const request = createRequestFunction(service)
|
||||
@@ -0,0 +1,73 @@
|
||||
import { notification } from 'ant-design-vue'
|
||||
|
||||
/**
|
||||
* @description 安全地解析 json 字符串
|
||||
* @param {String} jsonString 需要解析的 json 字符串
|
||||
* @param {String} defaultValue 默认值
|
||||
*/
|
||||
export function parse (jsonString = '{}', defaultValue = {}) {
|
||||
let result = defaultValue
|
||||
try {
|
||||
result = JSON.parse(jsonString)
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 接口请求返回
|
||||
* @param {Any} data 返回值
|
||||
* @param {String} msg 状态信息
|
||||
* @param {Number} code 状态码
|
||||
*/
|
||||
export function response (data = {}, msg = '', code = 0) {
|
||||
return [
|
||||
200,
|
||||
{ code, msg, data }
|
||||
]
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 接口请求返回 正确返回
|
||||
* @param {Any} data 返回值
|
||||
* @param {String} msg 状态信息
|
||||
*/
|
||||
export function responseSuccess (data = {}, msg = '成功') {
|
||||
return response(data, msg)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 接口请求返回 错误返回
|
||||
* @param {Any} data 返回值
|
||||
* @param {String} msg 状态信息
|
||||
* @param {Number} code 状态码
|
||||
*/
|
||||
export function responseError (data = {}, msg = '请求失败', code = 500) {
|
||||
return response(data, msg, code)
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 记录和显示错误
|
||||
* @param {Error} error 错误对象
|
||||
*/
|
||||
export function errorLog (error) {
|
||||
// 打印到控制台
|
||||
console.log(error)
|
||||
// 显示提示
|
||||
notification({
|
||||
message: error.message,
|
||||
type: 'error',
|
||||
duration: 5 * 1000
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @description 创建一个错误
|
||||
* @param {String} msg 错误信息
|
||||
*/
|
||||
export function errorCreate (msg) {
|
||||
const error = new Error(msg)
|
||||
errorLog(error)
|
||||
throw error
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import _ from 'lodash-es'
|
||||
|
||||
function handleInputs (inputs) {
|
||||
if (inputs == null) {
|
||||
return
|
||||
}
|
||||
_.forEach(inputs, (item, key) => {
|
||||
if (item.required === true) {
|
||||
if (item.component == null) {
|
||||
item.component = {}
|
||||
}
|
||||
let rules = item.component.rules
|
||||
if (rules == null) {
|
||||
item.component.rules = rules = []
|
||||
}
|
||||
if (rules.length > 0) {
|
||||
const hasRequired = rules.filter(rule => {
|
||||
return rule.required === true
|
||||
})
|
||||
if (hasRequired.length > 0) {
|
||||
return
|
||||
}
|
||||
}
|
||||
rules.push({ required: true, message: '该项必填' })
|
||||
delete item.required
|
||||
}
|
||||
})
|
||||
}
|
||||
export default {
|
||||
|
||||
handle (list) {
|
||||
_.forEach(list, item => {
|
||||
handleInputs(item.input)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user