对比常规请求与分布式请求:
export function doPackage(params) {return axios.post(package_url,params || {} )}
// 调用封装函数stepRequset()
export function doPackage (params, stepCallback, lastCallback, timeoutCallback) {
stepRequest(package_url, params || {}, stepCallback, lastCallback, 3600000, timeoutCallback)
}
分布拉取数据封装:
// import axios from 'axios';
import axios from './axios.js'
function makeUuid () {
var s = []
var hexDigits = '0123456789abcdef'
for (var i = 0; i < 36; i++) {
s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
}
// bits 12-15 of the time_hi_and_version field to 0010
s[14] = '4'
// bits 6-7 of the clock_seq_hi_and_reserved to 01
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
s[8] = s[13] = s[18] = s[23] = '-'
var uuid = s.join('')
return uuid
}
export const getResponseStepList = (uuid, seqid) =>
axios.post('/response/steplist', {
step_track_uuid: uuid,
step_next_seqid: seqid
}).then(function (response) {
return response
})
class TimeoutChecker {
constructor (timeoutMs) {
this.timeoutMs = timeoutMs || 3600000
this.restart()
}
restart () {
this.startTime = this.getNowMs()
}
isTimeout () {
return this.getNowMs() - this.startTime > this.timeoutMs
}
getNowMs () {
return new Date().getTime()
}
}
//
export const stepRequest = (
url, // 要封装调用接口路径
data, // 封装调用接口请求数据
stepCallback, // 中间步骤response回调,参数为response json
lastCallback, // 调用最后response回调,参数为response json
timeoutMs, // 执行超时时间
timeoutCallback // 超时回调,无参数
) => {
let nextSeqid = 0
let isSuccDone = false
let timeoutChecker = new TimeoutChecker(timeoutMs)
let uuid = makeUuid()
data['step_track_uuid'] = uuid
const doMainRequest = () => axios({
url: url,
method: 'post',
data: data,
timeout: 3600000
}).then(function (response) {
return response
})
const handleResponseList = (stepRes) => {
for (let response of stepRes.data.response_list) {
// eslint-disable-next-line
stepCallback(eval('(' + response + ')'))
}
}
const handleTimeout = () => {
if (timeoutCallback) {
let func = timeoutCallback
timeoutCallback = null
func()
}
}
let interval = setInterval(() => {
if (isSuccDone) {
clearInterval(interval)
handleTimeout()
} else {
if (timeoutChecker.isTimeout()) {
clearInterval(interval)
handleTimeout()
} else {
getResponseStepList(uuid, nextSeqid).then((stepRes) => {
if (isSuccDone) {
clearInterval(interval)
} else {
nextSeqid = stepRes.data.next_seqid
handleResponseList(stepRes)
}
})
}
}
}, 2000)
doMainRequest().then(res => {
if (!timeoutChecker.isTimeout()) {
isSuccDone = true
clearInterval(interval)
getResponseStepList(uuid, nextSeqid).then((stepRes) => {
handleResponseList(stepRes)
lastCallback(res.data)
})
} else {
handleTimeout()
}
})
}
引用:
import {stepRequest} from 'service/stepreq.js'
实际使用:
doPackageHandle () {
Var logMsg = ‘’ // 数组push也行
var param = {//this.form
'gitpath': this.form.gitpath,
'branch': this.form.branch,
'desc': this.form.desc,
}
var stepCallback = (res) => {
if (res.err_code === '0') {
// console.log(res,”正在打包中...");
logMsg += res.info
} else {
logMsg = ‘打包失败’+res.err_desc
}
}
var lastCallback = (res) => {
if (res.err_code === '0') {
// console.log(res,"成功”);
logMsg += res.info
} else {
// console.log(res,"失败”);
logMsg = ‘打包失败’+res.err_desc
}
}
var timeoutCallback = (res) => {
// console.log(res,”超时");
}
doPackage(param, stepCallback, lastCallback, timeoutCallback)
}
来源:https://www.cnblogs.com/wheatCatcher/p/12402395.html